3 #include <common/stats.qh>
4 #include <common/mutators/mutator/powerups/_mod.qh>
5 #include <common/mutators/mutator/status_effects/_mod.qh>
6 #include <common/weapons/_all.qh>
7 #include <server/bot/default/bot.qh>
8 #include <server/bot/default/cvars.qh>
9 #include <server/bot/default/havocbot/havocbot.qh>
10 #include <server/bot/default/navigation.qh>
11 #include <server/items/items.qh>
12 #include <server/items/spawning.qh>
13 #include <server/mutators/_mod.qh>
14 #include <server/resources.qh>
16 void havocbot_goalrating_waypoints(entity this, float ratingscale, vector org, float sradius)
18 // rate waypoints only if there's no alternative goal
19 if(navigation_bestgoal)
24 sradius = max(range, (0.5 + random() * 0.5) * sradius);
27 IL_EACH(g_waypoints, vdist(it.origin - org, <, sradius)
28 && vdist(it.origin - org, >, max(100, sradius - range))
29 && !(it.wpflags & WAYPOINTFLAG_TELEPORT),
31 if(vdist(it.origin - this.wp_goal_prev0.origin, <, range * 1.5))
33 else if(vdist(it.origin - this.wp_goal_prev1.origin, <, range * 1.5))
36 f = 0.5 + random() * 0.5;
37 navigation_routerating(this, it, ratingscale * f, 2000);
39 if(navigation_bestgoal)
45 bool havocbot_goalrating_item_can_be_left_to_teammate(entity this, entity player, entity item)
47 if (GetResource(item, RES_HEALTH) && GetResource(player, RES_HEALTH) <= GetResource(this, RES_HEALTH)) {return true;}
48 if (GetResource(item, RES_ARMOR) && GetResource(player, RES_ARMOR) <= GetResource(this, RES_ARMOR)) {return true;}
49 if (STAT(WEAPONS, item) && !(STAT(WEAPONS, player) & STAT(WEAPONS, item))) {return true;}
50 if (GetResource(item, RES_SHELLS) && GetResource(player, RES_SHELLS) <= GetResource(this, RES_SHELLS)) {return true;}
51 if (GetResource(item, RES_BULLETS) && GetResource(player, RES_BULLETS) <= GetResource(this, RES_BULLETS)) {return true;}
52 if (GetResource(item, RES_ROCKETS) && GetResource(player, RES_ROCKETS) <= GetResource(this, RES_ROCKETS)) {return true;}
53 if (GetResource(item, RES_CELLS) && GetResource(player, RES_CELLS) <= GetResource(this, RES_CELLS)) {return true;}
54 if (GetResource(item, RES_PLASMA) && GetResource(player, RES_PLASMA) <= GetResource(this, RES_PLASMA)) {return true;}
55 if (item.itemdef.instanceOfPowerup) {return true;}
60 bool havocbot_goalrating_item_pickable_check_players(entity this, vector org, entity item, vector item_org)
65 // these variables hold squared distances in order to optimize code
66 float friend_dist2 = FLOAT_MAX;
67 float enemy_dist2 = FLOAT_MAX;
70 FOREACH_CLIENT(IS_PLAYER(it) && it != this && !(IS_DEAD(it) || STAT(FROZEN, it)),
72 if (it.team == this.team)
74 if (!IS_REAL_CLIENT(it))
77 dist2 = vlen2(it.origin - item_org);
78 if (dist2 > friend_dist2)
81 if(havocbot_goalrating_item_can_be_left_to_teammate(this, it, item))
89 // If enemy only track distances
90 // TODO: track only if visible ?
91 dist2 = vlen2(it.origin - item_org);
92 if (dist2 < enemy_dist2)
97 // Rate the item only if no one needs it, or if an enemy is closer to it
98 dist2 = vlen2(item_org - org);
99 if ((enemy_dist2 < friend_dist2 && dist2 < enemy_dist2)
100 || (friend_dist2 > autocvar_bot_ai_friends_aware_pickup_radius ** 2)
101 || (dist2 < friend_dist2 && dist2 < 200 ** 2))
106 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
108 ratingscale = ratingscale * 0.0001;
110 IL_EACH(g_items, it.bot_pickup,
112 // ignore if bot already rated this item with a higher ratingscale
113 // NOTE: this code assumes each bot rates items in a different frame
114 if(it.bot_ratingscale_time == time && ratingscale < it.bot_ratingscale)
119 if(!autocvar_bot_ai_timeitems)
121 if(!it.scheduledrespawntime)
123 if(it.respawntime < max(11, autocvar_bot_ai_timeitems_minrespawndelay))
125 if(it.respawntimejitter && !it.itemdef.instanceOfPowerup)
129 if(it.itemdef.instanceOfPowerup)
130 t = bound(0, skill / 10, 1) * 6;
134 if(time < it.scheduledrespawntime - t)
137 it.bot_pickup_respawning = true;
139 vector o = (it.absmin + it.absmax) * 0.5;
140 if(vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
143 // Check if the item can be picked up safely
148 traceline(o, o + '0 0 -1500', true, NULL);
150 if(IN_LAVA(trace_endpos + '0 0 1'))
153 // this tracebox_hits_trigger_hurt call isn't needed:
154 // dropped weapons are removed as soon as they fall on a trigger_hurt
155 // and can't be rated while they are in the air
156 //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
161 if(IN_LAVA(it.origin + (it.mins + it.maxs) * 0.5))
165 if(!havocbot_goalrating_item_pickable_check_players(this, org, it, o))
168 it.bot_ratingscale_time = time;
169 it.bot_ratingscale = ratingscale;
170 float rating = it.bot_pickupevalfunc(this, it);
172 navigation_routerating(this, it, rating * ratingscale, 2000);
176 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
178 if (autocvar_bot_nofire)
181 // don't chase players if we're under water
182 if(this.waterlevel > WATERLEVEL_WETFEET)
185 ratingscale = ratingscale * 0.0001;
187 FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), {
188 // TODO: Merge this logic with the bot_shouldattack function
189 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
191 if(vdist(vec2(it.velocity), >, autocvar_sv_maxspeed * 2))
194 // rate only visible enemies
196 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
197 if (trace_fraction < 1 || trace_ent != it)
201 float t = ((GetResource(this, RES_HEALTH) + GetResource(this, RES_ARMOR)) - (GetResource(it, RES_HEALTH) + GetResource(it, RES_ARMOR))) / 150;
202 t = bound(0, 1 + t, 3);
205 if (time < StatusEffects_gettime(STATUSEFFECT_Strength, this) - 1) t += 0.5;
206 if (time < StatusEffects_gettime(STATUSEFFECT_Strength, it) - 1) t -= 0.5;
207 if (time < StatusEffects_gettime(STATUSEFFECT_Shield, this) - 1) t += 0.2;
208 if (time < StatusEffects_gettime(STATUSEFFECT_Shield, it) - 1) t -= 0.4;
210 t += max(0, 8 - skill) * 0.05; // less skilled bots attack more mindlessly
213 navigation_routerating(this, it, ratingscale * BOT_RATING_ENEMY, 2000);
217 // legacy bot role for standard gamemodes
219 void havocbot_role_generic(entity this)
224 if (navigation_goalrating_timeout(this))
226 navigation_goalrating_start(this);
227 havocbot_goalrating_items(this, 10000, this.origin, 10000);
228 havocbot_goalrating_enemyplayers(this, 10000, this.origin, 10000);
229 havocbot_goalrating_waypoints(this, 1, this.origin, 3000);
230 navigation_goalrating_end(this);
232 navigation_goalrating_timeout_set(this);
236 void havocbot_chooserole_generic(entity this)
238 this.havocbot_role = havocbot_role_generic;
241 void havocbot_chooserole(entity this)
243 LOG_TRACE("choosing a role...");
244 navigation_goalrating_timeout_force(this);
245 if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
246 havocbot_chooserole_generic(this);