g_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. //
  181. // fire targets
  182. //
  183. if (ent->target)
  184. {
  185. t = NULL;
  186. while ((t = G_Find (t, FOFS(targetname), ent->target)))
  187. {
  188. // doors fire area portals in a specific way
  189. if (!Q_stricmp(t->classname, "func_areaportal") &&
  190. (!Q_stricmp(ent->classname, "func_door") || !Q_stricmp(ent->classname, "func_door_rotating")))
  191. continue;
  192. if (t == ent)
  193. {
  194. gi.dprintf ("WARNING: Entity used itself.\n");
  195. }
  196. else
  197. {
  198. if (t->use)
  199. t->use (t, ent, activator);
  200. }
  201. if (!ent->inuse)
  202. {
  203. gi.dprintf("entity was removed while using targets\n");
  204. return;
  205. }
  206. }
  207. }
  208. }
  209. /*
  210. =============
  211. TempVector
  212. This is just a convenience function
  213. for making temporary vectors for function calls
  214. =============
  215. */
  216. float *tv (float x, float y, float z)
  217. {
  218. static int index;
  219. static vec3_t vecs[8];
  220. float *v;
  221. // use an array so that multiple tempvectors won't collide
  222. // for a while
  223. v = vecs[index];
  224. index = (index + 1)&7;
  225. v[0] = x;
  226. v[1] = y;
  227. v[2] = z;
  228. return v;
  229. }
  230. /*
  231. =============
  232. VectorToString
  233. This is just a convenience function
  234. for printing vectors
  235. =============
  236. */
  237. char *vtos (vec3_t v)
  238. {
  239. static int index;
  240. static char str[8][32];
  241. char *s;
  242. // use an array so that multiple vtos won't collide
  243. s = str[index];
  244. index = (index + 1)&7;
  245. Com_sprintf (s, 32, "(%i %i %i)", (int)v[0], (int)v[1], (int)v[2]);
  246. return s;
  247. }
  248. vec3_t VEC_UP = {0, -1, 0};
  249. vec3_t MOVEDIR_UP = {0, 0, 1};
  250. vec3_t VEC_DOWN = {0, -2, 0};
  251. vec3_t MOVEDIR_DOWN = {0, 0, -1};
  252. void G_SetMovedir (vec3_t angles, vec3_t movedir)
  253. {
  254. if (VectorCompare (angles, VEC_UP))
  255. {
  256. VectorCopy (MOVEDIR_UP, movedir);
  257. }
  258. else if (VectorCompare (angles, VEC_DOWN))
  259. {
  260. VectorCopy (MOVEDIR_DOWN, movedir);
  261. }
  262. else
  263. {
  264. AngleVectors (angles, movedir, NULL, NULL);
  265. }
  266. VectorClear (angles);
  267. }
  268. float vectoyaw (vec3_t vec)
  269. {
  270. float yaw;
  271. if (/*vec[YAW] == 0 &&*/ vec[PITCH] == 0)
  272. {
  273. yaw = 0;
  274. if (vec[YAW] > 0)
  275. yaw = 90;
  276. else if (vec[YAW] < 0)
  277. yaw = -90;
  278. }
  279. else
  280. {
  281. yaw = (int) (atan2(vec[YAW], vec[PITCH]) * 180 / M_PI);
  282. if (yaw < 0)
  283. yaw += 360;
  284. }
  285. return yaw;
  286. }
  287. void vectoangles (vec3_t value1, vec3_t angles)
  288. {
  289. float forward;
  290. float yaw, pitch;
  291. if (value1[1] == 0 && value1[0] == 0)
  292. {
  293. yaw = 0;
  294. if (value1[2] > 0)
  295. pitch = 90;
  296. else
  297. pitch = 270;
  298. }
  299. else
  300. {
  301. if (value1[0])
  302. yaw = (int) (atan2(value1[1], value1[0]) * 180 / M_PI);
  303. else if (value1[1] > 0)
  304. yaw = 90;
  305. else
  306. yaw = -90;
  307. if (yaw < 0)
  308. yaw += 360;
  309. forward = sqrt (value1[0]*value1[0] + value1[1]*value1[1]);
  310. pitch = (int) (atan2(value1[2], forward) * 180 / M_PI);
  311. if (pitch < 0)
  312. pitch += 360;
  313. }
  314. angles[PITCH] = -pitch;
  315. angles[YAW] = yaw;
  316. angles[ROLL] = 0;
  317. }
  318. char *G_CopyString (char *in)
  319. {
  320. char *out;
  321. out = gi.TagMalloc (strlen(in)+1, TAG_LEVEL);
  322. strcpy (out, in);
  323. return out;
  324. }
  325. void G_InitEdict (edict_t *e)
  326. {
  327. e->inuse = true;
  328. e->classname = "noclass";
  329. e->gravity = 1.0;
  330. e->s.number = e - g_edicts;
  331. }
  332. /*
  333. =================
  334. G_Spawn
  335. Either finds a free edict, or allocates a new one.
  336. Try to avoid reusing an entity that was recently freed, because it
  337. can cause the client to think the entity morphed into something else
  338. instead of being removed and recreated, which can cause interpolated
  339. angles and bad trails.
  340. =================
  341. */
  342. edict_t *G_Spawn (void)
  343. {
  344. int i;
  345. edict_t *e;
  346. e = &g_edicts[(int)maxclients->value+1];
  347. for ( i=maxclients->value+1 ; i<globals.num_edicts ; i++, e++)
  348. {
  349. // the first couple seconds of server time can involve a lot of
  350. // freeing and allocating, so relax the replacement policy
  351. if (!e->inuse && ( e->freetime < 2 || level.time - e->freetime > 0.5 ) )
  352. {
  353. G_InitEdict (e);
  354. return e;
  355. }
  356. }
  357. if (i == game.maxentities)
  358. gi.error ("ED_Alloc: no free edicts");
  359. globals.num_edicts++;
  360. G_InitEdict (e);
  361. return e;
  362. }
  363. /*
  364. =================
  365. G_FreeEdict
  366. Marks the edict as free
  367. =================
  368. */
  369. void G_FreeEdict (edict_t *ed)
  370. {
  371. gi.unlinkentity (ed); // unlink from world
  372. if ((ed - g_edicts) <= (maxclients->value + BODY_QUEUE_SIZE))
  373. {
  374. // gi.dprintf("tried to free special edict\n");
  375. return;
  376. }
  377. memset (ed, 0, sizeof(*ed));
  378. ed->classname = "freed";
  379. ed->freetime = level.time;
  380. ed->inuse = false;
  381. }
  382. /*
  383. ============
  384. G_TouchTriggers
  385. ============
  386. */
  387. void G_TouchTriggers (edict_t *ent)
  388. {
  389. int i, num;
  390. edict_t *touch[MAX_EDICTS], *hit;
  391. // dead things don't activate triggers!
  392. if ((ent->client || (ent->svflags & SVF_MONSTER)) && (ent->health <= 0))
  393. return;
  394. num = gi.BoxEdicts (ent->absmin, ent->absmax, touch
  395. , MAX_EDICTS, AREA_TRIGGERS);
  396. // be careful, it is possible to have an entity in this
  397. // list removed before we get to it (killtriggered)
  398. for (i=0 ; i<num ; i++)
  399. {
  400. hit = touch[i];
  401. if (!hit->inuse)
  402. continue;
  403. if (!hit->touch)
  404. continue;
  405. hit->touch (hit, ent, NULL, NULL);
  406. }
  407. }
  408. /*
  409. ============
  410. G_TouchSolids
  411. Call after linking a new trigger in during gameplay
  412. to force all entities it covers to immediately touch it
  413. ============
  414. */
  415. void G_TouchSolids (edict_t *ent)
  416. {
  417. int i, num;
  418. edict_t *touch[MAX_EDICTS], *hit;
  419. num = gi.BoxEdicts (ent->absmin, ent->absmax, touch
  420. , MAX_EDICTS, AREA_SOLID);
  421. // be careful, it is possible to have an entity in this
  422. // list removed before we get to it (killtriggered)
  423. for (i=0 ; i<num ; i++)
  424. {
  425. hit = touch[i];
  426. if (!hit->inuse)
  427. continue;
  428. if (ent->touch)
  429. ent->touch (hit, ent, NULL, NULL);
  430. if (!ent->inuse)
  431. break;
  432. }
  433. }
  434. /*
  435. ==============================================================================
  436. Kill box
  437. ==============================================================================
  438. */
  439. /*
  440. =================
  441. KillBox
  442. Kills all entities that would touch the proposed new positioning
  443. of ent. Ent should be unlinked before calling this!
  444. =================
  445. */
  446. qboolean KillBox (edict_t *ent)
  447. {
  448. trace_t tr;
  449. while (1)
  450. {
  451. tr = gi.trace (ent->s.origin, ent->mins, ent->maxs, ent->s.origin, NULL, MASK_PLAYERSOLID);
  452. if (!tr.ent)
  453. break;
  454. // nail it
  455. T_Damage (tr.ent, ent, ent, vec3_origin, ent->s.origin, vec3_origin, 100000, 0, DAMAGE_NO_PROTECTION, MOD_TELEFRAG);
  456. // if we didn't kill it, fail
  457. if (tr.ent->solid)
  458. return false;
  459. }
  460. return true; // all clear
  461. }