Debian Codegraph @

code/game/ai_vcmd.c

raw
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23
24 /*****************************************************************************
25 * name: ai_vcmd.c
26 *
27 * desc: Quake3 bot AI
28 *
29 * $Archive: /MissionPack/code/game/ai_vcmd.c $
30 *
31 *****************************************************************************/
32
33 #include "g_local.h"
34 #include "../botlib/botlib.h"
35 #include "../botlib/be_aas.h"
36 #include "../botlib/be_ea.h"
37 #include "../botlib/be_ai_char.h"
38 #include "../botlib/be_ai_chat.h"
39 #include "../botlib/be_ai_gen.h"
40 #include "../botlib/be_ai_goal.h"
41 #include "../botlib/be_ai_move.h"
42 #include "../botlib/be_ai_weap.h"
43 //
44 #include "ai_main.h"
45 #include "ai_dmq3.h"
46 #include "ai_chat.h"
47 #include "ai_cmd.h"
48 #include "ai_dmnet.h"
49 #include "ai_team.h"
50 #include "ai_vcmd.h"
51 //
52 #include "chars.h" //characteristics
53 #include "inv.h" //indexes into the inventory
54 #include "syn.h" //synonyms
55 #include "match.h" //string matching types and vars
56
57 // for the voice chats
58 #include "../../ui/menudef.h"
59
60
61 typedef struct voiceCommand_s
62 {
63 char *cmd;
64 void (*func)(bot_state_t *bs, int client, int mode);
65 } voiceCommand_t;
66
67 /*
68 ==================
69 BotVoiceChat_GetFlag
70 ==================
71 */
72 void BotVoiceChat_GetFlag(bot_state_t *bs, int client, int mode) {
73 //
74 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
75 if (!ctf_redflag.areanum || !ctf_blueflag.areanum)
76 return;
77 }
78 else if (gametype == GT_1FCTF) {
79 if (!ctf_neutralflag.areanum || !ctf_redflag.areanum || !ctf_blueflag.areanum)
80 return;
81 }
82 else {
83 return;
84 }
85 //
86 bs->decisionmaker = client;
87 bs->ordered = qtrue;
88 bs->order_time = FloatTime();
89 //set the time to send a message to the team mates
90 bs->teammessage_time = FloatTime() + 2 * random();
91 //set the ltg type
92 bs->ltgtype = LTG_GETFLAG;
93 //set the team goal time
94 bs->teamgoal_time = FloatTime() + CTF_GETFLAG_TIME;
95 // get an alternate route in ctf
96 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION) {
97 //get an alternative route goal towards the enemy base
98 BotGetAlternateRouteGoal(bs, BotOppositeTeam(bs));
99 }
100 //
101 BotSetTeamStatus(bs);
102 // remember last ordered task
103 BotRememberLastOrderedTask(bs);
104 #ifdef DEBUG
105 BotPrintTeamGoal(bs);
106 #endif //DEBUG
107 }
108
109 /*
110 ==================
111 BotVoiceChat_Offense
112 ==================
113 */
114 void BotVoiceChat_Offense(bot_state_t *bs, int client, int mode) {
115 if ( gametype == GT_CTF || gametype == GT_CTF_ELIMINATION || gametype == GT_1FCTF ) {
116 BotVoiceChat_GetFlag(bs, client, mode);
117 return;
118 }
119 if (gametype == GT_HARVESTER) {
120 //
121 bs->decisionmaker = client;
122 bs->ordered = qtrue;
123 bs->order_time = FloatTime();
124 //set the time to send a message to the team mates
125 bs->teammessage_time = FloatTime() + 2 * random();
126 //set the ltg type
127 bs->ltgtype = LTG_HARVEST;
128 //set the team goal time
129 bs->teamgoal_time = FloatTime() + TEAM_HARVEST_TIME;
130 bs->harvestaway_time = 0;
131 //
132 BotSetTeamStatus(bs);
133 // remember last ordered task
134 BotRememberLastOrderedTask(bs);
135 }
136 else
137 {
138 //
139 bs->decisionmaker = client;
140 bs->ordered = qtrue;
141 bs->order_time = FloatTime();
142 //set the time to send a message to the team mates
143 bs->teammessage_time = FloatTime() + 2 * random();
144 //set the ltg type
145 bs->ltgtype = LTG_ATTACKENEMYBASE;
146 //set the team goal time
147 bs->teamgoal_time = FloatTime() + TEAM_ATTACKENEMYBASE_TIME;
148 bs->attackaway_time = 0;
149 //
150 BotSetTeamStatus(bs);
151 // remember last ordered task
152 BotRememberLastOrderedTask(bs);
153 }
154 #ifdef DEBUG
155 BotPrintTeamGoal(bs);
156 #endif //DEBUG
157 }
158
159 /*
160 ==================
161 BotVoiceChat_Defend
162 ==================
163 */
164 void BotVoiceChat_Defend(bot_state_t *bs, int client, int mode) {
165 if ( gametype == GT_OBELISK || gametype == GT_HARVESTER) {
166 //
167 switch(BotTeam(bs)) {
168 case TEAM_RED: memcpy(&bs->teamgoal, &redobelisk, sizeof(bot_goal_t)); break;
169 case TEAM_BLUE: memcpy(&bs->teamgoal, &blueobelisk, sizeof(bot_goal_t)); break;
170 default: return;
171 }
172 }
173 else
174 if (gametype == GT_CTF || gametype == GT_CTF_ELIMINATION || gametype == GT_1FCTF ) {
175 //
176 switch(BotTeam(bs)) {
177 case TEAM_RED: memcpy(&bs->teamgoal, &ctf_redflag, sizeof(bot_goal_t)); break;
178 case TEAM_BLUE: memcpy(&bs->teamgoal, &ctf_blueflag, sizeof(bot_goal_t)); break;
179 default: return;
180 }
181 }
182 else {
183 return;
184 }
185 //
186 bs->decisionmaker = client;
187 bs->ordered = qtrue;
188 bs->order_time = FloatTime();
189 //set the time to send a message to the team mates
190 bs->teammessage_time = FloatTime() + 2 * random();
191 //set the ltg type
192 bs->ltgtype = LTG_DEFENDKEYAREA;
193 //get the team goal time
194 bs->teamgoal_time = FloatTime() + TEAM_DEFENDKEYAREA_TIME;
195 //away from defending
196 bs->defendaway_time = 0;
197 //
198 BotSetTeamStatus(bs);
199 // remember last ordered task
200 BotRememberLastOrderedTask(bs);
201 #ifdef DEBUG
202 BotPrintTeamGoal(bs);
203 #endif //DEBUG
204 }
205
206 /*
207 ==================
208 BotVoiceChat_DefendFlag
209 ==================
210 */
211 void BotVoiceChat_DefendFlag(bot_state_t *bs, int client, int mode) {
212 BotVoiceChat_Defend(bs, client, mode);
213 }
214
215 /*
216 ==================
217 BotVoiceChat_Patrol
218 ==================
219 */
220 void BotVoiceChat_Patrol(bot_state_t *bs, int client, int mode) {
221 //
222 bs->decisionmaker = client;
223 //
224 bs->ltgtype = 0;
225 bs->lead_time = 0;
226 bs->lastgoal_ltgtype = 0;
227 //
228 BotAI_BotInitialChat(bs, "dismissed", NULL);
229 trap_BotEnterChat(bs->cs, client, CHAT_TELL);
230 BotVoiceChatOnly(bs, -1, VOICECHAT_ONPATROL);
231 //
232 BotSetTeamStatus(bs);
233 #ifdef DEBUG
234 BotPrintTeamGoal(bs);
235 #endif //DEBUG
236 }
237
238 /*
239 ==================
240 BotVoiceChat_Camp
241 ==================
242 */
243 void BotVoiceChat_Camp(bot_state_t *bs, int client, int mode) {
244 int areanum;
245 aas_entityinfo_t entinfo;
246 char netname[MAX_NETNAME];
247
248 //
249 bs->teamgoal.entitynum = -1;
250 BotEntityInfo(client, &entinfo);
251 //if info is valid (in PVS)
252 if (entinfo.valid) {
253 areanum = BotPointAreaNum(entinfo.origin);
254 if (areanum) { // && trap_AAS_AreaReachability(areanum)) {
255 //NOTE: just assume the bot knows where the person is
256 //if (BotEntityVisible(bs->entitynum, bs->eye, bs->viewangles, 360, client)) {
257 bs->teamgoal.entitynum = client;
258 bs->teamgoal.areanum = areanum;
259 VectorCopy(entinfo.origin, bs->teamgoal.origin);
260 VectorSet(bs->teamgoal.mins, -8, -8, -8);
261 VectorSet(bs->teamgoal.maxs, 8, 8, 8);
262 //}
263 }
264 }
265 //if the other is not visible
266 if (bs->teamgoal.entitynum < 0) {
267 BotAI_BotInitialChat(bs, "whereareyou", EasyClientName(client, netname, sizeof(netname)), NULL);
268 trap_BotEnterChat(bs->cs, client, CHAT_TELL);
269 return;
270 }
271 //
272 bs->decisionmaker = client;
273 bs->ordered = qtrue;
274 bs->order_time = FloatTime();
275 //set the time to send a message to the team mates
276 bs->teammessage_time = FloatTime() + 2 * random();
277 //set the ltg type
278 bs->ltgtype = LTG_CAMPORDER;
279 //get the team goal time
280 bs->teamgoal_time = FloatTime() + TEAM_CAMP_TIME;
281 //the teammate that requested the camping
282 bs->teammate = client;
283 //not arrived yet
284 bs->arrive_time = 0;
285 //
286 BotSetTeamStatus(bs);
287 // remember last ordered task
288 BotRememberLastOrderedTask(bs);
289 #ifdef DEBUG
290 BotPrintTeamGoal(bs);
291 #endif //DEBUG
292 }
293
294 /*
295 ==================
296 BotVoiceChat_FollowMe
297 ==================
298 */
299 void BotVoiceChat_FollowMe(bot_state_t *bs, int client, int mode) {
300 int areanum;
301 aas_entityinfo_t entinfo;
302 char netname[MAX_NETNAME];
303
304 bs->teamgoal.entitynum = -1;
305 BotEntityInfo(client, &entinfo);
306 //if info is valid (in PVS)
307 if (entinfo.valid) {
308 areanum = BotPointAreaNum(entinfo.origin);
309 if (areanum) { // && trap_AAS_AreaReachability(areanum)) {
310 bs->teamgoal.entitynum = client;
311 bs->teamgoal.areanum = areanum;
312 VectorCopy(entinfo.origin, bs->teamgoal.origin);
313 VectorSet(bs->teamgoal.mins, -8, -8, -8);
314 VectorSet(bs->teamgoal.maxs, 8, 8, 8);
315 }
316 }
317 //if the other is not visible
318 if (bs->teamgoal.entitynum < 0) {
319 BotAI_BotInitialChat(bs, "whereareyou", EasyClientName(client, netname, sizeof(netname)), NULL);
320 trap_BotEnterChat(bs->cs, client, CHAT_TELL);
321 return;
322 }
323 //
324 bs->decisionmaker = client;
325 bs->ordered = qtrue;
326 bs->order_time = FloatTime();
327 //the team mate
328 bs->teammate = client;
329 //last time the team mate was assumed visible
330 bs->teammatevisible_time = FloatTime();
331 //set the time to send a message to the team mates
332 bs->teammessage_time = FloatTime() + 2 * random();
333 //get the team goal time
334 bs->teamgoal_time = FloatTime() + TEAM_ACCOMPANY_TIME;
335 //set the ltg type
336 bs->ltgtype = LTG_TEAMACCOMPANY;
337 bs->formation_dist = 3.5 * 32; //3.5 meter
338 bs->arrive_time = 0;
339 //
340 BotSetTeamStatus(bs);
341 // remember last ordered task
342 BotRememberLastOrderedTask(bs);
343 #ifdef DEBUG
344 BotPrintTeamGoal(bs);
345 #endif //DEBUG
346 }
347
348 /*
349 ==================
350 BotVoiceChat_FollowFlagCarrier
351 ==================
352 */
353 void BotVoiceChat_FollowFlagCarrier(bot_state_t *bs, int client, int mode) {
354 int carrier;
355
356 carrier = BotTeamFlagCarrier(bs);
357 if (carrier >= 0)
358 BotVoiceChat_FollowMe(bs, carrier, mode);
359 #ifdef DEBUG
360 BotPrintTeamGoal(bs);
361 #endif //DEBUG
362 }
363
364 /*
365 ==================
366 BotVoiceChat_ReturnFlag
367 ==================
368 */
369 void BotVoiceChat_ReturnFlag(bot_state_t *bs, int client, int mode) {
370 //if not in CTF mode
371 if ( gametype != GT_CTF && gametype != GT_CTF_ELIMINATION && gametype != GT_1FCTF ) {
372 return;
373 }
374 //
375 bs->decisionmaker = client;
376 bs->ordered = qtrue;
377 bs->order_time = FloatTime();
378 //set the time to send a message to the team mates
379 bs->teammessage_time = FloatTime() + 2 * random();
380 //set the ltg type
381 bs->ltgtype = LTG_RETURNFLAG;
382 //set the team goal time
383 bs->teamgoal_time = FloatTime() + CTF_RETURNFLAG_TIME;
384 bs->rushbaseaway_time = 0;
385 BotSetTeamStatus(bs);
386 #ifdef DEBUG
387 BotPrintTeamGoal(bs);
388 #endif //DEBUG
389 }
390
391 /*
392 ==================
393 BotVoiceChat_StartLeader
394 ==================
395 */
396 void BotVoiceChat_StartLeader(bot_state_t *bs, int client, int mode) {
397 ClientName(client, bs->teamleader, sizeof(bs->teamleader));
398 }
399
400 /*
401 ==================
402 BotVoiceChat_StopLeader
403 ==================
404 */
405 void BotVoiceChat_StopLeader(bot_state_t *bs, int client, int mode) {
406 char netname[MAX_MESSAGE_SIZE];
407
408 if (!Q_stricmp(bs->teamleader, ClientName(client, netname, sizeof(netname)))) {
409 bs->teamleader[0] = '\0';
410 notleader[client] = qtrue;
411 }
412 }
413
414 /*
415 ==================
416 BotVoiceChat_WhoIsLeader
417 ==================
418 */
419 void BotVoiceChat_WhoIsLeader(bot_state_t *bs, int client, int mode) {
420 char netname[MAX_MESSAGE_SIZE];
421
422 if (!TeamPlayIsOn()) return;
423
424 ClientName(bs->client, netname, sizeof(netname));
425 //if this bot IS the team leader
426 if (!Q_stricmp(netname, bs->teamleader)) {
427 BotAI_BotInitialChat(bs, "iamteamleader", NULL);
428 trap_BotEnterChat(bs->cs, 0, CHAT_TEAM);
429 BotVoiceChatOnly(bs, -1, VOICECHAT_STARTLEADER);
430 }
431 }
432
433 /*
434 ==================
435 BotVoiceChat_WantOnDefense
436 ==================
437 */
438 void BotVoiceChat_WantOnDefense(bot_state_t *bs, int client, int mode) {
439 char netname[MAX_NETNAME];
440 int preference;
441
442 preference = BotGetTeamMateTaskPreference(bs, client);
443 preference &= ~TEAMTP_ATTACKER;
444 preference |= TEAMTP_DEFENDER;
445 BotSetTeamMateTaskPreference(bs, client, preference);
446 //
447 EasyClientName(client, netname, sizeof(netname));
448 BotAI_BotInitialChat(bs, "keepinmind", netname, NULL);
449 trap_BotEnterChat(bs->cs, client, CHAT_TELL);
450 BotVoiceChatOnly(bs, client, VOICECHAT_YES);
451 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
452 }
453
454 /*
455 ==================
456 BotVoiceChat_WantOnOffense
457 ==================
458 */
459 void BotVoiceChat_WantOnOffense(bot_state_t *bs, int client, int mode) {
460 char netname[MAX_NETNAME];
461 int preference;
462
463 preference = BotGetTeamMateTaskPreference(bs, client);
464 preference &= ~TEAMTP_DEFENDER;
465 preference |= TEAMTP_ATTACKER;
466 BotSetTeamMateTaskPreference(bs, client, preference);
467 //
468 EasyClientName(client, netname, sizeof(netname));
469 BotAI_BotInitialChat(bs, "keepinmind", netname, NULL);
470 trap_BotEnterChat(bs->cs, client, CHAT_TELL);
471 BotVoiceChatOnly(bs, client, VOICECHAT_YES);
472 trap_EA_Action(bs->client, ACTION_AFFIRMATIVE);
473 }
474
475 void BotVoiceChat_Dummy(bot_state_t *bs, int client, int mode) {
476 }
477
478 voiceCommand_t voiceCommands[] = {
479 {VOICECHAT_GETFLAG, BotVoiceChat_GetFlag},
480 {VOICECHAT_OFFENSE, BotVoiceChat_Offense },
481 {VOICECHAT_DEFEND, BotVoiceChat_Defend },
482 {VOICECHAT_DEFENDFLAG, BotVoiceChat_DefendFlag },
483 {VOICECHAT_PATROL, BotVoiceChat_Patrol },
484 {VOICECHAT_CAMP, BotVoiceChat_Camp },
485 {VOICECHAT_FOLLOWME, BotVoiceChat_FollowMe },
486 {VOICECHAT_FOLLOWFLAGCARRIER, BotVoiceChat_FollowFlagCarrier },
487 {VOICECHAT_RETURNFLAG, BotVoiceChat_ReturnFlag },
488 {VOICECHAT_STARTLEADER, BotVoiceChat_StartLeader },
489 {VOICECHAT_STOPLEADER, BotVoiceChat_StopLeader },
490 {VOICECHAT_WHOISLEADER, BotVoiceChat_WhoIsLeader },
491 {VOICECHAT_WANTONDEFENSE, BotVoiceChat_WantOnDefense },
492 {VOICECHAT_WANTONOFFENSE, BotVoiceChat_WantOnOffense },
493 {NULL, BotVoiceChat_Dummy}
494 };
495
496 int BotVoiceChatCommand(bot_state_t *bs, int mode, char *voiceChat) {
497 int i, voiceOnly, clientNum, color;
498 char *ptr, buf[MAX_MESSAGE_SIZE], *cmd;
499
500 if (!TeamPlayIsOn()) {
501 return qfalse;
502 }
503
504 if ( mode == SAY_ALL ) {
505 return qfalse; // don't do anything with voice chats to everyone
506 }
507
508 Q_strncpyz(buf, voiceChat, sizeof(buf));
509 cmd = buf;
510 for (ptr = cmd; *cmd && *cmd > ' '; cmd++);
511 while (*cmd && *cmd <= ' ') *cmd++ = '\0';
512 voiceOnly = atoi(ptr);
513 for (ptr = cmd; *cmd && *cmd > ' '; cmd++);
514 while (*cmd && *cmd <= ' ') *cmd++ = '\0';
515 clientNum = atoi(ptr);
516 for (ptr = cmd; *cmd && *cmd > ' '; cmd++);
517 while (*cmd && *cmd <= ' ') *cmd++ = '\0';
518 color = atoi(ptr);
519
520 if (!BotSameTeam(bs, clientNum)) {
521 return qfalse;
522 }
523
524 for (i = 0; voiceCommands[i].cmd; i++) {
525 if (!Q_stricmp(cmd, voiceCommands[i].cmd)) {
526 voiceCommands[i].func(bs, clientNum, mode);
527 return qtrue;
528 }
529 }
530 return qfalse;
531 }