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