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