1 /*
2 	C-Dogs SDL
3 	A port of the legendary (and fun) action/arcade cdogs.
4 	Copyright (c) 2014-2021 Cong Xu
5 	All rights reserved.
6 
7 	Redistribution and use in source and binary forms, with or without
8 	modification, are permitted provided that the following conditions are met:
9 
10 	Redistributions of source code must retain the above copyright notice, this
11 	list of conditions and the following disclaimer.
12 	Redistributions in binary form must reproduce the above copyright notice,
13 	this list of conditions and the following disclaimer in the documentation
14 	and/or other materials provided with the distribution.
15 
16 	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 	POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "mission_convert.h"
29 
30 #include <assert.h>
31 
32 #include "algorithms.h"
33 #include "map_build.h"
34 
35 static void CopyMissionTileClasses(MissionTileClasses *mtc, Mission *m);
MissionConvertToType(Mission * m,Map * map,MapType type)36 void MissionConvertToType(Mission *m, Map *map, MapType type)
37 {
38 	MissionTileClasses mtc;
39 	RoomParams rooms;
40 	memset(&rooms, 0, sizeof rooms);
41 	bool copyRooms = false;
42 	DoorParams doors;
43 	memset(&doors, 0, sizeof doors);
44 	bool copyDoors = false;
45 	PillarParams pillars;
46 	memset(&pillars, 0, sizeof pillars);
47 	bool copyPillars = false;
48 	switch (m->Type)
49 	{
50 		case MAPTYPE_CLASSIC:
51 			memcpy(&rooms, &m->u.Classic.Rooms, sizeof rooms);
52 			copyRooms = true;
53 			memcpy(&doors, &m->u.Classic.Doors, sizeof doors);
54 			copyDoors = true;
55 			memcpy(&pillars, &m->u.Classic.Pillars, sizeof pillars);
56 			copyPillars = true;
57 			CopyMissionTileClasses(&mtc, m);
58 			break;
59 		case MAPTYPE_CAVE:
60 			memcpy(&rooms, &m->u.Cave.Rooms, sizeof rooms);
61 			copyRooms = true;
62 			CopyMissionTileClasses(&mtc, m);
63 			break;
64 		case MAPTYPE_INTERIOR:
65 			memcpy(&rooms, &m->u.Classic.Rooms, sizeof rooms);
66 			copyRooms = true;
67 			memcpy(&doors, &m->u.Classic.Doors, sizeof doors);
68 			copyDoors = true;
69 			memcpy(&pillars, &m->u.Classic.Pillars, sizeof pillars);
70 			copyPillars = true;
71 			CopyMissionTileClasses(&mtc, m);
72 			break;
73 		default:
74 			MissionTileClassesInitDefault(&mtc);
75 			break;
76 	}
77 
78 	memset(&m->u, 0, sizeof m->u);
79 	switch (type)
80 	{
81 	case MAPTYPE_CLASSIC:
82 		// Setup default parameters
83 		m->u.Classic.Walls = 10;
84 		m->u.Classic.WallLength = 5;
85 		m->u.Classic.CorridorWidth = 2;
86 		if (copyRooms)
87 		{
88 			memcpy(&m->u.Classic.Rooms, &rooms, sizeof rooms);
89 			m->u.Classic.Rooms.Min = MAX(5, m->u.Classic.Rooms.Min);
90 			m->u.Classic.Rooms.Max = MAX(m->u.Classic.Rooms.Min, m->u.Classic.Rooms.Max);
91 		}
92 		else
93 		{
94 			m->u.Classic.Rooms.Count = 10;
95 			m->u.Classic.Rooms.Min = 5;
96 			m->u.Classic.Rooms.Max = 8;
97 			m->u.Classic.Rooms.Edge = true;
98 			m->u.Classic.Rooms.Overlap = true;
99 			m->u.Classic.Rooms.Walls = 1;
100 			m->u.Classic.Rooms.WallLength = 1;
101 			m->u.Classic.Rooms.WallPad = 1;
102 		}
103 		m->u.Classic.Squares = 1;
104 		m->u.Classic.ExitEnabled = true;
105 		if (copyDoors)
106 		{
107 			memcpy(&m->u.Classic.Doors, &doors, sizeof doors);
108 			m->u.Classic.Doors.Min = MAX(1, m->u.Classic.Doors.Min);
109 			m->u.Classic.Doors.Max = MAX(m->u.Classic.Doors.Min, m->u.Classic.Doors.Max);
110 		}
111 		else
112 		{
113 			m->u.Classic.Doors.Enabled = true;
114 			m->u.Classic.Doors.Min = 1;
115 			m->u.Classic.Doors.Max = 2;
116 		}
117 		if (copyPillars)
118 		{
119 			memcpy(&m->u.Classic.Pillars, &pillars, sizeof pillars);
120 			m->u.Classic.Pillars.Min = MAX(1, m->u.Classic.Pillars.Min);
121 			m->u.Classic.Pillars.Max = MAX(m->u.Classic.Pillars.Min, m->u.Classic.Pillars.Max);
122 		}
123 		else
124 		{
125 			m->u.Classic.Pillars.Count = 1;
126 			m->u.Classic.Pillars.Min = 2;
127 			m->u.Classic.Pillars.Max = 3;
128 		}
129 		memcpy(&m->u.Classic.TileClasses, &mtc, sizeof mtc);
130 		break;
131 	case MAPTYPE_STATIC:
132 		MissionStaticFromMap(&m->u.Static, map);
133 		break;
134 	case MAPTYPE_CAVE:
135 		// Setup default parameters
136 		m->u.Cave.FillPercent = 40;
137 		m->u.Cave.Repeat = 4;
138 		m->u.Cave.R1 = 5;
139 		m->u.Cave.R2 = 2;
140 		m->u.Cave.CorridorWidth = 2;
141 		if (copyRooms)
142 		{
143 			memcpy(&m->u.Cave.Rooms, &rooms, sizeof rooms);
144 			m->u.Cave.Rooms.Min = MAX(5, m->u.Cave.Rooms.Min);
145 			m->u.Cave.Rooms.Max = MAX(m->u.Cave.Rooms.Min, m->u.Cave.Rooms.Max);
146 		}
147 		else
148 		{
149 			m->u.Cave.Rooms.Count = 10;
150 			m->u.Cave.Rooms.Min = 5;
151 			m->u.Cave.Rooms.Max = 8;
152 			m->u.Cave.Rooms.Edge = true;
153 			m->u.Cave.Rooms.Overlap = true;
154 			m->u.Cave.Rooms.Walls = 1;
155 			m->u.Cave.Rooms.WallLength = 1;
156 			m->u.Cave.Rooms.WallPad = 1;
157 		}
158 		m->u.Cave.Squares = 1;
159 		m->u.Cave.ExitEnabled = true;
160 		memcpy(&m->u.Cave.TileClasses, &mtc, sizeof mtc);
161 		break;
162 	case MAPTYPE_INTERIOR:
163 		// Setup default parameters
164 		m->u.Interior.CorridorWidth = 2;
165 		if (copyRooms)
166 		{
167 			memcpy(&m->u.Interior.Rooms, &rooms, sizeof rooms);
168 			m->u.Interior.Rooms.Min = MAX(5, m->u.Interior.Rooms.Min);
169 			m->u.Interior.Rooms.Max = MAX(2 * m->u.Interior.Rooms.Min, m->u.Interior.Rooms.Max);
170 		}
171 		else
172 		{
173 			m->u.Interior.Rooms.Min = 5;
174 			m->u.Interior.Rooms.Max = 10;
175 			m->u.Interior.Rooms.Walls = 1;
176 			m->u.Interior.Rooms.WallLength = 1;
177 			m->u.Interior.Rooms.WallPad = 1;
178 		}
179 		m->u.Interior.ExitEnabled = true;
180 		if (copyDoors)
181 		{
182 			memcpy(&m->u.Interior.Doors, &doors, sizeof doors);
183 			m->u.Interior.Doors.Min = MAX(1, m->u.Interior.Doors.Min);
184 			m->u.Interior.Doors.Max = MAX(m->u.Interior.Doors.Min, m->u.Interior.Doors.Max);
185 		}
186 		else
187 		{
188 			m->u.Interior.Doors.Enabled = true;
189 			m->u.Interior.Doors.Min = 1;
190 			m->u.Interior.Doors.Max = 2;
191 		}
192 		if (copyPillars)
193 		{
194 			memcpy(&m->u.Interior.Pillars, &pillars, sizeof pillars);
195 		}
196 		else
197 		{
198 			m->u.Interior.Pillars.Count = 1;
199 		}
200 		memcpy(&m->u.Interior.TileClasses, &mtc, sizeof mtc);
201 		break;
202 	default:
203 		CASSERT(false, "unknown map type");
204 		break;
205 	}
206 	m->Type = type;
207 }
CopyMissionTileClasses(MissionTileClasses * mtc,Mission * m)208 static void CopyMissionTileClasses(MissionTileClasses *mtc, Mission *m)
209 {
210 	MissionTileClasses *mtcOrig = MissionGetTileClasses(m);
211 	MissionTileClassesCopy(mtc, mtcOrig);
212 	MissionTileClassesTerminate(mtcOrig);
213 }
214