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