1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 /* interface header */
14 #include "BaseLocalPlayer.h"
15 
16 /* common implementation headers */
17 #include "BZDBCache.h"
18 #include "playing.h"
19 
20 
BaseLocalPlayer(const PlayerId & _id,const char * name,const char * _motto)21 BaseLocalPlayer::BaseLocalPlayer(const PlayerId& _id,
22                                  const char* name, const char* _motto) :
23     Player(_id, RogueTeam, name, _motto, TankPlayer),
24     lastTime(TimeKeeper::getTick()),
25     salt(0)
26 {
27     lastPosition[0] = 0.0f;
28     lastPosition[1] = 0.0f;
29     lastPosition[2] = 0.0f;
30     bbox[0][0] = bbox[1][0] = 0.0f;
31     bbox[0][1] = bbox[1][1] = 0.0f;
32     bbox[0][2] = bbox[1][2] = 0.0f;
33 }
34 
~BaseLocalPlayer()35 BaseLocalPlayer::~BaseLocalPlayer()
36 {
37     // do nothing
38 }
39 
getSalt()40 int BaseLocalPlayer::getSalt()
41 {
42     salt = (salt + 1) & 127;
43     return salt << 8;
44 }
45 
update(float inputDT)46 void BaseLocalPlayer::update(float inputDT)
47 {
48     // save last position
49     const float* oldPosition = getPosition();
50 
51     // update by time step
52     float dt = float(TimeKeeper::getTick() - lastTime);
53     if (dt < MIN_DT_LIMIT)
54         return;
55 
56     lastPosition[0] = oldPosition[0];
57     lastPosition[1] = oldPosition[1];
58     lastPosition[2] = oldPosition[2];
59 
60     lastTime = TimeKeeper::getTick();
61 
62     if (inputDT > 0)
63         dt = inputDT;
64 
65     if (dt < MIN_DT_LIMIT)
66         dt = MIN_DT_LIMIT;
67 
68     float dtLimit = MAX_DT_LIMIT;
69     float doneDT = dt;
70     if (dt > dtLimit)
71     {
72         dt = dtLimit;
73         doneDT -= dtLimit;
74     }
75 
76     while (doneDT > 0)
77     {
78         // update by time step
79         lastTime = TimeKeeper::getTick();
80         doUpdateMotion(dt);
81 
82         // compute motion's bounding box around center of tank
83         const float* newVelocity = getVelocity();
84         bbox[0][0] = bbox[1][0] = oldPosition[0];
85         bbox[0][1] = bbox[1][1] = oldPosition[1];
86         bbox[0][2] = bbox[1][2] = oldPosition[2];
87         if (newVelocity[0] > 0.0f)
88             bbox[1][0] += dt * newVelocity[0];
89         else
90             bbox[0][0] += dt * newVelocity[0];
91         if (newVelocity[1] > 0.0f)
92             bbox[1][1] += dt * newVelocity[1];
93         else
94             bbox[0][1] += dt * newVelocity[1];
95         if (newVelocity[2] > 0.0f)
96             bbox[1][2] += dt * newVelocity[2];
97         else
98             bbox[0][2] += dt * newVelocity[2];
99 
100         // expand bounding box to include entire tank
101         float size = BZDBCache::tankRadius;
102         if (getFlag() == Flags::Obesity) size *= BZDB.eval(StateDatabase::BZDB_OBESEFACTOR);
103         else if (getFlag() == Flags::Tiny) size *= BZDB.eval(StateDatabase::BZDB_TINYFACTOR);
104         else if (getFlag() == Flags::Thief) size *= BZDB.eval(StateDatabase::BZDB_THIEFTINYFACTOR);
105         bbox[0][0] -= size;
106         bbox[1][0] += size;
107         bbox[0][1] -= size;
108         bbox[1][1] += size;
109         bbox[1][2] += BZDBCache::tankHeight;
110 
111         // do remaining update stuff
112         doUpdate(dt);
113 
114         // subtract another chunk
115         doneDT -= dtLimit;
116 
117         // if we only have a nubby left, don't do a full dt.
118         if (doneDT < dtLimit)
119             dt = doneDT;
120     }
121 }
122 
getLastMotion() const123 Ray BaseLocalPlayer::getLastMotion() const
124 {
125     return Ray(lastPosition, getVelocity());
126 }
127 
128 const float (*BaseLocalPlayer::getLastMotionBBox() const)[3]
__anonfc7f8f530102null129 {
130     return bbox;
131 }
132 
133 
134 // Local Variables: ***
135 // mode: C++ ***
136 // tab-width: 4 ***
137 // c-basic-offset: 4 ***
138 // indent-tabs-mode: nil ***
139 // End: ***
140 // ex: shiftwidth=4 tabstop=4
141