p_user.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * Player related stuff.
  31. * Bobbing POV/weapon, movement.
  32. * Pending weapon.
  33. *
  34. *-----------------------------------------------------------------------------*/
  35. #include "doomstat.h"
  36. #include "d_event.h"
  37. #include "r_main.h"
  38. #include "p_map.h"
  39. #include "p_spec.h"
  40. #include "p_user.h"
  41. #include "r_demo.h"
  42. #include "r_fps.h"
  43. // Index of the special effects (INVUL inverse) map.
  44. #define INVERSECOLORMAP 32
  45. //
  46. // Movement.
  47. //
  48. // 16 pixels of bob
  49. #define MAXBOB 0x100000
  50. boolean onground; // whether player is on ground or in air
  51. //
  52. // P_Thrust
  53. // Moves the given origin along a given angle.
  54. //
  55. void P_Thrust(player_t* player,angle_t angle,fixed_t move)
  56. {
  57. angle >>= ANGLETOFINESHIFT;
  58. player->mo->momx += FixedMul(move,finecosine[angle]);
  59. player->mo->momy += FixedMul(move,finesine[angle]);
  60. }
  61. /*
  62. * P_Bob
  63. * Same as P_Thrust, but only affects bobbing.
  64. *
  65. * killough 10/98: We apply thrust separately between the real physical player
  66. * and the part which affects bobbing. This way, bobbing only comes from player
  67. * motion, nothing external, avoiding many problems, e.g. bobbing should not
  68. * occur on conveyors, unless the player walks on one, and bobbing should be
  69. * reduced at a regular rate, even on ice (where the player coasts).
  70. */
  71. static void P_Bob(player_t *player, angle_t angle, fixed_t move)
  72. {
  73. //e6y
  74. if (!mbf_features)
  75. return;
  76. player->momx += FixedMul(move,finecosine[angle >>= ANGLETOFINESHIFT]);
  77. player->momy += FixedMul(move,finesine[angle]);
  78. }
  79. //
  80. // P_CalcHeight
  81. // Calculate the walking / running height adjustment
  82. //
  83. void P_CalcHeight (player_t* player)
  84. {
  85. int angle;
  86. fixed_t bob;
  87. // Regular movement bobbing
  88. // (needs to be calculated for gun swing
  89. // even if not on ground)
  90. // OPTIMIZE: tablify angle
  91. // Note: a LUT allows for effects
  92. // like a ramp with low health.
  93. /* killough 10/98: Make bobbing depend only on player-applied motion.
  94. *
  95. * Note: don't reduce bobbing here if on ice: if you reduce bobbing here,
  96. * it causes bobbing jerkiness when the player moves from ice to non-ice,
  97. * and vice-versa.
  98. */
  99. player->bob = !mbf_features ?
  100. (FixedMul (player->mo->momx, player->mo->momx)
  101. + FixedMul (player->mo->momy,player->mo->momy))>>2 :
  102. player_bobbing ? (FixedMul(player->momx,player->momx) +
  103. FixedMul(player->momy,player->momy))>>2 : 0;
  104. //e6y
  105. if (compatibility_level >= boom_202_compatibility &&
  106. compatibility_level <= lxdoom_1_compatibility &&
  107. player->mo->friction > ORIG_FRICTION) // ice?
  108. {
  109. if (player->bob > (MAXBOB>>2))
  110. player->bob = MAXBOB>>2;
  111. }
  112. else
  113. if (player->bob > MAXBOB)
  114. player->bob = MAXBOB;
  115. if (!onground || player->cheats & CF_NOMOMENTUM)
  116. {
  117. player->viewz = player->mo->z + VIEWHEIGHT;
  118. if (player->viewz > player->mo->ceilingz-4*FRACUNIT)
  119. player->viewz = player->mo->ceilingz-4*FRACUNIT;
  120. // The following line was in the Id source and appears // phares 2/25/98
  121. // to be a bug. player->viewz is checked in a similar
  122. // manner at a different exit below.
  123. // player->viewz = player->mo->z + player->viewheight;
  124. return;
  125. }
  126. angle = (FINEANGLES/20*leveltime)&FINEMASK;
  127. bob = FixedMul(player->bob/2,finesine[angle]);
  128. // move viewheight
  129. if (player->playerstate == PST_LIVE)
  130. {
  131. player->viewheight += player->deltaviewheight;
  132. if (player->viewheight > VIEWHEIGHT)
  133. {
  134. player->viewheight = VIEWHEIGHT;
  135. player->deltaviewheight = 0;
  136. }
  137. if (player->viewheight < VIEWHEIGHT/2)
  138. {
  139. player->viewheight = VIEWHEIGHT/2;
  140. if (player->deltaviewheight <= 0)
  141. player->deltaviewheight = 1;
  142. }
  143. if (player->deltaviewheight)
  144. {
  145. player->deltaviewheight += FRACUNIT/4;
  146. if (!player->deltaviewheight)
  147. player->deltaviewheight = 1;
  148. }
  149. }
  150. player->viewz = player->mo->z + player->viewheight + bob;
  151. if (player->viewz > player->mo->ceilingz-4*FRACUNIT)
  152. player->viewz = player->mo->ceilingz-4*FRACUNIT;
  153. }
  154. //
  155. // P_MovePlayer
  156. //
  157. // Adds momentum if the player is not in the air
  158. //
  159. // killough 10/98: simplified
  160. void P_MovePlayer (player_t* player)
  161. {
  162. ticcmd_t *cmd = &player->cmd;
  163. mobj_t *mo = player->mo;
  164. mo->angle += cmd->angleturn << 16;
  165. onground = mo->z <= mo->floorz;
  166. // e6y
  167. if (demo_smoothturns && player == &players[displayplayer])
  168. R_SmoothPlaying_Add(cmd->angleturn << 16);
  169. // killough 10/98:
  170. //
  171. // We must apply thrust to the player and bobbing separately, to avoid
  172. // anomalies. The thrust applied to bobbing is always the same strength on
  173. // ice, because the player still "works just as hard" to move, while the
  174. // thrust applied to the movement varies with 'movefactor'.
  175. //e6y
  176. if ((!demo_compatibility && !mbf_features) || (cmd->forwardmove | cmd->sidemove)) // killough 10/98
  177. {
  178. if (onground || mo->flags & MF_BOUNCES) // killough 8/9/98
  179. {
  180. int friction, movefactor = P_GetMoveFactor(mo, &friction);
  181. // killough 11/98:
  182. // On sludge, make bobbing depend on efficiency.
  183. // On ice, make it depend on effort.
  184. int bobfactor =
  185. friction < ORIG_FRICTION ? movefactor : ORIG_FRICTION_FACTOR;
  186. if (cmd->forwardmove)
  187. {
  188. P_Bob(player,mo->angle,cmd->forwardmove*bobfactor);
  189. P_Thrust(player,mo->angle,cmd->forwardmove*movefactor);
  190. }
  191. if (cmd->sidemove)
  192. {
  193. P_Bob(player,mo->angle-ANG90,cmd->sidemove*bobfactor);
  194. P_Thrust(player,mo->angle-ANG90,cmd->sidemove*movefactor);
  195. }
  196. }
  197. if (mo->state == states+S_PLAY)
  198. P_SetMobjState(mo,S_PLAY_RUN1);
  199. }
  200. }
  201. #define ANG5 (ANG90/18)
  202. //
  203. // P_DeathThink
  204. // Fall on your face when dying.
  205. // Decrease POV height to floor height.
  206. //
  207. void P_DeathThink (player_t* player)
  208. {
  209. angle_t angle;
  210. angle_t delta;
  211. P_MovePsprites (player);
  212. // fall to the ground
  213. if (player->viewheight > 6*FRACUNIT)
  214. player->viewheight -= FRACUNIT;
  215. if (player->viewheight < 6*FRACUNIT)
  216. player->viewheight = 6*FRACUNIT;
  217. player->deltaviewheight = 0;
  218. onground = (player->mo->z <= player->mo->floorz);
  219. P_CalcHeight (player);
  220. if (player->attacker && player->attacker != player->mo)
  221. {
  222. angle = R_PointToAngle2 (player->mo->x,
  223. player->mo->y,
  224. player->attacker->x,
  225. player->attacker->y);
  226. delta = angle - player->mo->angle;
  227. if (delta < ANG5 || delta > (unsigned)-ANG5)
  228. {
  229. // Looking at killer,
  230. // so fade damage flash down.
  231. player->mo->angle = angle;
  232. if (player->damagecount)
  233. player->damagecount--;
  234. }
  235. else if (delta < ANG180)
  236. player->mo->angle += ANG5;
  237. else
  238. player->mo->angle -= ANG5;
  239. }
  240. else if (player->damagecount)
  241. player->damagecount--;
  242. if (player->cmd.buttons & BT_USE)
  243. player->playerstate = PST_REBORN;
  244. R_SmoothPlaying_Reset(player); // e6y
  245. }
  246. //
  247. // P_PlayerThink
  248. //
  249. void P_PlayerThink (player_t* player)
  250. {
  251. ticcmd_t* cmd;
  252. weapontype_t newweapon;
  253. if (movement_smooth && players && &players[displayplayer] == player)
  254. {
  255. original_view_vars.viewx = player->mo->x;
  256. original_view_vars.viewy = player->mo->y;
  257. original_view_vars.viewz = player->viewz;
  258. original_view_vars.viewangle = R_SmoothPlaying_Get(player->mo->angle) + viewangleoffset;
  259. }
  260. // killough 2/8/98, 3/21/98:
  261. if (player->cheats & CF_NOCLIP)
  262. player->mo->flags |= MF_NOCLIP;
  263. else
  264. player->mo->flags &= ~MF_NOCLIP;
  265. // chain saw run forward
  266. cmd = &player->cmd;
  267. if (player->mo->flags & MF_JUSTATTACKED)
  268. {
  269. cmd->angleturn = 0;
  270. cmd->forwardmove = 0xc800/512;
  271. cmd->sidemove = 0;
  272. player->mo->flags &= ~MF_JUSTATTACKED;
  273. }
  274. if (player->playerstate == PST_DEAD)
  275. {
  276. P_DeathThink (player);
  277. return;
  278. }
  279. // Move around.
  280. // Reactiontime is used to prevent movement
  281. // for a bit after a teleport.
  282. if (player->mo->reactiontime)
  283. player->mo->reactiontime--;
  284. else
  285. P_MovePlayer (player);
  286. P_CalcHeight (player); // Determines view height and bobbing
  287. // Determine if there's anything about the sector you're in that's
  288. // going to affect you, like painful floors.
  289. if (player->mo->subsector->sector->special)
  290. P_PlayerInSpecialSector (player);
  291. // Check for weapon change.
  292. if (cmd->buttons & BT_CHANGE)
  293. {
  294. // The actual changing of the weapon is done
  295. // when the weapon psprite can do it
  296. // (read: not in the middle of an attack).
  297. newweapon = (cmd->buttons & BT_WEAPONMASK)>>BT_WEAPONSHIFT;
  298. // killough 3/22/98: For demo compatibility we must perform the fist
  299. // and SSG weapons switches here, rather than in G_BuildTiccmd(). For
  300. // other games which rely on user preferences, we must use the latter.
  301. if (demo_compatibility)
  302. { // compatibility mode -- required for old demos -- killough
  303. if (newweapon == wp_fist && player->weaponowned[wp_chainsaw] &&
  304. (player->readyweapon != wp_chainsaw ||
  305. !player->powers[pw_strength]))
  306. newweapon = wp_chainsaw;
  307. if (gamemode == commercial &&
  308. newweapon == wp_shotgun &&
  309. player->weaponowned[wp_supershotgun] &&
  310. player->readyweapon != wp_supershotgun)
  311. newweapon = wp_supershotgun;
  312. }
  313. // killough 2/8/98, 3/22/98 -- end of weapon selection changes
  314. if (player->weaponowned[newweapon] && newweapon != player->readyweapon)
  315. // Do not go to plasma or BFG in shareware,
  316. // even if cheated.
  317. if ((newweapon != wp_plasma && newweapon != wp_bfg)
  318. || (gamemode != shareware) )
  319. player->pendingweapon = newweapon;
  320. }
  321. // check for use
  322. if (cmd->buttons & BT_USE)
  323. {
  324. if (!player->usedown)
  325. {
  326. P_UseLines (player);
  327. player->usedown = true;
  328. }
  329. }
  330. else
  331. player->usedown = false;
  332. // cycle psprites
  333. P_MovePsprites (player);
  334. // Counters, time dependent power ups.
  335. // Strength counts up to diminish fade.
  336. if (player->powers[pw_strength])
  337. player->powers[pw_strength]++;
  338. // killough 1/98: Make idbeholdx toggle:
  339. if (player->powers[pw_invulnerability] > 0) // killough
  340. player->powers[pw_invulnerability]--;
  341. if (player->powers[pw_invisibility] > 0) // killough
  342. if (! --player->powers[pw_invisibility] )
  343. player->mo->flags &= ~MF_SHADOW;
  344. if (player->powers[pw_infrared] > 0) // killough
  345. player->powers[pw_infrared]--;
  346. if (player->powers[pw_ironfeet] > 0) // killough
  347. player->powers[pw_ironfeet]--;
  348. if (player->damagecount)
  349. player->damagecount--;
  350. if (player->bonuscount)
  351. player->bonuscount--;
  352. // Handling colormaps.
  353. // killough 3/20/98: reformat to terse C syntax
  354. player->fixedcolormap = player->powers[pw_invulnerability] > 4*32 ||
  355. player->powers[pw_invulnerability] & 8 ? INVERSECOLORMAP :
  356. player->powers[pw_infrared] > 4*32 || player->powers[pw_infrared] & 8;
  357. }