]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/roles.qc
Bot AI: Fix buggy algorithm for deciding whether to leave an item to a human teammate...
[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
58         return false;
59 };
60
61 bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
62 {
63         if(!teamplay)
64                 return true;
65
66         float friend_distance = FLOAT_MAX;
67         float enemy_distance = FLOAT_MAX;
68
69         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it),
70         {
71                 if (it.team == this.team)
72                 {
73                         if (!IS_REAL_CLIENT(it))
74                                 continue;
75
76                         if(vdist(it.origin - item_org, >, friend_distance))
77                                 continue;
78
79                         if(havocbot_goalrating_item_can_be_left_to_teammate(this, it, item))
80                         {
81                                 friend_distance = vlen(it.origin - item_org);
82                                 continue;
83                         }
84                 }
85                 else
86                 {
87                         // If enemy only track distances
88                         // TODO: track only if visible ?
89                         if(vdist(it.origin - item_org, <, enemy_distance))
90                                 enemy_distance = vlen(it.origin - item_org);
91                 }
92         });
93
94         // Rate the item only if no one needs it, or if an enemy is closer to it
95         if ((enemy_distance < friend_distance && vdist(item_org - org, <, enemy_distance)) ||
96                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius))
97                 return true;
98         return false;
99 };
100
101 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
102 {
103         float rating;
104         vector o;
105         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
106
107         IL_EACH(g_items, it.bot_pickup,
108         {
109                 if(!it.solid)
110                 {
111                         if(!autocvar_bot_ai_timeitems)
112                                 continue;
113                         if(!it.scheduledrespawntime)
114                                 continue;
115                         if(it.respawntime < max(11, autocvar_bot_ai_timeitems_minrespawndelay))
116                                 continue;
117                         if(it.respawntimejitter && !it.itemdef.instanceOfPowerup)
118                                 continue;
119
120                         float t = 0;
121                         if(it.itemdef.instanceOfPowerup)
122                                 t = bound(0, skill / 10, 1) * 6;
123                         else if(skill >= 9)
124                                 t = 4;
125
126                         if(time < it.scheduledrespawntime - t)
127                                 continue;
128
129                         it.bot_pickup_respawning = true;
130                 }
131                 o = (it.absmin + it.absmax) * 0.5;
132                 if(vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
133                         continue;
134
135                 // Check if the item can be picked up safely
136                 if(it.classname == "droppedweapon")
137                 {
138                         if(!IS_ONGROUND(it))
139                                 continue;
140                         traceline(o, o + '0 0 -1500', true, NULL);
141
142                         if(IN_LAVA(trace_endpos + '0 0 1'))
143                                 continue;
144
145                         // this tracebox_hits_trigger_hurt call isn't needed:
146                         // dropped weapons are removed as soon as they fall on a trigger_hurt
147                         // and can't be rated while they are in the air
148                         //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
149                         //      continue;
150                 }
151                 else
152                 {
153                         if(IN_LAVA(it.origin + (it.mins + it.maxs) * 0.5))
154                                 continue;
155                 }
156
157                 if(!havocbot_goalrating_item_pickable_check_players(this, org, it, o))
158                         continue;
159                 
160                 rating = it.bot_pickupevalfunc(this, it);
161
162                 if(rating > 0)
163                         navigation_routerating(this, it, rating * ratingscale, 2000);
164         });
165 }
166
167 #define BOT_RATING_ENEMY 2500
168 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
169 {
170         if (autocvar_bot_nofire)
171                 return;
172
173         // don't chase players if we're under water
174         if(this.waterlevel>WATERLEVEL_WETFEET)
175                 return;
176
177         ratingscale = ratingscale * 0.00005; // enemies are rated around 20000 already
178
179         float t;
180         FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), {
181                 // TODO: Merge this logic with the bot_shouldattack function
182                 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
183                         continue;
184
185                 // rate only visible enemies
186                 /*
187                 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
188                 if (trace_fraction < 1 || trace_ent != it)
189                         continue;
190                 */
191
192                 t = ((this.health + this.armorvalue) - (it.health + it.armorvalue)) / 150;
193                 t = bound(0, 1 + t, 3);
194                 if (skill > 3)
195                 {
196                         if (time < this.strength_finished - 1) t += 0.5;
197                         if (time < it.strength_finished - 1) t -= 0.5;
198                         if (time < this.invincible_finished - 1) t += 0.2;
199                         if (time < it.invincible_finished - 1) t -= 0.4;
200                 }
201                 t += max(0, 8 - skill) * 0.05; // less skilled bots attack more mindlessly
202                 ratingscale *= t;
203                 if (ratingscale > 0)
204                         navigation_routerating(this, it, ratingscale * BOT_RATING_ENEMY, 2000);
205         });
206 }
207
208 // legacy bot role for standard gamemodes
209 // go to best items
210 void havocbot_role_generic(entity this)
211 {
212         if(IS_DEAD(this))
213                 return;
214
215         if (navigation_goalrating_timeout(this))
216         {
217                 navigation_goalrating_start(this);
218                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
219                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
220                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
221                 navigation_goalrating_end(this);
222
223                 navigation_goalrating_timeout_set(this);
224         }
225 }
226
227 void havocbot_chooserole_generic(entity this)
228 {
229         this.havocbot_role = havocbot_role_generic;
230 }
231
232 void havocbot_chooserole(entity this)
233 {
234         LOG_TRACE("choosing a role...");
235         navigation_goalrating_timeout_force(this);
236         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
237                 havocbot_chooserole_generic(this);
238 }