1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2014-2017, 2020-2021 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "actor_placement.h"
30
31 #include "actors.h"
32 #include "ai_utils.h"
33 #include "game_events.h"
34 #include "gamedata.h"
35 #include "handle_game_events.h"
36
PlacePlayerSimple(const Map * map)37 static struct vec2 PlacePlayerSimple(const Map *map)
38 {
39 const int halfMap =
40 MAX(map->Size.x * TILE_WIDTH, map->Size.y * TILE_HEIGHT) / 2;
41 int attemptsAwayFromExits = 0;
42
43 struct vec2 pos = svec2_zero();
44 bool ok = false;
45 for (int j = 0; j < 10000 && !ok; j++)
46 {
47 pos = MapGetRandomPos(map);
48 ok = MapIsPosOKForPlayer(map, pos, false);
49 if (!ok)
50 continue;
51 if (attemptsAwayFromExits < 100)
52 {
53 attemptsAwayFromExits++;
54 // Try to place at least half the map away from any exits
55 for (int i = 0; i < (int)map->exits.size; i++)
56 {
57 const struct vec2 exitPos = MapGetExitPos(map, i);
58 if (fabsf(pos.x - exitPos.x) > halfMap &&
59 fabsf(pos.y - exitPos.y) > halfMap)
60 {
61 ok = false;
62 break;
63 }
64 }
65 }
66 }
67 return pos;
68 }
69
PlaceActorNear(const Map * map,const struct vec2 nearPos,const bool allowAllTiles)70 static struct vec2 PlaceActorNear(
71 const Map *map, const struct vec2 nearPos, const bool allowAllTiles)
72 {
73 // Try a concentric rhombus pattern, clockwise from right
74 // That is, start by checking right, below, left, above,
75 // then continue with radius 2 right, below-right, below, below-left...
76 // (start from S:)
77 // 4
78 // 9 3 S 1 5
79 // 8 2 6
80 // 7
81 #define TRY_LOCATION() \
82 pos = svec2_add(nearPos, svec2(dx, dy)); \
83 if (MapIsPosOKForPlayer(map, pos, allowAllTiles)) \
84 { \
85 return pos; \
86 }
87 float dx = 0;
88 float dy = 0;
89 struct vec2 pos;
90 TRY_LOCATION();
91 const float inc = 1;
92 for (float radius = 12;; radius += 12)
93 {
94 // Going from right to below
95 for (dx = radius, dy = 0; dy < radius; dx -= inc, dy += inc)
96 {
97 TRY_LOCATION();
98 }
99 // below to left
100 for (dx = 0, dy = radius; dy > 0; dx -= inc, dy -= inc)
101 {
102 TRY_LOCATION();
103 }
104 // left to above
105 for (dx = -radius, dy = 0; dx < 0; dx += inc, dy -= inc)
106 {
107 TRY_LOCATION();
108 }
109 // above to right
110 for (dx = 0, dy = -radius; dy < 0; dx += inc, dy += inc)
111 {
112 TRY_LOCATION();
113 }
114 }
115 }
116
117 static bool TryPlaceOneAwayFromPlayers(
118 const Map *map, const struct vec2 pos, void *data);
PlaceAwayFromPlayers(const Map * map,const bool giveUp,const PlacementAccessFlags paFlags)119 struct vec2 PlaceAwayFromPlayers(
120 const Map *map, const bool giveUp, const PlacementAccessFlags paFlags)
121 {
122 struct vec2 out;
123 if (MapPlaceRandomPos(map, paFlags, TryPlaceOneAwayFromPlayers, &out))
124 {
125 return out;
126 }
127
128 // Keep trying, but this time try spawning anywhere,
129 // even close to player
130 for (int i = 0; i < 10000 || !giveUp; i++)
131 {
132 const struct vec2 pos = MapGetRandomPos(map);
133 if (MapIsTileAreaClear(map, pos, svec2i(ACTOR_W, ACTOR_H)))
134 {
135 return pos;
136 }
137 }
138
139 // Uh oh
140 // TODO: scan map for a safe position, to use as default
141 return svec2(TILE_WIDTH * 3 / 2, TILE_HEIGHT * 3 / 2);
142 }
TryPlaceOneAwayFromPlayers(const Map * map,const struct vec2 pos,void * data)143 static bool TryPlaceOneAwayFromPlayers(
144 const Map *map, const struct vec2 pos, void *data)
145 {
146 struct vec2 *out = data;
147 // Try spawning out of players' sights
148 *out = pos;
149
150 const TActor *closestPlayer = AIGetClosestPlayer(pos);
151 if ((closestPlayer == NULL || CHEBYSHEV_DISTANCE(
152 pos.x, pos.y, closestPlayer->Pos.x,
153 closestPlayer->Pos.y) >= 150) &&
154 MapIsTileAreaClear(map, pos, svec2i(ACTOR_W, ACTOR_H)))
155 {
156 *out = pos;
157 return true;
158 }
159 return false;
160 }
161
PlacePrisoner(const Map * map)162 struct vec2 PlacePrisoner(const Map *map)
163 {
164 struct vec2 pos;
165 do
166 {
167 do
168 {
169 pos = MapGetRandomPos(map);
170 } while (!MapPosIsInLockedRoom(map, pos));
171 } while (!MapIsTileAreaClear(map, pos, svec2i(ACTOR_W, ACTOR_H)));
172 return pos;
173 }
174
PlacePlayer(const Map * map,const PlayerData * p,const struct vec2 firstPos,const bool pumpEvents)175 struct vec2 PlacePlayer(
176 const Map *map, const PlayerData *p, const struct vec2 firstPos,
177 const bool pumpEvents)
178 {
179 struct vec2 pos;
180 if (IsPVP(gCampaign.Entry.Mode))
181 {
182 // In a PVP mode, always place players apart
183 pos = PlaceAwayFromPlayers(map, false, PLACEMENT_ACCESS_ANY);
184 }
185 else if (
186 ConfigGetEnum(&gConfig, "Interface.Splitscreen") ==
187 SPLITSCREEN_NEVER &&
188 !svec2_is_zero(firstPos))
189 {
190 // If never split screen, try to place players near the first player
191 pos = PlaceActorNear(map, firstPos, true);
192 }
193 else if (!svec2i_is_zero(map->start))
194 {
195 // place players near the start point
196 const struct vec2 startPoint = Vec2CenterOfTile(map->start);
197 pos = PlaceActorNear(map, startPoint, true);
198 }
199 else
200 {
201 pos = PlacePlayerSimple(map);
202 }
203 GameEvent e = GameEventNewActorAdd(pos, &p->Char, false);
204 e.u.ActorAdd.Direction = DIRECTION_DOWN;
205 e.u.ActorAdd.PlayerUID = p->UID;
206 Ammo2Net(&e.u.ActorAdd.Ammo_count, e.u.ActorAdd.Ammo, &p->ammo);
207 GameEventsEnqueue(&gGameEvents, e);
208
209 if (pumpEvents)
210 {
211 // Process the events that actually place the players
212 HandleGameEvents(&gGameEvents, NULL, NULL, NULL, NULL);
213 }
214
215 return NetToVec2(e.u.ActorAdd.Pos);
216 }
217