]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/roles.qc
Numerous enhancements to the new status effects system, split powerups into a dedicat...
[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/mutators/mutator/powerups/_mod.qh>
5 #include <common/mutators/mutator/status_effects/_mod.qh>
6 #include <common/weapons/_all.qh>
7 #include <server/bot/default/bot.qh>
8 #include <server/bot/default/cvars.qh>
9 #include <server/bot/default/havocbot/havocbot.qh>
10 #include <server/bot/default/navigation.qh>
11 #include <server/items/items.qh>
12 #include <server/items/spawning.qh>
13 #include <server/mutators/_mod.qh>
14 #include <server/resources.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
19         if(navigation_bestgoal)
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                 });
39                 if(navigation_bestgoal)
40                         break;
41                 sradius -= range;
42         }
43 };
44
45 bool havocbot_goalrating_item_can_be_left_to_teammate(entity this, entity player, entity item)
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
60 bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
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
165                 if(!havocbot_goalrating_item_pickable_check_players(this, org, it, o))
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 {
178         if (autocvar_bot_nofire)
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
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, 10000, 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 }