Remove a useless tracebox_hits_trigger_hurt: dropped weapons are removed as soon...
[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_items(entity this, float ratingscale, vector org, float sradius)
17 {
18         float rating, d, discard, friend_distance, enemy_distance;
19         vector o;
20         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
21
22         IL_EACH(g_items, it.bot_pickup,
23         {
24                 o = (it.absmin + it.absmax) * 0.5;
25                 friend_distance = 10000; enemy_distance = 10000;
26                 rating = 0;
27
28                 if(!it.solid || vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
29                         continue;
30
31                 // Check if the item can be picked up safely
32                 if(it.classname == "droppedweapon")
33                 {
34                         traceline(o, o + '0 0 -1500', true, NULL);
35
36                         d = pointcontents(trace_endpos + '0 0 1');
37                         if(d == CONTENT_WATER || d == CONTENT_SLIME || d == CONTENT_LAVA)
38                                 continue;
39                         // this tracebox_hits_trigger_hurt call isn't needed:
40                         // dropped weapons are removed as soon as they fall on a trigger_hurt
41                         // and can't be rated while they are in the air
42                         //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
43                         //      continue;
44                 }
45                 else
46                 {
47                         // Ignore items under water
48                         traceline(it.origin + it.maxs, it.origin + it.maxs, MOVE_NORMAL, it);
49                         if(trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
50                                 continue;
51                 }
52
53                 if(teamplay)
54                 {
55                         discard = false;
56
57                         entity picker = it;
58                         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it),
59                         {
60                                 d = vlen(it.origin - o); // distance between player and item
61
62                                 if ( it.team == this.team )
63                                 {
64                                         if ( !IS_REAL_CLIENT(it) || discard )
65                                                 continue;
66
67                                         if( d > friend_distance)
68                                                 continue;
69
70                                         friend_distance = d;
71
72                                         discard = true;
73
74                                         if( picker.health && it.health > this.health )
75                                                 continue;
76
77                                         if( picker.armorvalue && it.armorvalue > this.armorvalue)
78                                                 continue;
79
80                                         if( picker.weapons )
81                                         if( picker.weapons & ~it.weapons )
82                                                 continue;
83
84                                         if (picker.ammo_shells && it.ammo_shells > this.ammo_shells)
85                                                 continue;
86
87                                         if (picker.ammo_nails && it.ammo_nails > this.ammo_nails)
88                                                 continue;
89
90                                         if (picker.ammo_rockets && it.ammo_rockets > this.ammo_rockets)
91                                                 continue;
92
93                                         if (picker.ammo_cells && it.ammo_cells > this.ammo_cells)
94                                                 continue;
95
96                                         if (picker.ammo_plasma && it.ammo_plasma > this.ammo_plasma)
97                                                 continue;
98
99                                         discard = false;
100                                 }
101                                 else
102                                 {
103                                         // If enemy only track distances
104                                         // TODO: track only if visible ?
105                                         if( d < enemy_distance )
106                                                 enemy_distance = d;
107                                 }
108                         });
109
110                         // Rate the item only if no one needs it, or if an enemy is closer to it
111                         if ( (enemy_distance < friend_distance && vdist(o - org, <, enemy_distance)) ||
112                                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ) || !discard )
113                                 rating = it.bot_pickupevalfunc(this, it);
114
115                 }
116                 else
117                         rating = it.bot_pickupevalfunc(this, it);
118
119                 if(rating > 0)
120                         navigation_routerating(this, it, rating * ratingscale, 2000);
121         });
122 }
123
124 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
125 {
126         if (autocvar_bot_nofire)
127                 return;
128
129         // don't chase players if we're under water
130         if(this.waterlevel>WATERLEVEL_WETFEET)
131                 return;
132
133         int t;
134
135         FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), LAMBDA(
136                 // TODO: Merge this logic with the bot_shouldattack function
137                 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
138                         continue;
139
140                 // rate only visible enemies
141                 /*
142                 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
143                 if (trace_fraction < 1 || trace_ent != it)
144                         continue;
145                 */
146
147                 if((it.flags & FL_INWATER) || (it.flags & FL_PARTIALGROUND))
148                         continue;
149
150                 // not falling
151                 if(!IS_ONGROUND(it))
152                 {
153                         traceline(it.origin, it.origin + '0 0 -1500', true, NULL);
154                         t = pointcontents(trace_endpos + '0 0 1');
155                         if(t != CONTENT_SOLID )
156                         if(t == CONTENT_WATER || t == CONTENT_SLIME || t == CONTENT_LAVA)
157                                 continue;
158                         if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
159                                 continue;
160                 }
161
162                 // TODO: rate waypoints near the targeted player at that moment, instead of the player itself
163                 //               adding a player as a goal seems to be quite dangerous, especially on space maps
164                 //               remove hack in navigation_poptouchedgoals() after performing this change
165
166                 t = (this.health + this.armorvalue ) / (it.health + it.armorvalue );
167                 navigation_routerating(this, it, t * ratingscale, 2000);
168         ));
169 }
170
171 // legacy bot role for standard gamemodes
172 // go to best items
173 void havocbot_role_generic(entity this)
174 {
175         if(IS_DEAD(this))
176                 return;
177
178         if (this.bot_strategytime < time)
179         {
180                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
181                 navigation_goalrating_start(this);
182                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
183                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
184                 //havocbot_goalrating_waypoints(1, this.origin, 1000);
185                 navigation_goalrating_end(this);
186         }
187 }
188
189 void havocbot_chooserole_generic(entity this)
190 {
191         this.havocbot_role = havocbot_role_generic;
192 }
193
194 void havocbot_chooserole(entity this)
195 {
196         LOG_TRACE("choosing a role...");
197         this.bot_strategytime = 0;
198         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
199                 havocbot_chooserole_generic(this);
200 }