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