c93a3ecdec13ed8419169acdbc0f45f9ba83a3c8
[mercenary-reloaded.git] / src / mercenary / mercenary2.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 <string.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include "../libsdl/print.h"
26 #include "../libcpu/m68k.h"
27 #include "../libcpu/m68kcpu.h"
28 #include "../libcpu/execute.h"
29 #include "mercenary.h"
30
31 #define INITIAL_STACK   0x7fffa
32 #define RESET_VECTOR    0x59484
33
34 static int info_walking = 0;
35
36 /* VR heading to alter direction we walk to */
37 static struct vr_move vr_move;
38
39 /* interrupt CPU execution at special break points and tell emulation what to do */
40 const struct cpu_stop mercenary_stop_at[] = {
41         { 0x59a4e,      STOP_AT_WAIT_VBL },                     /* done with rendering, waiting for VBL */
42         { 0x54c26,      STOP_AT_WAIT_INPUT },                   /* after pressing 'HELP' key before showing menu line on benson */
43         { 0x55438,      STOP_AT_WAIT_INPUT },                   /* waiting for menu command */
44         { 0x54c2e,      STOP_AT_WAIT_INPUT },                   /* after pressing 'HELP' key while showing menu line on benson */
45         { 0x55446,      STOP_AT_WAIT_INPUT },                   /* after pressing 'RETURN' while game waits for other key to resume */
46         { 0x51620,      STOP_AT_WAIT_VBL },                     /* after dying, waiting for VBL to fade out palette */
47         { 0x596BC,      STOP_AT_CLEAR_SCREEN1 },                /* clear the screen inside building, no windows (here we know the color 8, this is wy we cannot do it earlier) */
48         { 0x596F4,      STOP_AT_CLEAR_SCREEN1 },                /* walking above ground */
49         { 0x59720,      STOP_AT_CLEAR_SCREEN1 },                /* unknown */
50         { 0x598A8,      STOP_AT_CLEAR_SCREEN1 },                /* space craft on ground */
51         { 0x598E6,      STOP_AT_CLEAR_SCREEN2 },                /* special case where we use color index 15 when we are flying (no raster split for ground color) */
52         { 0x59982,      STOP_AT_CLEAR_SCREEN3 },                /* special case where we are in universe and do not have any ground */
53         { 0x55F2E,      STOP_AT_DRAW_GROUND },                  /* the ground is rendered. the color index is captured at CLEAR_SCREEN */
54         { 0x596CE,      STOP_AT_DRAW_GROUND },                  /* at this point there is ground (inside building) rendered, because game uses raster split for ground color, but we do render opengl */
55         { 0x59710,      STOP_AT_DRAW_GROUND },                  /* at this point there is ground (outside) rendered, because game uses raster split for ground color, but we do render opengl */
56         { 0x531EA,      STOP_AT_INFO_OBJECT_MOVING },           /* get ID and position of next object */
57         /* note: there a no fix objects in this game (no taxi/bus/intercity) */
58         { 0x534EA,      STOP_AT_TAG_IS_OBJECT_1 },              /* indicates that the next tag is an object */
59         { 0x534EE,      STOP_AT_TAG_IS_OBJECT_0 },              /* indicates that the tag of object was rendered */
60         { 0x5346E,      STOP_AT_COORD_OBJECT },                 /* object coordinates are ready */
61         { 0x534F6,      STOP_AT_POLY_OBJECT_M2 },               /* object polygon is rendered */
62         { 0x534F0,      STOP_AT_LINE_OBJECT },                  /* object line is rendered */
63         { 0x5324A,      STOP_AT_COORD_BEACON },                 /* beacon's point coordinates are ready */
64         { 0x53284,      STOP_AT_POINT_BEACON },                 /* becon point is rendered */
65         { 0x53A00,      STOP_AT_COORD_BUILDING_EXTERIOR },      /* building (house) coordinates are ready */
66         { 0x53A5C,      STOP_AT_POLY_BUILDING_EXTERIOR },       /* building polygons are rendered */
67         { 0x53A54,      STOP_AT_LINE_BUILDING_EXTERIOR },       /* lines of building, like radio tower on icarus */
68         { 0x5AA10,      STOP_AT_COORD_BUILDING_INTERIOR },      /* building coordinates for interrior */
69         { 0x5B218,      STOP_AT_POLY_BUILDING_INTERIOR1 },      /* floor of building will be rendered */
70         { 0x5B1BE,      STOP_AT_POLY_BUILDING_INTERIOR2 },      /* ceiling of building will be rendered */
71         { 0x5B154,      STOP_AT_POLY_BUILDING_INTERIOR3 },      /* ceiling of window/door will be rendered */
72         { 0x5B0E0,      STOP_AT_POLY_BUILDING_INTERIOR4 },      /* floor of window will be rendered */
73         { 0x5B2DE,      STOP_AT_POLY_BUILDING_INTERIOR1to4 },   /* ceiling/floor of building is rendered */
74         { 0x5AFF4,      STOP_AT_POLY_BUILDING_INTERIOR5 },      /* part above window/door will be rendered */
75         { 0x5AF8A,      STOP_AT_POLY_BUILDING_INTERIOR6 },      /* part below window will be rendered */
76         { 0x5B0BE,      STOP_AT_POLY_BUILDING_INTERIOR5to6 },   /* part below/above window/door of building is rendered */
77         { 0x5B332,      STOP_AT_WALL_BUILDING },                /* a wall (between floor and ceiling/window/door) is rendered) */
78         { 0x4F462,      STOP_AT_COORD_COMET },                  /* comet's coordinates are ready */
79         { 0x4F496,      STOP_AT_MATRIX_COMET },                 /* what rotation matrix to use */
80         { 0x4F4B8,      STOP_AT_POLY_COMET },                   /* comet's horizontal polygon (without culling no need to render 0x4F4BC) */
81         { 0x4F4C0,      STOP_AT_POLY_COMET },                   /* comet's vertival polygon (without culling no need to render 0x4F4C4) */
82         { 0x54634,      STOP_AT_COORD_LINE_ROADS },             /* road's line coordinates are ready */
83         { 0x54676,      STOP_AT_LINE_ROADS },                   /* road's and ground surface's polygon */
84         { 0x5469C,      STOP_AT_LINE_ROADS },
85         { 0x546B2,      STOP_AT_LINE_ROADS },
86         { 0x56496,      STOP_AT_LINE_ROADS_CENTER },            /* center line of roads */
87         { 0x56442,      STOP_AT_COORD_POLY_ROADS },             /* road's and ground surface's coordinates are ready */
88         { 0x5649C,      STOP_AT_POLY_ROADS },                   /* road's and ground surface's polygon */
89         { 0x53CB6,      STOP_AT_COORD_TAGS },                   /* coordinates for tags, like key's marking are ready */
90         /* note: there are no coordinates for large tags in this game (no faces) */
91         /* note: there are no STOP_AT_LINE_TAGS1 and STOP_AT_POLY_TAGS1 in this game(given color) */
92         { 0x53CFA,      STOP_AT_LINE_TAGS2 },                   /* tag's line is rendered (use last color) */
93         { 0x53CF0,      STOP_AT_POLY_TAGS2 },                   /* tag's polygon is rendered (use last color) */
94         { 0x52BF4,      STOP_AT_COORD_PLANET },                 /* planet's coordinates are ready (viewed from ground) */
95         { 0x52C1A,      STOP_AT_MATRIX_PLANET },                /* what rotation matrix to use */
96         { 0x52756,      STOP_AT_COORD_PLANET },                 /* planet's coordinates are ready (viewed from universe) */
97         { 0x5277C,      STOP_AT_MATRIX_PLANET },                /* what rotation matrix to use */
98         { 0x52C70,      STOP_AT_DRAW_PLANET },                  /* planet's sphere is rendered, D0 is color (viewed from ground) */
99         { 0x52B36,      STOP_AT_DRAW_PLANET },                  /* planet's sphere is rendered, D0 is color (viewed from universe) */
100         { 0x4F4E6,      STOP_AT_DRAW_COMET },                   /* comet's sphere is rendered */
101         { 0x50006,      STOP_AT_DRAW_STARS_SPACE },             /* stars are rendered (viewed from universe) */
102         { 0x4FF4C,      STOP_AT_DRAW_STARS_GROUND },            /* stars are rendered (viewed from ground) */
103         { 0x4FE24,      STOP_AT_DRAW_STARS_FLYING },            /* stars are rendered (viewed from planet when flying) */
104         { 0x4FCAC,      STOP_AT_DRAW_STARS_FLYING2 },           /* same as above, but stars when upside down (above zenit) */
105         { 0x50FC2,      STOP_AT_DRAW_STARS_INTERSTELLAR },      /* interstellar star flight */
106         { 0x50BF4,      STOP_AT_DRAW_SUN_INTERSTELLAR },        /* draw sun dot while flying interstellar */
107         { 0x50BB8,      STOP_AT_CLEAR_SCREEN3 },                /* clear while flying interstellar */
108         { 0x563B8,      STOP_AT_COORD_ISLANDS },                /* island's coordinates are ready */
109         { 0x56416,      STOP_AT_POLY_ISLANDS },                 /* island's polygon is rendered */
110         { 0x56410,      STOP_AT_LINE_ISLANDS },
111         { 0x511FE,      STOP_AT_DRAW_SIGHTS },                  /* when sights are rendered */
112         { 0x5351A,      STOP_AT_COORD_EXPLOSION },              /* explosion debris coordinate */
113         { 0x5356C,      STOP_AT_DRAW_EXPLOSION },               /* explosion debris render */
114         { 0x4C514,      STOP_AT_COORD_EXPLOSION },
115         { 0x4C6F0,      STOP_AT_DRAW_EXPLOSION },
116         { 0x52C42,      STOP_AT_PATCH_RENDER },                 /* patch away planet check (behind observer) */
117         { 0x528C4,      STOP_AT_PATCH_RENDER },                 /* patch away planet check (behind observer) */
118         { 0x45806,      STOP_AT_PATCH_RENDER },                 /* patch away planet rendering (would crash without check above) */
119         { 0x53276,      STOP_AT_PATCH_RENDER },                 /* patch away beacon check (not visible on screen) */
120         { 0x59622,      STOP_AT_PATCH_VR },                     /* we are walking/taxi */
121         { 0x597CA,      STOP_AT_PATCH_VR },                     /* we are on a craft */
122         { 0x597F2,      STOP_AT_PATCH_VR },                     /* we are ??? */
123         { 0x59944,      STOP_AT_PATCH_VR },                     /* we are in space */
124         { 0x58EA8,      STOP_AT_PATCH_VR },                     /* step that moves player on surface */
125         { 0x58060,      STOP_AT_PATCH_VR },                     /* soft pitch over planet */
126         { 0x58918,      STOP_AT_PATCH_VR },                     /* soft pitch in space */
127         { 0x0,          STOP_AT_END },                          /* end */
128 };
129
130 extern const uint32_t mercenary2_hex[];
131 extern int mercenary2_hex_size;
132
133 void mercenary_load(void)
134 {
135         int i;
136
137         memset(&vr_move, 0, sizeof(vr_move));
138         vr_move.yaw = 256;
139         vr_move.pitch = 256;
140
141         /* load game binary from constant to volatile memory */
142         for (i = 0; i < mercenary2_hex_size; i += 4) {
143                 m68k_write_memory_32(i, mercenary2_hex[i / 4]);
144         }
145 }
146
147 void mercenary_patch(void)
148 {
149         uint32_t address;
150
151         /* initial stack */
152         m68k_write_memory_32(0x00000, INITIAL_STACK);
153
154         /* reset vector */
155         m68k_write_memory_32(0x00004, RESET_VECTOR);
156
157         /* remove function that checks what stars are rendered when flying above plante
158          * instead of just rendering the necessary parts, both parts are always rendered:
159          * 1. stars from horizont up to zenith
160          * 2. stars from zenith up to horizon (upside down)
161          * we need that, so opengl rendering can use wider FOV without missing stars.
162          * the game will actually have no problem with it, except that it requires more cpu cycles
163          */
164         for (address = 0x4FD90; address < 0x4FDB6; address += 2)
165                 m68k_write_memory_16(address, 0x4e71); /* nop */
166
167         /* remove wait for VBL */
168         m68k_write_memory_16(0x59a54, 0x4e71); /* nop */
169
170         /* reduce loop that waits for disk stepper to move */
171         if (m68k_read_memory_32(0x55398) != 0x0000091b) {
172                 print_error("expecting loop counter of 0x0000091b here, please fix!\n");
173                 exit(0);
174         }
175         m68k_write_memory_32(0x55398, 1);
176         /* reduce loop that waits for disk side change */
177         if (m68k_read_memory_32(0x54ffc) != 0x00000d02) {
178                 print_error("expecting loop counter of 0x00000d02 here, please fix!\n");
179                 exit(0);
180         }
181         m68k_write_memory_32(0x54ffc, 1);
182 }
183
184 /* skip certain parts when rendering improved graphics */
185 void mercenary_patch_render(void)
186 {
187         switch (REG_PC) {
188         case 0x52C42: /* take that branch, so planets get rendered behind obersver */
189                 REG_PC += 10;
190                 break;
191         case 0x528C4: /* take that branch, so planets get rendered behind obersver */
192                 REG_PC += 10;
193                 break;
194         case 0x45806: /* just RTS to avoid crashing of rendering functions when planets are behind obersver */
195                 REG_PC -= 2;
196                 break;
197         case 0x53276: /* skip that branch, so beacons get rendered outside screen */
198                 REG_PC += 2;
199                 break;
200         case 0x53284: /* skip point render, because projected coordinates may be invalid outside screen */
201                 REG_PC += 4;
202                 break;
203         }
204 }
205
206 /* patch execution for VR improvement */
207 void mercenary_patch_vr(void)
208 {
209         switch (REG_PC) {
210         case 0x59622: /* we are walking/taxi */
211                 info_walking = 1;
212                 break;
213         case 0x597CA: /* we are on a craft */
214                 info_walking = 0;
215                 break;
216         case 0x597F2: /* we are ??? */
217                 info_walking = 0;
218                 break;
219         case 0x59944: /* we are in space */
220                 info_walking = 0;
221                 break;
222         case 0x58EA8: /* at this point we process one step of the player walking on the ground */
223                 if (vr_move.override && vr_move.index < 4) {
224                         REG_D[2] = vr_move.east[vr_move.index];
225                         REG_D[3] = vr_move.north[vr_move.index];
226                         vr_move.index++;
227                 }
228                 break;
229         case 0x58060: /* soft pitch over surface */
230                 REG_D[1] = ((int16_t)REG_D[1] * vr_move.pitch / 256) & 0xfff;
231                 break;
232         }
233 }
234
235 uint32_t mercenary_palette_view(void)
236 {
237         return m68k_read_memory_32(0x007c14);
238 }
239
240 uint32_t mercenary_palette_render(void)
241 {
242         return m68k_read_memory_32(0x007c18);
243 }
244
245 uint32_t mercenary_palette_predefined(void)
246 {
247         return m68k_read_memory_32(0x007a0e);
248 }
249
250 uint32_t mercenary_palette_stars(void)
251 {
252         return 0x500C4+66;
253 }
254
255 void mercenary_get_orientation(double *roll, double *pitch, double *yaw)
256 {
257         int16_t r;
258
259         /* get observer's tilt, pitch, yaw */
260         r = (int16_t)(m68k_read_memory_16(0x007A9E) & 0x3ff);
261         *roll = (double)r / 1024.0 * 2 * M_PI;
262         r = (int16_t)((m68k_read_memory_16(0x007AA0) + 0x201) & 0x3ff); /* add one extra to make view leveled to ground */
263         *pitch = -(double)r / 1024.0 * 2 * M_PI;
264         r = (int16_t)((m68k_read_memory_16(0x007AA2) + 0x200) & 0x3ff);
265         *yaw = -(double)r / 1024.0 * 2 * M_PI;
266 }
267
268 void mercenary_set_orientation(double yaw)
269 {
270         int16_t r;
271
272         /* set the oberver */
273         r = (int16_t)fmod(-yaw * 1024.0 / 2.0 / M_PI, 1024.0);
274         m68k_write_memory_16(0x007AC2, (r - 0x200) & 0x3ff);
275 }
276
277 void mercenary_get_orientation_raw(int16_t *pitch, uint16_t *yaw)
278 {
279         *pitch = m68k_read_memory_16(0x007AA0);
280         *yaw = m68k_read_memory_16(0x007AA2);
281 }
282
283 void mercenary_get_orientation_planet(double *inclination, double *azimuth, int improved)
284 {
285         uint32_t t;
286
287         if (!improved) {
288                 /* get plant's inclination and azimuth */
289                 t = m68k_read_memory_16(0x42C70) & 0x3ff;
290                 *inclination = (double)t / 0x400 * 2 * M_PI;
291                 t = m68k_read_memory_16(0x42C6c) & 0x3ff;
292                 *azimuth = -(double)t / 0x400 * 2 * M_PI;
293         } else {
294                 /* inclination depends on north/south position */
295                 t = m68k_read_memory_32(0x007ABA) >> 2; /* get position */
296                 t += 0x01000000;
297                 t &= 0x03ffffff;
298                 *inclination = (double)t / 0x04000000 * 2 * M_PI;
299                 /* azimuth depends on east/west position and planet's rotation */
300                 t = m68k_read_memory_32(0x007AA6); /* get planet index */
301                 t = m68k_read_memory_32(25322 + t); /* get rotation of planet */
302                 t += m68k_read_memory_32(0x007AB2) >> 2; /* add position */
303                 t &= 0x03ffffff;
304                 *azimuth = -(double)t / 0x04000000 * 2 * M_PI;
305
306         }
307 }
308
309 void mercenary_get_location(int32_t *east, int32_t *height, int32_t *north)
310 {
311         *east = (int32_t)m68k_read_memory_32(0x7a92);
312         *height = (int32_t)m68k_read_memory_32(0x7a30);
313         *north = (int32_t)m68k_read_memory_32(0x7a9a);
314 }
315
316 void mercenary_get_object_info(int *id, int32_t *east, int32_t *height, int32_t *north)
317 {
318         *id = REG_A[0];
319         *east = (int32_t)m68k_read_memory_32(REG_A[0] + 25512);
320         *height = (int32_t)m68k_read_memory_32(REG_A[0] + 26536);
321         *north = (int32_t)m68k_read_memory_32(REG_A[0] + 27560);
322 }
323
324 void mercenary_coord_building_interior(int16_t *east, int32_t *height1, int32_t *height2, int32_t *height3, int32_t *height4, int16_t *north)
325 {
326         *east = (int16_t)m68k_read_memory_16(5698+REG_A[0]) - (int16_t)REG_A[2];
327         *north = (int16_t)m68k_read_memory_16(6722+REG_A[0]) - (int16_t)REG_A[3];
328         *height1 = -(int16_t)m68k_read_memory_16(0x79AC);
329         *height2 = (int16_t)m68k_read_memory_16(0x7B26) - (int16_t)m68k_read_memory_16(0x79AC);
330         *height3 = (int16_t)m68k_read_memory_16(0x7B28) - (int16_t)m68k_read_memory_16(0x79AC);
331         *height4 = (int16_t)m68k_read_memory_16(0x7B2A) - (int16_t)m68k_read_memory_16(0x79AC);
332 }
333
334 int mercenary_street_color_index(void)
335 {
336         return (m68k_read_memory_16(0x7BE2) >> 5) & 0xf;
337 }
338
339 int mercenary_line_tags_index(void)
340 {
341         return (m68k_read_memory_16(0x7BE2) >> 5) & 0xf;
342 }
343
344 uint16_t mercenary_poly_tags_color(void)
345 {
346         return m68k_read_memory_16(0x7B06);
347 }
348
349 int mercenary_background_index(void)
350 {
351         return m68k_read_memory_16(0x7AEA) >> 2;
352 }
353
354 uint32_t mercenary_planet_scale_index(void)
355 {
356         return 24640;
357 }
358
359 uint32_t mercenary_star_table(void)
360 {
361         return 0x005D8C0;
362 }
363
364 void mercenary_vr_move(int override, int32_t *east, int32_t *north, int yaw, int pitch)
365 {
366         vr_move.override = override;
367         if (east)
368                 memcpy(vr_move.east, east, sizeof(vr_move.east));
369         if (north)
370                 memcpy(vr_move.north, north, sizeof(vr_move.north));
371         vr_move.yaw = yaw;
372         vr_move.pitch = pitch;
373         vr_move.index = 0;
374 }
375
376 int mercenary_get_info_walking(void)
377 {
378         return info_walking;
379 }
380
381 const char *mercenary_name = "Mercenary II - Damocles";
382 const char *mercenary_gamesavesuffix = ".m2save";
383
384 extern const uint8_t mercenary2_mission_disk_1[5][0x1820 << 2];
385 extern const uint8_t mercenary2_mission_disk_2[9][0x1820 << 2];
386
387 const uint8_t *get_mission_disk(int disk, int mission)
388 {
389         switch (disk) {
390         case 1:
391                 if (mission < 1 || mission > 5)
392                         return NULL;
393                 return mercenary2_mission_disk_1[mission - 1];
394         case 2:
395                 if (mission < 1 || mission > 9)
396                         return NULL;
397                 return mercenary2_mission_disk_2[mission - 1];
398         default:
399                 return NULL;
400         }
401 }
402