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