Revert "Merge branch 'TimePath/bot_api' into 'master'\r"
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / havocbot / role_keyhunt.qc
1 #include "role_keyhunt.qh"
2 #include "../../_all.qh"
3
4 #include "havocbot.qh"
5
6 #include "../bot.qh"
7 #include "../navigation.qh"
8
9 #include "../../mutators/mutators_include.qh"
10
11 void() havocbot_role_kh_carrier;
12 void() havocbot_role_kh_defense;
13 void() havocbot_role_kh_offense;
14 void() havocbot_role_kh_freelancer;
15
16
17 void havocbot_goalrating_kh(float ratingscale_team, float ratingscale_dropped, float ratingscale_enemy)
18 {SELFPARAM();
19         entity head;
20         for (head = kh_worldkeylist; head; head = head.kh_worldkeynext)
21         {
22                 if(head.owner == self)
23                         continue;
24                 if(!kh_tracking_enabled)
25                 {
26                         // if it's carried by our team we know about it
27                         // otherwise we have to see it to know about it
28                         if(!head.owner || head.team != self.team)
29                         {
30                                 traceline(self.origin + self.view_ofs, head.origin, MOVE_NOMONSTERS, self);
31                                 if (trace_fraction < 1 && trace_ent != head)
32                                         continue; // skip what I can't see
33                         }
34                 }
35                 if(!head.owner)
36                         navigation_routerating(head, ratingscale_dropped * BOT_PICKUP_RATING_HIGH, 100000);
37                 else if(head.team == self.team)
38                         navigation_routerating(head.owner, ratingscale_team * BOT_PICKUP_RATING_HIGH, 100000);
39                 else
40                         navigation_routerating(head.owner, ratingscale_enemy * BOT_PICKUP_RATING_HIGH, 100000);
41         }
42
43         havocbot_goalrating_items(1, self.origin, 10000);
44 }
45
46 void havocbot_role_kh_carrier()
47 {SELFPARAM();
48         if(self.deadflag != DEAD_NO)
49                 return;
50
51         if (!(self.kh_next))
52         {
53                 LOG_TRACE("changing role to freelancer\n");
54                 self.havocbot_role = havocbot_role_kh_freelancer;
55                 self.havocbot_role_timeout = 0;
56                 return;
57         }
58
59         if (self.bot_strategytime < time)
60         {
61                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
62                 navigation_goalrating_start();
63
64                 if(kh_Key_AllOwnedByWhichTeam() == self.team)
65                         havocbot_goalrating_kh(10, 0.1, 0.1); // bring home
66                 else
67                         havocbot_goalrating_kh(4, 4, 1); // play defensively
68
69                 navigation_goalrating_end();
70         }
71 }
72
73 void havocbot_role_kh_defense()
74 {SELFPARAM();
75         if(self.deadflag != DEAD_NO)
76                 return;
77
78         if (self.kh_next)
79         {
80                 LOG_TRACE("changing role to carrier\n");
81                 self.havocbot_role = havocbot_role_kh_carrier;
82                 self.havocbot_role_timeout = 0;
83                 return;
84         }
85
86         if (!self.havocbot_role_timeout)
87                 self.havocbot_role_timeout = time + random() * 10 + 20;
88         if (time > self.havocbot_role_timeout)
89         {
90                 LOG_TRACE("changing role to freelancer\n");
91                 self.havocbot_role = havocbot_role_kh_freelancer;
92                 self.havocbot_role_timeout = 0;
93                 return;
94         }
95
96         if (self.bot_strategytime < time)
97         {
98                 float key_owner_team;
99                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
100                 navigation_goalrating_start();
101
102                 key_owner_team = kh_Key_AllOwnedByWhichTeam();
103                 if(key_owner_team == self.team)
104                         havocbot_goalrating_kh(10, 0.1, 0.1); // defend key carriers
105                 else if(key_owner_team == -1)
106                         havocbot_goalrating_kh(4, 1, 0.1); // play defensively
107                 else
108                         havocbot_goalrating_kh(0.1, 0.1, 10); // ATTACK ANYWAY
109
110                 navigation_goalrating_end();
111         }
112 }
113
114 void havocbot_role_kh_offense()
115 {SELFPARAM();
116         if(self.deadflag != DEAD_NO)
117                 return;
118
119         if (self.kh_next)
120         {
121                 LOG_TRACE("changing role to carrier\n");
122                 self.havocbot_role = havocbot_role_kh_carrier;
123                 self.havocbot_role_timeout = 0;
124                 return;
125         }
126
127         if (!self.havocbot_role_timeout)
128                 self.havocbot_role_timeout = time + random() * 10 + 20;
129         if (time > self.havocbot_role_timeout)
130         {
131                 LOG_TRACE("changing role to freelancer\n");
132                 self.havocbot_role = havocbot_role_kh_freelancer;
133                 self.havocbot_role_timeout = 0;
134                 return;
135         }
136
137         if (self.bot_strategytime < time)
138         {
139                 float key_owner_team;
140
141                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
142                 navigation_goalrating_start();
143
144                 key_owner_team = kh_Key_AllOwnedByWhichTeam();
145                 if(key_owner_team == self.team)
146                         havocbot_goalrating_kh(10, 0.1, 0.1); // defend anyway
147                 else if(key_owner_team == -1)
148                         havocbot_goalrating_kh(0.1, 1, 4); // play offensively
149                 else
150                         havocbot_goalrating_kh(0.1, 0.1, 10); // ATTACK! EMERGENCY!
151
152                 navigation_goalrating_end();
153         }
154 }
155
156 void havocbot_role_kh_freelancer()
157 {SELFPARAM();
158         if(self.deadflag != DEAD_NO)
159                 return;
160
161         if (self.kh_next)
162         {
163                 LOG_TRACE("changing role to carrier\n");
164                 self.havocbot_role = havocbot_role_kh_carrier;
165                 self.havocbot_role_timeout = 0;
166                 return;
167         }
168
169         if (!self.havocbot_role_timeout)
170                 self.havocbot_role_timeout = time + random() * 10 + 10;
171         if (time > self.havocbot_role_timeout)
172         {
173                 if (random() < 0.5)
174                 {
175                         LOG_TRACE("changing role to offense\n");
176                         self.havocbot_role = havocbot_role_kh_offense;
177                 }
178                 else
179                 {
180                         LOG_TRACE("changing role to defense\n");
181                         self.havocbot_role = havocbot_role_kh_defense;
182                 }
183                 self.havocbot_role_timeout = 0;
184                 return;
185         }
186
187         if (self.bot_strategytime < time)
188         {
189                 float key_owner_team;
190
191                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
192                 navigation_goalrating_start();
193
194                 key_owner_team = kh_Key_AllOwnedByWhichTeam();
195                 if(key_owner_team == self.team)
196                         havocbot_goalrating_kh(10, 0.1, 0.1); // defend anyway
197                 else if(key_owner_team == -1)
198                         havocbot_goalrating_kh(1, 10, 4); // prefer dropped keys
199                 else
200                         havocbot_goalrating_kh(0.1, 0.1, 10); // ATTACK ANYWAY
201
202                 navigation_goalrating_end();
203         }
204 }
205
206 void havocbot_chooserole_kh()
207 {SELFPARAM();
208         float r;
209
210         if(self.deadflag != DEAD_NO)
211                 return;
212
213         r = random() * 3;
214         if (r < 1)
215                 self.havocbot_role = havocbot_role_kh_offense;
216         else if (r < 2)
217                 self.havocbot_role = havocbot_role_kh_defense;
218         else
219                 self.havocbot_role = havocbot_role_kh_freelancer;
220 }