]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/roles.qc
Bot AI: don't rate enemies running too fast for chasing them
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / roles.qc
1 #include "roles.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "havocbot.qh"
6
7 #include "../cvars.qh"
8
9 #include "../bot.qh"
10 #include "../navigation.qh"
11
12 .float max_armorvalue;
13 .float havocbot_role_timeout;
14
15 .void(entity this) havocbot_previous_role;
16 .void(entity this) havocbot_role;
17
18 void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, float sradius)
19 {
20         // rate waypoints only if there's no alternative goal
21         if(navigation_bestgoal)
22                 return;
23
24         float f;
25         float range = 500;
26         sradius = max(range, (0.5 + random() * 0.5) * sradius);
27         while(sradius > 100)
28         {
29                 IL_EACH(g_waypoints, vdist(it.origin - org, <, sradius)
30                         && vdist(it.origin - org, >, max(100, sradius - range))
31                         && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
32                 {
33                         if(vdist(it.origin - this.wp_goal_prev0.origin, <, range * 1.5))
34                                 f = 0.1;
35                         else if(vdist(it.origin - this.wp_goal_prev1.origin, <, range * 1.5))
36                                 f = 0.1;
37                         else
38                                 f = 0.5 + random() * 0.5;
39                         navigation_routerating(this, it, ratingscale * f, 2000);
40                 });
41                 if(navigation_bestgoal)
42                         break;
43                 sradius -= range;
44         }
45 };
46
47 bool havocbot_goalrating_item_can_be_left_to_teammate(entity this, entity player, entity item)
48 {
49         if (item.health && player.health <= this.health) {return true;}
50         if (item.armorvalue && player.armorvalue <= this.armorvalue) {return true;}
51         if (item.weapons && !(player.weapons & item.weapons)) {return true;}
52         if (item.ammo_shells && player.ammo_shells <= this.ammo_shells) {return true;}
53         if (item.ammo_nails && player.ammo_nails <= this.ammo_nails) {return true;}
54         if (item.ammo_rockets && player.ammo_rockets <= this.ammo_rockets) {return true;}
55         if (item.ammo_cells && player.ammo_cells <= this.ammo_cells) {return true;}
56         if (item.ammo_plasma && player.ammo_plasma <= this.ammo_plasma) {return true;}
57         if (item.itemdef.instanceOfPowerup) {return true;}
58
59         return false;
60 };
61
62 bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
63 {
64         if(!teamplay)
65                 return true;
66
67         // actually these variables hold the squared distances in order to optimize code
68         float friend_distance = FLOAT_MAX;
69         float enemy_distance = FLOAT_MAX;
70         float dist;
71
72         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it),
73         {
74                 if (it.team == this.team)
75                 {
76                         if (!IS_REAL_CLIENT(it))
77                                 continue;
78
79                         dist = vlen2(it.origin - item_org);
80                         if(dist > friend_distance)
81                                 continue;
82
83                         if(havocbot_goalrating_item_can_be_left_to_teammate(this, it, item))
84                         {
85                                 friend_distance = dist;
86                                 continue;
87                         }
88                 }
89                 else
90                 {
91                         // If enemy only track distances
92                         // TODO: track only if visible ?
93                         dist = vlen2(it.origin - item_org);
94                         if(dist < enemy_distance)
95                                 enemy_distance = dist;
96                 }
97         });
98
99         // Rate the item only if no one needs it, or if an enemy is closer to it
100         dist = vlen2(item_org - org);
101         if ((enemy_distance < friend_distance && dist < enemy_distance) ||
102                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ** 2) ||
103                 (dist < friend_distance && dist < 200 ** 2))
104                 return true;
105         return false;
106 };
107
108 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
109 {
110         float rating;
111         vector o;
112         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
113
114         IL_EACH(g_items, it.bot_pickup,
115         {
116                 if(!it.solid)
117                 {
118                         if(!autocvar_bot_ai_timeitems)
119                                 continue;
120                         if(!it.scheduledrespawntime)
121                                 continue;
122                         if(it.respawntime < max(11, autocvar_bot_ai_timeitems_minrespawndelay))
123                                 continue;
124                         if(it.respawntimejitter && !it.itemdef.instanceOfPowerup)
125                                 continue;
126
127                         float t = 0;
128                         if(it.itemdef.instanceOfPowerup)
129                                 t = bound(0, skill / 10, 1) * 6;
130                         else if(skill >= 9)
131                                 t = 4;
132
133                         if(time < it.scheduledrespawntime - t)
134                                 continue;
135
136                         it.bot_pickup_respawning = true;
137                 }
138                 o = (it.absmin + it.absmax) * 0.5;
139                 if(vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
140                         continue;
141
142                 // Check if the item can be picked up safely
143                 if(it.classname == "droppedweapon")
144                 {
145                         if(!IS_ONGROUND(it))
146                                 continue;
147                         traceline(o, o + '0 0 -1500', true, NULL);
148
149                         if(IN_LAVA(trace_endpos + '0 0 1'))
150                                 continue;
151
152                         // this tracebox_hits_trigger_hurt call isn't needed:
153                         // dropped weapons are removed as soon as they fall on a trigger_hurt
154                         // and can't be rated while they are in the air
155                         //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
156                         //      continue;
157                 }
158                 else
159                 {
160                         if(IN_LAVA(it.origin + (it.mins + it.maxs) * 0.5))
161                                 continue;
162                 }
163
164                 if(!havocbot_goalrating_item_pickable_check_players(this, org, it, o))
165                         continue;
166                 
167                 rating = it.bot_pickupevalfunc(this, it);
168
169                 if(rating > 0)
170                         navigation_routerating(this, it, rating * ratingscale, 2000);
171         });
172 }
173
174 #define BOT_RATING_ENEMY 2500
175 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
176 {
177         if (autocvar_bot_nofire)
178                 return;
179
180         // don't chase players if we're under water
181         if(this.waterlevel>WATERLEVEL_WETFEET)
182                 return;
183
184         ratingscale = ratingscale * 0.00005; // enemies are rated around 20000 already
185
186         float t;
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                 t = ((this.health + this.armorvalue) - (it.health + it.armorvalue)) / 150;
202                 t = bound(0, 1 + t, 3);
203                 if (skill > 3)
204                 {
205                         if (time < this.strength_finished - 1) t += 0.5;
206                         if (time < it.strength_finished - 1) t -= 0.5;
207                         if (time < this.invincible_finished - 1) t += 0.2;
208                         if (time < it.invincible_finished - 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
219 void havocbot_role_generic(entity this)
220 {
221         if(IS_DEAD(this))
222                 return;
223
224         if (navigation_goalrating_timeout(this))
225         {
226                 navigation_goalrating_start(this);
227                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
228                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
229                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
230                 navigation_goalrating_end(this);
231
232                 navigation_goalrating_timeout_set(this);
233         }
234 }
235
236 void havocbot_chooserole_generic(entity this)
237 {
238         this.havocbot_role = havocbot_role_generic;
239 }
240
241 void havocbot_chooserole(entity this)
242 {
243         LOG_TRACE("choosing a role...");
244         navigation_goalrating_timeout_force(this);
245         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
246                 havocbot_chooserole_generic(this);
247 }