]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/roles.qc
33d6ec1e7518182e29a367db5c404f1ecde75e27
[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 <server/items/items.qh>
6 #include <server/items/spawning.qh>
7 #include <server/resources.qh>
8 #include "havocbot.qh"
9
10 #include "../cvars.qh"
11
12 #include "../bot.qh"
13 #include "../navigation.qh"
14
15 void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, float sradius)
16 {
17         // rate waypoints only if there's no alternative goal
18         if(navigation_bestgoal)
19                 return;
20
21         float f;
22         float range = 500;
23         sradius = max(range, (0.5 + random() * 0.5) * sradius);
24         while(sradius > 100)
25         {
26                 IL_EACH(g_waypoints, vdist(it.origin - org, <, sradius)
27                         && vdist(it.origin - org, >, max(100, sradius - range))
28                         && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
29                 {
30                         if(vdist(it.origin - this.wp_goal_prev0.origin, <, range * 1.5))
31                                 f = 0.1;
32                         else if(vdist(it.origin - this.wp_goal_prev1.origin, <, range * 1.5))
33                                 f = 0.1;
34                         else
35                                 f = 0.5 + random() * 0.5;
36                         navigation_routerating(this, it, ratingscale * f, 2000);
37                 });
38                 if(navigation_bestgoal)
39                         break;
40                 sradius -= range;
41         }
42 };
43
44 bool havocbot_goalrating_item_can_be_left_to_teammate(entity this, entity player, entity item)
45 {
46         if (GetResource(item, RES_HEALTH) && GetResource(player, RES_HEALTH) <= GetResource(this, RES_HEALTH)) {return true;}
47         if (GetResource(item, RES_ARMOR) && GetResource(player, RES_ARMOR) <= GetResource(this, RES_ARMOR)) {return true;}
48         if (STAT(WEAPONS, item) && !(STAT(WEAPONS, player) & STAT(WEAPONS, item))) {return true;}
49         if (GetResource(item, RES_SHELLS) && GetResource(player, RES_SHELLS) <= GetResource(this, RES_SHELLS)) {return true;}
50         if (GetResource(item, RES_BULLETS) && GetResource(player, RES_BULLETS) <= GetResource(this, RES_BULLETS)) {return true;}
51         if (GetResource(item, RES_ROCKETS) && GetResource(player, RES_ROCKETS) <= GetResource(this, RES_ROCKETS)) {return true;}
52         if (GetResource(item, RES_CELLS) && GetResource(player, RES_CELLS) <= GetResource(this, RES_CELLS)) {return true;}
53         if (GetResource(item, RES_PLASMA) && GetResource(player, RES_PLASMA) <= GetResource(this, RES_PLASMA)) {return true;}
54         if (item.itemdef.instanceOfPowerup) {return true;}
55
56         return false;
57 };
58
59 bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
60 {
61         if(!teamplay)
62                 return true;
63
64         // these variables hold squared distances in order to optimize code
65         float friend_dist2 = FLOAT_MAX;
66         float enemy_dist2 = FLOAT_MAX;
67         float dist2;
68
69         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !(IS_DEAD(it) || STAT(FROZEN, it)),
70         {
71                 if (it.team == this.team)
72                 {
73                         if (!IS_REAL_CLIENT(it))
74                                 continue;
75
76                         dist2 = vlen2(it.origin - item_org);
77                         if (dist2 > friend_dist2)
78                                 continue;
79
80                         if(havocbot_goalrating_item_can_be_left_to_teammate(this, it, item))
81                         {
82                                 friend_dist2 = dist2;
83                                 continue;
84                         }
85                 }
86                 else
87                 {
88                         // If enemy only track distances
89                         // TODO: track only if visible ?
90                         dist2 = vlen2(it.origin - item_org);
91                         if (dist2 < enemy_dist2)
92                                 enemy_dist2 = dist2;
93                 }
94         });
95
96         // Rate the item only if no one needs it, or if an enemy is closer to it
97         dist2 = vlen2(item_org - org);
98         if ((enemy_dist2 < friend_dist2 && dist2 < enemy_dist2)
99                 || (friend_dist2 > autocvar_bot_ai_friends_aware_pickup_radius ** 2)
100                 || (dist2 < friend_dist2 && dist2 < 200 ** 2))
101                 return true;
102         return false;
103 };
104
105 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
106 {
107         ratingscale = ratingscale * 0.0001;
108
109         IL_EACH(g_items, it.bot_pickup,
110         {
111                 // ignore if bot already rated this item with a higher ratingscale
112                 // NOTE: this code assumes each bot rates items in a different frame
113                 if(it.bot_ratingscale_time == time && ratingscale < it.bot_ratingscale)
114                         continue;
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                 vector 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(Item_IsLoot(it))
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                 it.bot_ratingscale_time = time;
168                 it.bot_ratingscale = ratingscale;
169                 float rating = it.bot_pickupevalfunc(this, it);
170                 if(rating > 0)
171                         navigation_routerating(this, it, rating * ratingscale, 2000);
172         });
173 }
174
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.0001;
185
186         FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), {
187                 // TODO: Merge this logic with the bot_shouldattack function
188                 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
189                         continue;
190                 if(vdist(vec2(it.velocity), >, autocvar_sv_maxspeed * 2))
191                         continue;
192
193                 // rate only visible enemies
194                 /*
195                 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
196                 if (trace_fraction < 1 || trace_ent != it)
197                         continue;
198                 */
199
200                 float t = ((GetResource(this, RES_HEALTH) + GetResource(this, RES_ARMOR)) - (GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR))) / 150;
201                 t = bound(0, 1 + t, 3);
202                 if (skill > 3)
203                 {
204                         if (time < STAT(STRENGTH_FINISHED, this) - 1) t += 0.5;
205                         if (time < STAT(STRENGTH_FINISHED, it) - 1) t -= 0.5;
206                         if (time < STAT(INVINCIBLE_FINISHED, this) - 1) t += 0.2;
207                         if (time < STAT(INVINCIBLE_FINISHED, it) - 1) t -= 0.4;
208                 }
209                 t += max(0, 8 - skill) * 0.05; // less skilled bots attack more mindlessly
210                 ratingscale *= t;
211                 if (ratingscale > 0)
212                         navigation_routerating(this, it, ratingscale * BOT_RATING_ENEMY, 2000);
213         });
214 }
215
216 // legacy bot role for standard gamemodes
217 // go to best items
218 void havocbot_role_generic(entity this)
219 {
220         if(IS_DEAD(this))
221                 return;
222
223         if (navigation_goalrating_timeout(this))
224         {
225                 navigation_goalrating_start(this);
226                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
227                 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
228                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
229                 navigation_goalrating_end(this);
230
231                 navigation_goalrating_timeout_set(this);
232         }
233 }
234
235 void havocbot_chooserole_generic(entity this)
236 {
237         this.havocbot_role = havocbot_role_generic;
238 }
239
240 void havocbot_chooserole(entity this)
241 {
242         LOG_TRACE("choosing a role...");
243         navigation_goalrating_timeout_force(this);
244         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
245                 havocbot_chooserole_generic(this);
246 }