m_move.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. // m_move.c -- monster movement
  16. #include "g_local.h"
  17. #define STEPSIZE 18
  18. /*
  19. =============
  20. M_CheckBottom
  21. Returns false if any part of the bottom of the entity is off an edge that
  22. is not a staircase.
  23. =============
  24. */
  25. int c_yes, c_no;
  26. qboolean M_CheckBottom (edict_t *ent)
  27. {
  28. vec3_t mins, maxs, start, stop;
  29. trace_t trace;
  30. int x, y;
  31. float mid, bottom;
  32. VectorAdd (ent->s.origin, ent->mins, mins);
  33. VectorAdd (ent->s.origin, ent->maxs, maxs);
  34. // if all of the points under the corners are solid world, don't bother
  35. // with the tougher checks
  36. // the corners must be within 16 of the midpoint
  37. start[2] = mins[2] - 1;
  38. for (x=0 ; x<=1 ; x++)
  39. for (y=0 ; y<=1 ; y++)
  40. {
  41. start[0] = x ? maxs[0] : mins[0];
  42. start[1] = y ? maxs[1] : mins[1];
  43. if (gi.pointcontents (start) != CONTENTS_SOLID)
  44. goto realcheck;
  45. }
  46. c_yes++;
  47. return true; // we got out easy
  48. realcheck:
  49. c_no++;
  50. //
  51. // check it for real...
  52. //
  53. start[2] = mins[2];
  54. // the midpoint must be within 16 of the bottom
  55. start[0] = stop[0] = (mins[0] + maxs[0])*0.5;
  56. start[1] = stop[1] = (mins[1] + maxs[1])*0.5;
  57. stop[2] = start[2] - 2*STEPSIZE;
  58. trace = gi.trace (start, vec3_origin, vec3_origin, stop, ent, MASK_MONSTERSOLID);
  59. if (trace.fraction == 1.0)
  60. return false;
  61. mid = bottom = trace.endpos[2];
  62. // the corners must be within 16 of the midpoint
  63. for (x=0 ; x<=1 ; x++)
  64. for (y=0 ; y<=1 ; y++)
  65. {
  66. start[0] = stop[0] = x ? maxs[0] : mins[0];
  67. start[1] = stop[1] = y ? maxs[1] : mins[1];
  68. trace = gi.trace (start, vec3_origin, vec3_origin, stop, ent, MASK_MONSTERSOLID);
  69. if (trace.fraction != 1.0 && trace.endpos[2] > bottom)
  70. bottom = trace.endpos[2];
  71. if (trace.fraction == 1.0 || mid - trace.endpos[2] > STEPSIZE)
  72. return false;
  73. }
  74. c_yes++;
  75. return true;
  76. }
  77. /*
  78. =============
  79. SV_movestep
  80. Called by monster program code.
  81. The move will be adjusted for slopes and stairs, but if the move isn't
  82. possible, no move is done, false is returned, and
  83. pr_global_struct->trace_normal is set to the normal of the blocking wall
  84. =============
  85. */
  86. //FIXME since we need to test end position contents here, can we avoid doing
  87. //it again later in catagorize position?
  88. qboolean SV_movestep (edict_t *ent, vec3_t move, qboolean relink)
  89. {
  90. float dz;
  91. vec3_t oldorg, neworg, end;
  92. trace_t trace;
  93. int i;
  94. float stepsize;
  95. vec3_t test;
  96. int contents;
  97. // try the move
  98. VectorCopy (ent->s.origin, oldorg);
  99. VectorAdd (ent->s.origin, move, neworg);
  100. // flying monsters don't step up
  101. if ( ent->flags & (FL_SWIM | FL_FLY) )
  102. {
  103. // try one move with vertical motion, then one without
  104. for (i=0 ; i<2 ; i++)
  105. {
  106. VectorAdd (ent->s.origin, move, neworg);
  107. if (i == 0 && ent->enemy)
  108. {
  109. if (!ent->goalentity)
  110. ent->goalentity = ent->enemy;
  111. dz = ent->s.origin[2] - ent->goalentity->s.origin[2];
  112. if (ent->goalentity->client)
  113. {
  114. if (dz > 40)
  115. neworg[2] -= 8;
  116. if (!((ent->flags & FL_SWIM) && (ent->waterlevel < 2)))
  117. if (dz < 30)
  118. neworg[2] += 8;
  119. }
  120. else
  121. {
  122. if (dz > 8)
  123. neworg[2] -= 8;
  124. else if (dz > 0)
  125. neworg[2] -= dz;
  126. else if (dz < -8)
  127. neworg[2] += 8;
  128. else
  129. neworg[2] += dz;
  130. }
  131. }
  132. trace = gi.trace (ent->s.origin, ent->mins, ent->maxs, neworg, ent, MASK_MONSTERSOLID);
  133. // fly monsters don't enter water voluntarily
  134. if (ent->flags & FL_FLY)
  135. {
  136. if (!ent->waterlevel)
  137. {
  138. test[0] = trace.endpos[0];
  139. test[1] = trace.endpos[1];
  140. test[2] = trace.endpos[2] + ent->mins[2] + 1;
  141. contents = gi.pointcontents(test);
  142. if (contents & MASK_WATER)
  143. return false;
  144. }
  145. }
  146. // swim monsters don't exit water voluntarily
  147. if (ent->flags & FL_SWIM)
  148. {
  149. if (ent->waterlevel < 2)
  150. {
  151. test[0] = trace.endpos[0];
  152. test[1] = trace.endpos[1];
  153. test[2] = trace.endpos[2] + ent->mins[2] + 1;
  154. contents = gi.pointcontents(test);
  155. if (!(contents & MASK_WATER))
  156. return false;
  157. }
  158. }
  159. if (trace.fraction == 1)
  160. {
  161. VectorCopy (trace.endpos, ent->s.origin);
  162. if (relink)
  163. {
  164. gi.linkentity (ent);
  165. G_TouchTriggers (ent);
  166. }
  167. return true;
  168. }
  169. if (!ent->enemy)
  170. break;
  171. }
  172. return false;
  173. }
  174. // push down from a step height above the wished position
  175. if (!(ent->monsterinfo.aiflags & AI_NOSTEP))
  176. stepsize = STEPSIZE;
  177. else
  178. stepsize = 1;
  179. neworg[2] += stepsize;
  180. VectorCopy (neworg, end);
  181. end[2] -= stepsize*2;
  182. trace = gi.trace (neworg, ent->mins, ent->maxs, end, ent, MASK_MONSTERSOLID);
  183. if (trace.allsolid)
  184. return false;
  185. if (trace.startsolid)
  186. {
  187. neworg[2] -= stepsize;
  188. trace = gi.trace (neworg, ent->mins, ent->maxs, end, ent, MASK_MONSTERSOLID);
  189. if (trace.allsolid || trace.startsolid)
  190. return false;
  191. }
  192. // don't go in to water
  193. if (ent->waterlevel == 0)
  194. {
  195. test[0] = trace.endpos[0];
  196. test[1] = trace.endpos[1];
  197. test[2] = trace.endpos[2] + ent->mins[2] + 1;
  198. contents = gi.pointcontents(test);
  199. if (contents & MASK_WATER)
  200. return false;
  201. }
  202. if (trace.fraction == 1)
  203. {
  204. // if monster had the ground pulled out, go ahead and fall
  205. if ( ent->flags & FL_PARTIALGROUND )
  206. {
  207. VectorAdd (ent->s.origin, move, ent->s.origin);
  208. if (relink)
  209. {
  210. gi.linkentity (ent);
  211. G_TouchTriggers (ent);
  212. }
  213. ent->groundentity = NULL;
  214. return true;
  215. }
  216. return false; // walked off an edge
  217. }
  218. // check point traces down for dangling corners
  219. VectorCopy (trace.endpos, ent->s.origin);
  220. if (!M_CheckBottom (ent))
  221. {
  222. if ( ent->flags & FL_PARTIALGROUND )
  223. { // entity had floor mostly pulled out from underneath it
  224. // and is trying to correct
  225. if (relink)
  226. {
  227. gi.linkentity (ent);
  228. G_TouchTriggers (ent);
  229. }
  230. return true;
  231. }
  232. VectorCopy (oldorg, ent->s.origin);
  233. return false;
  234. }
  235. if ( ent->flags & FL_PARTIALGROUND )
  236. {
  237. ent->flags &= ~FL_PARTIALGROUND;
  238. }
  239. ent->groundentity = trace.ent;
  240. ent->groundentity_linkcount = trace.ent->linkcount;
  241. // the move is ok
  242. if (relink)
  243. {
  244. gi.linkentity (ent);
  245. G_TouchTriggers (ent);
  246. }
  247. return true;
  248. }
  249. //============================================================================
  250. /*
  251. ===============
  252. M_ChangeYaw
  253. ===============
  254. */
  255. void M_ChangeYaw (edict_t *ent)
  256. {
  257. float ideal;
  258. float current;
  259. float move;
  260. float speed;
  261. current = anglemod(ent->s.angles[YAW]);
  262. ideal = ent->ideal_yaw;
  263. if (current == ideal)
  264. return;
  265. move = ideal - current;
  266. speed = ent->yaw_speed;
  267. if (ideal > current)
  268. {
  269. if (move >= 180)
  270. move = move - 360;
  271. }
  272. else
  273. {
  274. if (move <= -180)
  275. move = move + 360;
  276. }
  277. if (move > 0)
  278. {
  279. if (move > speed)
  280. move = speed;
  281. }
  282. else
  283. {
  284. if (move < -speed)
  285. move = -speed;
  286. }
  287. ent->s.angles[YAW] = anglemod (current + move);
  288. }
  289. /*
  290. ======================
  291. SV_StepDirection
  292. Turns to the movement direction, and walks the current distance if
  293. facing it.
  294. ======================
  295. */
  296. qboolean SV_StepDirection (edict_t *ent, float yaw, float dist)
  297. {
  298. vec3_t move, oldorigin;
  299. float delta;
  300. ent->ideal_yaw = yaw;
  301. M_ChangeYaw (ent);
  302. yaw = yaw*M_PI*2 / 360;
  303. move[0] = cos(yaw)*dist;
  304. move[1] = sin(yaw)*dist;
  305. move[2] = 0;
  306. VectorCopy (ent->s.origin, oldorigin);
  307. if (SV_movestep (ent, move, false))
  308. {
  309. delta = ent->s.angles[YAW] - ent->ideal_yaw;
  310. if (delta > 45 && delta < 315)
  311. { // not turned far enough, so don't take the step
  312. VectorCopy (oldorigin, ent->s.origin);
  313. }
  314. gi.linkentity (ent);
  315. G_TouchTriggers (ent);
  316. return true;
  317. }
  318. gi.linkentity (ent);
  319. G_TouchTriggers (ent);
  320. return false;
  321. }
  322. /*
  323. ======================
  324. SV_FixCheckBottom
  325. ======================
  326. */
  327. void SV_FixCheckBottom (edict_t *ent)
  328. {
  329. ent->flags |= FL_PARTIALGROUND;
  330. }
  331. /*
  332. ================
  333. SV_NewChaseDir
  334. ================
  335. */
  336. #define DI_NODIR -1
  337. void SV_NewChaseDir (edict_t *actor, edict_t *enemy, float dist)
  338. {
  339. float deltax,deltay;
  340. float d[3];
  341. float tdir, olddir, turnaround;
  342. //FIXME: how did we get here with no enemy
  343. if (!enemy)
  344. return;
  345. olddir = anglemod( (int)(actor->ideal_yaw/45)*45 );
  346. turnaround = anglemod(olddir - 180);
  347. deltax = enemy->s.origin[0] - actor->s.origin[0];
  348. deltay = enemy->s.origin[1] - actor->s.origin[1];
  349. if (deltax>10)
  350. d[1]= 0;
  351. else if (deltax<-10)
  352. d[1]= 180;
  353. else
  354. d[1]= DI_NODIR;
  355. if (deltay<-10)
  356. d[2]= 270;
  357. else if (deltay>10)
  358. d[2]= 90;
  359. else
  360. d[2]= DI_NODIR;
  361. // try direct route
  362. if (d[1] != DI_NODIR && d[2] != DI_NODIR)
  363. {
  364. if (d[1] == 0)
  365. tdir = d[2] == 90 ? 45 : 315;
  366. else
  367. tdir = d[2] == 90 ? 135 : 215;
  368. if (tdir != turnaround && SV_StepDirection(actor, tdir, dist))
  369. return;
  370. }
  371. // try other directions
  372. if ( ((rand()&3) & 1) || abs(deltay)>abs(deltax))
  373. {
  374. tdir=d[1];
  375. d[1]=d[2];
  376. d[2]=tdir;
  377. }
  378. if (d[1]!=DI_NODIR && d[1]!=turnaround
  379. && SV_StepDirection(actor, d[1], dist))
  380. return;
  381. if (d[2]!=DI_NODIR && d[2]!=turnaround
  382. && SV_StepDirection(actor, d[2], dist))
  383. return;
  384. /* there is no direct path to the player, so pick another direction */
  385. if (olddir!=DI_NODIR && SV_StepDirection(actor, olddir, dist))
  386. return;
  387. if (rand()&1) /*randomly determine direction of search*/
  388. {
  389. for (tdir=0 ; tdir<=315 ; tdir += 45)
  390. if (tdir!=turnaround && SV_StepDirection(actor, tdir, dist) )
  391. return;
  392. }
  393. else
  394. {
  395. for (tdir=315 ; tdir >=0 ; tdir -= 45)
  396. if (tdir!=turnaround && SV_StepDirection(actor, tdir, dist) )
  397. return;
  398. }
  399. if (turnaround != DI_NODIR && SV_StepDirection(actor, turnaround, dist) )
  400. return;
  401. actor->ideal_yaw = olddir; // can't move
  402. // if a bridge was pulled out from underneath a monster, it may not have
  403. // a valid standing position at all
  404. if (!M_CheckBottom (actor))
  405. SV_FixCheckBottom (actor);
  406. }
  407. /*
  408. ======================
  409. SV_CloseEnough
  410. ======================
  411. */
  412. qboolean SV_CloseEnough (edict_t *ent, edict_t *goal, float dist)
  413. {
  414. int i;
  415. for (i=0 ; i<3 ; i++)
  416. {
  417. if (goal->absmin[i] > ent->absmax[i] + dist)
  418. return false;
  419. if (goal->absmax[i] < ent->absmin[i] - dist)
  420. return false;
  421. }
  422. return true;
  423. }
  424. /*
  425. ======================
  426. M_MoveToGoal
  427. ======================
  428. */
  429. void M_MoveToGoal (edict_t *ent, float dist)
  430. {
  431. edict_t *goal;
  432. goal = ent->goalentity;
  433. if (!ent->groundentity && !(ent->flags & (FL_FLY|FL_SWIM)))
  434. return;
  435. // if the next step hits the enemy, return immediately
  436. if (ent->enemy && SV_CloseEnough (ent, ent->enemy, dist) )
  437. return;
  438. // bump around...
  439. if ( (rand()&3)==1 || !SV_StepDirection (ent, ent->ideal_yaw, dist))
  440. {
  441. if (ent->inuse)
  442. SV_NewChaseDir (ent, goal, dist);
  443. }
  444. }
  445. /*
  446. ===============
  447. M_walkmove
  448. ===============
  449. */
  450. qboolean M_walkmove (edict_t *ent, float yaw, float dist)
  451. {
  452. vec3_t move;
  453. if (!ent->groundentity && !(ent->flags & (FL_FLY|FL_SWIM)))
  454. return false;
  455. yaw = yaw*M_PI*2 / 360;
  456. move[0] = cos(yaw)*dist;
  457. move[1] = sin(yaw)*dist;
  458. move[2] = 0;
  459. return SV_movestep(ent, move, true);
  460. }