| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- // This file contains ShapeBase methods used by all the derived classes
- $DeathDuration = 10000;
- $CorpseTimeoutValue = 20000;
- //-----------------------------------------------------------------------------
- // ShapeBase object
- //-----------------------------------------------------------------------------
- // A raycast helper function to keep from having to duplicate code everytime
- // that a raycast is needed.
- // %this = the object doing the cast, usually a player
- // %range = range to search
- // %mask = what to look for
- function ShapeBase::doRaycast(%this, %range, %mask)
- {
- // get the eye vector and eye transform of the player
- %eyeVec = %this.getEyeVector();
- %eyeTrans = %this.getEyeTransform();
- // extract the position of the player's camera from the eye transform (first 3 words)
- %eyePos = getWord(%eyeTrans, 0) SPC getWord(%eyeTrans, 1) SPC getWord(%eyeTrans, 2);
- // normalize the eye vector
- %nEyeVec = VectorNormalize(%eyeVec);
- // scale (lengthen) the normalized eye vector according to the search range
- %scEyeVec = VectorScale(%nEyeVec, %range);
- // add the scaled & normalized eye vector to the position of the camera
- %eyeEnd = VectorAdd(%eyePos, %scEyeVec);
- // see if anything gets hit
- %searchResult = containerRayCast(%eyePos, %eyeEnd, %mask, %this);
- return %searchResult;
- }
- //-----------------------------------------------------------------------------
- function ShapeBase::damage(%this, %sourceObject, %position, %damage, %damageType)
- {
- // All damage applied by one object to another should go through this method.
- // This function is provided to allow objects some chance of overriding or
- // processing damage values and types. As opposed to having weapons call
- // ShapeBase::applyDamage directly. Damage is redirected to the datablock,
- // this is standard procedure for many built in callbacks.
- if (isObject(%this))
- %this.getDataBlock().damage(%this, %sourceObject, %position, %damage, %damageType);
- }
- //-----------------------------------------------------------------------------
- function ShapeBase::setDamageDt(%this, %damageAmount, %damageType)
- {
- // This function is used to apply damage over time. The damage is applied
- // at a fixed rate (50 ms). Damage could be applied over time using the
- // built in ShapBase C++ repair functions (using a neg. repair), but this
- // has the advantage of going through the normal script channels.
- if (%this.getState() !$= "Dead")
- {
- %this.damage(0, "0 0 0", %damageAmount, %damageType);
- %this.damageSchedule = %this.schedule(50, "setDamageDt", %damageAmount, %damageType);
- }
- else
- %this.damageSchedule = "";
- }
- function ShapeBase::clearDamageDt(%this)
- {
- if (%this.damageSchedule !$= "")
- {
- cancel(%this.damageSchedule);
- %this.damageSchedule = "";
- }
- }
- function GameBase::damage(%this, %sourceObject, %position, %damage, %damageType)
- {
- // All damage applied by one object to another should go through this method.
- // This function is provided to allow objects some chance of overriding or
- // processing damage values and types. As opposed to having weapons call
- // ShapeBase::applyDamage directly. Damage is redirected to the datablock,
- // this is standard procedure for many built in callbacks.
-
- %datablock = %this.getDataBlock();
- if ( isObject( %datablock ) )
- %datablock.damage(%this, %sourceObject, %position, %damage, %damageType);
- }
- //-----------------------------------------------------------------------------
- // ShapeBase datablock
- //-----------------------------------------------------------------------------
- function GameBaseData::damage(%this, %obj, %source, %position, %amount, %damageType)
- {
- // Ignore damage by default. This empty method is here to
- // avoid console warnings.
- }
- function ShapeBaseData::onAdd(%this, %obj)
- {
- %obj.setDamageState("Enabled");
- }
- function ShapeBaseData::setDamageDirection(%this, %obj, %sourceObject, %damagePos)
- {
- if (!%obj.client) return;
- if (%damagePos $= "" && isObject(%sourceObject))
- {
- if (%sourceObject.isField(initialPosition))
- {
- // Projectiles have this field set to the muzzle point of
- // the firing weapon at the time the projectile was created.
- // This gives a damage direction towards the firing object,
- // turret, vehicle, etc. Bullets and weapon fired grenades
- // are examples of projectiles.
- %damagePos = %sourceObject.initialPosition;
- }
- else
- {
- // Other objects that cause damage, such as mines, use their own
- // location as the damage position. This gives a damage direction
- // towards the explosive origin rather than the person that lay the
- // explosives.
- %damagePos = %sourceObject.getPosition();
- }
- }
- // Rotate damage vector into object space
- %damageVec = VectorSub(%damagePos, %obj.getWorldBoxCenter());
- %damageVec = VectorNormalize(%damageVec);
- %damageVec = MatrixMulVector(%obj.client.getCameraObject().getInverseTransform(), %damageVec);
- // Determine largest component of damage vector to get direction
- %vecComponents = -%damageVec.x SPC %damageVec.x SPC -%damageVec.y SPC %damageVec.y SPC -%damageVec.z SPC %damageVec.z;
- %vecDirections = "Left" SPC "Right" SPC "Bottom" SPC "Front" SPC "Bottom" SPC "Top";
- %max = -1;
- for (%i = 0; %i < 6; %i++)
- {
- %value = getWord(%vecComponents, %i);
- if (%value > %max)
- {
- %max = %value;
- %damageDir = getWord(%vecDirections, %i);
- }
- }
- commandToClient(%obj.client, 'setDamageDirection', %damageDir);
- }
- function ShapeBaseData::onCollision(%this, %obj, %collObj, %vec, %len )
- {
- if ((!isObject(%obj) || %obj.getDamageState() !$= "Enabled"))
- return;
-
- //echo(%this SPC %obj SPC %collObj SPC %vec SPC %len );
- %dmgPos = VectorSub(%obj.getPosition(), %vec);
- %dmgAmt = %len/%this.minImpactSpeed * %this.collisionMul;
-
- %this.damage(%obj, %collObj, %dmgPos, %dmgAmt, "impact");
- }
- function ShapeBaseData::onImpact(%this, %obj, %collObj, %vec, %len )
- {
- if ((!isObject(%obj) || %obj.getDamageState() !$= "Enabled"))
- return;
-
- //echo(%this SPC %obj SPC %collObj SPC %vec SPC %len );
- %dmgPos = VectorSub(%obj.getPosition(), %vec);
- %dmgAmt = %len/%this.minImpactSpeed * %this.impactMul;
-
- %this.damage(%obj, %collObj, %dmgPos, %dmgAmt, "impact");
- }
- //----------------------------------------------------------------------------
- function ShapeBaseData::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
- {
- if (!isObject(%obj) || %obj.getDamageState() !$= "Enabled" || !%damage)
- return;
-
- %rootObj = %obj;
- if (%obj.healthFromMount)
- %rootObj = findRootObject(%obj);
-
- %rootObj.applyDamage(%damage);
- %this.onDamage(%rootObj, %damage);
-
- %this.setDamageDirection(%obj, %sourceObject, %position);
-
- // Deal with client callbacks here because we don't have this
- // information in the onDamage or onDisable methods
- %client = %rootObj.client;
- %sourceClient = %sourceObject ? %sourceObject.client : 0;
- if (isObject(%client))
- {
- if (%rootObj.getDamageState() !$= "Enabled")
- {
- callGamemodeFunction("onDeath", %client, %sourceObject, %sourceClient, %damageType, "");
- }
- }
- }
- function ShapeBaseData::onDamage(%this, %obj, %delta)
- {
- // This method is invoked by the ShapeBase code whenever the
- // object's damage level changes.
- if (%delta > 0 && %obj.getDamageState() $= "Enabled")
- {
- // Apply a damage flash
- %obj.setDamageFlash(1);
-
- //total raw damage allowed to be stored
- if (%this.maxDamage> 1.0 && %obj.getDamageLevel() >= %this.maxDamage)
- %obj.setDamageState("Destroyed");
- //damage before we are considered destroyed (can animate via "damage" thread)
- else if (%this.destroyedLevel> 1.0 && %obj.getDamageLevel() >= %this.destroyedLevel)
- %obj.setDamageState("Destroyed");
- //optional additional disabled level
- else if (%this.disabledLevel> 1.0 && %obj.getDamageLevel() >= %this.disabledLevel)
- %obj.setDamageState("Disabled");
- }
- }
- function ShapeBaseData::onDisabled(%this, %obj, %state)
- {
- // Release the weapon triggers
- for (%slot = 0; %slot<4; %slot++)
- {
- if (%obj.getMountedImage(%slot))
- %obj.setImageTrigger(%slot, false);
- }
- }
- function ShapeBaseData::onDestroyed(%this, %obj, %state)
- {
- // Release the weapon triggers
- for (%slot = 0; %slot<4; %slot++)
- {
- if (%obj.getMountedImage(%slot))
- %obj.setImageTrigger(%slot, false);
- }
-
- if (%obj.client)
- {
- %obj.client.player = 0;
- %obj.client.schedule($DeathDuration, "spawnControlObject");
- }
- // Schedule corpse removal. Just keeping the place clean.
- %obj.schedule($CorpseTimeoutValue - 1000, "startFade", 1000, 0, true);
- %obj.schedule($CorpseTimeoutValue, "delete");
- %obj.schedule($CorpseTimeoutValue,"blowUp");
- }
- function ShapeBaseData::onRemove(%this, %obj)
- {
- Parent::onRemove(%this, %obj);
- deleteMountchain(%obj);
- }
|