g_mover.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. //
  19. #include "g_local.h"
  20. /*
  21. ===============================================================================
  22. PUSHMOVE
  23. ===============================================================================
  24. */
  25. void MatchTeam( gentity_t *teamLeader, int moverState, int time );
  26. typedef struct {
  27. gentity_t *ent;
  28. vec3_t origin;
  29. vec3_t angles;
  30. float deltayaw;
  31. } pushed_t;
  32. pushed_t pushed[MAX_GENTITIES], *pushed_p;
  33. /*
  34. ============
  35. G_TestEntityPosition
  36. ============
  37. */
  38. gentity_t *G_TestEntityPosition( gentity_t *ent ) {
  39. trace_t tr;
  40. int mask;
  41. if ( ent->clipmask ) {
  42. mask = ent->clipmask;
  43. } else {
  44. mask = MASK_SOLID;
  45. }
  46. if ( ent->client ) {
  47. trap_Trace( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, ent->client->ps.origin, ent->s.number, mask );
  48. } else {
  49. trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, ent->s.pos.trBase, ent->s.number, mask );
  50. }
  51. if (tr.startsolid)
  52. return &g_entities[ tr.entityNum ];
  53. return NULL;
  54. }
  55. /*
  56. ================
  57. G_CreateRotationMatrix
  58. ================
  59. */
  60. void G_CreateRotationMatrix(vec3_t angles, vec3_t matrix[3]) {
  61. AngleVectors(angles, matrix[0], matrix[1], matrix[2]);
  62. VectorInverse(matrix[1]);
  63. }
  64. /*
  65. ================
  66. G_TransposeMatrix
  67. ================
  68. */
  69. void G_TransposeMatrix(vec3_t matrix[3], vec3_t transpose[3]) {
  70. int i, j;
  71. for (i = 0; i < 3; i++) {
  72. for (j = 0; j < 3; j++) {
  73. transpose[i][j] = matrix[j][i];
  74. }
  75. }
  76. }
  77. /*
  78. ================
  79. G_RotatePoint
  80. ================
  81. */
  82. void G_RotatePoint(vec3_t point, vec3_t matrix[3]) {
  83. vec3_t tvec;
  84. VectorCopy(point, tvec);
  85. point[0] = DotProduct(matrix[0], tvec);
  86. point[1] = DotProduct(matrix[1], tvec);
  87. point[2] = DotProduct(matrix[2], tvec);
  88. }
  89. /*
  90. ==================
  91. G_TryPushingEntity
  92. Returns qfalse if the move is blocked
  93. ==================
  94. */
  95. qboolean G_TryPushingEntity( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) {
  96. vec3_t matrix[3], transpose[3];
  97. vec3_t org, org2, move2;
  98. gentity_t *block;
  99. // EF_MOVER_STOP will just stop when contacting another entity
  100. // instead of pushing it, but entities can still ride on top of it
  101. if ( ( pusher->s.eFlags & EF_MOVER_STOP ) &&
  102. check->s.groundEntityNum != pusher->s.number ) {
  103. return qfalse;
  104. }
  105. // save off the old position
  106. if (pushed_p > &pushed[MAX_GENTITIES]) {
  107. G_Error( "pushed_p > &pushed[MAX_GENTITIES]" );
  108. }
  109. pushed_p->ent = check;
  110. VectorCopy (check->s.pos.trBase, pushed_p->origin);
  111. VectorCopy (check->s.apos.trBase, pushed_p->angles);
  112. if ( check->client ) {
  113. pushed_p->deltayaw = check->client->ps.delta_angles[YAW];
  114. VectorCopy (check->client->ps.origin, pushed_p->origin);
  115. }
  116. pushed_p++;
  117. // try moving the contacted entity
  118. // figure movement due to the pusher's amove
  119. G_CreateRotationMatrix( amove, transpose );
  120. G_TransposeMatrix( transpose, matrix );
  121. if ( check->client ) {
  122. VectorSubtract (check->client->ps.origin, pusher->r.currentOrigin, org);
  123. }
  124. else {
  125. VectorSubtract (check->s.pos.trBase, pusher->r.currentOrigin, org);
  126. }
  127. VectorCopy( org, org2 );
  128. G_RotatePoint( org2, matrix );
  129. VectorSubtract (org2, org, move2);
  130. // add movement
  131. VectorAdd (check->s.pos.trBase, move, check->s.pos.trBase);
  132. VectorAdd (check->s.pos.trBase, move2, check->s.pos.trBase);
  133. if ( check->client ) {
  134. VectorAdd (check->client->ps.origin, move, check->client->ps.origin);
  135. VectorAdd (check->client->ps.origin, move2, check->client->ps.origin);
  136. // make sure the client's view rotates when on a rotating mover
  137. check->client->ps.delta_angles[YAW] += ANGLE2SHORT(amove[YAW]);
  138. }
  139. // may have pushed them off an edge
  140. if ( check->s.groundEntityNum != pusher->s.number ) {
  141. check->s.groundEntityNum = -1;
  142. }
  143. block = G_TestEntityPosition( check );
  144. if (!block) {
  145. // pushed ok
  146. if ( check->client ) {
  147. VectorCopy( check->client->ps.origin, check->r.currentOrigin );
  148. } else {
  149. VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
  150. }
  151. trap_LinkEntity (check);
  152. return qtrue;
  153. }
  154. // if it is ok to leave in the old position, do it
  155. // this is only relevent for riding entities, not pushed
  156. // Sliding trapdoors can cause this.
  157. VectorCopy( (pushed_p-1)->origin, check->s.pos.trBase);
  158. if ( check->client ) {
  159. VectorCopy( (pushed_p-1)->origin, check->client->ps.origin);
  160. }
  161. VectorCopy( (pushed_p-1)->angles, check->s.apos.trBase );
  162. block = G_TestEntityPosition (check);
  163. if ( !block ) {
  164. check->s.groundEntityNum = -1;
  165. pushed_p--;
  166. return qtrue;
  167. }
  168. // blocked
  169. return qfalse;
  170. }
  171. /*
  172. ==================
  173. G_CheckProxMinePosition
  174. ==================
  175. */
  176. qboolean G_CheckProxMinePosition( gentity_t *check ) {
  177. vec3_t start, end;
  178. trace_t tr;
  179. VectorMA(check->s.pos.trBase, 0.125, check->movedir, start);
  180. VectorMA(check->s.pos.trBase, 2, check->movedir, end);
  181. trap_Trace( &tr, start, NULL, NULL, end, check->s.number, MASK_SOLID );
  182. if (tr.startsolid || tr.fraction < 1)
  183. return qfalse;
  184. return qtrue;
  185. }
  186. /*
  187. ==================
  188. G_TryPushingProxMine
  189. ==================
  190. */
  191. qboolean G_TryPushingProxMine( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) {
  192. vec3_t forward, right, up;
  193. vec3_t org, org2, move2;
  194. int ret;
  195. // we need this for pushing things later
  196. VectorSubtract (vec3_origin, amove, org);
  197. AngleVectors (org, forward, right, up);
  198. // try moving the contacted entity
  199. VectorAdd (check->s.pos.trBase, move, check->s.pos.trBase);
  200. // figure movement due to the pusher's amove
  201. VectorSubtract (check->s.pos.trBase, pusher->r.currentOrigin, org);
  202. org2[0] = DotProduct (org, forward);
  203. org2[1] = -DotProduct (org, right);
  204. org2[2] = DotProduct (org, up);
  205. VectorSubtract (org2, org, move2);
  206. VectorAdd (check->s.pos.trBase, move2, check->s.pos.trBase);
  207. ret = G_CheckProxMinePosition( check );
  208. if (ret) {
  209. VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
  210. trap_LinkEntity (check);
  211. }
  212. return ret;
  213. }
  214. void G_ExplodeMissile( gentity_t *ent );
  215. /*
  216. ============
  217. G_MoverPush
  218. Objects need to be moved back on a failed push,
  219. otherwise riders would continue to slide.
  220. If qfalse is returned, *obstacle will be the blocking entity
  221. ============
  222. */
  223. qboolean G_MoverPush( gentity_t *pusher, vec3_t move, vec3_t amove, gentity_t **obstacle ) {
  224. int i, e;
  225. gentity_t *check;
  226. vec3_t mins, maxs;
  227. pushed_t *p;
  228. int entityList[MAX_GENTITIES];
  229. int listedEntities;
  230. vec3_t totalMins, totalMaxs;
  231. *obstacle = NULL;
  232. // mins/maxs are the bounds at the destination
  233. // totalMins / totalMaxs are the bounds for the entire move
  234. if ( pusher->r.currentAngles[0] || pusher->r.currentAngles[1] || pusher->r.currentAngles[2]
  235. || amove[0] || amove[1] || amove[2] ) {
  236. float radius;
  237. radius = RadiusFromBounds( pusher->r.mins, pusher->r.maxs );
  238. for ( i = 0 ; i < 3 ; i++ ) {
  239. mins[i] = pusher->r.currentOrigin[i] + move[i] - radius;
  240. maxs[i] = pusher->r.currentOrigin[i] + move[i] + radius;
  241. totalMins[i] = mins[i] - move[i];
  242. totalMaxs[i] = maxs[i] - move[i];
  243. }
  244. } else {
  245. for (i=0 ; i<3 ; i++) {
  246. mins[i] = pusher->r.absmin[i] + move[i];
  247. maxs[i] = pusher->r.absmax[i] + move[i];
  248. }
  249. VectorCopy( pusher->r.absmin, totalMins );
  250. VectorCopy( pusher->r.absmax, totalMaxs );
  251. for (i=0 ; i<3 ; i++) {
  252. if ( move[i] > 0 ) {
  253. totalMaxs[i] += move[i];
  254. } else {
  255. totalMins[i] += move[i];
  256. }
  257. }
  258. }
  259. // unlink the pusher so we don't get it in the entityList
  260. trap_UnlinkEntity( pusher );
  261. listedEntities = trap_EntitiesInBox( totalMins, totalMaxs, entityList, MAX_GENTITIES );
  262. // move the pusher to it's final position
  263. VectorAdd( pusher->r.currentOrigin, move, pusher->r.currentOrigin );
  264. VectorAdd( pusher->r.currentAngles, amove, pusher->r.currentAngles );
  265. trap_LinkEntity( pusher );
  266. // see if any solid entities are inside the final position
  267. for ( e = 0 ; e < listedEntities ; e++ ) {
  268. check = &g_entities[ entityList[ e ] ];
  269. #ifdef MISSIONPACK
  270. if ( check->s.eType == ET_MISSILE ) {
  271. // if it is a prox mine
  272. if ( !strcmp(check->classname, "prox mine") ) {
  273. // if this prox mine is attached to this mover try to move it with the pusher
  274. if ( check->enemy == pusher ) {
  275. if (!G_TryPushingProxMine( check, pusher, move, amove )) {
  276. //explode
  277. check->s.loopSound = 0;
  278. G_AddEvent( check, EV_PROXIMITY_MINE_TRIGGER, 0 );
  279. G_ExplodeMissile(check);
  280. if (check->activator) {
  281. G_FreeEntity(check->activator);
  282. check->activator = NULL;
  283. }
  284. //G_Printf("prox mine explodes\n");
  285. }
  286. }
  287. else {
  288. //check if the prox mine is crushed by the mover
  289. if (!G_CheckProxMinePosition( check )) {
  290. //explode
  291. check->s.loopSound = 0;
  292. G_AddEvent( check, EV_PROXIMITY_MINE_TRIGGER, 0 );
  293. G_ExplodeMissile(check);
  294. if (check->activator) {
  295. G_FreeEntity(check->activator);
  296. check->activator = NULL;
  297. }
  298. //G_Printf("prox mine explodes\n");
  299. }
  300. }
  301. continue;
  302. }
  303. }
  304. #endif
  305. // only push items and players
  306. if ( check->s.eType != ET_ITEM && check->s.eType != ET_PLAYER && !check->physicsObject ) {
  307. continue;
  308. }
  309. // if the entity is standing on the pusher, it will definitely be moved
  310. if ( check->s.groundEntityNum != pusher->s.number ) {
  311. // see if the ent needs to be tested
  312. if ( check->r.absmin[0] >= maxs[0]
  313. || check->r.absmin[1] >= maxs[1]
  314. || check->r.absmin[2] >= maxs[2]
  315. || check->r.absmax[0] <= mins[0]
  316. || check->r.absmax[1] <= mins[1]
  317. || check->r.absmax[2] <= mins[2] ) {
  318. continue;
  319. }
  320. // see if the ent's bbox is inside the pusher's final position
  321. // this does allow a fast moving object to pass through a thin entity...
  322. if (!G_TestEntityPosition (check)) {
  323. continue;
  324. }
  325. }
  326. // the entity needs to be pushed
  327. if ( G_TryPushingEntity( check, pusher, move, amove ) ) {
  328. continue;
  329. }
  330. // the move was blocked an entity
  331. // bobbing entities are instant-kill and never get blocked
  332. if ( pusher->s.pos.trType == TR_SINE || pusher->s.apos.trType == TR_SINE ) {
  333. G_Damage( check, pusher, pusher, NULL, NULL, 99999, 0, MOD_CRUSH );
  334. continue;
  335. }
  336. // save off the obstacle so we can call the block function (crush, etc)
  337. *obstacle = check;
  338. // move back any entities we already moved
  339. // go backwards, so if the same entity was pushed
  340. // twice, it goes back to the original position
  341. for ( p=pushed_p-1 ; p>=pushed ; p-- ) {
  342. VectorCopy (p->origin, p->ent->s.pos.trBase);
  343. VectorCopy (p->angles, p->ent->s.apos.trBase);
  344. if ( p->ent->client ) {
  345. p->ent->client->ps.delta_angles[YAW] = p->deltayaw;
  346. VectorCopy (p->origin, p->ent->client->ps.origin);
  347. }
  348. trap_LinkEntity (p->ent);
  349. }
  350. return qfalse;
  351. }
  352. return qtrue;
  353. }
  354. /*
  355. =================
  356. G_MoverTeam
  357. =================
  358. */
  359. void G_MoverTeam( gentity_t *ent ) {
  360. vec3_t move, amove;
  361. gentity_t *part, *obstacle;
  362. vec3_t origin, angles;
  363. obstacle = NULL;
  364. // make sure all team slaves can move before commiting
  365. // any moves or calling any think functions
  366. // if the move is blocked, all moved objects will be backed out
  367. pushed_p = pushed;
  368. for (part = ent ; part ; part=part->teamchain) {
  369. // get current position
  370. BG_EvaluateTrajectory( &part->s.pos, level.time, origin );
  371. BG_EvaluateTrajectory( &part->s.apos, level.time, angles );
  372. VectorSubtract( origin, part->r.currentOrigin, move );
  373. VectorSubtract( angles, part->r.currentAngles, amove );
  374. if ( !G_MoverPush( part, move, amove, &obstacle ) ) {
  375. break; // move was blocked
  376. }
  377. }
  378. if (part) {
  379. // go back to the previous position
  380. for ( part = ent ; part ; part = part->teamchain ) {
  381. part->s.pos.trTime += level.time - level.previousTime;
  382. part->s.apos.trTime += level.time - level.previousTime;
  383. BG_EvaluateTrajectory( &part->s.pos, level.time, part->r.currentOrigin );
  384. BG_EvaluateTrajectory( &part->s.apos, level.time, part->r.currentAngles );
  385. trap_LinkEntity( part );
  386. }
  387. // if the pusher has a "blocked" function, call it
  388. if (ent->blocked) {
  389. ent->blocked( ent, obstacle );
  390. }
  391. return;
  392. }
  393. // the move succeeded
  394. for ( part = ent ; part ; part = part->teamchain ) {
  395. // call the reached function if time is at or past end point
  396. if ( part->s.pos.trType == TR_LINEAR_STOP ) {
  397. if ( level.time >= part->s.pos.trTime + part->s.pos.trDuration ) {
  398. if ( part->reached ) {
  399. part->reached( part );
  400. }
  401. }
  402. }
  403. }
  404. }
  405. /*
  406. ================
  407. G_RunMover
  408. ================
  409. */
  410. void G_RunMover( gentity_t *ent ) {
  411. // if not a team captain, don't do anything, because
  412. // the captain will handle everything
  413. if ( ent->flags & FL_TEAMSLAVE ) {
  414. return;
  415. }
  416. // if stationary at one of the positions, don't move anything
  417. if ( ent->s.pos.trType != TR_STATIONARY || ent->s.apos.trType != TR_STATIONARY ) {
  418. G_MoverTeam( ent );
  419. }
  420. // check think function
  421. G_RunThink( ent );
  422. }
  423. /*
  424. ============================================================================
  425. GENERAL MOVERS
  426. Doors, plats, and buttons are all binary (two position) movers
  427. Pos1 is "at rest", pos2 is "activated"
  428. ============================================================================
  429. */
  430. /*
  431. ===============
  432. SetMoverState
  433. ===============
  434. */
  435. void SetMoverState( gentity_t *ent, moverState_t moverState, int time ) {
  436. vec3_t delta;
  437. float f;
  438. ent->moverState = moverState;
  439. ent->s.pos.trTime = time;
  440. switch( moverState ) {
  441. case MOVER_POS1:
  442. VectorCopy( ent->pos1, ent->s.pos.trBase );
  443. ent->s.pos.trType = TR_STATIONARY;
  444. break;
  445. case MOVER_POS2:
  446. VectorCopy( ent->pos2, ent->s.pos.trBase );
  447. ent->s.pos.trType = TR_STATIONARY;
  448. break;
  449. case MOVER_1TO2:
  450. VectorCopy( ent->pos1, ent->s.pos.trBase );
  451. VectorSubtract( ent->pos2, ent->pos1, delta );
  452. f = 1000.0 / ent->s.pos.trDuration;
  453. VectorScale( delta, f, ent->s.pos.trDelta );
  454. ent->s.pos.trType = TR_LINEAR_STOP;
  455. break;
  456. case MOVER_2TO1:
  457. VectorCopy( ent->pos2, ent->s.pos.trBase );
  458. VectorSubtract( ent->pos1, ent->pos2, delta );
  459. f = 1000.0 / ent->s.pos.trDuration;
  460. VectorScale( delta, f, ent->s.pos.trDelta );
  461. ent->s.pos.trType = TR_LINEAR_STOP;
  462. break;
  463. }
  464. BG_EvaluateTrajectory( &ent->s.pos, level.time, ent->r.currentOrigin );
  465. trap_LinkEntity( ent );
  466. }
  467. /*
  468. ================
  469. MatchTeam
  470. All entities in a mover team will move from pos1 to pos2
  471. in the same amount of time
  472. ================
  473. */
  474. void MatchTeam( gentity_t *teamLeader, int moverState, int time ) {
  475. gentity_t *slave;
  476. for ( slave = teamLeader ; slave ; slave = slave->teamchain ) {
  477. SetMoverState( slave, moverState, time );
  478. }
  479. }
  480. /*
  481. ================
  482. ReturnToPos1
  483. ================
  484. */
  485. void ReturnToPos1( gentity_t *ent ) {
  486. MatchTeam( ent, MOVER_2TO1, level.time );
  487. // looping sound
  488. ent->s.loopSound = ent->soundLoop;
  489. // starting sound
  490. if ( ent->sound2to1 ) {
  491. G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  492. }
  493. }
  494. /*
  495. ================
  496. Reached_BinaryMover
  497. ================
  498. */
  499. void Reached_BinaryMover( gentity_t *ent ) {
  500. // stop the looping sound
  501. ent->s.loopSound = ent->soundLoop;
  502. if ( ent->moverState == MOVER_1TO2 ) {
  503. // reached pos2
  504. SetMoverState( ent, MOVER_POS2, level.time );
  505. // play sound
  506. if ( ent->soundPos2 ) {
  507. G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
  508. }
  509. // return to pos1 after a delay
  510. ent->think = ReturnToPos1;
  511. ent->nextthink = level.time + ent->wait;
  512. // fire targets
  513. if ( !ent->activator ) {
  514. ent->activator = ent;
  515. }
  516. G_UseTargets( ent, ent->activator );
  517. } else if ( ent->moverState == MOVER_2TO1 ) {
  518. // reached pos1
  519. SetMoverState( ent, MOVER_POS1, level.time );
  520. // play sound
  521. if ( ent->soundPos1 ) {
  522. G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );
  523. }
  524. // close areaportals
  525. if ( ent->teammaster == ent || !ent->teammaster ) {
  526. trap_AdjustAreaPortalState( ent, qfalse );
  527. }
  528. } else {
  529. G_Error( "Reached_BinaryMover: bad moverState" );
  530. }
  531. }
  532. /*
  533. ================
  534. Use_BinaryMover
  535. ================
  536. */
  537. void Use_BinaryMover( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
  538. int total;
  539. int partial;
  540. // only the master should be used
  541. if ( ent->flags & FL_TEAMSLAVE ) {
  542. Use_BinaryMover( ent->teammaster, other, activator );
  543. return;
  544. }
  545. ent->activator = activator;
  546. if ( ent->moverState == MOVER_POS1 ) {
  547. // start moving 50 msec later, becase if this was player
  548. // triggered, level.time hasn't been advanced yet
  549. MatchTeam( ent, MOVER_1TO2, level.time + 50 );
  550. // starting sound
  551. if ( ent->sound1to2 ) {
  552. G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  553. }
  554. // looping sound
  555. ent->s.loopSound = ent->soundLoop;
  556. // open areaportal
  557. if ( ent->teammaster == ent || !ent->teammaster ) {
  558. trap_AdjustAreaPortalState( ent, qtrue );
  559. }
  560. return;
  561. }
  562. // if all the way up, just delay before coming down
  563. if ( ent->moverState == MOVER_POS2 ) {
  564. ent->nextthink = level.time + ent->wait;
  565. return;
  566. }
  567. // only partway down before reversing
  568. if ( ent->moverState == MOVER_2TO1 ) {
  569. total = ent->s.pos.trDuration;
  570. partial = level.time - ent->s.pos.trTime;
  571. if ( partial > total ) {
  572. partial = total;
  573. }
  574. MatchTeam( ent, MOVER_1TO2, level.time - ( total - partial ) );
  575. if ( ent->sound1to2 ) {
  576. G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  577. }
  578. return;
  579. }
  580. // only partway up before reversing
  581. if ( ent->moverState == MOVER_1TO2 ) {
  582. total = ent->s.pos.trDuration;
  583. partial = level.time - ent->s.pos.trTime;
  584. if ( partial > total ) {
  585. partial = total;
  586. }
  587. MatchTeam( ent, MOVER_2TO1, level.time - ( total - partial ) );
  588. if ( ent->sound2to1 ) {
  589. G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  590. }
  591. return;
  592. }
  593. }
  594. /*
  595. ================
  596. InitMover
  597. "pos1", "pos2", and "speed" should be set before calling,
  598. so the movement delta can be calculated
  599. ================
  600. */
  601. void InitMover( gentity_t *ent ) {
  602. vec3_t move;
  603. float distance;
  604. float light;
  605. vec3_t color;
  606. qboolean lightSet, colorSet;
  607. char *sound;
  608. // if the "model2" key is set, use a seperate model
  609. // for drawing, but clip against the brushes
  610. if ( ent->model2 ) {
  611. ent->s.modelindex2 = G_ModelIndex( ent->model2 );
  612. }
  613. // if the "loopsound" key is set, use a constant looping sound when moving
  614. if ( G_SpawnString( "noise", "100", &sound ) ) {
  615. ent->s.loopSound = G_SoundIndex( sound );
  616. }
  617. // if the "color" or "light" keys are set, setup constantLight
  618. lightSet = G_SpawnFloat( "light", "100", &light );
  619. colorSet = G_SpawnVector( "color", "1 1 1", color );
  620. if ( lightSet || colorSet ) {
  621. int r, g, b, i;
  622. r = color[0] * 255;
  623. if ( r > 255 ) {
  624. r = 255;
  625. }
  626. g = color[1] * 255;
  627. if ( g > 255 ) {
  628. g = 255;
  629. }
  630. b = color[2] * 255;
  631. if ( b > 255 ) {
  632. b = 255;
  633. }
  634. i = light / 4;
  635. if ( i > 255 ) {
  636. i = 255;
  637. }
  638. ent->s.constantLight = r | ( g << 8 ) | ( b << 16 ) | ( i << 24 );
  639. }
  640. ent->use = Use_BinaryMover;
  641. ent->reached = Reached_BinaryMover;
  642. ent->moverState = MOVER_POS1;
  643. ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
  644. ent->s.eType = ET_MOVER;
  645. VectorCopy (ent->pos1, ent->r.currentOrigin);
  646. trap_LinkEntity (ent);
  647. ent->s.pos.trType = TR_STATIONARY;
  648. VectorCopy( ent->pos1, ent->s.pos.trBase );
  649. // calculate time to reach second position from speed
  650. VectorSubtract( ent->pos2, ent->pos1, move );
  651. distance = VectorLength( move );
  652. if ( ! ent->speed ) {
  653. ent->speed = 100;
  654. }
  655. VectorScale( move, ent->speed, ent->s.pos.trDelta );
  656. ent->s.pos.trDuration = distance * 1000 / ent->speed;
  657. if ( ent->s.pos.trDuration <= 0 ) {
  658. ent->s.pos.trDuration = 1;
  659. }
  660. }
  661. /*
  662. ===============================================================================
  663. DOOR
  664. A use can be triggered either by a touch function, by being shot, or by being
  665. targeted by another entity.
  666. ===============================================================================
  667. */
  668. /*
  669. ================
  670. Blocked_Door
  671. ================
  672. */
  673. void Blocked_Door( gentity_t *ent, gentity_t *other ) {
  674. // remove anything other than a client
  675. if ( !other->client ) {
  676. // except CTF flags!!!!
  677. if( other->s.eType == ET_ITEM && other->item->giType == IT_TEAM ) {
  678. Team_DroppedFlagThink( other );
  679. return;
  680. }
  681. G_TempEntity( other->s.origin, EV_ITEM_POP );
  682. G_FreeEntity( other );
  683. return;
  684. }
  685. if ( ent->damage ) {
  686. G_Damage( other, ent, ent, NULL, NULL, ent->damage, 0, MOD_CRUSH );
  687. }
  688. if ( ent->spawnflags & 4 ) {
  689. return; // crushers don't reverse
  690. }
  691. // reverse direction
  692. Use_BinaryMover( ent, ent, other );
  693. }
  694. /*
  695. ================
  696. Touch_DoorTriggerSpectator
  697. ================
  698. */
  699. static void Touch_DoorTriggerSpectator( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  700. int i, axis;
  701. vec3_t origin, dir, angles;
  702. axis = ent->count;
  703. VectorClear(dir);
  704. if (fabs(other->s.origin[axis] - ent->r.absmax[axis]) <
  705. fabs(other->s.origin[axis] - ent->r.absmin[axis])) {
  706. origin[axis] = ent->r.absmin[axis] - 10;
  707. dir[axis] = -1;
  708. }
  709. else {
  710. origin[axis] = ent->r.absmax[axis] + 10;
  711. dir[axis] = 1;
  712. }
  713. for (i = 0; i < 3; i++) {
  714. if (i == axis) continue;
  715. origin[i] = (ent->r.absmin[i] + ent->r.absmax[i]) * 0.5;
  716. }
  717. vectoangles(dir, angles);
  718. TeleportPlayer(other, origin, angles );
  719. }
  720. /*
  721. ================
  722. Touch_DoorTrigger
  723. ================
  724. */
  725. void Touch_DoorTrigger( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  726. if ( other->client && other->client->sess.sessionTeam == TEAM_SPECTATOR ) {
  727. // if the door is not open and not opening
  728. if ( ent->parent->moverState != MOVER_1TO2 &&
  729. ent->parent->moverState != MOVER_POS2) {
  730. Touch_DoorTriggerSpectator( ent, other, trace );
  731. }
  732. }
  733. else if ( ent->parent->moverState != MOVER_1TO2 ) {
  734. Use_BinaryMover( ent->parent, ent, other );
  735. }
  736. }
  737. /*
  738. ======================
  739. Think_SpawnNewDoorTrigger
  740. All of the parts of a door have been spawned, so create
  741. a trigger that encloses all of them
  742. ======================
  743. */
  744. void Think_SpawnNewDoorTrigger( gentity_t *ent ) {
  745. gentity_t *other;
  746. vec3_t mins, maxs;
  747. int i, best;
  748. // set all of the slaves as shootable
  749. for ( other = ent ; other ; other = other->teamchain ) {
  750. other->takedamage = qtrue;
  751. }
  752. // find the bounds of everything on the team
  753. VectorCopy (ent->r.absmin, mins);
  754. VectorCopy (ent->r.absmax, maxs);
  755. for (other = ent->teamchain ; other ; other=other->teamchain) {
  756. AddPointToBounds (other->r.absmin, mins, maxs);
  757. AddPointToBounds (other->r.absmax, mins, maxs);
  758. }
  759. // find the thinnest axis, which will be the one we expand
  760. best = 0;
  761. for ( i = 1 ; i < 3 ; i++ ) {
  762. if ( maxs[i] - mins[i] < maxs[best] - mins[best] ) {
  763. best = i;
  764. }
  765. }
  766. maxs[best] += 120;
  767. mins[best] -= 120;
  768. // create a trigger with this size
  769. other = G_Spawn ();
  770. other->classname = "door_trigger";
  771. VectorCopy (mins, other->r.mins);
  772. VectorCopy (maxs, other->r.maxs);
  773. other->parent = ent;
  774. other->r.contents = CONTENTS_TRIGGER;
  775. other->touch = Touch_DoorTrigger;
  776. // remember the thinnest axis
  777. other->count = best;
  778. trap_LinkEntity (other);
  779. MatchTeam( ent, ent->moverState, level.time );
  780. }
  781. void Think_MatchTeam( gentity_t *ent ) {
  782. MatchTeam( ent, ent->moverState, level.time );
  783. }
  784. /*QUAKED func_door (0 .5 .8) ? START_OPEN x CRUSHER
  785. TOGGLE wait in both the start and end states for a trigger event.
  786. START_OPEN the door to moves to its destination when spawned, and operate in reverse. It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
  787. NOMONSTER monsters will not trigger this door
  788. "model2" .md3 model to also draw
  789. "angle" determines the opening direction
  790. "targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
  791. "speed" movement speed (100 default)
  792. "wait" wait before returning (3 default, -1 = never return)
  793. "lip" lip remaining at end of move (8 default)
  794. "dmg" damage to inflict when blocked (2 default)
  795. "color" constantLight color
  796. "light" constantLight radius
  797. "health" if set, the door must be shot open
  798. */
  799. void SP_func_door (gentity_t *ent) {
  800. vec3_t abs_movedir;
  801. float distance;
  802. vec3_t size;
  803. float lip;
  804. ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/doors/dr1_strt.wav");
  805. ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/doors/dr1_end.wav");
  806. ent->blocked = Blocked_Door;
  807. // default speed of 400
  808. if (!ent->speed)
  809. ent->speed = 400;
  810. // default wait of 2 seconds
  811. if (!ent->wait)
  812. ent->wait = 2;
  813. ent->wait *= 1000;
  814. // default lip of 8 units
  815. G_SpawnFloat( "lip", "8", &lip );
  816. // default damage of 2 points
  817. G_SpawnInt( "dmg", "2", &ent->damage );
  818. // first position at start
  819. VectorCopy( ent->s.origin, ent->pos1 );
  820. // calculate second position
  821. trap_SetBrushModel( ent, ent->model );
  822. G_SetMovedir (ent->s.angles, ent->movedir);
  823. abs_movedir[0] = fabs(ent->movedir[0]);
  824. abs_movedir[1] = fabs(ent->movedir[1]);
  825. abs_movedir[2] = fabs(ent->movedir[2]);
  826. VectorSubtract( ent->r.maxs, ent->r.mins, size );
  827. distance = DotProduct( abs_movedir, size ) - lip;
  828. VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );
  829. // if "start_open", reverse position 1 and 2
  830. if ( ent->spawnflags & 1 ) {
  831. vec3_t temp;
  832. VectorCopy( ent->pos2, temp );
  833. VectorCopy( ent->s.origin, ent->pos2 );
  834. VectorCopy( temp, ent->pos1 );
  835. }
  836. InitMover( ent );
  837. ent->nextthink = level.time + FRAMETIME;
  838. if ( ! (ent->flags & FL_TEAMSLAVE ) ) {
  839. int health;
  840. G_SpawnInt( "health", "0", &health );
  841. if ( health ) {
  842. ent->takedamage = qtrue;
  843. }
  844. if ( ent->targetname || health ) {
  845. // non touch/shoot doors
  846. ent->think = Think_MatchTeam;
  847. } else {
  848. ent->think = Think_SpawnNewDoorTrigger;
  849. }
  850. }
  851. }
  852. /*
  853. ===============================================================================
  854. PLAT
  855. ===============================================================================
  856. */
  857. /*
  858. ==============
  859. Touch_Plat
  860. Don't allow decent if a living player is on it
  861. ===============
  862. */
  863. void Touch_Plat( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  864. if ( !other->client || other->client->ps.stats[STAT_HEALTH] <= 0 ) {
  865. return;
  866. }
  867. // delay return-to-pos1 by one second
  868. if ( ent->moverState == MOVER_POS2 ) {
  869. ent->nextthink = level.time + 1000;
  870. }
  871. }
  872. /*
  873. ==============
  874. Touch_PlatCenterTrigger
  875. If the plat is at the bottom position, start it going up
  876. ===============
  877. */
  878. void Touch_PlatCenterTrigger(gentity_t *ent, gentity_t *other, trace_t *trace ) {
  879. if ( !other->client ) {
  880. return;
  881. }
  882. if ( ent->parent->moverState == MOVER_POS1 ) {
  883. Use_BinaryMover( ent->parent, ent, other );
  884. }
  885. }
  886. /*
  887. ================
  888. SpawnPlatTrigger
  889. Spawn a trigger in the middle of the plat's low position
  890. Elevator cars require that the trigger extend through the entire low position,
  891. not just sit on top of it.
  892. ================
  893. */
  894. void SpawnPlatTrigger( gentity_t *ent ) {
  895. gentity_t *trigger;
  896. vec3_t tmin, tmax;
  897. // the middle trigger will be a thin trigger just
  898. // above the starting position
  899. trigger = G_Spawn();
  900. trigger->classname = "plat_trigger";
  901. trigger->touch = Touch_PlatCenterTrigger;
  902. trigger->r.contents = CONTENTS_TRIGGER;
  903. trigger->parent = ent;
  904. tmin[0] = ent->pos1[0] + ent->r.mins[0] + 33;
  905. tmin[1] = ent->pos1[1] + ent->r.mins[1] + 33;
  906. tmin[2] = ent->pos1[2] + ent->r.mins[2];
  907. tmax[0] = ent->pos1[0] + ent->r.maxs[0] - 33;
  908. tmax[1] = ent->pos1[1] + ent->r.maxs[1] - 33;
  909. tmax[2] = ent->pos1[2] + ent->r.maxs[2] + 8;
  910. if ( tmax[0] <= tmin[0] ) {
  911. tmin[0] = ent->pos1[0] + (ent->r.mins[0] + ent->r.maxs[0]) *0.5;
  912. tmax[0] = tmin[0] + 1;
  913. }
  914. if ( tmax[1] <= tmin[1] ) {
  915. tmin[1] = ent->pos1[1] + (ent->r.mins[1] + ent->r.maxs[1]) *0.5;
  916. tmax[1] = tmin[1] + 1;
  917. }
  918. VectorCopy (tmin, trigger->r.mins);
  919. VectorCopy (tmax, trigger->r.maxs);
  920. trap_LinkEntity (trigger);
  921. }
  922. /*QUAKED func_plat (0 .5 .8) ?
  923. Plats are always drawn in the extended position so they will light correctly.
  924. "lip" default 8, protrusion above rest position
  925. "height" total height of movement, defaults to model height
  926. "speed" overrides default 200.
  927. "dmg" overrides default 2
  928. "model2" .md3 model to also draw
  929. "color" constantLight color
  930. "light" constantLight radius
  931. */
  932. void SP_func_plat (gentity_t *ent) {
  933. float lip, height;
  934. ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/plats/pt1_strt.wav");
  935. ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/plats/pt1_end.wav");
  936. VectorClear (ent->s.angles);
  937. G_SpawnFloat( "speed", "200", &ent->speed );
  938. G_SpawnInt( "dmg", "2", &ent->damage );
  939. G_SpawnFloat( "wait", "1", &ent->wait );
  940. G_SpawnFloat( "lip", "8", &lip );
  941. ent->wait = 1000;
  942. // create second position
  943. trap_SetBrushModel( ent, ent->model );
  944. if ( !G_SpawnFloat( "height", "0", &height ) ) {
  945. height = (ent->r.maxs[2] - ent->r.mins[2]) - lip;
  946. }
  947. // pos1 is the rest (bottom) position, pos2 is the top
  948. VectorCopy( ent->s.origin, ent->pos2 );
  949. VectorCopy( ent->pos2, ent->pos1 );
  950. ent->pos1[2] -= height;
  951. InitMover( ent );
  952. // touch function keeps the plat from returning while
  953. // a live player is standing on it
  954. ent->touch = Touch_Plat;
  955. ent->blocked = Blocked_Door;
  956. ent->parent = ent; // so it can be treated as a door
  957. // spawn the trigger if one hasn't been custom made
  958. if ( !ent->targetname ) {
  959. SpawnPlatTrigger(ent);
  960. }
  961. }
  962. /*
  963. ===============================================================================
  964. BUTTON
  965. ===============================================================================
  966. */
  967. /*
  968. ==============
  969. Touch_Button
  970. ===============
  971. */
  972. void Touch_Button(gentity_t *ent, gentity_t *other, trace_t *trace ) {
  973. if ( !other->client ) {
  974. return;
  975. }
  976. if ( ent->moverState == MOVER_POS1 ) {
  977. Use_BinaryMover( ent, other, other );
  978. }
  979. }
  980. /*QUAKED func_button (0 .5 .8) ?
  981. When a button is touched, it moves some distance in the direction of it's angle, triggers all of it's targets, waits some time, then returns to it's original position where it can be triggered again.
  982. "model2" .md3 model to also draw
  983. "angle" determines the opening direction
  984. "target" all entities with a matching targetname will be used
  985. "speed" override the default 40 speed
  986. "wait" override the default 1 second wait (-1 = never return)
  987. "lip" override the default 4 pixel lip remaining at end of move
  988. "health" if set, the button must be killed instead of touched
  989. "color" constantLight color
  990. "light" constantLight radius
  991. */
  992. void SP_func_button( gentity_t *ent ) {
  993. vec3_t abs_movedir;
  994. float distance;
  995. vec3_t size;
  996. float lip;
  997. ent->sound1to2 = G_SoundIndex("sound/movers/switches/butn2.wav");
  998. if ( !ent->speed ) {
  999. ent->speed = 40;
  1000. }
  1001. if ( !ent->wait ) {
  1002. ent->wait = 1;
  1003. }
  1004. ent->wait *= 1000;
  1005. // first position
  1006. VectorCopy( ent->s.origin, ent->pos1 );
  1007. // calculate second position
  1008. trap_SetBrushModel( ent, ent->model );
  1009. G_SpawnFloat( "lip", "4", &lip );
  1010. G_SetMovedir( ent->s.angles, ent->movedir );
  1011. abs_movedir[0] = fabs(ent->movedir[0]);
  1012. abs_movedir[1] = fabs(ent->movedir[1]);
  1013. abs_movedir[2] = fabs(ent->movedir[2]);
  1014. VectorSubtract( ent->r.maxs, ent->r.mins, size );
  1015. distance = abs_movedir[0] * size[0] + abs_movedir[1] * size[1] + abs_movedir[2] * size[2] - lip;
  1016. VectorMA (ent->pos1, distance, ent->movedir, ent->pos2);
  1017. if (ent->health) {
  1018. // shootable button
  1019. ent->takedamage = qtrue;
  1020. } else {
  1021. // touchable button
  1022. ent->touch = Touch_Button;
  1023. }
  1024. InitMover( ent );
  1025. }
  1026. /*
  1027. ===============================================================================
  1028. TRAIN
  1029. ===============================================================================
  1030. */
  1031. #define TRAIN_START_ON 1
  1032. #define TRAIN_TOGGLE 2
  1033. #define TRAIN_BLOCK_STOPS 4
  1034. /*
  1035. ===============
  1036. Think_BeginMoving
  1037. The wait time at a corner has completed, so start moving again
  1038. ===============
  1039. */
  1040. void Think_BeginMoving( gentity_t *ent ) {
  1041. ent->s.pos.trTime = level.time;
  1042. ent->s.pos.trType = TR_LINEAR_STOP;
  1043. }
  1044. /*
  1045. ===============
  1046. Reached_Train
  1047. ===============
  1048. */
  1049. void Reached_Train( gentity_t *ent ) {
  1050. gentity_t *next;
  1051. float speed;
  1052. vec3_t move;
  1053. float length;
  1054. // copy the apropriate values
  1055. next = ent->nextTrain;
  1056. if ( !next || !next->nextTrain ) {
  1057. return; // just stop
  1058. }
  1059. // fire all other targets
  1060. G_UseTargets( next, NULL );
  1061. // set the new trajectory
  1062. ent->nextTrain = next->nextTrain;
  1063. VectorCopy( next->s.origin, ent->pos1 );
  1064. VectorCopy( next->nextTrain->s.origin, ent->pos2 );
  1065. // if the path_corner has a speed, use that
  1066. if ( next->speed ) {
  1067. speed = next->speed;
  1068. } else {
  1069. // otherwise use the train's speed
  1070. speed = ent->speed;
  1071. }
  1072. if ( speed < 1 ) {
  1073. speed = 1;
  1074. }
  1075. // calculate duration
  1076. VectorSubtract( ent->pos2, ent->pos1, move );
  1077. length = VectorLength( move );
  1078. ent->s.pos.trDuration = length * 1000 / speed;
  1079. // looping sound
  1080. ent->s.loopSound = next->soundLoop;
  1081. // start it going
  1082. SetMoverState( ent, MOVER_1TO2, level.time );
  1083. // if there is a "wait" value on the target, don't start moving yet
  1084. if ( next->wait ) {
  1085. ent->nextthink = level.time + next->wait * 1000;
  1086. ent->think = Think_BeginMoving;
  1087. ent->s.pos.trType = TR_STATIONARY;
  1088. }
  1089. }
  1090. /*
  1091. ===============
  1092. Think_SetupTrainTargets
  1093. Link all the corners together
  1094. ===============
  1095. */
  1096. void Think_SetupTrainTargets( gentity_t *ent ) {
  1097. gentity_t *path, *next, *start;
  1098. ent->nextTrain = G_Find( NULL, FOFS(targetname), ent->target );
  1099. if ( !ent->nextTrain ) {
  1100. G_Printf( "func_train at %s with an unfound target\n",
  1101. vtos(ent->r.absmin) );
  1102. return;
  1103. }
  1104. start = NULL;
  1105. for ( path = ent->nextTrain ; path != start ; path = next ) {
  1106. if ( !start ) {
  1107. start = path;
  1108. }
  1109. if ( !path->target ) {
  1110. G_Printf( "Train corner at %s without a target\n",
  1111. vtos(path->s.origin) );
  1112. return;
  1113. }
  1114. // find a path_corner among the targets
  1115. // there may also be other targets that get fired when the corner
  1116. // is reached
  1117. next = NULL;
  1118. do {
  1119. next = G_Find( next, FOFS(targetname), path->target );
  1120. if ( !next ) {
  1121. G_Printf( "Train corner at %s without a target path_corner\n",
  1122. vtos(path->s.origin) );
  1123. return;
  1124. }
  1125. } while ( strcmp( next->classname, "path_corner" ) );
  1126. path->nextTrain = next;
  1127. }
  1128. // start the train moving from the first corner
  1129. Reached_Train( ent );
  1130. }
  1131. /*QUAKED path_corner (.5 .3 0) (-8 -8 -8) (8 8 8)
  1132. Train path corners.
  1133. Target: next path corner and other targets to fire
  1134. "speed" speed to move to the next corner
  1135. "wait" seconds to wait before behining move to next corner
  1136. */
  1137. void SP_path_corner( gentity_t *self ) {
  1138. if ( !self->targetname ) {
  1139. G_Printf ("path_corner with no targetname at %s\n", vtos(self->s.origin));
  1140. G_FreeEntity( self );
  1141. return;
  1142. }
  1143. // path corners don't need to be linked in
  1144. }
  1145. /*QUAKED func_train (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
  1146. A train is a mover that moves between path_corner target points.
  1147. Trains MUST HAVE AN ORIGIN BRUSH.
  1148. The train spawns at the first target it is pointing at.
  1149. "model2" .md3 model to also draw
  1150. "speed" default 100
  1151. "dmg" default 2
  1152. "noise" looping sound to play when the train is in motion
  1153. "target" next path corner
  1154. "color" constantLight color
  1155. "light" constantLight radius
  1156. */
  1157. void SP_func_train (gentity_t *self) {
  1158. VectorClear (self->s.angles);
  1159. if (self->spawnflags & TRAIN_BLOCK_STOPS) {
  1160. self->damage = 0;
  1161. } else {
  1162. if (!self->damage) {
  1163. self->damage = 2;
  1164. }
  1165. }
  1166. if ( !self->speed ) {
  1167. self->speed = 100;
  1168. }
  1169. if ( !self->target ) {
  1170. G_Printf ("func_train without a target at %s\n", vtos(self->r.absmin));
  1171. G_FreeEntity( self );
  1172. return;
  1173. }
  1174. trap_SetBrushModel( self, self->model );
  1175. InitMover( self );
  1176. self->reached = Reached_Train;
  1177. // start trains on the second frame, to make sure their targets have had
  1178. // a chance to spawn
  1179. self->nextthink = level.time + FRAMETIME;
  1180. self->think = Think_SetupTrainTargets;
  1181. }
  1182. /*
  1183. ===============================================================================
  1184. STATIC
  1185. ===============================================================================
  1186. */
  1187. /*QUAKED func_static (0 .5 .8) ?
  1188. A bmodel that just sits there, doing nothing. Can be used for conditional walls and models.
  1189. "model2" .md3 model to also draw
  1190. "color" constantLight color
  1191. "light" constantLight radius
  1192. */
  1193. void SP_func_static( gentity_t *ent ) {
  1194. trap_SetBrushModel( ent, ent->model );
  1195. InitMover( ent );
  1196. VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1197. VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1198. }
  1199. /*
  1200. ===============================================================================
  1201. ROTATING
  1202. ===============================================================================
  1203. */
  1204. /*QUAKED func_rotating (0 .5 .8) ? START_ON - X_AXIS Y_AXIS
  1205. You need to have an origin brush as part of this entity. The center of that brush will be
  1206. the point around which it is rotated. It will rotate around the Z axis by default. You can
  1207. check either the X_AXIS or Y_AXIS box to change that.
  1208. "model2" .md3 model to also draw
  1209. "speed" determines how fast it moves; default value is 100.
  1210. "dmg" damage to inflict when blocked (2 default)
  1211. "color" constantLight color
  1212. "light" constantLight radius
  1213. */
  1214. void SP_func_rotating (gentity_t *ent) {
  1215. if ( !ent->speed ) {
  1216. ent->speed = 100;
  1217. }
  1218. // set the axis of rotation
  1219. ent->s.apos.trType = TR_LINEAR;
  1220. if ( ent->spawnflags & 4 ) {
  1221. ent->s.apos.trDelta[2] = ent->speed;
  1222. } else if ( ent->spawnflags & 8 ) {
  1223. ent->s.apos.trDelta[0] = ent->speed;
  1224. } else {
  1225. ent->s.apos.trDelta[1] = ent->speed;
  1226. }
  1227. if (!ent->damage) {
  1228. ent->damage = 2;
  1229. }
  1230. trap_SetBrushModel( ent, ent->model );
  1231. InitMover( ent );
  1232. VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1233. VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
  1234. VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );
  1235. trap_LinkEntity( ent );
  1236. }
  1237. /*
  1238. ===============================================================================
  1239. BOBBING
  1240. ===============================================================================
  1241. */
  1242. /*QUAKED func_bobbing (0 .5 .8) ? X_AXIS Y_AXIS
  1243. Normally bobs on the Z axis
  1244. "model2" .md3 model to also draw
  1245. "height" amplitude of bob (32 default)
  1246. "speed" seconds to complete a bob cycle (4 default)
  1247. "phase" the 0.0 to 1.0 offset in the cycle to start at
  1248. "dmg" damage to inflict when blocked (2 default)
  1249. "color" constantLight color
  1250. "light" constantLight radius
  1251. */
  1252. void SP_func_bobbing (gentity_t *ent) {
  1253. float height;
  1254. float phase;
  1255. G_SpawnFloat( "speed", "4", &ent->speed );
  1256. G_SpawnFloat( "height", "32", &height );
  1257. G_SpawnInt( "dmg", "2", &ent->damage );
  1258. G_SpawnFloat( "phase", "0", &phase );
  1259. trap_SetBrushModel( ent, ent->model );
  1260. InitMover( ent );
  1261. VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1262. VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1263. ent->s.pos.trDuration = ent->speed * 1000;
  1264. ent->s.pos.trTime = ent->s.pos.trDuration * phase;
  1265. ent->s.pos.trType = TR_SINE;
  1266. // set the axis of bobbing
  1267. if ( ent->spawnflags & 1 ) {
  1268. ent->s.pos.trDelta[0] = height;
  1269. } else if ( ent->spawnflags & 2 ) {
  1270. ent->s.pos.trDelta[1] = height;
  1271. } else {
  1272. ent->s.pos.trDelta[2] = height;
  1273. }
  1274. }
  1275. /*
  1276. ===============================================================================
  1277. PENDULUM
  1278. ===============================================================================
  1279. */
  1280. /*QUAKED func_pendulum (0 .5 .8) ?
  1281. You need to have an origin brush as part of this entity.
  1282. Pendulums always swing north / south on unrotated models. Add an angles field to the model to allow rotation in other directions.
  1283. Pendulum frequency is a physical constant based on the length of the beam and gravity.
  1284. "model2" .md3 model to also draw
  1285. "speed" the number of degrees each way the pendulum swings, (30 default)
  1286. "phase" the 0.0 to 1.0 offset in the cycle to start at
  1287. "dmg" damage to inflict when blocked (2 default)
  1288. "color" constantLight color
  1289. "light" constantLight radius
  1290. */
  1291. void SP_func_pendulum(gentity_t *ent) {
  1292. float freq;
  1293. float length;
  1294. float phase;
  1295. float speed;
  1296. G_SpawnFloat( "speed", "30", &speed );
  1297. G_SpawnInt( "dmg", "2", &ent->damage );
  1298. G_SpawnFloat( "phase", "0", &phase );
  1299. trap_SetBrushModel( ent, ent->model );
  1300. // find pendulum length
  1301. length = fabs( ent->r.mins[2] );
  1302. if ( length < 8 ) {
  1303. length = 8;
  1304. }
  1305. freq = 1 / ( M_PI * 2 ) * sqrt( g_gravity.value / ( 3 * length ) );
  1306. ent->s.pos.trDuration = ( 1000 / freq );
  1307. InitMover( ent );
  1308. VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1309. VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1310. VectorCopy( ent->s.angles, ent->s.apos.trBase );
  1311. ent->s.apos.trDuration = 1000 / freq;
  1312. ent->s.apos.trTime = ent->s.apos.trDuration * phase;
  1313. ent->s.apos.trType = TR_SINE;
  1314. ent->s.apos.trDelta[2] = speed;
  1315. }