]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/havocbot/roles.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / havocbot / roles.qc
1
2 .float max_armorvalue;
3 .float havocbot_role_timeout;
4
5 .void() havocbot_previous_role;
6 .void() havocbot_role;
7
8 void havocbot_goalrating_items(float ratingscale, vector org, float sradius)
9 {
10         local entity head;
11         local entity player;
12         local float rating, d, discard, distance, friend_distance, enemy_distance;
13         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
14         head = findchainfloat(bot_pickup, TRUE);
15
16         while (head)
17         {
18                 distance = vlen(head.origin - org);
19                 friend_distance = 10000; enemy_distance = 10000;
20                 rating = 0;
21
22                 if(!head.solid || distance > sradius || (head == self.ignoregoal && time < self.ignoregoaltime) )
23                 {
24                         head = head.chain;
25                         continue;
26                 }
27
28                 // Check if the item can be picked up safely
29                 if(head.classname == "droppedweapon")
30                 {
31                         traceline(head.origin, head.origin + '0 0 -1500', TRUE, world);
32
33                         d = pointcontents(trace_endpos + '0 0 1');
34                         if(d & CONTENT_WATER || d & CONTENT_SLIME || d & CONTENT_LAVA)
35                         {
36                                 head = head.chain;
37                                 continue;
38                         }
39                         if(tracebox_hits_trigger_hurt(head.origin, head.mins, head.maxs, trace_endpos))
40                         {
41                                 head = head.chain;
42                                 continue;
43                         }
44                 }
45                 else
46                 {
47                         // Ignore items under water
48                         traceline(head.origin + head.maxs, head.origin + head.maxs, MOVE_NORMAL, head);
49                         if(trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
50                         {
51                                 head = head.chain;
52                                 continue;
53                         }
54                 }
55
56                 if(teamplay)
57                 {
58                         discard = FALSE;
59
60                         FOR_EACH_PLAYER(player)
61                         {
62
63                                 if ( self == player || player.deadflag )
64                                         continue;
65
66                                 d = vlen(player.origin - head.origin); // distance between player and item
67
68                                 if ( player.team == self.team )
69                                 {
70                                         if ( clienttype(player) != CLIENTTYPE_REAL || discard )
71                                                 continue;
72
73                                         if( d > friend_distance)
74                                                 continue;
75
76                                         friend_distance = d;
77
78                                         discard = TRUE;
79
80                                         if( head.health && player.health > self.health )
81                                                 continue;
82
83                                         if( head.armorvalue && player.armorvalue > self.armorvalue)
84                                                 continue;
85
86                                         if( head.weapons )
87                                         if( (player.weapons & head.weapons) != head.weapons)
88                                                 continue;
89
90                                         if (head.ammo_shells && player.ammo_shells > self.ammo_shells)
91                                                 continue;
92
93                                         if (head.ammo_nails && player.ammo_nails > self.ammo_nails)
94                                                 continue;
95
96                                         if (head.ammo_rockets && player.ammo_rockets > self.ammo_rockets)
97                                                 continue;
98
99                                         if (head.ammo_cells && player.ammo_cells > self.ammo_cells )
100                                                 continue;
101
102                                         discard = FALSE;
103                                 }
104                                 else
105                                 {
106                                         // If enemy only track distances
107                                         // TODO: track only if visible ?
108                                         if( d < enemy_distance )
109                                                 enemy_distance = d;
110                                 }
111                         }
112
113                         // Rate the item only if no one needs it, or if an enemy is closer to it
114                         if ( (enemy_distance < friend_distance && distance < enemy_distance) ||
115                                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ) || !discard )
116                                 rating = head.bot_pickupevalfunc(self, head);
117
118                 }
119                 else
120                         rating = head.bot_pickupevalfunc(self, head);
121
122                 if(rating > 0)
123                         navigation_routerating(head, rating * ratingscale, 2000);
124                 head = head.chain;
125         }
126 };
127
128 void havocbot_goalrating_controlpoints(float ratingscale, vector org, float sradius)
129 {
130         local entity head;
131         head = findchain(classname, "dom_controlpoint");
132         while (head)
133         {
134                 if (vlen(head.origin - org) < sradius)
135                 {
136                         if(head.cnt > -1) // this is just being fought for
137                                 navigation_routerating(head, ratingscale, 5000);
138                         else if(head.goalentity.cnt == 0) // unclaimed point
139                                 navigation_routerating(head, ratingscale * 0.5, 5000);
140                         else if(head.goalentity.team != self.team) // other team's point
141                                 navigation_routerating(head, ratingscale * 0.2, 5000);
142                 }
143                 head = head.chain;
144         }
145 };
146
147 void havocbot_goalrating_enemyplayers(float ratingscale, vector org, float sradius)
148 {
149         local entity head;
150         local float t, noteam, distance;
151         noteam = ((self.team == 0) || !teamplay); // fteqcc sucks
152
153         if (autocvar_bot_nofire)
154                 return;
155
156         // don't chase players if we're under water
157         if(self.waterlevel>WATERLEVEL_WETFEET)
158                 return;
159
160         FOR_EACH_PLAYER(head)
161         {
162                 // TODO: Merge this logic with the bot_shouldattack function
163                 if (self != head)
164                 if (head.health > 0)
165                 if ((noteam && (!bot_ignore_bots || clienttype(head) == CLIENTTYPE_REAL)) || head.team != self.team)
166                 {
167                         distance = vlen(head.origin - org);
168                         if (distance < 100 || distance > sradius)
169                                 continue;
170
171                         if (head.freezetag_frozen)
172                                 continue;
173
174                         if(g_minstagib)
175                         if(head.items & IT_STRENGTH)
176                                 continue;
177
178                         // rate only visible enemies
179                         /*
180                         traceline(self.origin + self.view_ofs, head.origin, MOVE_NOMONSTERS, self);
181                         if (trace_fraction < 1 || trace_ent != head)
182                                 continue;
183                         */
184
185                         if(head.flags & FL_INWATER || head.flags & FL_PARTIALGROUND)
186                                 continue;
187
188                         // not falling
189                         if(head.flags & FL_ONGROUND == 0)
190                         {
191                                 traceline(head.origin, head.origin + '0 0 -1500', TRUE, world);
192                                 t = pointcontents(trace_endpos + '0 0 1');
193                                 if( t != CONTENT_SOLID )
194                                 if(t & CONTENT_WATER || t & CONTENT_SLIME || t & CONTENT_LAVA)
195                                         continue;
196                                 if(tracebox_hits_trigger_hurt(head.origin, head.mins, head.maxs, trace_endpos))
197                                         continue;
198                         }
199
200                         t = (self.health + self.armorvalue ) / (head.health + head.armorvalue );
201                         navigation_routerating(head, t * ratingscale, 2000);
202                 }
203         }
204 };
205
206 // choose a role according to the situation
207 void() havocbot_role_dm;
208
209 //DOM:
210 //go to best items, or control points you don't own
211 void havocbot_role_dom()
212 {
213         if(self.deadflag != DEAD_NO)
214                 return;
215
216         if (self.bot_strategytime < time)
217         {
218                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
219                 navigation_goalrating_start();
220                 havocbot_goalrating_controlpoints(10000, self.origin, 15000);
221                 havocbot_goalrating_items(8000, self.origin, 8000);
222                 //havocbot_goalrating_enemyplayers(3000, self.origin, 2000);
223                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
224                 navigation_goalrating_end();
225         }
226 };
227
228 //DM:
229 //go to best items
230 void havocbot_role_dm()
231 {
232         if(self.deadflag != DEAD_NO)
233                 return;
234
235         if (self.bot_strategytime < time)
236         {
237                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
238                 navigation_goalrating_start();
239                 havocbot_goalrating_items(10000, self.origin, 10000);
240                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
241                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
242                 navigation_goalrating_end();
243         }
244 };
245
246 //Race:
247 //go to next checkpoint, and annoy enemies
248 .float race_checkpoint;
249 void havocbot_role_race()
250 {
251         if(self.deadflag != DEAD_NO)
252                 return;
253
254         entity e;
255         if (self.bot_strategytime < time)
256         {
257                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
258                 navigation_goalrating_start();
259                 /*
260                 havocbot_goalrating_items(100, self.origin, 10000);
261                 havocbot_goalrating_enemyplayers(500, self.origin, 20000);
262                 */
263
264                 for(e = world; (e = find(e, classname, "trigger_race_checkpoint")) != world; )
265                 {
266                         if(e.cnt == self.race_checkpoint)
267                         {
268                                 navigation_routerating(e, 1000000, 5000);
269                         }
270                         else if(self.race_checkpoint == -1)
271                         {
272                                 navigation_routerating(e, 1000000, 5000);
273                         }
274                 }
275
276                 navigation_goalrating_end();
277         }
278 };
279
280 void havocbot_chooserole_dm()
281 {
282         self.havocbot_role = havocbot_role_dm;
283 };
284
285 void havocbot_chooserole_race()
286 {
287         self.havocbot_role = havocbot_role_race;
288 };
289
290 void havocbot_chooserole_dom()
291 {
292         self.havocbot_role = havocbot_role_dom;
293 };
294
295 void havocbot_chooserole()
296 {
297         dprint("choosing a role...\n");
298         self.bot_strategytime = 0;
299         if (g_ctf)
300                 havocbot_chooserole_ctf();
301         else if (g_domination)
302                 havocbot_chooserole_dom();
303         else if (g_keyhunt)
304                 havocbot_chooserole_kh();
305         else if (g_race || g_cts)
306                 havocbot_chooserole_race();
307         else if (g_onslaught)
308                 havocbot_chooserole_ons();
309         else if (g_keepaway)
310                 havocbot_chooserole_ka();
311         else if (g_freezetag)
312                 havocbot_chooserole_ft();
313         else // assume anything else is deathmatch
314                 havocbot_chooserole_dm();
315 };