0a5a76d858ef141cac627d51354be72f7605cef2
[mercenary-reloaded.git] / src / mercenary / mercenary3.c
1 /* game specials
2  *
3  * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
4  * All Rights Reserved
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include "../libsdl/print.h"
25 #include "../libcpu/m68k.h"
26 #include "../libcpu/m68kcpu.h"
27 #include "../libcpu/execute.h"
28 #include "mercenary.h"
29
30 #define DS_0 0x530c
31 #define DS_41 0x505E2
32 #define DS_64 0x53d34
33 #define DS_84 0x5D856
34
35 #define INITIAL_STACK   0x7fffa
36 #define RESET_VECTOR    0x5a16c
37 // #define RESET_VECTOR 0x5a1a4 /* strange reset vector that causes the player to fly around */
38
39 /* interrupt CPU execution at special break points and tell emulation what to do */
40 const struct cpu_stop mercenary_stop_at[] = {
41         { 0x5a826,      STOP_AT_WAIT_VBL },                     /* done with rendering, waiting for VBL */
42         { 0x55d8c,      STOP_AT_WAIT_INPUT },                   /* after pressing 'HELP' key before showing menu line on benson */
43         { 0x56398,      STOP_AT_WAIT_INPUT },                   /* waiting for menu command */
44         { 0x55d94,      STOP_AT_WAIT_INPUT },                   /* after pressing 'HELP' key while showing menu line on benson */
45         { 0x563a6,      STOP_AT_WAIT_INPUT },                   /* after pressing 'RETURN' while game waits for other key to resume */
46         { 0x52946,      STOP_AT_WAIT_VBL },                     /* after dying, waiting for VBL to fade out palette */
47         { 0x5A456,      STOP_AT_CLEAR_SCREEN1 },                /* clear the screen (here we know the color 8, this is wy we cannot do it earlier) */
48         { 0x5A48E,      STOP_AT_CLEAR_SCREEN1 },
49         { 0x5A4BA,      STOP_AT_CLEAR_SCREEN1 },
50         { 0x5A672,      STOP_AT_CLEAR_SCREEN1 },
51         { 0x5A6B0,      STOP_AT_CLEAR_SCREEN2 },                /* special case where we use color index 15 when we are flying (no raster split for ground color) */
52         { 0x5A770,      STOP_AT_CLEAR_SCREEN3 },                /* special case where we are in universe and do not have any ground */
53         { 0x56E00,      STOP_AT_DRAW_GROUND },                  /* the ground is rendered. the color index is captured at CLEAR_SCREEN */
54         { 0x5A4AA,      STOP_AT_DRAW_GROUND },                  /* at this point there is ground rendered, because game uses raster split for ground color, but we do render opengl */
55         { 0x53FE2,      STOP_AT_INFO_OBJECT_MOVING },           /* get ID and position of next object */
56         { 0x53E42,      STOP_AT_INFO_OBJECT_FIX },              /* next object is FIX (taxi/bus/intercity) */
57         { 0x53E8E,      STOP_AT_INFO_OBJECT_FIX },
58         { 0x54330,      STOP_AT_TAG_IS_OBJECT_1 },              /* indicates that the next tag is an object */
59         { 0x54334,      STOP_AT_TAG_IS_OBJECT_0 },              /* indicates that the tag of object was rendered */
60         { 0x542B2,      STOP_AT_COORD_OBJECT },                 /* object coordinates are ready */
61         { 0x5431a,      STOP_AT_POLY_OBJECT_M3 },               /* object polygon is rendered */
62         { 0x54342,      STOP_AT_LINE_OBJECT },                  /* object line is rendered */
63         { 0x54042,      STOP_AT_COORD_BEACON },                 /* beacon's point coordinates are ready */
64         { 0x54088,      STOP_AT_POINT_BEACON },                 /* becon point is rendered */
65         { 0x54848,      STOP_AT_COORD_BUILDING_EXTERIOR },      /* building (house) coordinates are ready */
66         { 0x548a4,      STOP_AT_POLY_BUILDING_EXTERIOR },       /* building polygons are rendered */
67         { 0x5489C,      STOP_AT_LINE_BUILDING_EXTERIOR },       /* lines of building, like radio tower on icarus */
68         { 0x5B810,      STOP_AT_COORD_BUILDING_INTERIOR },      /* building coordinates for interrior */
69         { 0x5C01E,      STOP_AT_POLY_BUILDING_INTERIOR1 },      /* floor of building will be rendered */
70         { 0x5BFc4,      STOP_AT_POLY_BUILDING_INTERIOR2 },      /* ceiling of building will be rendered */
71         { 0x5BF5a,      STOP_AT_POLY_BUILDING_INTERIOR3 },      /* ceiling of window/door will be rendered */
72         { 0x5BEE6,      STOP_AT_POLY_BUILDING_INTERIOR4 },      /* floor of window will be rendered */
73         { 0x5C0E4,      STOP_AT_POLY_BUILDING_INTERIOR1to4 },   /* ceiling/floor of building is rendered */
74         { 0x5BDFA,      STOP_AT_POLY_BUILDING_INTERIOR5 },      /* part above window/door will be rendered */
75         { 0x5BD90,      STOP_AT_POLY_BUILDING_INTERIOR6 },      /* part below window will be rendered */
76         { 0x5BEC4,      STOP_AT_POLY_BUILDING_INTERIOR5to6 },   /* part below/above window/door of building is rendered */
77         { 0x5C138,      STOP_AT_WALL_BUILDING },                /* a wall (between floor and ceiling/window/door) is rendered) */
78         { 0x4FB08,      STOP_AT_COORD_COMET },                  /* comet's coordinates are ready */
79         { 0x4FB3C,      STOP_AT_MATRIX_COMET },                 /* what rotation matrix to use */
80         { 0x4FB5E,      STOP_AT_POLY_COMET },                   /* comet's horizontal polygon (without culling no need to render 0x4FB62) */
81         { 0x4FB66,      STOP_AT_POLY_COMET },                   /* comet's vertival polygon (without culling no need to render 0x4FB6A) */
82         { 0x5573E,      STOP_AT_COORD_LINE_ROADS },             /* road's line coordinates are ready */
83         { 0x55780,      STOP_AT_LINE_ROADS },                   /* road's and ground surface's polygon */
84         { 0x557A6,      STOP_AT_LINE_ROADS },
85         { 0x557BC,      STOP_AT_LINE_ROADS },
86         { 0x57370,      STOP_AT_LINE_ROADS_CENTER },            /* center line of roads */
87         { 0x5731C,      STOP_AT_COORD_POLY_ROADS },             /* road's and ground surface's coordinates are ready */
88         { 0x57376,      STOP_AT_POLY_ROADS },                   /* road's and ground surface's polygon */
89         { 0x54BF4,      STOP_AT_COORD_TAGS },                   /* coordinates for tags, like key's marking are ready */
90         { 0x54BAC,      STOP_AT_COORD_TAGS2 },                  /* coordinates for large tags are ready, like bill's face */
91         { 0x54C40,      STOP_AT_LINE_TAGS1 },                   /* tag's line is rendered (new color D0) */
92         { 0x54C3A,      STOP_AT_LINE_TAGS2 },                   /* tag's line is rendered (use last color) */
93         { 0x54C54,      STOP_AT_POLY_TAGS1 },                   /* tag's polygon is rendered (new color D0) */
94         { 0x54C30,      STOP_AT_POLY_TAGS2 },                   /* tag's polygon is rendered (use last color) */
95         { 0x53A1E,      STOP_AT_COORD_PLANET },                 /* planet's coordinates are ready (viewed from ground) */
96         { 0x53A44,      STOP_AT_MATRIX_PLANET },                /* what rotation matrix to use */
97         { 0x53712,      STOP_AT_COORD_PLANET },                 /* planet's coordinates are ready (viewed from universe) */
98         { 0x537A4,      STOP_AT_MATRIX_PLANET },                /* what rotation matrix to use */
99         { 0x53A9A,      STOP_AT_DRAW_PLANET },                  /* planet's sphere is rendered, D0 is color (viewed from ground) */
100         { 0x53960,      STOP_AT_DRAW_PLANET },                  /* planet's sphere is rendered, D0 is color (viewed from universe) */
101         { 0x4FB8C,      STOP_AT_DRAW_COMET },                   /* comet's sphere is rendered */
102         { 0x50524,      STOP_AT_DRAW_STARS_SPACE },             /* stars are rendered (viewed from universe) */
103         { 0x5046A,      STOP_AT_DRAW_STARS_GROUND },            /* stars are rendered (viewed from ground) */
104         { 0x50342,      STOP_AT_DRAW_STARS_FLYING },            /* stars are rendered (viewed from planet when flying) */
105         { 0x501CA,      STOP_AT_DRAW_STARS_FLYING2 },           /* same as above, but stars when upside down (above zenit) */
106         { 0x57290,      STOP_AT_COORD_ISLANDS },                /* island's coordinates are ready */
107         { 0x572EE,      STOP_AT_POLY_ISLANDS },                 /* island's polygon is rendered */
108         { 0x572E8,      STOP_AT_LINE_ISLANDS },
109         { 0x5214E,      STOP_AT_DRAW_SIGHTS },                  /* when sights are rendered */
110         { 0x4F748,      STOP_AT_POLY_UKN2 },
111         { 0x54362,      STOP_AT_EXPLOSION },                    /* explosion debris */
112         { 0x4CA18,      STOP_AT_EXPLOSION },
113         { 0x53A6C,      STOP_AT_PATCH_RENDER },                 /* patch away planet check (behind observer) */
114         { 0x537B0,      STOP_AT_PATCH_RENDER },                 /* patch away planet check (behind observer) */
115         { 0x45806,      STOP_AT_PATCH_RENDER },                 /* patch away planet rendering (would crash without check above) */
116         { 0x5406E,      STOP_AT_PATCH_RENDER },                 /* patch away beacon check (not visible on screen) */
117         { 0x0,          STOP_AT_END },                          /* end */
118 };
119
120 extern const uint32_t mercenary3_hex[];
121 extern int mercenary3_hex_size;
122
123 void mercenary_load(void)
124 {
125         int i;
126
127         /* load game binary from constant to volatile memory */
128         for (i = 0; i < mercenary3_hex_size; i += 4) {
129                 m68k_write_memory_32(i, mercenary3_hex[i / 4]);
130         }
131 }
132
133 void mercenary_patch(void)
134 {
135         uint32_t address;
136
137         /* initial stack */
138         m68k_write_memory_32(0x00000, INITIAL_STACK);
139
140         /* reset vector */
141         m68k_write_memory_32(0x00004, RESET_VECTOR);
142
143         /* patch away the function call that is outside chip memory
144          * is this a copy protection????
145          */
146         m68k_write_memory_16(0x5a17c, 0x4e71); /* nop */
147         m68k_write_memory_16(0x5a17e, 0x4e71); /* nop */
148         m68k_write_memory_16(0x5a180, 0x4e71); /* nop */
149
150         /* remove function that checks what stars are rendered when flying above plante
151          * instead of just rendering the necessary parts, both parts are always rendered:
152          * 1. stars from horizont up to zenith
153          * 2. stars from zenith up to horizon (upside down)
154          * we need that, so opengl rendering can use wider FOV without missing stars.
155          * the game will actually have no problem with it, except that it requires more cpu cycles
156          */
157         for (address = 0x502AE; address < 0x502D4; address += 2)
158                 m68k_write_memory_16(address, 0x4e71); /* nop */
159
160 //m68k_write_memory_16(0x53A42, 0x2C20);
161
162 //m68k_write_memory_16(0x58388, 0x4E75); /* rts */
163 //m68k_write_memory_16(0x4FE3E, 0x4E75); /* rts */
164
165 //m68k_write_memory_16(0x53A6C, 0x6008); /* bra */
166 //m68k_write_memory_16(0x537B0, 0x6008); /* bra */
167 //m68k_write_memory_16(0x45806, 0x4E75); /* rts */
168
169 //m68k_write_memory_16(0x54342, 0x4e71); /* nop */
170 //m68k_write_memory_16(0x54344, 0x4e71); /* nop */
171 //m68k_write_memory_16(0x5059e, 0x3f0); /* nop */
172 #if 0
173 m68k_write_memory_16(0x4FB5E, 0x4e71); /* nop */
174 m68k_write_memory_16(0x4FB60, 0x4e71); /* nop */
175 m68k_write_memory_16(0x4FB62, 0x4e71); /* nop */
176 m68k_write_memory_16(0x4FB64, 0x4e71); /* nop */
177 m68k_write_memory_16(0x4FB66, 0x4e71); /* nop */
178 m68k_write_memory_16(0x4FB68, 0x4e71); /* nop */
179 m68k_write_memory_16(0x4FB6a, 0x4e71); /* nop */
180 m68k_write_memory_16(0x4FB6c, 0x4e71); /* nop */
181 #endif
182
183         /* remove wait for VBL */
184         m68k_write_memory_16(0x5a82C, 0x4e71); /* nop */
185
186         /* reduce loop that waits for disk stepper to move */
187         if (m68k_read_memory_32(0x562f8) != 0x000091b0) {
188                 print_error("expecting loop counter of 0x000091b0 here, please fix!\n");
189                 exit(0);
190         }
191         m68k_write_memory_32(0x562f8, 1);
192         /* reduce loop that waits for disk side change */
193         if (m68k_read_memory_32(0x55f5c) != 0x0000d020) {
194                 print_error("expecting loop counter of 0x0000d020 here, please fix!\n");
195                 exit(0);
196         }
197         m68k_write_memory_32(0x55f5c, 1);
198 }
199
200 /* skip certain parts when rendering improved graphics */
201 void mercenary_patch_render(void)
202 {
203         switch (REG_PC) {
204         case 0x53A6C: /* take that branch, so planets get rendered behind obersver */
205                 REG_PC += 10;
206                 break;
207         case 0x537B0: /* take that branch, so planets get rendered behind obersver */
208                 REG_PC += 10;
209                 break;
210         case 0x45806: /* just RTS to avoid crashing of rendering functions when planets are behind obersver */
211                 REG_PC -= 2;
212                 break;
213         case 0x5406E: /* skip that branch, so beacons get rendered outside screen */
214                 REG_PC += 2;
215                 break;
216         case 0x54088: /* skip point render, because projected coordinates may be invalid outside screen */
217                 REG_PC += 4;
218                 break;
219         }
220 }
221
222 uint32_t mercenary_palette_view(void)
223 {
224         return m68k_read_memory_32(0x0072b0);
225 }
226
227 uint32_t mercenary_palette_render(void)
228 {
229         return m68k_read_memory_32(0x0072b4);
230 }
231
232 uint32_t mercenary_palette_predefined(void)
233 {
234         return m68k_read_memory_32(0x0070a4);
235 }
236
237 uint32_t mercenary_palette_stars(void)
238 {
239         return DS_41+66;
240 }
241
242 void mercenary_get_orientation(double *roll, double *pitch, double *yaw)
243 {
244         int16_t r;
245
246         /* we could use 0x1e4*, but then "floating on the water" is not included */
247
248         /* get observer's tilt, pitch, yaw */
249         r = (int16_t)(m68k_read_memory_16(DS_0+0x1E26) & 0x3ff);
250         *roll = (double)r / 1024.0 * 2 * M_PI;
251         r = (int16_t)((m68k_read_memory_16(DS_0+0x1E28) + 0x201) & 0x3ff); /* add one extra to make view leveled to ground */
252         *pitch = -(double)r / 1024.0 * 2 * M_PI;
253         r = (int16_t)((m68k_read_memory_16(DS_0+0x1E2a) + 0x200) & 0x3ff);
254         *yaw = -(double)r / 1024.0 * 2 * M_PI;
255 }
256
257 void mercenary_get_orientation_raw(int16_t *pitch, uint16_t *yaw)
258 {
259         *pitch = m68k_read_memory_16(DS_0+0x1E28);
260         *yaw = m68k_read_memory_16(DS_0+0x1E2a);
261 }
262
263 void mercenary_get_orientation_planet(double *inclination, double *azimuth, int improved)
264 {
265         uint32_t t;
266
267         if (!improved) {
268                 /* get plant's inclination and azimuth */
269                 t = m68k_read_memory_16(0x42C70) & 0x3ff;
270                 *inclination = (double)t / 0x400 * 2 * M_PI;
271                 t = m68k_read_memory_16(0x42C6c) & 0x3ff;
272                 *azimuth = -(double)t / 0x400 * 2 * M_PI;
273         } else {
274                 /* inclination depends on north/south position */
275                 t = m68k_read_memory_32(DS_0+0x1E42) >> 2; /* get position */
276                 t += 0x01000000;
277                 t &= 0x03ffffff;
278                 *inclination = (double)t / 0x04000000 * 2 * M_PI;
279                 /* azimuth depends on east/west position and planet's rotation */
280                 t = m68k_read_memory_32(DS_0+0x1E2E); /* get planet index */
281                 t = m68k_read_memory_32(22362 + t); /* get rotation of planet */
282                 t += m68k_read_memory_32(DS_0+0x1E3A) >> 2; /* add position */
283                 t &= 0x03ffffff;
284                 *azimuth = -(double)t / 0x04000000 * 2 * M_PI;
285
286         }
287 }
288
289 void mercenary_get_location(int32_t *east, int32_t *height, int32_t *north)
290 {
291         *east = (int32_t)m68k_read_memory_32(DS_0+0x1E1A);
292         *height = (int32_t)m68k_read_memory_32(DS_0+0x1DBA);
293         *north = (int32_t)m68k_read_memory_32(DS_0+0x1E22);
294 }
295
296 void mercenary_get_object_info(int *id, int32_t *east, int32_t *height, int32_t *north)
297 {
298         *id = REG_A[0];
299         *east = (int32_t)m68k_read_memory_32(REG_A[0] + 22556);
300         *height = (int32_t)m68k_read_memory_32(REG_A[0] + 23580);
301         *north = (int32_t)m68k_read_memory_32(REG_A[0] + 24604);
302 }
303
304 void mercenary_coord_building_interior(int16_t *east, int32_t *height1, int32_t *height2, int32_t *height3, int32_t *height4, int16_t *north)
305 {
306         *east = (int16_t)m68k_read_memory_16(5698+REG_A[0]) - (int16_t)REG_A[2];
307         *north = (int16_t)m68k_read_memory_16(6298+REG_A[0]) - (int16_t)REG_A[3];
308         *height1 = -(int16_t)m68k_read_memory_16(DS_0+0x1D30);
309         *height2 = (int16_t)m68k_read_memory_16(DS_0+0x1EB2) - (int16_t)m68k_read_memory_16(DS_0+0x1D30);
310         *height3 = (int16_t)m68k_read_memory_16(DS_0+0x1EB4) - (int16_t)m68k_read_memory_16(DS_0+0x1D30);
311         *height4 = (int16_t)m68k_read_memory_16(DS_0+0x1EB6) - (int16_t)m68k_read_memory_16(DS_0+0x1D30);
312
313 }
314
315 int mercenary_street_color_index(void)
316 {
317         return (m68k_read_memory_16(DS_0+0x1f70) >> 5) & 0xf;
318 }
319
320 int mercenary_line_tags_index(void)
321 {
322         return (m68k_read_memory_16(DS_0+0x1f70) >> 5) & 0xf;
323 }
324
325 uint16_t mercenary_poly_tags_color(void)
326 {
327         return m68k_read_memory_16(DS_0+0x1e92);
328 }
329
330 int mercenary_background_index(void)
331 {
332         return m68k_read_memory_16(DS_0+0x1E76) >> 2;
333 }
334
335 uint32_t mercenary_planet_scale_index(void)
336 {
337         return 21584;
338 }
339
340 uint32_t mercenary_star_table(void)
341 {
342         return DS_84+0x6A;
343 }
344
345 const char *mercenary_name = "Mercenary III - The Dion Crisis";
346 const char *mercenary_gamesavesuffix = ".m3save";
347