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