Debian Codegraph @

src/cdogs/ai.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2014, 2016-2017, 2020-2021, 2023 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "ai.h"
50
51 #include <assert.h>
52 #include <stdlib.h>
53
54 #include "actor_placement.h"
55 #include "actors.h"
56 #include "ai_utils.h"
57 #include "collision/collision.h"
58 #include "config.h"
59 #include "defs.h"
60 #include "events.h"
61 #include "game_events.h"
62 #include "gamedata.h"
63 #include "handle_game_events.h"
64 #include "mission.h"
65 #include "net_util.h"
66 #include "sys_specifics.h"
67 #include "utils.h"
68
69 #define AI_WAKE_SOUND_RANGE (8 * TILE_WIDTH)
70 #define AI_WAKE_SOUND_RANGE_INDIRECT (4 * TILE_WIDTH)
71
72 static int gBaddieCount = 0;
73 static bool sAreGoodGuysPresent = false;
74
75 static bool IsFacingPlayer(TActor *actor, direction_e d)
76 {
77 CA_FOREACH(const PlayerData, p, gPlayerDatas)
78 if (!IsPlayerAlive(p))
79 {
80 continue;
81 }
82 const TActor *player = ActorGetByUID(p->ActorUID);
83 if (AIIsFacing(actor, player->Pos, d))
84 {
85 return true;
86 }
87 CA_FOREACH_END()
88 return false;
89 }
90
91 #define Distance(a, b) CHEBYSHEV_DISTANCE(a->x, a->y, b->x, b->y)
92
93 static bool IsCloseToPlayer(const struct vec2 pos, const float distance)
94 {
95 const TActor *closestPlayer = AIGetClosestPlayer(pos);
96 const float distance2 = distance * distance;
97 return closestPlayer &&
98 svec2_distance_squared(pos, closestPlayer->Pos) < distance2;
99 }
100
101 static bool CanSeeActor(const TActor *a, const TActor *target)
102 {
103 // Can see if:
104 // - They are close
105 // - Or if they can see them with line of sight
106 const float distance2 = svec2_distance_squared(a->Pos, target->Pos);
107 const bool isClose = distance2 < SQUARED(16 * 2);
108 return isClose || AICanSee(a, target->Pos, a->direction);
109 }
110
111 static bool CanSeeAPlayer(const TActor *a)
112 {
113 CA_FOREACH(const PlayerData, p, gPlayerDatas)
114 if (!IsPlayerAlive(p))
115 {
116 continue;
117 }
118 const TActor *player = ActorGetByUID(p->ActorUID);
119 if (CanSeeActor(a, player))
120 {
121 return true;
122 }
123 CA_FOREACH_END()
124 return false;
125 }
126
127 static bool CanSeeActorBeingAttacked(const TActor *a)
128 {
129 CA_FOREACH(const TActor, target, gActors)
130 if (!target->isInUse)
131 {
132 continue;
133 }
134 // Use grimacing as a proxy to being attacked / being aggressive
135 if (!ActorIsGrimacing(target) && target->dead == 0)
136 {
137 continue;
138 }
139 if (CanSeeActor(a, target))
140 {
141 return true;
142 }
143 CA_FOREACH_END()
144 return false;
145 }
146
147 static bool IsPosOK(const TActor *actor, const struct vec2 pos)
148 {
149 if (IsCollisionDiamond(&gMap, pos, actor->thing.size))
150 {
151 return false;
152 }
153 const CollisionParams params = {
154 THING_IMPASSABLE, CalcCollisionTeam(true, actor),
155 IsPVP(gCampaign.Entry.Mode), false};
156 if (OverlapGetFirstItem(
157 &actor->thing, pos, actor->thing.size, actor->thing.Vel, params))
158 {
159 return false;
160 }
161 return true;
162 }
163
164 #define STEPSIZE 4
165
166 static bool IsDirectionOK(const TActor *a, const int dir)
167 {
168 switch (dir)
169 {
170 case DIRECTION_UP:
171 return IsPosOK(a, svec2_add(a->Pos, svec2(0, -STEPSIZE)));
172 case DIRECTION_UPLEFT:
173 return IsPosOK(a, svec2_add(a->Pos, svec2(-STEPSIZE, -STEPSIZE))) ||
174 IsPosOK(a, svec2_add(a->Pos, svec2(-STEPSIZE, 0))) ||
175 IsPosOK(a, svec2_add(a->Pos, svec2(0, -STEPSIZE)));
176 case DIRECTION_LEFT:
177 return IsPosOK(a, svec2_add(a->Pos, svec2(-STEPSIZE, 0)));
178 case DIRECTION_DOWNLEFT:
179 return IsPosOK(a, svec2_add(a->Pos, svec2(-STEPSIZE, STEPSIZE))) ||
180 IsPosOK(a, svec2_add(a->Pos, svec2(-STEPSIZE, 0))) ||
181 IsPosOK(a, svec2_add(a->Pos, svec2(0, STEPSIZE)));
182 case DIRECTION_DOWN:
183 return IsPosOK(a, svec2_add(a->Pos, svec2(0, STEPSIZE)));
184 case DIRECTION_DOWNRIGHT:
185 return IsPosOK(a, svec2_add(a->Pos, svec2(STEPSIZE, STEPSIZE))) ||
186 IsPosOK(a, svec2_add(a->Pos, svec2(STEPSIZE, 0))) ||
187 IsPosOK(a, svec2_add(a->Pos, svec2(0, STEPSIZE)));
188 case DIRECTION_RIGHT:
189 return IsPosOK(a, svec2_add(a->Pos, svec2(STEPSIZE, 0)));
190 case DIRECTION_UPRIGHT:
191 return IsPosOK(a, svec2_add(a->Pos, svec2(STEPSIZE, -STEPSIZE))) ||
192 IsPosOK(a, svec2_add(a->Pos, svec2(STEPSIZE, 0))) ||
193 IsPosOK(a, svec2_add(a->Pos, svec2(0, -STEPSIZE)));
194 }
195 return 0;
196 }
197
198 static int BrightWalk(TActor *actor, int roll)
199 {
200 const CharBot *bot = ActorGetCharacter(actor)->bot;
201 if (!!(actor->flags & FLAGS_VISIBLE) && roll < bot->probabilityToTrack)
202 {
203 actor->flags &= ~FLAGS_DETOURING;
204 return AIHuntClosest(actor);
205 }
206
207 if (actor->flags & FLAGS_TRYRIGHT)
208 {
209 if (IsDirectionOK(actor, (actor->direction + 7) % 8))
210 {
211 actor->direction = (actor->direction + 7) % 8;
212 actor->turns--;
213 if (actor->turns == 0)
214 {
215 actor->flags &= ~FLAGS_DETOURING;
216 }
217 }
218 else if (!IsDirectionOK(actor, actor->direction))
219 {
220 actor->direction = (actor->direction + 1) % 8;
221 actor->turns++;
222 if (actor->turns == 4)
223 {
224 actor->flags &= ~(FLAGS_DETOURING | FLAGS_TRYRIGHT);
225 actor->turns = 0;
226 }
227 }
228 }
229 else
230 {
231 if (IsDirectionOK(actor, (actor->direction + 1) % 8))
232 {
233 actor->direction = (actor->direction + 1) % 8;
234 actor->turns--;
235 if (actor->turns == 0)
236 actor->flags &= ~FLAGS_DETOURING;
237 }
238 else if (!IsDirectionOK(actor, actor->direction))
239 {
240 actor->direction = (actor->direction + 7) % 8;
241 actor->turns++;
242 if (actor->turns == 4)
243 {
244 actor->flags &= ~(FLAGS_DETOURING | FLAGS_TRYRIGHT);
245 actor->turns = 0;
246 }
247 }
248 }
249 return DirectionToCmd(actor->direction);
250 }
251
252 static int WillFire(TActor *actor, int roll)
253 {
254 const CharBot *bot = ActorGetCharacter(actor)->bot;
255 if ((actor->flags & FLAGS_VISIBLE) != 0 &&
256 roll < bot->probabilityToShoot &&
257 ActorGetCanFireBarrel(actor, ACTOR_GET_WEAPON(actor)) >= 0)
258 {
259 if ((actor->flags & FLAGS_GOOD_GUY) != 0)
260 return 1; //! FacingPlayer( actor);
261 else if (sAreGoodGuysPresent)
262 {
263 return 1;
264 }
265 else
266 {
267 return IsFacingPlayer(actor, actor->direction);
268 }
269 }
270 return 0;
271 }
272
273 void Detour(TActor *actor)
274 {
275 actor->flags |= FLAGS_DETOURING;
276 actor->turns = 1;
277 if (actor->flags & FLAGS_TRYRIGHT)
278 actor->direction = (CmdToDirection(actor->lastCmd) + 1) % 8;
279 else
280 actor->direction = (CmdToDirection(actor->lastCmd) + 7) % 8;
281 }
282
283 static bool DidPlayerShoot(void)
284 {
285 CA_FOREACH(const PlayerData, p, gPlayerDatas)
286 if (!IsPlayerAlive(p))
287 {
288 continue;
289 }
290 const TActor *player = ActorGetByUID(p->ActorUID);
291 if (Button1(player->lastCmd))
292 {
293 return true;
294 }
295 CA_FOREACH_END()
296 return false;
297 }
298
299 static bool IsAIEnabled(const TActor *a)
300 {
301 return a->isInUse && a->PlayerUID < 0 && !a->dead &&
302 !ActorGetCharacter(a)->Class->Vehicle;
303 }
304
305 static int Follow(TActor *a);
306 static int GetCmd(TActor *actor, const int delayModifier, const int rollLimit);
307 int AICommand(const int ticks)
308 {
309 int count = 0;
310 int delayModifier;
311 int rollLimit;
312
313 switch (ConfigGetEnum(&gConfig, "Game.Difficulty"))
314 {
315 case DIFFICULTY_VERYEASY:
316 delayModifier = 4;
317 rollLimit = 300;
318 break;
319 case DIFFICULTY_EASY:
320 delayModifier = 2;
321 rollLimit = 200;
322 break;
323 case DIFFICULTY_HARD:
324 delayModifier = 1;
325 rollLimit = 75;
326 break;
327 case DIFFICULTY_VERYHARD:
328 delayModifier = 1;
329 rollLimit = 50;
330 break;
331 default:
332 delayModifier = 1;
333 rollLimit = 100;
334 break;
335 }
336
337 CA_FOREACH(TActor, actor, gActors)
338 if (!IsAIEnabled(actor))
339 {
340 continue;
341 }
342 int cmd = 0;
343 if (!(actor->flags & FLAGS_PRISONER))
344 {
345 if (actor->flags & (FLAGS_VICTIM | FLAGS_GOOD_GUY))
346 {
347 sAreGoodGuysPresent = true;
348 }
349 cmd = GetCmd(actor, delayModifier, rollLimit);
350 actor->aiContext->Delay = MAX(0, actor->aiContext->Delay - ticks);
351 }
352 actor->aiContext->LastCmd = CommandActor(actor, cmd, ticks);
353 count++;
354 CA_FOREACH_END()
355 return count;
356 }
357 static int GetCmd(TActor *actor, const int delayModifier, const int rollLimit)
358 {
359 const CharBot *bot = ActorGetCharacter(actor)->bot;
360
361 int cmd = 0;
362
363 // Wake up if it can see a player or someone dying
364 if ((actor->flags & FLAGS_SLEEPING) && (actor->flags & FLAGS_VISIBLE) &&
365 actor->aiContext->Delay == 0 &&
366 (CanSeeAPlayer(actor) || CanSeeActorBeingAttacked(actor)))
367 {
368 AIWake(actor, delayModifier);
369 }
370 // Fully wake up
371 if ((actor->flags & FLAGS_WAKING) && actor->aiContext->Delay == 0)
372 {
373 actor->flags &= ~FLAGS_WAKING;
374 }
375 // Go to sleep if the player's too far away
376 if (!(actor->flags & FLAGS_SLEEPING) && actor->aiContext->Delay == 0 &&
377 !(actor->flags & FLAGS_AWAKEALWAYS))
378 {
379 if (!IsCloseToPlayer(actor->Pos, 40 * 16))
380 {
381 actor->flags |= FLAGS_SLEEPING;
382 actor->flags &= ~FLAGS_WAKING;
383 ActorSetAIState(actor, AI_STATE_IDLE);
384 }
385 }
386
387 // Don't do anything if the AI is sleeping or waking
388 if (actor->flags & (FLAGS_SLEEPING | FLAGS_WAKING))
389 {
390 return cmd;
391 }
392
393 bool bypass = false;
394 const int roll = rand() % rollLimit;
395 if (actor->flags & FLAGS_FOLLOWER)
396 {
397 cmd = Follow(actor);
398 }
399 else if (
400 !!(actor->flags & FLAGS_SNEAKY) && !!(actor->flags & FLAGS_VISIBLE) &&
401 DidPlayerShoot())
402 {
403 cmd = AIHuntClosest(actor) | CMD_BUTTON1;
404 if (actor->flags & FLAGS_RUNS_AWAY)
405 {
406 // Turn back and shoot for running away characters
407 cmd = AIReverseDirection(cmd);
408 }
409 bypass = true;
410 ActorSetAIState(actor, AI_STATE_HUNT);
411 }
412 else if (actor->flags & FLAGS_DETOURING)
413 {
414 cmd = BrightWalk(actor, roll);
415 ActorSetAIState(actor, AI_STATE_TRACK);
416 }
417 else if (actor->flags & FLAGS_RESCUED)
418 {
419 // If we haven't completed all objectives, act as follower
420 if (!CanCompleteMission(&gMission) || gMap.exits.size > 1)
421 {
422 cmd = Follow(actor);
423 }
424 else
425 {
426 // Run towards the exit if there is one
427 if (gMap.exits.size == 1)
428 {
429 const struct vec2 exitPos = MapGetExitPos(&gMap, 0);
430 cmd = AIGoto(actor, exitPos, false);
431 }
432 }
433 }
434 else if (actor->aiContext->Delay > 0)
435 {
436 cmd = actor->lastCmd & ~CMD_BUTTON1;
437 }
438 else
439 {
440 if (roll < bot->probabilityToTrack)
441 {
442 cmd = AIHuntClosest(actor);
443 ActorSetAIState(actor, AI_STATE_HUNT);
444 }
445 else if (roll < bot->probabilityToMove)
446 {
447 cmd = DirectionToCmd(rand() & 7);
448 ActorSetAIState(actor, AI_STATE_TRACK);
449 }
450 actor->aiContext->Delay = bot->actionDelay * delayModifier;
451 }
452 if (!bypass)
453 {
454 if (WillFire(actor, roll))
455 {
456 cmd |= CMD_BUTTON1;
457 if (!!(actor->flags & FLAGS_FOLLOWER) &&
458 (actor->flags & FLAGS_GOOD_GUY))
459 {
460 // Shoot in a random direction away
461 for (int j = 0; j < 10; j++)
462 {
463 direction_e d = (direction_e)(rand() % DIRECTION_COUNT);
464 if (!IsFacingPlayer(actor, d))
465 {
466 cmd = DirectionToCmd(d) | CMD_BUTTON1;
467 break;
468 }
469 }
470 }
471 if (actor->flags & FLAGS_RUNS_AWAY)
472 {
473 // Turn back and shoot for running away characters
474 cmd |= AIReverseDirection(AIHuntClosest(actor));
475 }
476 ActorSetAIState(actor, AI_STATE_HUNT);
477 }
478 else
479 {
480 if ((actor->flags & FLAGS_VISIBLE) == 0)
481 {
482 // I think this is some hack to make sure invisible enemies
483 // don't fire so much
484 Weapon *w = ACTOR_GET_WEAPON(actor);
485 for (int i = 0; i < WeaponClassNumBarrels(w->Gun); i++)
486 {
487 w->barrels[i].lock = 40;
488 }
489 }
490 if (cmd && !IsDirectionOK(actor, CmdToDirection(cmd)) &&
491 (actor->flags & FLAGS_DETOURING) == 0)
492 {
493 Detour(actor);
494 cmd = 0;
495 ActorSetAIState(actor, AI_STATE_TRACK);
496 }
497 }
498 }
499 return cmd;
500 }
501 void AIWake(TActor *a, const int delayModifier)
502 {
503 if (!a->aiContext || !(a->flags & FLAGS_SLEEPING))
504 return;
505 a->flags &= ~FLAGS_SLEEPING;
506 a->flags |= FLAGS_WAKING;
507 ActorSetAIState(a, AI_STATE_NONE);
508 const CharBot *bot = ActorGetCharacter(a)->bot;
509 if (bot == NULL)
510 return;
511 a->aiContext->Delay = bot->actionDelay * delayModifier;
512
513 // Don't play alert sound for invisible enemies
514 if (!(a->flags & FLAGS_SEETHROUGH))
515 {
516 GameEvent es = GameEventNew(GAME_EVENT_SOUND_AT);
517 CharacterClassGetSound(
518 ActorGetCharacter(a)->Class, es.u.SoundAt.Sound, "alert");
519 es.u.SoundAt.Pos = Vec2ToNet(a->thing.Pos);
520 GameEventsEnqueue(&gGameEvents, es);
521 }
522 }
523 static int Follow(TActor *a)
524 {
525 // If we are a rescue objective and we are in the same exit as another
526 // player, stop following and stay in the exit
527 const Character *ch = ActorGetCharacter(a);
528 const CharacterStore *store = &gCampaign.Setting.characters;
529 if (CharacterIsPrisoner(store, ch) && CanCompleteMission(&gMission))
530 {
531 const int exit = MapIsTileInExit(&gMap, &a->thing, -1);
532 if (exit != -1)
533 {
534 CA_FOREACH(const PlayerData, pd, gPlayerDatas)
535 if (!IsPlayerAlive(pd))
536 {
537 continue;
538 }
539 const TActor *p = ActorGetByUID(pd->ActorUID);
540 const int playerExit = MapIsTileInExit(&gMap, &p->thing, -1);
541 if (playerExit == exit)
542 {
543 a->flags &= ~FLAGS_FOLLOWER;
544 a->flags |= FLAGS_RESCUED;
545 return 0;
546 }
547 CA_FOREACH_END()
548 }
549 }
550 if (IsCloseToPlayer(a->Pos, 32))
551 {
552 ActorSetAIState(a, AI_STATE_IDLE);
553 return 0;
554 }
555 else
556 {
557 ActorSetAIState(a, AI_STATE_FOLLOW);
558 return AIGoto(a, AIGetClosestPlayerPos(a->Pos), true);
559 }
560 }
561
562 void AICommandLast(const int ticks)
563 {
564 CA_FOREACH(TActor, actor, gActors)
565 if (!IsAIEnabled(actor))
566 {
567 continue;
568 }
569 const int cmd = actor->aiContext->LastCmd;
570 actor->aiContext->Delay = MAX(0, actor->aiContext->Delay - ticks);
571 CommandActor(actor, cmd, ticks);
572 CA_FOREACH_END()
573 }
574
575 void AIWakeOnSoundAt(const struct vec2 pos)
576 {
577 CA_FOREACH(TActor, actor, gActors)
578 if (!actor->isInUse || actor->PlayerUID >= 0 || actor->dead ||
579 !(actor->flags & FLAGS_SLEEPING) || (actor->flags & FLAGS_DEAF))
580 {
581 continue;
582 }
583 const float d =
584 CHEBYSHEV_DISTANCE(pos.x, pos.y, actor->Pos.x, actor->Pos.y);
585 if (d > AI_WAKE_SOUND_RANGE)
586 {
587 continue;
588 }
589 if (d > AI_WAKE_SOUND_RANGE_INDIRECT &&
590 !AIHasClearView(actor, pos, AI_WAKE_SOUND_RANGE_INDIRECT))
591 {
592 continue;
593 }
594 AIWake(actor, 1);
595 CA_FOREACH_END()
596 }
597
598 void AIAddRandomEnemies(const int enemies, const Mission *m)
599 {
600 if (m->Enemies.size > 0 && m->EnemyDensity > 0 &&
601 enemies < MAX(1, (m->EnemyDensity *
602 ConfigGetInt(&gConfig, "Game.EnemyDensity")) /
603 100))
604 {
605 const int charId =
606 CharacterStoreGetRandomBaddieId(&gCampaign.Setting.characters);
607 const Character *c =
608 CArrayGet(&gCampaign.Setting.characters.OtherChars, charId);
609 GameEvent e = GameEventNewActorAdd(
610 PlaceAwayFromPlayers(&gMap, true, PLACEMENT_ACCESS_ANY), c, NULL);
611 e.u.ActorAdd.CharId = charId;
612 GameEventsEnqueue(&gGameEvents, e);
613 gBaddieCount++;
614 }
615 }
616
617 void InitializeBadGuys(void)
618 {
619 CA_FOREACH(Objective, o, gMission.missionData->Objectives)
620 const PlacementAccessFlags paFlags = ObjectiveGetPlacementAccessFlags(o);
621 if (o->Type == OBJECTIVE_KILL &&
622 gMission.missionData->SpecialChars.size > 0)
623 {
624 for (; o->placed < o->Count; o->placed++)
625 {
626 const int charId = CharacterStoreGetRandomSpecialId(
627 &gCampaign.Setting.characters);
628 const Character *c =
629 CArrayGet(&gCampaign.Setting.characters.OtherChars, charId);
630 GameEvent e = GameEventNewActorAdd(
631 PlaceAwayFromPlayers(&gMap, false, paFlags), c, NULL);
632 e.u.ActorAdd.CharId = charId;
633 e.u.ActorAdd.ThingFlags = ObjectiveToThing(_ca_index);
634 GameEventsEnqueue(&gGameEvents, e);
635
636 // Process the events that actually place the actors
637 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
638 }
639 }
640 else if (o->Type == OBJECTIVE_RESCUE)
641 {
642 const int charId =
643 CharacterStoreGetPrisonerId(&gCampaign.Setting.characters, 0);
644 const Character *c =
645 CArrayGet(&gCampaign.Setting.characters.OtherChars, charId);
646 for (; o->placed < o->Count; o->placed++)
647 {
648 GameEvent e = GameEventNewActorAdd(
649 MapHasLockedRooms(&gMap)
650 ? PlacePrisoner(&gMap)
651 : PlaceAwayFromPlayers(&gMap, false, paFlags),
652 c, NULL);
653 e.u.ActorAdd.CharId = charId;
654 e.u.ActorAdd.ThingFlags = ObjectiveToThing(_ca_index);
655 GameEventsEnqueue(&gGameEvents, e);
656
657 // Process the events that actually place the actors
658 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
659 }
660 }
661 CA_FOREACH_END()
662
663 gBaddieCount = gMission.index * 4;
664 sAreGoodGuysPresent = false;
665 }
666
667 void CreateEnemies(void)
668 {
669 if (gCampaign.Setting.characters.baddieIds.size == 0)
670 {
671 return;
672 }
673
674 const int density = gMission.missionData->EnemyDensity *
675 ConfigGetInt(&gConfig, "Game.EnemyDensity");
676 for (int i = 0; i < density / 100; i++)
677 {
678 const int charId =
679 CharacterStoreGetRandomBaddieId(&gCampaign.Setting.characters);
680 const Character *c =
681 CArrayGet(&gCampaign.Setting.characters.OtherChars, charId);
682 GameEvent e = GameEventNewActorAdd(
683 PlaceAwayFromPlayers(&gMap, true, PLACEMENT_ACCESS_ANY), c, NULL);
684 e.u.ActorAdd.CharId = charId;
685 GameEventsEnqueue(&gGameEvents, e);
686 gBaddieCount++;
687
688 // Process the events that actually place the actors
689 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
690 }
691 }