g_object.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // leave this line at the top for all g_xxxx.cpp files...
  2. #include "g_headers.h"
  3. #include "g_local.h"
  4. #include "g_functions.h"
  5. extern void G_MoverTouchPushTriggers( gentity_t *ent, vec3_t oldOrg );
  6. void G_StopObjectMoving( gentity_t *object );
  7. /*
  8. ================
  9. G_BounceMissile
  10. ================
  11. */
  12. void G_BounceObject( gentity_t *ent, trace_t *trace )
  13. {
  14. vec3_t velocity;
  15. float dot;
  16. int hitTime;
  17. // reflect the velocity on the trace plane
  18. hitTime = level.previousTime + ( level.time - level.previousTime ) * trace->fraction;
  19. EvaluateTrajectoryDelta( &ent->s.pos, hitTime, velocity );
  20. dot = DotProduct( velocity, trace->plane.normal );
  21. float bounceFactor = 60/ent->mass;
  22. if ( bounceFactor > 1.0f )
  23. {
  24. bounceFactor = 1.0f;
  25. }
  26. VectorMA( velocity, -2*dot*bounceFactor, trace->plane.normal, ent->s.pos.trDelta );
  27. //FIXME: customized or material-based impact/bounce sounds
  28. if ( ent->s.eFlags & EF_BOUNCE_HALF )
  29. {
  30. VectorScale( ent->s.pos.trDelta, 0.5, ent->s.pos.trDelta );
  31. // check for stop
  32. if ( ((trace->plane.normal[2] > 0.7&&g_gravity->value>0) || (trace->plane.normal[2]<-0.7&&g_gravity->value<0)) && ((ent->s.pos.trDelta[2]<40&&g_gravity->value>0)||(ent->s.pos.trDelta[2]>-40&&g_gravity->value<0)) ) //this can happen even on very slightly sloped walls, so changed it from > 0 to > 0.7
  33. {
  34. //G_SetOrigin( ent, trace->endpos );
  35. //ent->nextthink = level.time + 500;
  36. ent->s.apos.trType = TR_STATIONARY;
  37. VectorCopy( ent->currentAngles, ent->s.apos.trBase );
  38. VectorCopy( trace->endpos, ent->currentOrigin );
  39. VectorCopy( trace->endpos, ent->s.pos.trBase );
  40. ent->s.pos.trTime = level.time;
  41. return;
  42. }
  43. }
  44. // NEW--It would seem that we want to set our trBase to the trace endpos
  45. // and set the trTime to the actual time of impact....
  46. // FIXME: Should we still consider adding the normal though??
  47. VectorCopy( trace->endpos, ent->currentOrigin );
  48. ent->s.pos.trTime = hitTime;
  49. VectorCopy( ent->currentOrigin, ent->s.pos.trBase );
  50. VectorCopy( trace->plane.normal, ent->pos1 );//???
  51. }
  52. /*
  53. ================
  54. G_RunObject
  55. TODO: When transition to 0 grav, push away from surface you were resting on
  56. TODO: When free-floating in air, apply some friction to your trDelta (based on mass?)
  57. ================
  58. */
  59. extern void DoImpact( gentity_t *self, gentity_t *other, qboolean damageSelf, trace_t *trace );
  60. void G_RunObject( gentity_t *ent )
  61. {
  62. vec3_t origin, oldOrg;
  63. trace_t tr;
  64. gentity_t *traceEnt = NULL;
  65. //FIXME: floaters need to stop floating up after a while, even if gravity stays negative?
  66. if ( ent->s.pos.trType == TR_STATIONARY )//g_gravity->value <= 0 &&
  67. {
  68. ent->s.pos.trType = TR_GRAVITY;
  69. VectorCopy( ent->currentOrigin, ent->s.pos.trBase );
  70. ent->s.pos.trTime = level.previousTime;//?necc?
  71. if ( !g_gravity->value )
  72. {
  73. ent->s.pos.trDelta[2] += 100;
  74. }
  75. }
  76. ent->nextthink = level.time + FRAMETIME;
  77. VectorCopy( ent->currentOrigin, oldOrg );
  78. // get current position
  79. EvaluateTrajectory( &ent->s.pos, level.time, origin );
  80. //Get current angles?
  81. EvaluateTrajectory( &ent->s.apos, level.time, ent->currentAngles );
  82. if ( VectorCompare( ent->currentOrigin, origin ) )
  83. {//error - didn't move at all!
  84. return;
  85. }
  86. // trace a line from the previous position to the current position,
  87. // ignoring interactions with the missile owner
  88. gi.trace( &tr, ent->currentOrigin, ent->mins, ent->maxs, origin,
  89. ent->owner ? ent->owner->s.number : ent->s.number, ent->clipmask );
  90. if ( !tr.startsolid && !tr.allsolid && tr.fraction )
  91. {
  92. VectorCopy( tr.endpos, ent->currentOrigin );
  93. gi.linkentity( ent );
  94. }
  95. else
  96. //if ( tr.startsolid )
  97. {
  98. tr.fraction = 0;
  99. }
  100. G_MoverTouchPushTriggers( ent, oldOrg );
  101. /*
  102. if ( !(ent->s.eFlags & EF_TELEPORT_BIT) && !(ent->svFlags & SVF_NO_TELEPORT) )
  103. {
  104. G_MoverTouchTeleportTriggers( ent, oldOrg );
  105. if ( ent->s.eFlags & EF_TELEPORT_BIT )
  106. {//was teleported
  107. return;
  108. }
  109. }
  110. else
  111. {
  112. ent->s.eFlags &= ~EF_TELEPORT_BIT;
  113. }
  114. */
  115. if ( tr.fraction == 1 )
  116. {
  117. if ( g_gravity->value <= 0 )
  118. {
  119. if ( ent->s.apos.trType == TR_STATIONARY )
  120. {
  121. VectorCopy( ent->currentAngles, ent->s.apos.trBase );
  122. ent->s.apos.trType = TR_LINEAR;
  123. ent->s.apos.trDelta[1] = Q_flrand( -300, 300 );
  124. ent->s.apos.trDelta[0] = Q_flrand( -10, 10 );
  125. ent->s.apos.trDelta[2] = Q_flrand( -10, 10 );
  126. ent->s.apos.trTime = level.time;
  127. }
  128. }
  129. //friction in zero-G
  130. if ( !g_gravity->value )
  131. {
  132. float friction = 0.975f;
  133. /*friction -= ent->mass/1000.0f;
  134. if ( friction < 0.1 )
  135. {
  136. friction = 0.1f;
  137. }
  138. */
  139. VectorScale( ent->s.pos.trDelta, friction, ent->s.pos.trDelta );
  140. VectorCopy( ent->currentOrigin, ent->s.pos.trBase );
  141. ent->s.pos.trTime = level.time;
  142. }
  143. return;
  144. }
  145. //hit something
  146. //Do impact damage
  147. traceEnt = &g_entities[tr.entityNum];
  148. if ( tr.fraction || (traceEnt && traceEnt->takedamage) )
  149. {
  150. if ( !VectorCompare( ent->currentOrigin, oldOrg ) )
  151. {//moved and impacted
  152. if ( (traceEnt && traceEnt->takedamage) )
  153. {//hurt someone
  154. vec3_t fxDir;
  155. VectorNormalize2( ent->s.pos.trDelta, fxDir );
  156. VectorScale( fxDir, -1, fxDir );
  157. G_PlayEffect( G_EffectIndex( "melee/kick_impact" ), tr.endpos, fxDir );
  158. //G_Sound( ent, G_SoundIndex( va( "sound/weapons/melee/punch%d", Q_irand( 1, 4 ) ) ) );
  159. }
  160. else
  161. {
  162. G_PlayEffect( G_EffectIndex( "melee/kick_impact_silent" ), tr.endpos, tr.plane.normal );
  163. }
  164. if ( ent->mass > 100 )
  165. {
  166. G_Sound( ent, G_SoundIndex( "sound/movers/objects/objectHitHeavy.wav" ) );
  167. }
  168. else
  169. {
  170. G_Sound( ent, G_SoundIndex( "sound/movers/objects/objectHit.wav" ) );
  171. }
  172. }
  173. DoImpact( ent, traceEnt, !(tr.surfaceFlags&SURF_NODAMAGE), &tr );
  174. }
  175. if ( !ent || (ent->takedamage&&ent->health <= 0) )
  176. {//been destroyed by impact
  177. //chunks?
  178. G_Sound( ent, G_SoundIndex( "sound/movers/objects/objectBreak.wav" ) );
  179. return;
  180. }
  181. //do impact physics
  182. if ( ent->s.pos.trType == TR_GRAVITY )//tr.fraction < 1.0 &&
  183. {//FIXME: only do this if no trDelta
  184. if ( g_gravity->value <= 0 || tr.plane.normal[2] < 0.7 )
  185. {
  186. if ( ent->s.eFlags&(EF_BOUNCE|EF_BOUNCE_HALF) )
  187. {
  188. if ( tr.fraction <= 0.0f )
  189. {
  190. VectorCopy( tr.endpos, ent->currentOrigin );
  191. VectorCopy( tr.endpos, ent->s.pos.trBase );
  192. VectorClear( ent->s.pos.trDelta );
  193. ent->s.pos.trTime = level.time;
  194. }
  195. else
  196. {
  197. G_BounceObject( ent, &tr );
  198. }
  199. }
  200. else
  201. {//slide down?
  202. //FIXME: slide off the slope
  203. }
  204. }
  205. else
  206. {
  207. ent->s.apos.trType = TR_STATIONARY;
  208. pitch_roll_for_slope( ent, tr.plane.normal );
  209. //ent->currentAngles[0] = 0;//FIXME: match to slope
  210. //ent->currentAngles[2] = 0;//FIXME: match to slope
  211. VectorCopy( ent->currentAngles, ent->s.apos.trBase );
  212. //okay, we hit the floor, might as well stop or prediction will
  213. //make us go through the floor!
  214. //FIXME: this means we can't fall if something is pulled out from under us...
  215. G_StopObjectMoving( ent );
  216. }
  217. }
  218. else
  219. {
  220. ent->s.apos.trType = TR_STATIONARY;
  221. pitch_roll_for_slope( ent, tr.plane.normal );
  222. //ent->currentAngles[0] = 0;//FIXME: match to slope
  223. //ent->currentAngles[2] = 0;//FIXME: match to slope
  224. VectorCopy( ent->currentAngles, ent->s.apos.trBase );
  225. }
  226. //call touch func
  227. GEntity_TouchFunc( ent, &g_entities[tr.entityNum], &tr );
  228. }
  229. void G_StopObjectMoving( gentity_t *object )
  230. {
  231. object->s.pos.trType = TR_STATIONARY;
  232. VectorCopy( object->currentOrigin, object->s.origin );
  233. VectorCopy( object->currentOrigin, object->s.pos.trBase );
  234. VectorClear( object->s.pos.trDelta );
  235. /*
  236. //Stop spinning
  237. VectorClear( self->s.apos.trDelta );
  238. vectoangles(trace->plane.normal, self->s.angles);
  239. VectorCopy(self->s.angles, self->currentAngles );
  240. VectorCopy(self->s.angles, self->s.apos.trBase);
  241. */
  242. }
  243. void G_StartObjectMoving( gentity_t *object, vec3_t dir, float speed, trType_t trType )
  244. {
  245. VectorNormalize (dir);
  246. //object->s.eType = ET_GENERAL;
  247. object->s.pos.trType = trType;
  248. VectorCopy( object->currentOrigin, object->s.pos.trBase );
  249. VectorScale(dir, speed, object->s.pos.trDelta );
  250. object->s.pos.trTime = level.time;
  251. /*
  252. //FIXME: incorporate spin?
  253. vectoangles(dir, object->s.angles);
  254. VectorCopy(object->s.angles, object->s.apos.trBase);
  255. VectorSet(object->s.apos.trDelta, 300, 0, 0 );
  256. object->s.apos.trTime = level.time;
  257. */
  258. //FIXME: make these objects go through G_RunObject automatically, like missiles do
  259. if ( object->e_ThinkFunc == thinkF_NULL )
  260. {
  261. object->nextthink = level.time + FRAMETIME;
  262. object->e_ThinkFunc = thinkF_G_RunObject;
  263. }
  264. else
  265. {//You're responsible for calling RunObject
  266. }
  267. }
  268. gentity_t *G_CreateObject ( gentity_t *owner, vec3_t origin, vec3_t angles, int modelIndex, int frame, trType_t trType, int effectID = 0 )
  269. {
  270. gentity_t *object;
  271. object = G_Spawn();
  272. if ( object == NULL )
  273. {
  274. return NULL;
  275. }
  276. object->classname = "object";//?
  277. object->nextthink = level.time + FRAMETIME;
  278. object->e_ThinkFunc = thinkF_G_RunObject;
  279. object->s.eType = ET_GENERAL;
  280. object->s.eFlags |= EF_AUTO_SIZE;//CG_Ents will create the mins & max itself based on model bounds
  281. object->s.modelindex = modelIndex;
  282. //FIXME: allow to set a targetname/script_targetname and animation info?
  283. object->s.frame = object->startFrame = object->endFrame = frame;
  284. object->owner = owner;
  285. //object->damage = 100;
  286. //object->splashDamage = 200;
  287. //object->splashRadius = 200;
  288. //object->methodOfDeath = MOD_EXPLOSIVE;
  289. //object->splashMethodOfDeath = MOD_EXPLOSIVE_SPLASH;
  290. object->clipmask = MASK_SOLID;//?
  291. //object->e_TouchFunc = touchF_charge_stick;
  292. // The effect to play.
  293. object->count = effectID;
  294. //Give it SOME size for now
  295. VectorSet( object->mins, -4, -4, -4 );
  296. VectorSet( object->maxs, 4, 4, 4 );
  297. //Origin
  298. G_SetOrigin( object, origin );
  299. object->s.pos.trType = trType;
  300. VectorCopy( origin, object->s.pos.trBase );
  301. //Velocity
  302. VectorClear( object->s.pos.trDelta );
  303. object->s.pos.trTime = level.time;
  304. //VectorScale( dir, 300, object->s.pos.trDelta );
  305. //object->s.pos.trTime = level.time;
  306. //Angles
  307. VectorCopy( angles, object->s.angles );
  308. VectorCopy( object->s.angles, object->s.apos.trBase );
  309. //Angular Velocity
  310. VectorClear( object->s.apos.trDelta );
  311. object->s.apos.trTime = level.time;
  312. //VectorSet( object->s.apos.trDelta, 300, 0, 0 );
  313. //object->s.apos.trTime = level.time;
  314. gi.linkentity( object );
  315. return object;
  316. }