Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
capture_common.hpp
Go to the documentation of this file.
1 #pragma once
3 #include "inexor/shared/geom.hpp"
7 #include "inexor/shared/ents.hpp"
9 
10 #include <algorithm>
11 
13 {
14  static const int CAPTURERADIUS = 64;
15  static const int CAPTUREHEIGHT = 24;
16  static const int OCCUPYBONUS = 1;
17  static const int OCCUPYPOINTS = 1;
18  static const int OCCUPYENEMYLIMIT = 28;
19  static const int OCCUPYNEUTRALLIMIT = 14;
20  static const int SCORESECS = 10;
21  static const int AMMOSECS = 15;
22  static const int REGENSECS = 1;
23  static const int REGENHEALTH = 10;
24  static const int REGENARMOUR = 10;
25  static const int REGENAMMO = 20;
26  static const int MAXAMMO = 5;
27  static const int REPAMMODIST = 32;
28  static const int RESPAWNSECS = 10;
29  static const int REGENRESPAWNSECS = 0;
30  static const int MAXBASES = 100;
31 
32  struct baseinfo
33  {
34  vec o;
35  string owner, enemy;
37  string name, info;
39 
41 
42  baseinfo() { reset(); }
43 
44  bool valid() const { return ammotype>0 && ammotype<=I_CARTRIDGES-I_SHELLS+1; }
45 
46  void noenemy()
47  {
48  enemy[0] = '\0';
49  enemies = 0;
50  converted = 0;
51  }
52 
53  void reset()
54  {
55  noenemy();
56  owner[0] = '\0';
57  capturetime = -1;
58  ammogroup = 0;
59  ammotype = 0;
60  ammo = 0;
61  owners = 0;
62  }
63 
64  bool enter(const char *team)
65  {
66  if(!strcmp(owner, team))
67  {
68  owners++;
69  return false;
70  }
71  if(!enemies)
72  {
73  if(strcmp(enemy, team))
74  {
75  converted = 0;
76  copystring(enemy, team);
77  }
78  enemies++;
79  return true;
80  } else if(strcmp(enemy, team)) return false;
81  else enemies++;
82  return false;
83  }
84 
85  bool steal(const char *team)
86  {
87  return !enemies && strcmp(owner, team);
88  }
89 
90  bool leave(const char *team)
91  {
92  if(!strcmp(owner, team) && owners > 0)
93  {
94  owners--;
95  return false;
96  }
97  if(strcmp(enemy, team) || enemies <= 0) return false;
98  enemies--;
99  return !enemies;
100  }
101 
102  int occupy(const char *team, int units)
103  {
104  if(strcmp(enemy, team)) return -1;
105  converted += units;
106  if(units<0)
107  {
108  if(converted<=0) noenemy();
109  return -1;
110  } else if(converted<(owner[0] ? int(OCCUPYENEMYLIMIT) : int(OCCUPYNEUTRALLIMIT))) return -1;
111  if(owner[0]) { owner[0] = '\0'; converted = 0; copystring(enemy, team); return 0; } else { copystring(owner, team); ammo = 0; capturetime = 0; owners = enemies; noenemy(); return 1; }
112  }
113 
114  bool addammo(int i)
115  {
116  if(ammo>=MAXAMMO) return false;
117  ammo = std::min(ammo+i, int(MAXAMMO));
118  return true;
119  }
120 
121  bool takeammo(const char *team)
122  {
123  if(strcmp(owner, team) || ammo<=0) return false;
124  ammo--;
125  return true;
126  }
127  };
128 
130 
131  struct score
132  {
133  string team;
134  int total;
135  };
136 
138 
139  int captures = 0;
140 
141  void resetbases()
142  {
143  bases.shrink(0);
144  scores.shrink(0);
145  captures = 0;
146  }
147 
148  bool hidefrags() { return true; }
149 
150  int getteamscore(const char *team)
151  {
152  loopv(scores)
153  {
154  score &cs = scores[i];
155  if(!strcmp(cs.team, team)) return cs.total;
156  }
157  return 0;
158  }
159 
161  {
162  loopv(scores) teamscores.add(teamscore(scores[i].team, scores[i].total));
163  }
164 
165  score &findscore(const char *team)
166  {
167  loopv(scores)
168  {
169  score &cs = scores[i];
170  if(!strcmp(cs.team, team)) return cs;
171  }
172  score &cs = scores.add();
173  copystring(cs.team, team);
174  cs.total = 0;
175  return cs;
176  }
177 
178  void addbase(int ammotype, const vec &o)
179  {
180  if(bases.length() >= MAXBASES) return;
181  baseinfo &b = bases.add();
182  b.ammogroup = min(ammotype, 0);
183  b.ammotype = ammotype > 0 ? ammotype : rnd(I_GRENADES-I_SHELLS+1)+1;
184  b.o = o;
185 
186  if(b.ammogroup)
187  {
188  loopi(bases.length()-1) if(b.ammogroup == bases[i].ammogroup)
189  {
190  b.ammotype = bases[i].ammotype;
191  return;
192  }
193  int uses[I_GRENADES-I_SHELLS+1];
194  memset(uses, 0, sizeof(uses));
195  loopi(bases.length()-1) if(bases[i].ammogroup)
196  {
197  loopj(i) if(bases[j].ammogroup == bases[i].ammogroup) goto nextbase;
198  uses[bases[i].ammotype-1]++;
199  nextbase:;
200  }
201  int mintype = 0;
202  loopi(I_GRENADES-I_SHELLS+1) if(uses[i] < uses[mintype]) mintype = i;
203  int numavail = 0, avail[I_GRENADES-I_SHELLS+1];
204  loopi(I_GRENADES-I_SHELLS+1) if(uses[i] == uses[mintype]) avail[numavail++] = i+1;
205  b.ammotype = avail[rnd(numavail)];
206  }
207  }
208 
209  void initbase(int i, int ammotype, const char *owner, const char *enemy, int converted, int ammo)
210  {
211  if(!bases.inrange(i)) return;
212  baseinfo &b = bases[i];
213  b.ammotype = ammotype;
214  copystring(b.owner, owner);
215  copystring(b.enemy, enemy);
216  b.converted = converted;
217  b.ammo = ammo;
218  }
219 
220  bool hasbases(const char *team)
221  {
222  loopv(bases)
223  {
224  baseinfo &b = bases[i];
225  if(b.owner[0] && !strcmp(b.owner, team)) return true;
226  }
227  return false;
228  }
229 
231  {
232  float dist = 1e10f;
233  loopv(bases)
234  {
235  baseinfo &e = bases[i];
236  if(e.owner[0] && strcmp(b.owner, e.owner))
237  dist = min(dist, b.o.dist(e.o));
238  }
239  return dist;
240  }
241 
242  bool insidebase(const baseinfo &b, const vec &o)
243  {
244  float dx = (b.o.x-o.x), dy = (b.o.y-o.y), dz = (b.o.z-o.z);
245  return dx*dx + dy*dy <= CAPTURERADIUS*CAPTURERADIUS && fabs(dz) <= CAPTUREHEIGHT;
246  }
247 
248 };
Definition: ents.hpp:82
static const int CAPTURERADIUS
Definition: capture_common.hpp:14
static const int OCCUPYPOINTS
Definition: capture_common.hpp:17
Vector template.
Definition: cube_vector.hpp:22
string team
Definition: capture_common.hpp:133
string enemy
Definition: capture_common.hpp:35
static const int MAXAMMO
Definition: capture_common.hpp:26
bool addammo(int i)
Definition: capture_common.hpp:114
static const int CAPTUREHEIGHT
Definition: capture_common.hpp:15
int capturetime
Definition: capture_common.hpp:40
Definition: ents.hpp:41
bool insidebase(const baseinfo &b, const vec &o)
Definition: capture_common.hpp:242
bool takeammo(const char *team)
Definition: capture_common.hpp:121
vector< score > scores
Definition: capture_common.hpp:137
baseinfo()
Definition: capture_common.hpp:42
static const int REGENSECS
Definition: capture_common.hpp:22
void initbase(int i, int ammotype, const char *owner, const char *enemy, int converted, int ammo)
Definition: capture_common.hpp:209
vec o
Definition: capture_common.hpp:34
void resetbases()
Definition: capture_common.hpp:141
string owner
Definition: capture_common.hpp:35
int ammotype
Definition: capture_common.hpp:40
float dist(const vec &e) const
Definition: geom.hpp:204
entitylight light
Definition: capture_common.hpp:38
int converted
Definition: capture_common.hpp:40
bool steal(const char *team)
Definition: capture_common.hpp:85
int owners
Definition: capture_common.hpp:40
static const int REPAMMODIST
Definition: capture_common.hpp:27
cone-shaped spotlights
Definition: ents.hpp:41
else loopi(numargs)
Definition: command.cpp:3019
void reset()
Definition: capture_common.hpp:53
bool enemy(fpsent *d, aistate &b, const vec &pos, float guard=SIGHTMIN, int pursue=0)
Definition: ai.cpp:312
static const int OCCUPYBONUS
Definition: capture_common.hpp:16
T & add(const T &x)
Add new index to vector.
Definition: cube_vector.hpp:73
static const int REGENARMOUR
Definition: capture_common.hpp:24
int rnd(int Rmax)
Function alias. Should be replaced inline actually!
Definition: tools.hpp:64
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
static const int SCORESECS
Definition: capture_common.hpp:20
Definition: capture_common.hpp:131
const T & min(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:210
int total
Definition: capture_common.hpp:134
Definition: capture_common.hpp:32
Definition: ents.hpp:41
bool hidefrags()
Definition: capture_common.hpp:148
Definition: capture_common.hpp:12
static const int OCCUPYENEMYLIMIT
Definition: capture_common.hpp:18
bool enter(const char *team)
Definition: capture_common.hpp:64
int getteamscore(const char *team)
Definition: capture_common.hpp:150
static const int REGENRESPAWNSECS
Definition: capture_common.hpp:29
string info
Definition: capture_common.hpp:37
void addbase(int ammotype, const vec &o)
Definition: capture_common.hpp:178
static const int AMMOSECS
Definition: capture_common.hpp:21
static const int MAXBASES
Definition: capture_common.hpp:30
int enemies
Definition: capture_common.hpp:40
char * copystring(char *d, const char *s, size_t len)
Definition: cube_tools.hpp:56
#define loopj(m)
Definition: cube_loops.hpp:9
vector< baseinfo > bases
Definition: capture_common.hpp:129
int ammogroup
Definition: capture_common.hpp:40
static const int REGENHEALTH
Definition: capture_common.hpp:23
some team modes allow more than 2 teams allow sorting multiple teams using team scores ...
Definition: teaminfo.hpp:43
float disttoenemy(baseinfo &b)
Definition: capture_common.hpp:230
static map entities ("entity") and dynamic entities (players/monsters, "dynent") the gamecode extends...
static const int OCCUPYNEUTRALLIMIT
Definition: capture_common.hpp:19
mathmatics for vectors, matrices, quaterions and more
bool hasbases(const char *team)
Definition: capture_common.hpp:220
void noenemy()
Definition: capture_common.hpp:46
vec ammopos
Definition: capture_common.hpp:36
#define loopv(v)
Definition: cube_loops.hpp:21
void getteamscores(vector< teamscore > &teamscores)
Definition: capture_common.hpp:160
int occupy(const char *team, int units)
Definition: capture_common.hpp:102
score & findscore(const char *team)
Definition: capture_common.hpp:165
string name
Definition: capture_common.hpp:37
int ammo
Definition: capture_common.hpp:40
int captures
Definition: capture_common.hpp:139
static const int RESPAWNSECS
Definition: capture_common.hpp:28
bool leave(const char *team)
Definition: capture_common.hpp:90
static const int REGENAMMO
Definition: capture_common.hpp:25
bool valid() const
Definition: capture_common.hpp:44