]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/roles.qc
Merge branch 'master' into DefaultUser/kick_teamkiller
[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 "havocbot.qh"
7
8 #include "../cvars.qh"
9
10 #include "../bot.qh"
11 #include "../navigation.qh"
12
13 .float max_armorvalue;
14 .float havocbot_role_timeout;
15
16 .void(entity this) havocbot_previous_role;
17 .void(entity this) havocbot_role;
18
19 void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, float sradius)
20 {
21         // rate waypoints only if there's no alternative goal
22         if(navigation_bestgoal)
23                 return;
24
25         float f;
26         float range = 500;
27         sradius = max(range, (0.5 + random() * 0.5) * sradius);
28         while(sradius > 100)
29         {
30                 IL_EACH(g_waypoints, vdist(it.origin - org, <, sradius)
31                         && vdist(it.origin - org, >, max(100, sradius - range))
32                         && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
33                 {
34                         if(vdist(it.origin - this.wp_goal_prev0.origin, <, range * 1.5))
35                                 f = 0.1;
36                         else if(vdist(it.origin - this.wp_goal_prev1.origin, <, range * 1.5))
37                                 f = 0.1;
38                         else
39                                 f = 0.5 + random() * 0.5;
40                         navigation_routerating(this, it, ratingscale * f, 2000);
41                 });
42                 if(navigation_bestgoal)
43                         break;
44                 sradius -= range;
45         }
46 };
47
48 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
49 {
50         float rating, discard, friend_distance, enemy_distance;
51         vector o;
52         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
53
54         IL_EACH(g_items, it.bot_pickup,
55         {
56                 rating = 0;
57
58                 if(!it.solid)
59                 {
60                         if(!autocvar_bot_ai_timeitems)
61                                 continue;
62                         if(!it.scheduledrespawntime)
63                                 continue;
64                         if(it.respawntime < max(11, autocvar_bot_ai_timeitems_minrespawndelay))
65                                 continue;
66                         if(it.respawntimejitter && !it.itemdef.instanceOfPowerup)
67                                 continue;
68
69                         float t = 0;
70                         if(it.itemdef.instanceOfPowerup)
71                                 t = bound(0, skill / 10, 1) * 6;
72                         else if(skill >= 9)
73                                 t = 4;
74
75                         if(time < it.scheduledrespawntime - t)
76                                 continue;
77
78                         it.bot_pickup_respawning = true;
79                 }
80                 o = (it.absmin + it.absmax) * 0.5;
81                 if(vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
82                         continue;
83
84                 // Check if the item can be picked up safely
85                 if(Item_IsLoot(it))
86                 {
87                         if(!IS_ONGROUND(it))
88                                 continue;
89                         traceline(o, o + '0 0 -1500', true, NULL);
90
91                         if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(trace_endpos + '0 0 1')) & DPCONTENTS_LIQUIDSMASK)
92                                 continue;
93                         // this tracebox_hits_trigger_hurt call isn't needed:
94                         // dropped weapons are removed as soon as they fall on a trigger_hurt
95                         // and can't be rated while they are in the air
96                         //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
97                         //      continue;
98                 }
99                 else
100                 {
101                         // Ignore items under water
102                         // TODO: can't .waterlevel be used here?
103                         if(Mod_Q1BSP_SuperContentsFromNativeContents(pointcontents(it.origin + ((it.mins + it.maxs) * 0.5))) & DPCONTENTS_LIQUIDSMASK)
104                                 continue;
105                 }
106
107                 if(teamplay)
108                 {
109                         friend_distance = 10000; enemy_distance = 10000;
110                         discard = false;
111
112                         entity picker = it;
113                         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it),
114                         {
115                                 if ( it.team == this.team )
116                                 {
117                                         if ( !IS_REAL_CLIENT(it) || discard )
118                                                 continue;
119
120                                         if( vdist(it.origin - o, >, friend_distance) )
121                                                 continue;
122
123                                         friend_distance = vlen(it.origin - o); // distance between player and item
124                                         discard = true;
125
126                                         if (picker.health && it.health > this.health) continue;
127                                         if (picker.armorvalue && it.armorvalue > this.armorvalue) continue;
128
129                                         if (picker.weapons && (picker.weapons & ~it.weapons)) continue;
130
131                                         if (picker.ammo_shells && it.ammo_shells > this.ammo_shells) continue;
132                                         if (picker.ammo_nails && it.ammo_nails > this.ammo_nails) continue;
133                                         if (picker.ammo_rockets && it.ammo_rockets > this.ammo_rockets) continue;
134                                         if (picker.ammo_cells && it.ammo_cells > this.ammo_cells) continue;
135                                         if (picker.ammo_plasma && it.ammo_plasma > this.ammo_plasma) continue;
136
137                                         discard = false;
138                                 }
139                                 else
140                                 {
141                                         // If enemy only track distances
142                                         // TODO: track only if visible ?
143                                         if( vdist(it.origin - o, <, enemy_distance) )
144                                                 enemy_distance = vlen(it.origin - o); // distance between player and item
145                                 }
146                         });
147
148                         // Rate the item only if no one needs it, or if an enemy is closer to it
149                         if ( (enemy_distance < friend_distance && vdist(o - org, <, enemy_distance)) ||
150                                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ) || !discard )
151                                 rating = it.bot_pickupevalfunc(this, it);
152                 }
153                 else
154                         rating = it.bot_pickupevalfunc(this, it);
155
156                 if(rating > 0)
157                         navigation_routerating(this, it, rating * ratingscale, 2000);
158         });
159 }
160
161 #define BOT_RATING_ENEMY 2500
162 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
163 {
164         if (autocvar_bot_nofire)
165                 return;
166
167         // don't chase players if we're under water
168         if(this.waterlevel>WATERLEVEL_WETFEET)
169                 return;
170
171         ratingscale = ratingscale * 0.00005; // enemies are rated around 20000 already
172
173         float t;
174         FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), {
175                 // TODO: Merge this logic with the bot_shouldattack function
176                 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
177                         continue;
178
179                 // rate only visible enemies
180                 /*
181                 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
182                 if (trace_fraction < 1 || trace_ent != it)
183                         continue;
184                 */
185
186                 t = ((this.health + this.armorvalue) - (it.health + it.armorvalue)) / 150;
187                 t = bound(0, 1 + t, 3);
188                 if (skill > 3)
189                 {
190                         if (time < this.strength_finished - 1) t += 0.5;
191                         if (time < it.strength_finished - 1) t -= 0.5;
192                 }
193                 t += max(0, 8 - skill) * 0.05; // less skilled bots attack more mindlessly
194                 ratingscale *= t;
195                 if (ratingscale > 0)
196                         navigation_routerating(this, it, ratingscale * BOT_RATING_ENEMY, 2000);
197         });
198 }
199
200 // legacy bot role for standard gamemodes
201 // go to best items
202 void havocbot_role_generic(entity this)
203 {
204         if(IS_DEAD(this))
205                 return;
206
207         if (this.bot_strategytime < time)
208         {
209                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
210                 navigation_goalrating_start(this);
211                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
212                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
213                 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
214                 navigation_goalrating_end(this);
215
216                 if(IS_PLAYER(this.goalentity))
217                         this.bot_strategytime = time + min(2, autocvar_bot_ai_strategyinterval);
218         }
219 }
220
221 void havocbot_chooserole_generic(entity this)
222 {
223         this.havocbot_role = havocbot_role_generic;
224 }
225
226 void havocbot_chooserole(entity this)
227 {
228         LOG_TRACE("choosing a role...");
229         this.bot_strategytime = 0;
230         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
231                 havocbot_chooserole_generic(this);
232 }