Merge branch 'master' into terencehill/bot_AI_improvements
[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                         it.bot_pickup_respawning = true;
43                 }
44                 o = (it.absmin + it.absmax) * 0.5;
45                 if(vdist(o - org, >, sradius) || (it == this.ignoregoal && time < this.ignoregoaltime) )
46                         continue;
47
48                 // Check if the item can be picked up safely
49                 if(it.classname == "droppedweapon")
50                 {
51                         traceline(o, o + '0 0 -1500', true, NULL);
52
53                         d = pointcontents(trace_endpos + '0 0 1');
54                         if(d == CONTENT_WATER || d == CONTENT_SLIME || d == CONTENT_LAVA)
55                                 continue;
56                         // this tracebox_hits_trigger_hurt call isn't needed:
57                         // dropped weapons are removed as soon as they fall on a trigger_hurt
58                         // and can't be rated while they are in the air
59                         //if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
60                         //      continue;
61                 }
62                 else
63                 {
64                         // Ignore items under water
65                         traceline(it.origin + it.maxs, it.origin + it.maxs, MOVE_NORMAL, it);
66                         if(trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
67                                 continue;
68                 }
69
70                 if(teamplay)
71                 {
72                         friend_distance = 10000; enemy_distance = 10000;
73                         discard = false;
74
75                         entity picker = it;
76                         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it),
77                         {
78                                 d = vlen(it.origin - o); // distance between player and item
79
80                                 if ( it.team == this.team )
81                                 {
82                                         if ( !IS_REAL_CLIENT(it) || discard )
83                                                 continue;
84
85                                         if( d > friend_distance)
86                                                 continue;
87
88                                         friend_distance = d;
89                                         discard = true;
90
91                                         if (picker.health && it.health > this.health) continue;
92                                         if (picker.armorvalue && it.armorvalue > this.armorvalue) continue;
93
94                                         if (picker.weapons && (picker.weapons & ~it.weapons)) continue;
95
96                                         if (picker.ammo_shells && it.ammo_shells > this.ammo_shells) continue;
97                                         if (picker.ammo_nails && it.ammo_nails > this.ammo_nails) continue;
98                                         if (picker.ammo_rockets && it.ammo_rockets > this.ammo_rockets) continue;
99                                         if (picker.ammo_cells && it.ammo_cells > this.ammo_cells) continue;
100                                         if (picker.ammo_plasma && it.ammo_plasma > this.ammo_plasma) continue;
101
102                                         discard = false;
103                                 }
104                                 else
105                                 {
106                                         // If enemy only track distances
107                                         // TODO: track only if visible ?
108                                         if( d < enemy_distance )
109                                                 enemy_distance = d;
110                                 }
111                         });
112
113                         // Rate the item only if no one needs it, or if an enemy is closer to it
114                         if ( (enemy_distance < friend_distance && vdist(o - org, <, enemy_distance)) ||
115                                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ) || !discard )
116                                 rating = it.bot_pickupevalfunc(this, it);
117                 }
118                 else
119                         rating = it.bot_pickupevalfunc(this, it);
120
121                 if(rating > 0)
122                         navigation_routerating(this, it, rating * ratingscale, 2000);
123         });
124 }
125
126 #define BOT_RATING_ENEMY 2500
127 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
128 {
129         if (autocvar_bot_nofire)
130                 return;
131
132         // don't chase players if we're under water
133         if(this.waterlevel>WATERLEVEL_WETFEET)
134                 return;
135
136         ratingscale = ratingscale * 0.00005; // enemies are rated around 20000 already
137
138         float t;
139         FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), LAMBDA(
140                 // TODO: Merge this logic with the bot_shouldattack function
141                 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
142                         continue;
143
144                 // rate only visible enemies
145                 /*
146                 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
147                 if (trace_fraction < 1 || trace_ent != it)
148                         continue;
149                 */
150
151                 t = ((this.health + this.armorvalue) - (it.health + it.armorvalue)) / 150;
152                 t = bound(0, 1 + t, 3);
153                 if (skill > 3)
154                 {
155                         if (time < this.strength_finished - 1) t += 0.5;
156                         if (time < it.strength_finished - 1) t -= 0.5;
157                 }
158                 t += max(0, 8 - skill) * 0.05; // less skilled bots attack more mindlessly
159                 ratingscale *= t;
160                 if (ratingscale > 0)
161                         navigation_routerating(this, it, ratingscale * BOT_RATING_ENEMY, 2000);
162         ));
163 }
164
165 // legacy bot role for standard gamemodes
166 // go to best items
167 void havocbot_role_generic(entity this)
168 {
169         if(IS_DEAD(this))
170                 return;
171
172         if (this.bot_strategytime < time)
173         {
174                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
175                 navigation_goalrating_start(this);
176                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
177                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
178                 //havocbot_goalrating_waypoints(1, this.origin, 1000);
179                 navigation_goalrating_end(this);
180         }
181 }
182
183 void havocbot_chooserole_generic(entity this)
184 {
185         this.havocbot_role = havocbot_role_generic;
186 }
187
188 void havocbot_chooserole(entity this)
189 {
190         LOG_TRACE("choosing a role...");
191         this.bot_strategytime = 0;
192         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
193                 havocbot_chooserole_generic(this);
194 }