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