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