]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/havocbot/roles.qc
Make most server includes order insensitive
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / havocbot / roles.qc
1 #include "../../_.qh"
2
3 #include "havocbot.qh"
4 #include "role_keyhunt.qh"
5 #include "role_onslaught.qh"
6
7 #include "../bot.qh"
8 #include "../navigation.qh"
9
10 .float max_armorvalue;
11 .float havocbot_role_timeout;
12
13 .void() havocbot_previous_role;
14 .void() havocbot_role;
15
16 void havocbot_goalrating_items(float ratingscale, vector org, float sradius)
17 {
18         entity head;
19         entity player;
20         float rating, d, discard, distance, friend_distance, enemy_distance;
21         vector o;
22         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
23         head = findchainfloat(bot_pickup, true);
24
25         while (head)
26         {
27                 o = (head.absmin + head.absmax) * 0.5;
28                 distance = vlen(o - org);
29                 friend_distance = 10000; enemy_distance = 10000;
30                 rating = 0;
31
32                 if(!head.solid || distance > sradius || (head == self.ignoregoal && time < self.ignoregoaltime) )
33                 {
34                         head = head.chain;
35                         continue;
36                 }
37
38                 // Check if the item can be picked up safely
39                 if(head.classname == "droppedweapon")
40                 {
41                         traceline(o, o + '0 0 -1500', true, world);
42
43                         d = pointcontents(trace_endpos + '0 0 1');
44                         if(d & CONTENT_WATER || d & CONTENT_SLIME || d & CONTENT_LAVA)
45                         {
46                                 head = head.chain;
47                                 continue;
48                         }
49                         if(tracebox_hits_trigger_hurt(head.origin, head.mins, head.maxs, trace_endpos))
50                         {
51                                 head = head.chain;
52                                 continue;
53                         }
54                 }
55                 else
56                 {
57                         // Ignore items under water
58                         traceline(head.origin + head.maxs, head.origin + head.maxs, MOVE_NORMAL, head);
59                         if(trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
60                         {
61                                 head = head.chain;
62                                 continue;
63                         }
64                 }
65
66                 if(teamplay)
67                 {
68                         discard = false;
69
70                         FOR_EACH_PLAYER(player)
71                         {
72
73                                 if ( self == player || player.deadflag )
74                                         continue;
75
76                                 d = vlen(player.origin - o); // distance between player and item
77
78                                 if ( player.team == self.team )
79                                 {
80                                         if ( !IS_REAL_CLIENT(player) || discard )
81                                                 continue;
82
83                                         if( d > friend_distance)
84                                                 continue;
85
86                                         friend_distance = d;
87
88                                         discard = true;
89
90                                         if( head.health && player.health > self.health )
91                                                 continue;
92
93                                         if( head.armorvalue && player.armorvalue > self.armorvalue)
94                                                 continue;
95
96                                         if( head.weapons )
97                                         if( head.weapons & ~player.weapons )
98                                                 continue;
99
100                                         if (head.ammo_shells && player.ammo_shells > self.ammo_shells)
101                                                 continue;
102
103                                         if (head.ammo_nails && player.ammo_nails > self.ammo_nails)
104                                                 continue;
105
106                                         if (head.ammo_rockets && player.ammo_rockets > self.ammo_rockets)
107                                                 continue;
108
109                                         if (head.ammo_cells && player.ammo_cells > self.ammo_cells)
110                                                 continue;
111
112                                         if (head.ammo_plasma && player.ammo_plasma > self.ammo_plasma)
113                                                 continue;
114
115                                         discard = false;
116                                 }
117                                 else
118                                 {
119                                         // If enemy only track distances
120                                         // TODO: track only if visible ?
121                                         if( d < enemy_distance )
122                                                 enemy_distance = d;
123                                 }
124                         }
125
126                         // Rate the item only if no one needs it, or if an enemy is closer to it
127                         if ( (enemy_distance < friend_distance && distance < enemy_distance) ||
128                                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ) || !discard )
129                                 rating = head.bot_pickupevalfunc(self, head);
130
131                 }
132                 else
133                         rating = head.bot_pickupevalfunc(self, head);
134
135                 if(rating > 0)
136                         navigation_routerating(head, rating * ratingscale, 2000);
137                 head = head.chain;
138         }
139 }
140
141 void havocbot_goalrating_controlpoints(float ratingscale, vector org, float sradius)
142 {
143         entity head;
144         head = findchain(classname, "dom_controlpoint");
145         while (head)
146         {
147                 if (vlen(( ( head.absmin + head.absmax ) * 0.5 ) - org) < sradius)
148                 {
149                         if(head.cnt > -1) // this is just being fought for
150                                 navigation_routerating(head, ratingscale, 5000);
151                         else if(head.goalentity.cnt == 0) // unclaimed point
152                                 navigation_routerating(head, ratingscale * 0.5, 5000);
153                         else if(head.goalentity.team != self.team) // other team's point
154                                 navigation_routerating(head, ratingscale * 0.2, 5000);
155                 }
156                 head = head.chain;
157         }
158 }
159
160 void havocbot_goalrating_enemyplayers(float ratingscale, vector org, float sradius)
161 {
162         entity head;
163         int t;
164         float distance;
165         noref bool noteam = ((self.team == 0) || !teamplay);
166
167         if (autocvar_bot_nofire)
168                 return;
169
170         // don't chase players if we're under water
171         if(self.waterlevel>WATERLEVEL_WETFEET)
172                 return;
173
174         FOR_EACH_PLAYER(head)
175         {
176                 // TODO: Merge this logic with the bot_shouldattack function
177                 if(bot_shouldattack(head))
178                 {
179                         distance = vlen(head.origin - org);
180                         if (distance < 100 || distance > sradius)
181                                 continue;
182
183                         // rate only visible enemies
184                         /*
185                         traceline(self.origin + self.view_ofs, head.origin, MOVE_NOMONSTERS, self);
186                         if (trace_fraction < 1 || trace_ent != head)
187                                 continue;
188                         */
189
190                         if((head.flags & FL_INWATER) || (head.flags & FL_PARTIALGROUND))
191                                 continue;
192
193                         // not falling
194                         if((head.flags & FL_ONGROUND) == 0)
195                         {
196                                 traceline(head.origin, head.origin + '0 0 -1500', true, world);
197                                 t = pointcontents(trace_endpos + '0 0 1');
198                                 if( t != CONTENT_SOLID )
199                                 if(t & CONTENT_WATER || t & CONTENT_SLIME || t & CONTENT_LAVA)
200                                         continue;
201                                 if(tracebox_hits_trigger_hurt(head.origin, head.mins, head.maxs, trace_endpos))
202                                         continue;
203                         }
204
205                         // TODO: rate waypoints near the targetted player at that moment, instead of the player itself
206                         //               adding a player as a goal seems to be quite dangerous, especially on space maps
207                         //               remove hack in navigation_poptouchedgoals() after performing this change
208
209                         t = (self.health + self.armorvalue ) / (head.health + head.armorvalue );
210                         navigation_routerating(head, t * ratingscale, 2000);
211                 }
212         }
213 }
214
215 // choose a role according to the situation
216 void havocbot_role_dm();
217
218 //DM:
219 //go to best items
220 void havocbot_role_dm()
221 {
222         if(self.deadflag != DEAD_NO)
223                 return;
224
225         if (self.bot_strategytime < time)
226         {
227                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
228                 navigation_goalrating_start();
229                 havocbot_goalrating_items(10000, self.origin, 10000);
230                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
231                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
232                 navigation_goalrating_end();
233         }
234 }
235
236 void havocbot_chooserole_dm()
237 {
238         self.havocbot_role = havocbot_role_dm;
239 }
240
241 void havocbot_chooserole()
242 {
243         dprint("choosing a role...\n");
244         self.bot_strategytime = 0;
245         if (MUTATOR_CALLHOOK(HavocBot_ChooseRole))
246                 return;
247         else if (g_keyhunt)
248                 havocbot_chooserole_kh();
249         else if (g_onslaught)
250                 havocbot_chooserole_ons();
251         else // assume anything else is deathmatch
252                 havocbot_chooserole_dm();
253 }