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