g_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // g_utils.c -- misc utility functions for game module
  16. #include "g_local.h"
  17. void G_ProjectSource (vec3_t point, vec3_t distance, vec3_t forward, vec3_t right, vec3_t result)
  18. {
  19. result[0] = point[0] + forward[0] * distance[0] + right[0] * distance[1];
  20. result[1] = point[1] + forward[1] * distance[0] + right[1] * distance[1];
  21. result[2] = point[2] + forward[2] * distance[0] + right[2] * distance[1] + distance[2];
  22. }
  23. /*
  24. =============
  25. G_Find
  26. Searches all active entities for the next one that holds
  27. the matching string at fieldofs (use the FOFS() macro) in the structure.
  28. Searches beginning at the edict after from, or the beginning if NULL
  29. NULL will be returned if the end of the list is reached.
  30. =============
  31. */
  32. edict_t *G_Find (edict_t *from, int fieldofs, char *match)
  33. {
  34. char *s;
  35. if (!from)
  36. from = g_edicts;
  37. else
  38. from++;
  39. for ( ; from < &g_edicts[globals.num_edicts] ; from++)
  40. {
  41. if (!from->inuse)
  42. continue;
  43. s = *(char **) ((byte *)from + fieldofs);
  44. if (!s)
  45. continue;
  46. if (!Q_stricmp (s, match))
  47. return from;
  48. }
  49. return NULL;
  50. }
  51. /*
  52. =================
  53. findradius
  54. Returns entities that have origins within a spherical area
  55. findradius (origin, radius)
  56. =================
  57. */
  58. edict_t *findradius (edict_t *from, vec3_t org, float rad)
  59. {
  60. vec3_t eorg;
  61. int j;
  62. if (!from)
  63. from = g_edicts;
  64. else
  65. from++;
  66. for ( ; from < &g_edicts[globals.num_edicts]; from++)
  67. {
  68. if (!from->inuse)
  69. continue;
  70. if (from->solid == SOLID_NOT)
  71. continue;
  72. for (j=0 ; j<3 ; j++)
  73. eorg[j] = org[j] - (from->s.origin[j] + (from->mins[j] + from->maxs[j])*0.5);
  74. if (VectorLength(eorg) > rad)
  75. continue;
  76. return from;
  77. }
  78. return NULL;
  79. }
  80. /*
  81. =============
  82. G_PickTarget
  83. Searches all active entities for the next one that holds
  84. the matching string at fieldofs (use the FOFS() macro) in the structure.
  85. Searches beginning at the edict after from, or the beginning if NULL
  86. NULL will be returned if the end of the list is reached.
  87. =============
  88. */
  89. #define MAXCHOICES 8
  90. edict_t *G_PickTarget (char *targetname)
  91. {
  92. edict_t *ent = NULL;
  93. int num_choices = 0;
  94. edict_t *choice[MAXCHOICES];
  95. if (!targetname)
  96. {
  97. gi.dprintf("G_PickTarget called with NULL targetname\n");
  98. return NULL;
  99. }
  100. while(1)
  101. {
  102. ent = G_Find (ent, FOFS(targetname), targetname);
  103. if (!ent)
  104. break;
  105. choice[num_choices++] = ent;
  106. if (num_choices == MAXCHOICES)
  107. break;
  108. }
  109. if (!num_choices)
  110. {
  111. gi.dprintf("G_PickTarget: target %s not found\n", targetname);
  112. return NULL;
  113. }
  114. return choice[rand() % num_choices];
  115. }
  116. void Think_Delay (edict_t *ent)
  117. {
  118. G_UseTargets (ent, ent->activator);
  119. G_FreeEdict (ent);
  120. }
  121. /*
  122. ==============================
  123. G_UseTargets
  124. the global "activator" should be set to the entity that initiated the firing.
  125. If self.delay is set, a DelayedUse entity will be created that will actually
  126. do the SUB_UseTargets after that many seconds have passed.
  127. Centerprints any self.message to the activator.
  128. Search for (string)targetname in all entities that
  129. match (string)self.target and call their .use function
  130. ==============================
  131. */
  132. void G_UseTargets (edict_t *ent, edict_t *activator)
  133. {
  134. edict_t *t;
  135. //
  136. // check for a delay
  137. //
  138. if (ent->delay)
  139. {
  140. // create a temp object to fire at a later time
  141. t = G_Spawn();
  142. t->classname = "DelayedUse";
  143. t->nextthink = level.time + ent->delay;
  144. t->think = Think_Delay;
  145. t->activator = activator;
  146. if (!activator)
  147. gi.dprintf ("Think_Delay with no activator\n");
  148. t->message = ent->message;
  149. t->target = ent->target;
  150. t->killtarget = ent->killtarget;
  151. return;
  152. }
  153. //
  154. // print the message
  155. //
  156. if ((ent->message) && !(activator->svflags & SVF_MONSTER))
  157. {
  158. gi.centerprintf (activator, "%s", ent->message);
  159. if (ent->noise_index)
  160. gi.sound (activator, CHAN_AUTO, ent->noise_index, 1, ATTN_NORM, 0);
  161. else
  162. gi.sound (activator, CHAN_AUTO, gi.soundindex ("misc/talk1.wav"), 1, ATTN_NORM, 0);
  163. }
  164. //
  165. // kill killtargets
  166. //
  167. if (ent->killtarget)
  168. {
  169. t = NULL;
  170. while ((t = G_Find (t, FOFS(targetname), ent->killtarget)))
  171. {
  172. G_FreeEdict (t);
  173. if (!ent->inuse)
  174. {
  175. gi.dprintf("entity was removed while using killtargets\n");
  176. return;
  177. }
  178. }
  179. }
  180. // gi.dprintf("TARGET: activating %s\n", ent->target);
  181. //
  182. // fire targets
  183. //
  184. if (ent->target)
  185. {
  186. t = NULL;
  187. while ((t = G_Find (t, FOFS(targetname), ent->target)))
  188. {
  189. // doors fire area portals in a specific way
  190. if (!Q_stricmp(t->classname, "func_areaportal") &&
  191. (!Q_stricmp(ent->classname, "func_door") || !Q_stricmp(ent->classname, "func_door_rotating")))
  192. continue;
  193. if (t == ent)
  194. {
  195. gi.dprintf ("WARNING: Entity used itself.\n");
  196. }
  197. else
  198. {
  199. if (t->use)
  200. t->use (t, ent, activator);
  201. }
  202. if (!ent->inuse)
  203. {
  204. gi.dprintf("entity was removed while using targets\n");
  205. return;
  206. }
  207. }
  208. }
  209. }
  210. /*
  211. =============
  212. TempVector
  213. This is just a convenience function
  214. for making temporary vectors for function calls
  215. =============
  216. */
  217. float *tv (float x, float y, float z)
  218. {
  219. static int index;
  220. static vec3_t vecs[8];
  221. float *v;
  222. // use an array so that multiple tempvectors won't collide
  223. // for a while
  224. v = vecs[index];
  225. index = (index + 1)&7;
  226. v[0] = x;
  227. v[1] = y;
  228. v[2] = z;
  229. return v;
  230. }
  231. /*
  232. =============
  233. VectorToString
  234. This is just a convenience function
  235. for printing vectors
  236. =============
  237. */
  238. char *vtos (vec3_t v)
  239. {
  240. static int index;
  241. static char str[8][32];
  242. char *s;
  243. // use an array so that multiple vtos won't collide
  244. s = str[index];
  245. index = (index + 1)&7;
  246. Com_sprintf (s, 32, "(%i %i %i)", (int)v[0], (int)v[1], (int)v[2]);
  247. return s;
  248. }
  249. vec3_t VEC_UP = {0, -1, 0};
  250. vec3_t MOVEDIR_UP = {0, 0, 1};
  251. vec3_t VEC_DOWN = {0, -2, 0};
  252. vec3_t MOVEDIR_DOWN = {0, 0, -1};
  253. void G_SetMovedir (vec3_t angles, vec3_t movedir)
  254. {
  255. if (VectorCompare (angles, VEC_UP))
  256. {
  257. VectorCopy (MOVEDIR_UP, movedir);
  258. }
  259. else if (VectorCompare (angles, VEC_DOWN))
  260. {
  261. VectorCopy (MOVEDIR_DOWN, movedir);
  262. }
  263. else
  264. {
  265. AngleVectors (angles, movedir, NULL, NULL);
  266. }
  267. VectorClear (angles);
  268. }
  269. float vectoyaw (vec3_t vec)
  270. {
  271. float yaw;
  272. if (/* vec[YAW] == 0 && */ vec[PITCH] == 0)
  273. {
  274. yaw = 0;
  275. if (vec[YAW] > 0)
  276. yaw = 90;
  277. else if (vec[YAW] < 0)
  278. yaw = -90;
  279. }
  280. else
  281. {
  282. yaw = (int) (atan2(vec[YAW], vec[PITCH]) * 180 / M_PI);
  283. if (yaw < 0)
  284. yaw += 360;
  285. }
  286. return yaw;
  287. }
  288. void vectoangles (vec3_t value1, vec3_t angles)
  289. {
  290. float forward;
  291. float yaw, pitch;
  292. if (value1[1] == 0 && value1[0] == 0)
  293. {
  294. yaw = 0;
  295. if (value1[2] > 0)
  296. pitch = 90;
  297. else
  298. pitch = 270;
  299. }
  300. else
  301. {
  302. if (value1[0])
  303. yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
  304. else if (value1[1] > 0)
  305. yaw = 90;
  306. else
  307. yaw = -90;
  308. if (yaw < 0)
  309. yaw += 360;
  310. forward = sqrt (value1[0]*value1[0] + value1[1]*value1[1]);
  311. pitch = (int) (atan2(value1[2], forward) * 180 / M_PI);
  312. if (pitch < 0)
  313. pitch += 360;
  314. }
  315. angles[PITCH] = -pitch;
  316. angles[YAW] = yaw;
  317. angles[ROLL] = 0;
  318. }
  319. char *G_CopyString (char *in)
  320. {
  321. char *out;
  322. out = gi.TagMalloc (strlen(in)+1, TAG_LEVEL);
  323. strcpy (out, in);
  324. return out;
  325. }
  326. void G_InitEdict (edict_t *e)
  327. {
  328. e->inuse = true;
  329. e->classname = "noclass";
  330. e->gravity = 1.0;
  331. e->s.number = e - g_edicts;
  332. }
  333. /*
  334. =================
  335. G_Spawn
  336. Either finds a free edict, or allocates a new one.
  337. Try to avoid reusing an entity that was recently freed, because it
  338. can cause the client to think the entity morphed into something else
  339. instead of being removed and recreated, which can cause interpolated
  340. angles and bad trails.
  341. =================
  342. */
  343. edict_t *G_Spawn (void)
  344. {
  345. int i;
  346. edict_t *e;
  347. e = &g_edicts[(int)maxclients->value+1];
  348. for ( i=maxclients->value+1 ; i<globals.num_edicts ; i++, e++)
  349. {
  350. // the first couple seconds of server time can involve a lot of
  351. // freeing and allocating, so relax the replacement policy
  352. if (!e->inuse && ( e->freetime < 2 || level.time - e->freetime > 0.5 ) )
  353. {
  354. G_InitEdict (e);
  355. return e;
  356. }
  357. }
  358. if (i == game.maxentities)
  359. gi.error ("ED_Alloc: no free edicts");
  360. globals.num_edicts++;
  361. G_InitEdict (e);
  362. return e;
  363. }
  364. /*
  365. =================
  366. G_FreeEdict
  367. Marks the edict as free
  368. =================
  369. */
  370. void G_FreeEdict (edict_t *ed)
  371. {
  372. gi.unlinkentity (ed); // unlink from world
  373. if ((ed - g_edicts) <= (maxclients->value + BODY_QUEUE_SIZE))
  374. {
  375. // gi.dprintf("tried to free special edict\n");
  376. return;
  377. }
  378. memset (ed, 0, sizeof(*ed));
  379. ed->classname = "freed";
  380. ed->freetime = level.time;
  381. ed->inuse = false;
  382. }
  383. /*
  384. ============
  385. G_TouchTriggers
  386. ============
  387. */
  388. void G_TouchTriggers (edict_t *ent)
  389. {
  390. int i, num;
  391. edict_t *touch[MAX_EDICTS], *hit;
  392. // dead things don't activate triggers!
  393. if ((ent->client || (ent->svflags & SVF_MONSTER)) && (ent->health <= 0))
  394. return;
  395. num = gi.BoxEdicts (ent->absmin, ent->absmax, touch
  396. , MAX_EDICTS, AREA_TRIGGERS);
  397. // be careful, it is possible to have an entity in this
  398. // list removed before we get to it (killtriggered)
  399. for (i=0 ; i<num ; i++)
  400. {
  401. hit = touch[i];
  402. if (!hit->inuse)
  403. continue;
  404. if (!hit->touch)
  405. continue;
  406. hit->touch (hit, ent, NULL, NULL);
  407. }
  408. }
  409. /*
  410. ============
  411. G_TouchSolids
  412. Call after linking a new trigger in during gameplay
  413. to force all entities it covers to immediately touch it
  414. ============
  415. */
  416. void G_TouchSolids (edict_t *ent)
  417. {
  418. int i, num;
  419. edict_t *touch[MAX_EDICTS], *hit;
  420. num = gi.BoxEdicts (ent->absmin, ent->absmax, touch
  421. , MAX_EDICTS, AREA_SOLID);
  422. // be careful, it is possible to have an entity in this
  423. // list removed before we get to it (killtriggered)
  424. for (i=0 ; i<num ; i++)
  425. {
  426. hit = touch[i];
  427. if (!hit->inuse)
  428. continue;
  429. if (ent->touch)
  430. ent->touch (hit, ent, NULL, NULL);
  431. if (!ent->inuse)
  432. break;
  433. }
  434. }
  435. /*
  436. ==============================================================================
  437. Kill box
  438. ==============================================================================
  439. */
  440. /*
  441. =================
  442. KillBox
  443. Kills all entities that would touch the proposed new positioning
  444. of ent. Ent should be unlinked before calling this!
  445. =================
  446. */
  447. qboolean KillBox (edict_t *ent)
  448. {
  449. trace_t tr;
  450. while (1)
  451. {
  452. tr = gi.trace (ent->s.origin, ent->mins, ent->maxs, ent->s.origin, NULL, MASK_PLAYERSOLID);
  453. if (!tr.ent)
  454. break;
  455. // nail it
  456. T_Damage (tr.ent, ent, ent, vec3_origin, ent->s.origin, vec3_origin, 100000, 0, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
  457. // if we didn't kill it, fail
  458. if (tr.ent->solid)
  459. return false;
  460. }
  461. return true; // all clear
  462. }