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