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