Author Topic: Explosion raycast function -- taking cover from grenades  (Read 585 times)

I don't really have an issue, I just wanted to share this code I wrote because it seems to be working pretty well. Feel free to leave any criticism/suggest what I can do better, or use it if you think you'd like to. I'd like to know if I'm doing anything terribly wrong before I continue with the project. this is something I'd like to see more in blockland, especially with things like the HE grenade which have large damage radii and are kinda impossible to get away from

What it does is when an explosion happens, it finds all the players within a radius, and does a raycast at 3 z positions at the player looking for bricks in-between. If all 3 raycasts are blocked by a brick, the player is safe. If not, it determines the distance the player is from the explosion and damages them accordingly.

it has a lot of debug information, and highlights bricks in the way, was just kinda too lazy to remove it honestly

Code: [Select]
function solHEGrenadeProjectile::OnExplode(%this, %obj, %pos)
{

%client = %obj.client;

%haveFound = 0;

%damage = 200;
%radius = 17;
%mask = $TypeMasks::PlayerObjectType;
%brickMask = $TypeMasks::FxBrickObjectType;

messageAll('', "\c6----------------------------------------------------------------------------------");
messageAll('', "\c3NEW-" @ %this.getName() SPC "thrown by" SPC %client.name SPC "exploded at" SPC %pos);
messageAll('', "Doing container search at" SPC %pos SPC "with radius" SPC %radius SPC "for typemask" SPC %mask);

//spawnPongExplosion(%pos);

initContainerRadiusSearch(%pos, %radius, %mask);


while(%search = containerSearchNext())
{
%grenadeFoundPlayer = false;
%searchPosition = %search.getPosition();
messageAll('', "\c3NEW RESULT: " @ %haveFound SPC"\c1Found player" SPC %search SPC "at position" SPC %searchPosition);

// check if its a bot
if(!isObject(%search.client))
messageAll('', "\c3Object" SPC %search SPC "is a bot.");


//This part is where we check for parts on the player that the grenade can see

for(%i = 1; %i < 4; %i++)
{

%searchPosition = %search.getPosition();

messageAll('', "\c1Pass number:" SPC %i);

//adjust position upwards
%x = getWord(%searchPosition, 0);
%y = getWord(%searchPosition, 1);
%z = getWord(%searchPosition, 2);

%z += %i;

%searchPosition = %x SPC %y SPC %z;


//Raycast
messageAll('', "\c1Doing raycast to player with end point" SPC %searchPosition);
%raycast = containerRaycast(%pos, %searchPosition, %brickMask, 0);
messageAll('', "\c1Raycast returned:" SPC %raycast);


if(%raycast)
{

//found a brick
messageAll('', "\c5Raycast found a brick between explosion and" SPC %search);
getWord(%raycast, 0).setColorFX(3);

}
else
{

//found nothing
messageAll('', "\c4Raycast did not find a brick between explosion and" SPC %search);
%grenadeFoundPlayer = true;


}




}

if(%grenadeFoundPlayer)
{
messageAll('', "\c0Player" SPC %search SPC "hit by explosion!");

%distanceFromExplosion = VectorDist( %pos , %search.getPosition() );

messageAll('', "\c0Player" SPC %search SPC "was" SPC %distanceFromExplosion SPC "units from the explosion.");
messageAll('', "\c6The explosion did" SPC %damage SPC "damage at the epicenter...");

%damageMultiplier = 1 - (%distanceFromExplosion / 20);

%finalDamage = %damage * %damageMultiplier;

messageAll('', "\c6At this range, the explosion does" SPC mFloor(%damageMultiplier * 100) @ "% of the damage.");
messageAll('', "\c0" @ %finalDamage SPC "damage dealt.");

//(%this, %obj, %pos, %damage, %damageType)
%search.Damage(%search, %pos, %finalDamage, $DamageType::SolExplosion);


}
else
{
messageAll('', "\c2Player" SPC %search SPC "safe.");


}


%haveFound++;
}


//spawnPongExplosion(%searchPosition);

//%search = containerSearchNext();

messageAll('', "\c3Found nothing else. Ending search after" SPC %haveFound SPC "total. Time of search:" SPC getSimTime());



Parent::OnExplode(%this, %obj, %pos);
}
« Last Edit: December 22, 2016, 02:13:26 PM by Rally »

ports obstruct radius damage does this automatically for every radius damage on the server. he does the same method: fires a raycast to the top of the player, raycast to hack position, and raycast to position (bottom of player) to determine if radius damage should be applied

its packaged on onExplode, which is called on every object in the radius of the explosion
« Last Edit: December 22, 2016, 02:24:59 PM by Conan »

Aw dang it I thought I was being original

Can I get a link to that?

Found it https://forum.blockland.us/index.php?topic=242211.0
« Last Edit: December 22, 2016, 02:28:19 PM by Rally »