]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/havocbot/roles.qc
Merge branch 'master' into terencehill/dynamic_hud
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / havocbot / roles.qc
1 #include "roles.qh"
2
3 #include "havocbot.qh"
4
5 #include "../bot.qh"
6 #include "../navigation.qh"
7
8 .float max_armorvalue;
9 .float havocbot_role_timeout;
10
11 .void(entity this) havocbot_previous_role;
12 .void(entity this) havocbot_role;
13
14 void havocbot_goalrating_items(entity this, float ratingscale, vector org, float sradius)
15 {
16         entity head;
17         float rating, d, discard, distance, friend_distance, enemy_distance;
18         vector o;
19         ratingscale = ratingscale * 0.0001; // items are rated around 10000 already
20         head = findchainfloat(bot_pickup, true);
21
22         while (head)
23         {
24                 o = (head.absmin + head.absmax) * 0.5;
25                 distance = vlen(o - org);
26                 friend_distance = 10000; enemy_distance = 10000;
27                 rating = 0;
28
29                 if(!head.solid || distance > sradius || (head == this.ignoregoal && time < this.ignoregoaltime) )
30                 {
31                         head = head.chain;
32                         continue;
33                 }
34
35                 // Check if the item can be picked up safely
36                 if(head.classname == "droppedweapon")
37                 {
38                         traceline(o, o + '0 0 -1500', true, world);
39
40                         d = pointcontents(trace_endpos + '0 0 1');
41                         if(d & CONTENT_WATER || d & CONTENT_SLIME || d & CONTENT_LAVA)
42                         {
43                                 head = head.chain;
44                                 continue;
45                         }
46                         if(tracebox_hits_trigger_hurt(head.origin, head.mins, head.maxs, trace_endpos))
47                         {
48                                 head = head.chain;
49                                 continue;
50                         }
51                 }
52                 else
53                 {
54                         // Ignore items under water
55                         traceline(head.origin + head.maxs, head.origin + head.maxs, MOVE_NORMAL, head);
56                         if(trace_dpstartcontents & DPCONTENTS_LIQUIDSMASK)
57                         {
58                                 head = head.chain;
59                                 continue;
60                         }
61                 }
62
63                 if(teamplay)
64                 {
65                         discard = false;
66
67                         FOREACH_CLIENT(IS_PLAYER(it) && it != this && !IS_DEAD(it), LAMBDA(
68                                 d = vlen(it.origin - o); // distance between player and item
69
70                                 if ( it.team == this.team )
71                                 {
72                                         if ( !IS_REAL_CLIENT(it) || discard )
73                                                 continue;
74
75                                         if( d > friend_distance)
76                                                 continue;
77
78                                         friend_distance = d;
79
80                                         discard = true;
81
82                                         if( head.health && it.health > this.health )
83                                                 continue;
84
85                                         if( head.armorvalue && it.armorvalue > this.armorvalue)
86                                                 continue;
87
88                                         if( head.weapons )
89                                         if( head.weapons & ~it.weapons )
90                                                 continue;
91
92                                         if (head.ammo_shells && it.ammo_shells > this.ammo_shells)
93                                                 continue;
94
95                                         if (head.ammo_nails && it.ammo_nails > this.ammo_nails)
96                                                 continue;
97
98                                         if (head.ammo_rockets && it.ammo_rockets > this.ammo_rockets)
99                                                 continue;
100
101                                         if (head.ammo_cells && it.ammo_cells > this.ammo_cells)
102                                                 continue;
103
104                                         if (head.ammo_plasma && it.ammo_plasma > this.ammo_plasma)
105                                                 continue;
106
107                                         discard = false;
108                                 }
109                                 else
110                                 {
111                                         // If enemy only track distances
112                                         // TODO: track only if visible ?
113                                         if( d < enemy_distance )
114                                                 enemy_distance = d;
115                                 }
116                         ));
117
118                         // Rate the item only if no one needs it, or if an enemy is closer to it
119                         if ( (enemy_distance < friend_distance && distance < enemy_distance) ||
120                                 (friend_distance > autocvar_bot_ai_friends_aware_pickup_radius ) || !discard )
121                                 rating = head.bot_pickupevalfunc(this, head);
122
123                 }
124                 else
125                         rating = head.bot_pickupevalfunc(this, head);
126
127                 if(rating > 0)
128                         navigation_routerating(this, head, rating * ratingscale, 2000);
129                 head = head.chain;
130         }
131 }
132
133 void havocbot_goalrating_controlpoints(entity this, float ratingscale, vector org, float sradius)
134 {
135         entity head;
136         head = findchain(classname, "dom_controlpoint");
137         while (head)
138         {
139                 if(vdist((((head.absmin + head.absmax) * 0.5) - org), <, sradius))
140                 {
141                         if(head.cnt > -1) // this is just being fought for
142                                 navigation_routerating(this, head, ratingscale, 5000);
143                         else if(head.goalentity.cnt == 0) // unclaimed point
144                                 navigation_routerating(this, head, ratingscale * 0.5, 5000);
145                         else if(head.goalentity.team != this.team) // other team's point
146                                 navigation_routerating(this, head, ratingscale * 0.2, 5000);
147                 }
148                 head = head.chain;
149         }
150 }
151
152 void havocbot_goalrating_enemyplayers(entity this, float ratingscale, vector org, float sradius)
153 {
154         if (autocvar_bot_nofire)
155                 return;
156
157         // don't chase players if we're under water
158         if(this.waterlevel>WATERLEVEL_WETFEET)
159                 return;
160
161         int t;
162
163         FOREACH_CLIENT(IS_PLAYER(it) && bot_shouldattack(this, it), LAMBDA(
164                 // TODO: Merge this logic with the bot_shouldattack function
165                 if(vdist(it.origin - org, <, 100) || vdist(it.origin - org, >, sradius))
166                         continue;
167
168                 // rate only visible enemies
169                 /*
170                 traceline(this.origin + this.view_ofs, it.origin, MOVE_NOMONSTERS, this);
171                 if (trace_fraction < 1 || trace_ent != it)
172                         continue;
173                 */
174
175                 if((it.flags & FL_INWATER) || (it.flags & FL_PARTIALGROUND))
176                         continue;
177
178                 // not falling
179                 if((IS_ONGROUND(it)) == 0)
180                 {
181                         traceline(it.origin, it.origin + '0 0 -1500', true, world);
182                         t = pointcontents(trace_endpos + '0 0 1');
183                         if(t != CONTENT_SOLID )
184                         if(t & CONTENT_WATER || t & CONTENT_SLIME || t & CONTENT_LAVA)
185                                 continue;
186                         if(tracebox_hits_trigger_hurt(it.origin, it.mins, it.maxs, trace_endpos))
187                                 continue;
188                 }
189
190                 // TODO: rate waypoints near the targetted player at that moment, instead of the player itthis
191                 //               adding a player as a goal seems to be quite dangerous, especially on space maps
192                 //               remove hack in navigation_poptouchedgoals() after performing this change
193
194                 t = (this.health + this.armorvalue ) / (it.health + it.armorvalue );
195                 navigation_routerating(this, it, t * ratingscale, 2000);
196         ));
197 }
198
199 // legacy bot role for standard gamemodes
200 // go to best items
201 void havocbot_role_generic(entity this)
202 {
203         if(IS_DEAD(this))
204                 return;
205
206         if (this.bot_strategytime < time)
207         {
208                 this.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
209                 navigation_goalrating_start(this);
210                 havocbot_goalrating_items(this, 10000, this.origin, 10000);
211                 havocbot_goalrating_enemyplayers(this, 20000, this.origin, 10000);
212                 //havocbot_goalrating_waypoints(1, this.origin, 1000);
213                 navigation_goalrating_end(this);
214         }
215 }
216
217 void havocbot_chooserole_generic(entity this)
218 {
219         this.havocbot_role = havocbot_role_generic;
220 }
221
222 void havocbot_chooserole(entity this)
223 {
224         LOG_TRACE("choosing a role...\n");
225         this.bot_strategytime = 0;
226         if(!MUTATOR_CALLHOOK(HavocBot_ChooseRole, this))
227                 havocbot_chooserole_generic(this);
228 }