Author Topic: LegoSabers - Edit  (Read 517 times)

I got it to reflect the projectile, although it does reflect them before they get near enough to the saber, so I need help on making it reflect them when they're closer to it.  Also, it breaks the saber's projectile when I added in the reflector script?  Can someone fix this code?  Thanks!
Code: [Select]
//projectile
AddDamageType("lgls",   '<bitmap:add-ons/Weapon_Legosabers/CI_lrls> %1',    '%2 <bitmap:add-ons/Weapon_Legosabers/CI_lrls> %1',0.75,1);
datablock ProjectileData(lglsProjectile)
{
   directDamage        = 40;
   directDamageType  = $DamageType::lgls;
   radiusDamageType  = $DamageType::lgls;
   explosion           = lglloveplosion;
   //particleEmitter     = as;

   muzzleVelocity      = 50;
   velInheritFactor    = 1;

   armingDelay         = 0;
   lifetime            = 100;
   fadeDelay           = 70;
   bounceElasticity    = 0;
   bounceFriction      = 0;
   isBallistic         = false;
   gravityMod = 0.0;

   hasLight    = true;
   lightRadius = 3.0;
   lightColor  = "0 1 0 0.5";

   uiName = "LEGO Green Lightsaber Slice";
};

function lglsImage::onFire(%this,%obj,%slot)
{
%obj.setImageAmmo(0, 0); //Disable the gun.
%client = %obj.client;
%eye = %client.player.getEyeTransform(); //Because I hate typing it out over and over again.
%mask = $TypeMasks::ProjectileObjectType; //I was originally planning to make it shove players around, but I decided this should be better for medium-long range encounters.
InitContainerRadiusSearch(%eye, 12, %mask);
while(isObject(%Proj = containerSearchNext()))
{
%velAvg = (mAbs(getWord(%proj.getVelocity(), 0)) + mAbs(getWord(%proj.getVelocity(), 1)) + mAbs(getWord(%proj.getVelocity(), 2))) / 3;
if(!%proj.origVelocity || %proj.origVelocity < %proj.getDatablock().muzzleVelocity / 2) //We don't want too small of an original velocity to base our reflections on, otherwise we couldn't get it above half normal speed.
%proj.origVelocity = %velAvg; //Speed cap is based on how fast the projectile was at first deflection.
%repelForce = ((15 - vectorDist(%eye, %proj.getTransform())) / 6) * %velAvg; //Allow us to boost the speed based on what it already is, instead of the default speed.
if(%repelForce > %proj.origVelocity * 2.25)
%repelForce = (2.25 * %proj.origVelocity); //We have to cap it at some point.
if(%repelForce <= %proj.getDatablock().muzzleVelocity / 2)
%repelForce = (%proj.getDatablock().muzzleVelocity / 2); //Also imposing a minimum.
%repelDir = vectorNormalize(vectorSub(%proj.getTransform(), %eye));

%anglediff = vectorSub(%repelDir,vectorNormalize(%client.player.getEyeVector()));
%anglediffavg = (mAbs(getWord(%angleDiff, 0)) + mAbs(getWord(%angleDiff, 1)) + mAbs(getWord(%angleDiff, 2))) / 3;
if(%anglediffavg > 0.3) //Makeshift vector angle comparison. It doesn't work as accurately as I wish it did, but I'm not all that good with vectors.
continue;

if(vectorDist(%eye, %proj.getTransform()) <= 6 && isObject(%proj.client.player) && %client != %proj.client)
{
%ownerDir = vectorNormalize(vectorSub(%proj.client.player.getEyeTransform(), %eye));
%ownerAngleDiff = vectorSub(%ownerDir, vectorNormalize(%client.player.getEyeVector()));
%ownerAngleDiffAvg = (mAbs(getWord(%ownerAngleDiff, 0)) + mAbs(getWord(%ownerAngleDiff, 1)) + mAbs(getWord(%ownerAngleDiff, 2))) / 3; //I thought I've seen a cone-search function somewhere before...
if(%anglediffavg <= 0.5) //If they're out of our sight, we don't send it at them.
%repelDir = vectorNormalize(vectorSub(%proj.client.player.getEyeTransform(), %proj.getTransform())); //Step in and tell us where it should go.
}

%newVelocity = vectorScale(%repelDir, %repelForce); //Get our final calculations as to where we should shoot this thing.

serverPlay3d(RedirectSound, %proj.getPosition());
%obj.setImageAmmo(0, 1); //Reenable the gun if we actually used it on something

%p = new Projectile() //Because Projectile::Redirect didn't work, and Projectile::SetVelocity doesn't exist for some reason.
{
initialVelocity = %newVelocity;
origVelocity = %proj.origVelocity;
initialPosition = %proj.getPosition();
datablock = %proj.getDatablock();
sourceObject = %proj.client.player;
sourceSlot = %proj.sourceSlot;
client = %client; //Change the owner to us so we can damage the original attacker.
scale = %proj.getScale();
};
missionCleanup.add(%p);
//More cool stuff that I haven't gotten working yet
//if(isObject(vectorProjectile))
// if(%proj.getdatablock() == vectorProjectile.getID())
// vectorProjectile.schedule(getRandom(100,200),"ChangeForm",%p);
%proj.delete(); //Do away with the old one so we don't get facerocketed.
}
}

EDIT: Is there a way to make it so when a lightsaber projectile is reflected, it explodes it, that'd make for even more epic light saber duels, so they actually clash instead of ping ponging reflected projectiles at each other.
« Last Edit: October 20, 2009, 01:45:24 AM by Tezuni »

Use some sort of delay?

The deflector is designed to basically search in a connical area for projectiles and reflect them, but I doubt it does this perfectly and I want to rework it some day.

Anyway, it may be better to use the deflector broom script if you want to make a melee weapon do this. The deflector probably won't work well at close range.

FIXED.  Big Thanks to Jetz! :D
« Last Edit: October 20, 2009, 10:19:40 PM by Tezuni »