sv_move.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. Copyright (C) 1996-1997 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. // sv_move.c -- monster movement
  16. #include "quakedef.h"
  17. #define STEPSIZE 18
  18. /*
  19. =============
  20. SV_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 SV_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->v.origin, ent->v.mins, mins);
  33. VectorAdd (ent->v.origin, ent->v.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 (SV_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 = SV_Move (start, vec3_origin, vec3_origin, stop, true, ent);
  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 = SV_Move (start, vec3_origin, vec3_origin, stop, true, ent);
  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. qboolean SV_movestep (edict_t *ent, vec3_t move, qboolean relink)
  87. {
  88. float dz;
  89. vec3_t oldorg, neworg, end;
  90. trace_t trace;
  91. int i;
  92. edict_t *enemy;
  93. // try the move
  94. VectorCopy (ent->v.origin, oldorg);
  95. VectorAdd (ent->v.origin, move, neworg);
  96. // flying monsters don't step up
  97. if ( (int)ent->v.flags & (FL_SWIM | FL_FLY) )
  98. {
  99. // try one move with vertical motion, then one without
  100. for (i=0 ; i<2 ; i++)
  101. {
  102. VectorAdd (ent->v.origin, move, neworg);
  103. enemy = PROG_TO_EDICT(ent->v.enemy);
  104. if (i == 0 && enemy != sv.edicts)
  105. {
  106. dz = ent->v.origin[2] - PROG_TO_EDICT(ent->v.enemy)->v.origin[2];
  107. if (dz > 40)
  108. neworg[2] -= 8;
  109. if (dz < 30)
  110. neworg[2] += 8;
  111. }
  112. trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, neworg, false, ent);
  113. if (trace.fraction == 1)
  114. {
  115. if ( ((int)ent->v.flags & FL_SWIM) && SV_PointContents(trace.endpos) == CONTENTS_EMPTY )
  116. return false; // swim monster left water
  117. VectorCopy (trace.endpos, ent->v.origin);
  118. if (relink)
  119. SV_LinkEdict (ent, true);
  120. return true;
  121. }
  122. if (enemy == sv.edicts)
  123. break;
  124. }
  125. return false;
  126. }
  127. // push down from a step height above the wished position
  128. neworg[2] += STEPSIZE;
  129. VectorCopy (neworg, end);
  130. end[2] -= STEPSIZE*2;
  131. trace = SV_Move (neworg, ent->v.mins, ent->v.maxs, end, false, ent);
  132. if (trace.allsolid)
  133. return false;
  134. if (trace.startsolid)
  135. {
  136. neworg[2] -= STEPSIZE;
  137. trace = SV_Move (neworg, ent->v.mins, ent->v.maxs, end, false, ent);
  138. if (trace.allsolid || trace.startsolid)
  139. return false;
  140. }
  141. if (trace.fraction == 1)
  142. {
  143. // if monster had the ground pulled out, go ahead and fall
  144. if ( (int)ent->v.flags & FL_PARTIALGROUND )
  145. {
  146. VectorAdd (ent->v.origin, move, ent->v.origin);
  147. if (relink)
  148. SV_LinkEdict (ent, true);
  149. ent->v.flags = (int)ent->v.flags & ~FL_ONGROUND;
  150. // Con_Printf ("fall down\n");
  151. return true;
  152. }
  153. return false; // walked off an edge
  154. }
  155. // check point traces down for dangling corners
  156. VectorCopy (trace.endpos, ent->v.origin);
  157. if (!SV_CheckBottom (ent))
  158. {
  159. if ( (int)ent->v.flags & FL_PARTIALGROUND )
  160. { // entity had floor mostly pulled out from underneath it
  161. // and is trying to correct
  162. if (relink)
  163. SV_LinkEdict (ent, true);
  164. return true;
  165. }
  166. VectorCopy (oldorg, ent->v.origin);
  167. return false;
  168. }
  169. if ( (int)ent->v.flags & FL_PARTIALGROUND )
  170. {
  171. // Con_Printf ("back on ground\n");
  172. ent->v.flags = (int)ent->v.flags & ~FL_PARTIALGROUND;
  173. }
  174. ent->v.groundentity = EDICT_TO_PROG(trace.ent);
  175. // the move is ok
  176. if (relink)
  177. SV_LinkEdict (ent, true);
  178. return true;
  179. }
  180. //============================================================================
  181. /*
  182. ======================
  183. SV_StepDirection
  184. Turns to the movement direction, and walks the current distance if
  185. facing it.
  186. ======================
  187. */
  188. void PF_changeyaw (void);
  189. qboolean SV_StepDirection (edict_t *ent, float yaw, float dist)
  190. {
  191. vec3_t move, oldorigin;
  192. float delta;
  193. ent->v.ideal_yaw = yaw;
  194. PF_changeyaw();
  195. yaw = yaw*M_PI*2 / 360;
  196. move[0] = cos(yaw)*dist;
  197. move[1] = sin(yaw)*dist;
  198. move[2] = 0;
  199. VectorCopy (ent->v.origin, oldorigin);
  200. if (SV_movestep (ent, move, false))
  201. {
  202. delta = ent->v.angles[YAW] - ent->v.ideal_yaw;
  203. if (delta > 45 && delta < 315)
  204. { // not turned far enough, so don't take the step
  205. VectorCopy (oldorigin, ent->v.origin);
  206. }
  207. SV_LinkEdict (ent, true);
  208. return true;
  209. }
  210. SV_LinkEdict (ent, true);
  211. return false;
  212. }
  213. /*
  214. ======================
  215. SV_FixCheckBottom
  216. ======================
  217. */
  218. void SV_FixCheckBottom (edict_t *ent)
  219. {
  220. // Con_Printf ("SV_FixCheckBottom\n");
  221. ent->v.flags = (int)ent->v.flags | FL_PARTIALGROUND;
  222. }
  223. /*
  224. ================
  225. SV_NewChaseDir
  226. ================
  227. */
  228. #define DI_NODIR -1
  229. void SV_NewChaseDir (edict_t *actor, edict_t *enemy, float dist)
  230. {
  231. float deltax,deltay;
  232. float d[3];
  233. float tdir, olddir, turnaround;
  234. olddir = anglemod( (int)(actor->v.ideal_yaw/45)*45 );
  235. turnaround = anglemod(olddir - 180);
  236. deltax = enemy->v.origin[0] - actor->v.origin[0];
  237. deltay = enemy->v.origin[1] - actor->v.origin[1];
  238. if (deltax>10)
  239. d[1]= 0;
  240. else if (deltax<-10)
  241. d[1]= 180;
  242. else
  243. d[1]= DI_NODIR;
  244. if (deltay<-10)
  245. d[2]= 270;
  246. else if (deltay>10)
  247. d[2]= 90;
  248. else
  249. d[2]= DI_NODIR;
  250. // try direct route
  251. if (d[1] != DI_NODIR && d[2] != DI_NODIR)
  252. {
  253. if (d[1] == 0)
  254. tdir = d[2] == 90 ? 45 : 315;
  255. else
  256. tdir = d[2] == 90 ? 135 : 215;
  257. if (tdir != turnaround && SV_StepDirection(actor, tdir, dist))
  258. return;
  259. }
  260. // try other directions
  261. if ( ((rand()&3) & 1) || abs(deltay)>abs(deltax))
  262. {
  263. tdir=d[1];
  264. d[1]=d[2];
  265. d[2]=tdir;
  266. }
  267. if (d[1]!=DI_NODIR && d[1]!=turnaround
  268. && SV_StepDirection(actor, d[1], dist))
  269. return;
  270. if (d[2]!=DI_NODIR && d[2]!=turnaround
  271. && SV_StepDirection(actor, d[2], dist))
  272. return;
  273. /* there is no direct path to the player, so pick another direction */
  274. if (olddir!=DI_NODIR && SV_StepDirection(actor, olddir, dist))
  275. return;
  276. if (rand()&1) /*randomly determine direction of search*/
  277. {
  278. for (tdir=0 ; tdir<=315 ; tdir += 45)
  279. if (tdir!=turnaround && SV_StepDirection(actor, tdir, dist) )
  280. return;
  281. }
  282. else
  283. {
  284. for (tdir=315 ; tdir >=0 ; tdir -= 45)
  285. if (tdir!=turnaround && SV_StepDirection(actor, tdir, dist) )
  286. return;
  287. }
  288. if (turnaround != DI_NODIR && SV_StepDirection(actor, turnaround, dist) )
  289. return;
  290. actor->v.ideal_yaw = olddir; // can't move
  291. // if a bridge was pulled out from underneath a monster, it may not have
  292. // a valid standing position at all
  293. if (!SV_CheckBottom (actor))
  294. SV_FixCheckBottom (actor);
  295. }
  296. /*
  297. ======================
  298. SV_CloseEnough
  299. ======================
  300. */
  301. qboolean SV_CloseEnough (edict_t *ent, edict_t *goal, float dist)
  302. {
  303. int i;
  304. for (i=0 ; i<3 ; i++)
  305. {
  306. if (goal->v.absmin[i] > ent->v.absmax[i] + dist)
  307. return false;
  308. if (goal->v.absmax[i] < ent->v.absmin[i] - dist)
  309. return false;
  310. }
  311. return true;
  312. }
  313. /*
  314. ======================
  315. SV_MoveToGoal
  316. ======================
  317. */
  318. void SV_MoveToGoal (void)
  319. {
  320. edict_t *ent, *goal;
  321. float dist;
  322. #ifdef QUAKE2
  323. edict_t *enemy;
  324. #endif
  325. ent = PROG_TO_EDICT(pr_global_struct->self);
  326. goal = PROG_TO_EDICT(ent->v.goalentity);
  327. dist = G_FLOAT(OFS_PARM0);
  328. if ( !( (int)ent->v.flags & (FL_ONGROUND|FL_FLY|FL_SWIM) ) )
  329. {
  330. G_FLOAT(OFS_RETURN) = 0;
  331. return;
  332. }
  333. // if the next step hits the enemy, return immediately
  334. #ifdef QUAKE2
  335. enemy = PROG_TO_EDICT(ent->v.enemy);
  336. if (enemy != sv.edicts && SV_CloseEnough (ent, enemy, dist) )
  337. #else
  338. if ( PROG_TO_EDICT(ent->v.enemy) != sv.edicts && SV_CloseEnough (ent, goal, dist) )
  339. #endif
  340. return;
  341. // bump around...
  342. if ( (rand()&3)==1 ||
  343. !SV_StepDirection (ent, ent->v.ideal_yaw, dist))
  344. {
  345. SV_NewChaseDir (ent, goal, dist);
  346. }
  347. }