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