Xonotic
roles.qc
Go to the documentation of this file.
1 #include "roles.qh"
2 
4 #include <common/stats.qh>
7 #include <common/weapons/_all.qh>
12 #include <server/items/items.qh>
13 #include <server/items/spawning.qh>
14 #include <server/mutators/_mod.qh>
15 
16 void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, float sradius)
17 {
18  // rate waypoints only if there's no alternative goal
20  return;
21 
22  float f;
23  float range = 500;
24  sradius = max(range, (0.5 + random() * 0.5) * sradius);
25  while(sradius > 100)
26  {
27  IL_EACH(g_waypoints, vdist(it.origin - org, <, sradius)
28  && vdist(it.origin - org, >, max(100, sradius - range))
29  && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
30  {
31  if(vdist(it.origin - this.wp_goal_prev0.origin, <, range * 1.5))
32  f = 0.1;
33  else if(vdist(it.origin - this.wp_goal_prev1.origin, <, range * 1.5))
34  f = 0.1;
35  else
36  f = 0.5 + random() * 0.5;
37  navigation_routerating(this, it, ratingscale * f, 2000);
38  });
40  break;
41  sradius -= range;
42  }
43 };
44 
46 {
47  if (GetResource(item, RES_HEALTH) && GetResource(player, RES_HEALTH) <= GetResource(this, RES_HEALTH)) {return true;}
48  if (GetResource(item, RES_ARMOR) && GetResource(player, RES_ARMOR) <= GetResource(this, RES_ARMOR)) {return true;}
49  if (STAT(WEAPONS, item) && !(STAT(WEAPONS, player) & STAT(WEAPONS, item))) {return true;}
50  if (GetResource(item, RES_SHELLS) && GetResource(player, RES_SHELLS) <= GetResource(this, RES_SHELLS)) {return true;}
51  if (GetResource(item, RES_BULLETS) && GetResource(player, RES_BULLETS) <= GetResource(this, RES_BULLETS)) {return true;}
52  if (GetResource(item, RES_ROCKETS) && GetResource(player, RES_ROCKETS) <= GetResource(this, RES_ROCKETS)) {return true;}
53  if (GetResource(item, RES_CELLS) && GetResource(player, RES_CELLS) <= GetResource(this, RES_CELLS)) {return true;}
54  if (GetResource(item, RES_PLASMA) && GetResource(player, RES_PLASMA) <= GetResource(this, RES_PLASMA)) {return true;}
55  if (item.itemdef.instanceOfPowerup) {return true;}
56 
57  return false;
58 };
59 
61 {
62  if(!teamplay)
63  return true;
64 
65  // these variables hold squared distances in order to optimize code
66  float friend_dist2 = FLOAT_MAX;
67  float enemy_dist2 = FLOAT_MAX;
68  float dist2;
69 
70  FOREACH_CLIENT(IS_PLAYER(it) && it != this && !(IS_DEAD(it) || STAT(FROZEN, it)),
71  {
72  if (it.team == this.team)
73  {
74  if (!IS_REAL_CLIENT(it))
75  continue;
76 
77  dist2 = vlen2(it.origin - item_org);
78  if (dist2 > friend_dist2)
79  continue;
80 
81  if(havocbot_goalrating_item_can_be_left_to_teammate(this, it, item))
82  {
83  friend_dist2 = dist2;
84  continue;
85  }
86  }
87  else
88  {
89  // If enemy only track distances
90  // TODO: track only if visible ?
91  dist2 = vlen2(it.origin - item_org);
92  if (dist2 < enemy_dist2)
93  enemy_dist2 = dist2;
94  }
95  });
96 
97  // Rate the item only if no one needs it, or if an enemy is closer to it
98  dist2 = vlen2(item_org - org);
99  if ((enemy_dist2 < friend_dist2 && dist2 < enemy_dist2)
100  || (friend_dist2 > autocvar_bot_ai_friends_aware_pickup_radius ** 2)
101  || (dist2 < friend_dist2 && dist2 < 200 ** 2))
102  return true;
103  return false;
104 };
105 
106 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
107 {
108  ratingscale = ratingscale * 0.0001;
109 
110  IL_EACH(g_items, it.bot_pickup,
111  {
112  // ignore if bot already rated this item with a higher ratingscale
113  // NOTE: this code assumes each bot rates items in a different frame
114  if(it.bot_ratingscale_time == time && ratingscale < it.bot_ratingscale)
115  continue;
116 
117  if(!it.solid)
118  {
119  if(!autocvar_bot_ai_timeitems)
120  continue;
121  if(!it.scheduledrespawntime)
122  continue;
123  if(it.respawntime < max(11, autocvar_bot_ai_timeitems_minrespawndelay))
124  continue;
125  if(it.respawntimejitter && !it.itemdef.instanceOfPowerup)
126  continue;
127 
128  float t = 0;
129  if(it.itemdef.instanceOfPowerup)
130  t = bound(0, skill / 10, 1) * 6;
131  else if(skill >= 9)
132  t = 4;
133 
134  if(time < it.scheduledrespawntime - t)
135  continue;
136 
137  it.bot_pickup_respawning = true;
138  }
139  vector o = (it.absmin + it.absmax) * 0.5;
140  if(vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
141  continue;
142 
143  // Check if the item can be picked up safely
144  if(Item_IsLoot(it))
145  {
146  if(!IS_ONGROUND(it))
147  continue;
148  traceline(o, o + '0 0 -1500', true, NULL);
149 
150  if(IN_LAVA(trace_endpos + '0 0 1'))
151  continue;
152 
153  // this tracebox_hits_trigger_hurt call isn't needed:
154  // dropped weapons are removed as soon as they fall on a trigger_hurt
155  // and can't be rated while they are in the air
156  //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
157  // continue;
158  }
159  else
160  {
161  if(IN_LAVA(it.origin + (it.mins + it.maxs) * 0.5))
162  continue;
163  }
164 
166  continue;
167 
168  it.bot_ratingscale_time = time;
169  it.bot_ratingscale = ratingscale;
170  float rating = it.bot_pickupevalfunc(this, it);
171  if(rating > 0)
172  navigation_routerating(this, it, rating * ratingscale, 2000);
173  });
174 }
175 
176 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
177 {
179  return;
180 
181  // don't chase players if we're under water
182  if(this.waterlevel > WATERLEVEL_WETFEET)
183  return;
184 
185  ratingscale = ratingscale * 0.0001;
186 
187  FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), {
188  // TODO: Merge this logic with the bot_shouldattack function
189  if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
190  continue;
191  if(vdist(vec2(it.velocity), >, autocvar_sv_maxspeed * 2))
192  continue;
193 
194  // rate only visible enemies
195  /*
196  traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
197  if (trace_fraction < 1 || trace_ent != it)
198  continue;
199  */
200 
201  float t = ((GetResource(this, RES_HEALTH) + GetResource(this, RES_ARMOR)) - (GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR))) / 150;
202  t = bound(0, 1 + t, 3);
203  if (skill > 3)
204  {
205  if (time < StatusEffects_gettime(STATUSEFFECT_Strength, this) - 1) t += 0.5;
206  if (time < StatusEffects_gettime(STATUSEFFECT_Strength, it) - 1) t -= 0.5;
207  if (time < StatusEffects_gettime(STATUSEFFECT_Shield, this) - 1) t += 0.2;
208  if (time < StatusEffects_gettime(STATUSEFFECT_Shield, it) - 1) t -= 0.4;
209  }
210  t += max(0, 8 - skill) * 0.05; // less skilled bots attack more mindlessly
211  ratingscale *= t;
212  if (ratingscale > 0)
213  navigation_routerating(this, it, ratingscale * BOT_RATING_ENEMY, 2000);
214  });
215 }
216 
217 // legacy bot role for standard gamemodes
218 // go to best items
220 {
221  if(IS_DEAD(this))
222  return;
223 
225  {
227  havocbot_goalrating_items(this, 10000, this.origin, 10000);
228  havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
229  havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
231 
233  }
234 }
235 
237 {
238  this.havocbot_role = havocbot_role_generic;
239 }
240 
242 {
243  LOG_TRACE("choosing a role...");
245  if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
247 }
#define IL_EACH(this, cond, body)
float ignoregoaltime
Definition: api.qh:98
entity ignoregoal
Definition: api.qh:99
void navigation_goalrating_start(entity this)
Definition: navigation.qc:1830
#define IN_LAVA(pos)
Definition: bot.qh:83
void navigation_goalrating_end(entity this)
Definition: navigation.qc:1845
Header file that describes the resource system.
float waterlevel
Definition: progsdefs.qc:181
entity() spawn
#define FOREACH_CLIENT(cond, body)
Definition: utils.qh:49
#define IS_ONGROUND(s)
Definition: movetypes.qh:16
float skill
Definition: api.qh:35
void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
Definition: roles.qc:176
origin
Definition: ent_cs.qc:114
void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, float sradius)
Definition: roles.qc:16
void navigation_goalrating_timeout_force(entity this)
Definition: navigation.qc:28
void navigation_routerating(entity this, entity e, float f, float rangebias)
Definition: navigation.qc:1220
RES_HEALTH
Definition: ent_cs.qc:126
void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
Definition: roles.qc:106
#define vlen2(v)
Definition: vector.qh:4
bool navigation_goalrating_timeout(entity this)
Definition: navigation.qc:43
const int WAYPOINTFLAG_TELEPORT
Definition: api.qh:13
const float BOT_RATING_ENEMY
Definition: roles.qh:3
IntrusiveList g_items
Definition: items.qh:126
#define NULL
Definition: post.qh:17
void havocbot_chooserole_generic(entity this)
Definition: roles.qc:236
vector trace_endpos
Definition: csprogsdefs.qc:37
float teamplay
Definition: progsdefs.qc:31
const int WATERLEVEL_WETFEET
Definition: movetypes.qh:12
#define IS_DEAD(s)
Definition: utils.qh:26
IntrusiveList g_waypoints
Definition: api.qh:148
void havocbot_chooserole(entity this)
Definition: roles.qc:241
void navigation_goalrating_timeout_set(entity this)
Definition: navigation.qc:19
vector(float skel, float bonenum) _skel_get_boneabs_hidden
bool autocvar_bot_nofire
Definition: cvars.qh:54
void havocbot_role_generic(entity this)
Definition: roles.qc:219
float GetResource(entity e, Resource res_type)
Returns the current amount of resource the given entity has.
Definition: cl_resources.qc:10
float autocvar_bot_ai_friends_aware_pickup_radius
Definition: cvars.qh:38
bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
Definition: roles.qc:60
#define vdist(v, cmp, f)
Vector distance comparison, avoids sqrt()
Definition: vector.qh:8
#define LOG_TRACE(...)
Definition: log.qh:81
bool havocbot_goalrating_item_can_be_left_to_teammate(entity this, entity player, entity item)
Definition: roles.qc:45
#define MUTATOR_CALLHOOK(id,...)
Definition: base.qh:140
#define vec2(...)
Definition: vector.qh:90
bool bot_shouldattack(entity this, entity e)
Definition: aim.qc:112
bool Item_IsLoot(entity item)
Returns whether the item is loot.
Definition: spawning.qc:121
Header file that describes the functions related to game items.
float time
Definition: csprogsdefs.qc:16
#define IS_PLAYER(v)
Definition: utils.qh:9
const float FLOAT_MAX
Definition: float.qh:3