shapeBase.tscript 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // This file contains ShapeBase methods used by all the derived classes
  23. $DeathDuration = 10000;
  24. $CorpseTimeoutValue = 20000;
  25. //-----------------------------------------------------------------------------
  26. // ShapeBase object
  27. //-----------------------------------------------------------------------------
  28. // A raycast helper function to keep from having to duplicate code everytime
  29. // that a raycast is needed.
  30. // %this = the object doing the cast, usually a player
  31. // %range = range to search
  32. // %mask = what to look for
  33. function ShapeBase::doRaycast(%this, %range, %mask)
  34. {
  35. // get the eye vector and eye transform of the player
  36. %eyeVec = %this.getEyeVector();
  37. %eyeTrans = %this.getEyeTransform();
  38. // extract the position of the player's camera from the eye transform (first 3 words)
  39. %eyePos = getWord(%eyeTrans, 0) SPC getWord(%eyeTrans, 1) SPC getWord(%eyeTrans, 2);
  40. // normalize the eye vector
  41. %nEyeVec = VectorNormalize(%eyeVec);
  42. // scale (lengthen) the normalized eye vector according to the search range
  43. %scEyeVec = VectorScale(%nEyeVec, %range);
  44. // add the scaled & normalized eye vector to the position of the camera
  45. %eyeEnd = VectorAdd(%eyePos, %scEyeVec);
  46. // see if anything gets hit
  47. %searchResult = containerRayCast(%eyePos, %eyeEnd, %mask, %this);
  48. return %searchResult;
  49. }
  50. //-----------------------------------------------------------------------------
  51. function ShapeBase::damage(%this, %sourceObject, %position, %damage, %damageType)
  52. {
  53. // All damage applied by one object to another should go through this method.
  54. // This function is provided to allow objects some chance of overriding or
  55. // processing damage values and types. As opposed to having weapons call
  56. // ShapeBase::applyDamage directly. Damage is redirected to the datablock,
  57. // this is standard procedure for many built in callbacks.
  58. if (isObject(%this))
  59. %this.getDataBlock().damage(%this, %sourceObject, %position, %damage, %damageType);
  60. }
  61. //-----------------------------------------------------------------------------
  62. function ShapeBase::setDamageDt(%this, %damageAmount, %damageType)
  63. {
  64. // This function is used to apply damage over time. The damage is applied
  65. // at a fixed rate (50 ms). Damage could be applied over time using the
  66. // built in ShapBase C++ repair functions (using a neg. repair), but this
  67. // has the advantage of going through the normal script channels.
  68. if (%this.getState() !$= "Dead")
  69. {
  70. %this.damage(0, "0 0 0", %damageAmount, %damageType);
  71. %this.damageSchedule = %this.schedule(50, "setDamageDt", %damageAmount, %damageType);
  72. }
  73. else
  74. %this.damageSchedule = "";
  75. }
  76. function ShapeBase::clearDamageDt(%this)
  77. {
  78. if (%this.damageSchedule !$= "")
  79. {
  80. cancel(%this.damageSchedule);
  81. %this.damageSchedule = "";
  82. }
  83. }
  84. function GameBase::damage(%this, %sourceObject, %position, %damage, %damageType)
  85. {
  86. // All damage applied by one object to another should go through this method.
  87. // This function is provided to allow objects some chance of overriding or
  88. // processing damage values and types. As opposed to having weapons call
  89. // ShapeBase::applyDamage directly. Damage is redirected to the datablock,
  90. // this is standard procedure for many built in callbacks.
  91. %datablock = %this.getDataBlock();
  92. if ( isObject( %datablock ) )
  93. %datablock.damage(%this, %sourceObject, %position, %damage, %damageType);
  94. }
  95. //-----------------------------------------------------------------------------
  96. // ShapeBase datablock
  97. //-----------------------------------------------------------------------------
  98. function GameBaseData::damage(%this, %obj, %source, %position, %amount, %damageType)
  99. {
  100. // Ignore damage by default. This empty method is here to
  101. // avoid console warnings.
  102. }
  103. function ShapeBaseData::onAdd(%this, %obj)
  104. {
  105. %obj.setDamageState("Enabled");
  106. }
  107. function ShapeBaseData::setDamageDirection(%this, %obj, %sourceObject, %damagePos)
  108. {
  109. if (!%obj.client) return;
  110. if (%damagePos $= "" && isObject(%sourceObject))
  111. {
  112. if (%sourceObject.isField(initialPosition))
  113. {
  114. // Projectiles have this field set to the muzzle point of
  115. // the firing weapon at the time the projectile was created.
  116. // This gives a damage direction towards the firing object,
  117. // turret, vehicle, etc. Bullets and weapon fired grenades
  118. // are examples of projectiles.
  119. %damagePos = %sourceObject.initialPosition;
  120. }
  121. else
  122. {
  123. // Other objects that cause damage, such as mines, use their own
  124. // location as the damage position. This gives a damage direction
  125. // towards the explosive origin rather than the person that lay the
  126. // explosives.
  127. %damagePos = %sourceObject.getPosition();
  128. }
  129. }
  130. // Rotate damage vector into object space
  131. %damageVec = VectorSub(%damagePos, %obj.getWorldBoxCenter());
  132. %damageVec = VectorNormalize(%damageVec);
  133. %damageVec = MatrixMulVector(%obj.client.getCameraObject().getInverseTransform(), %damageVec);
  134. // Determine largest component of damage vector to get direction
  135. %vecComponents = -%damageVec.x SPC %damageVec.x SPC -%damageVec.y SPC %damageVec.y SPC -%damageVec.z SPC %damageVec.z;
  136. %vecDirections = "Left" SPC "Right" SPC "Bottom" SPC "Front" SPC "Bottom" SPC "Top";
  137. %max = -1;
  138. for (%i = 0; %i < 6; %i++)
  139. {
  140. %value = getWord(%vecComponents, %i);
  141. if (%value > %max)
  142. {
  143. %max = %value;
  144. %damageDir = getWord(%vecDirections, %i);
  145. }
  146. }
  147. commandToClient(%obj.client, 'setDamageDirection', %damageDir);
  148. }
  149. function ShapeBaseData::onCollision(%this, %obj, %collObj, %vec, %len )
  150. {
  151. if ((!isObject(%obj) || %obj.getDamageState() !$= "Enabled"))
  152. return;
  153. //echo(%this SPC %obj SPC %collObj SPC %vec SPC %len );
  154. %dmgPos = VectorSub(%obj.getPosition(), %vec);
  155. %dmgAmt = %len/%this.minImpactSpeed * %this.collisionMul;
  156. %this.damage(%obj, %collObj, %dmgPos, %dmgAmt, "impact");
  157. }
  158. function ShapeBaseData::onImpact(%this, %obj, %collObj, %vec, %len )
  159. {
  160. if ((!isObject(%obj) || %obj.getDamageState() !$= "Enabled"))
  161. return;
  162. //echo(%this SPC %obj SPC %collObj SPC %vec SPC %len );
  163. %dmgPos = VectorSub(%obj.getPosition(), %vec);
  164. %dmgAmt = %len/%this.minImpactSpeed * %this.impactMul;
  165. %this.damage(%obj, %collObj, %dmgPos, %dmgAmt, "impact");
  166. }
  167. //----------------------------------------------------------------------------
  168. function ShapeBaseData::damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
  169. {
  170. if (!isObject(%obj) || %obj.getDamageState() !$= "Enabled" || !%damage)
  171. return;
  172. %rootObj = %obj;
  173. if (%obj.healthFromMount)
  174. %rootObj = findRootObject(%obj);
  175. %rootObj.applyDamage(%damage);
  176. %this.onDamage(%rootObj, %damage);
  177. %this.setDamageDirection(%obj, %sourceObject, %position);
  178. // Deal with client callbacks here because we don't have this
  179. // information in the onDamage or onDisable methods
  180. %client = %rootObj.client;
  181. %sourceClient = %sourceObject ? %sourceObject.client : 0;
  182. if (isObject(%client))
  183. {
  184. if (%rootObj.getDamageState() !$= "Enabled")
  185. {
  186. callGamemodeFunction("onDeath", %client, %sourceObject, %sourceClient, %damageType, "");
  187. }
  188. }
  189. }
  190. function ShapeBaseData::onDamage(%this, %obj, %delta)
  191. {
  192. // This method is invoked by the ShapeBase code whenever the
  193. // object's damage level changes.
  194. if (%delta > 0 && %obj.getDamageState() $= "Enabled")
  195. {
  196. // Apply a damage flash
  197. %obj.setDamageFlash(1);
  198. //total raw damage allowed to be stored
  199. if (%this.maxDamage> 1.0 && %obj.getDamageLevel() >= %this.maxDamage)
  200. %obj.setDamageState("Destroyed");
  201. //damage before we are considered destroyed (can animate via "damage" thread)
  202. else if (%this.destroyedLevel> 1.0 && %obj.getDamageLevel() >= %this.destroyedLevel)
  203. %obj.setDamageState("Destroyed");
  204. //optional additional disabled level
  205. else if (%this.disabledLevel> 1.0 && %obj.getDamageLevel() >= %this.disabledLevel)
  206. %obj.setDamageState("Disabled");
  207. }
  208. }
  209. function ShapeBaseData::onDisabled(%this, %obj, %state)
  210. {
  211. // Release the weapon triggers
  212. for (%slot = 0; %slot<4; %slot++)
  213. {
  214. if (%obj.getMountedImage(%slot))
  215. %obj.setImageTrigger(%slot, false);
  216. }
  217. }
  218. function ShapeBaseData::onDestroyed(%this, %obj, %state)
  219. {
  220. // Release the weapon triggers
  221. for (%slot = 0; %slot<4; %slot++)
  222. {
  223. if (%obj.getMountedImage(%slot))
  224. %obj.setImageTrigger(%slot, false);
  225. }
  226. if (%obj.client)
  227. {
  228. %obj.client.player = 0;
  229. %obj.client.schedule($DeathDuration, "spawnControlObject");
  230. }
  231. // Schedule corpse removal. Just keeping the place clean.
  232. %obj.schedule($CorpseTimeoutValue - 1000, "startFade", 1000, 0, true);
  233. %obj.schedule($CorpseTimeoutValue, "delete");
  234. %obj.schedule($CorpseTimeoutValue,"blowUp");
  235. }
  236. function ShapeBaseData::onRemove(%this, %obj)
  237. {
  238. Parent::onRemove(%this, %obj);
  239. deleteMountchain(%obj);
  240. }