World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
MM7.h
См. документацию.
1 #pragma once
2 #include <array>
3 
4 typedef unsigned int uint;
5 
6 #define PID(type, id) (uint32_t)((((8 * (id))) | (type)) & 0xFFFF) // packed id
7 #define PID_TYPE(pid) (ObjectType)((pid)&7) // extract type
8 #define PID_ID(pid) (uint32_t)(((pid)&0xFFFF) >> 3) // extract value
9 #define PID_INVALID (-1)
10 
11 // typedef char _UNKNOWN;
12 typedef unsigned int uint;
13 
14 inline void memset32(void *ptr, uint32_t value, int count) {
15  uint32_t *p = (uint32_t *)ptr;
16  for (int i = 0; i < count; i++) {
17  *p++ = value;
18  }
19 }
20 
21 #if defined(__GNUC__)
22 typedef long long ll;
23 typedef unsigned long long ull;
24 #define __int64 long long
25 #define __int32 int
26 #define __int16 short
27 #define __int8 char
28 #define MAKELL(num) num##LL
29 #define FMT_64 "ll"
30 #elif defined(_MSC_VER)
31 typedef __int64 ll;
32 typedef unsigned __int64 ull;
33 #define MAKELL(num) num##i64
34 #define FMT_64 "I64"
35 #elif defined(__BORLANDC__)
36 typedef __int64 ll;
37 typedef unsigned __int64 ull;
38 #define MAKELL(num) num##i64
39 #define FMT_64 "L"
40 #else
41 #error "unknown compiler"
42 #endif
43 typedef unsigned int uint;
44 typedef unsigned char uchar;
45 typedef unsigned short ushort;
46 typedef unsigned long ulong;
47 
48 typedef char int8;
49 typedef signed char sint8;
50 typedef unsigned char uint8;
51 typedef short int16;
52 typedef signed short sint16;
53 typedef unsigned short uint16;
54 typedef int int32;
55 typedef signed int sint32;
56 typedef unsigned int uint32;
57 typedef ll int64;
58 typedef ll sint64;
59 typedef ull uint64;
60 
61 // Partially defined types:
62 #define _BYTE uint8
63 #define _WORD uint16
64 #define _DWORD uint32
65 #define _QWORD uint64
66 #if !defined(_MSC_VER)
67 #define _LONGLONG __int128
68 #endif
69 
70 #define HEXRAYS_LOBYTE(x) (*((_BYTE *)&(x))) // low byte
71 #define HEXRAYS_LOWORD(x) (*((_WORD *)&(x))) // low word
72 #define HEXRAYS_LODWORD(x) (*((_DWORD *)&(x))) // low dword
73 #define HEXRAYS_HIBYTE(x) (*((_BYTE *)&(x) + 1))
74 #define HEXRAYS_HIWORD(x) (*((_WORD *)&(x) + 1))
75 #define HEXRAYS_HIDWORD(x) (*((_DWORD *)&(x) + 1))
76 #define BYTEn(x, n) (*((_BYTE *)&(x) + n))
77 #define WORDn(x, n) (*((_WORD *)&(x) + n))
78 #define BYTE1(x) BYTEn(x, 1) // byte 1 (counting from 0)
79 #define BYTE2(x) BYTEn(x, 2)
80 #define BYTE3(x) BYTEn(x, 3)
81 #define BYTE4(x) BYTEn(x, 4)
82 #define BYTE5(x) BYTEn(x, 5)
83 #define BYTE6(x) BYTEn(x, 6)
84 #define BYTE7(x) BYTEn(x, 7)
85 #define BYTE8(x) BYTEn(x, 8)
86 #define BYTE9(x) BYTEn(x, 9)
87 #define BYTE10(x) BYTEn(x, 10)
88 #define BYTE11(x) BYTEn(x, 11)
89 #define BYTE12(x) BYTEn(x, 12)
90 #define BYTE13(x) BYTEn(x, 13)
91 #define BYTE14(x) BYTEn(x, 14)
92 #define BYTE15(x) BYTEn(x, 15)
93 #define WORD1(x) WORDn(x, 1)
94 #define WORD2(x) WORDn(x, 2) // third word of the object, unsigned
95 #define WORD3(x) WORDn(x, 3)
96 #define WORD4(x) WORDn(x, 4)
97 #define WORD5(x) WORDn(x, 5)
98 #define WORD6(x) WORDn(x, 6)
99 #define WORD7(x) WORDn(x, 7)
100 
101 // now signed macros (the same but with sign extension)
102 #define HEXRAYS_SLOBYTE(x) (*((int8 *)&(x)))
103 #define HEXRAYS_SLOWORD(x) (*((int16 *)&(x)))
104 #define HEXRAYS_SLODWORD(x) (*((int32 *)&(x)))
105 #define HEXRAYS_SHIBYTE(x) (*((int8 *)&(x) + 1))
106 #define HEXRAYS_SHIWORD(x) (*((int16 *)&(x) + 1))
107 #define HEXRAYS_SHIDWORD(x) (*((int32 *)&(x) + 1))
108 #define SBYTEn(x, n) (*((int8 *)&(x) + n))
109 #define SWORDn(x, n) (*((int16 *)&(x) + n))
110 #define SBYTE1(x) SBYTEn(x, 1)
111 #define SBYTE2(x) SBYTEn(x, 2)
112 #define SBYTE3(x) SBYTEn(x, 3)
113 #define SBYTE4(x) SBYTEn(x, 4)
114 #define SBYTE5(x) SBYTEn(x, 5)
115 #define SBYTE6(x) SBYTEn(x, 6)
116 #define SBYTE7(x) SBYTEn(x, 7)
117 #define SBYTE8(x) SBYTEn(x, 8)
118 #define SBYTE9(x) SBYTEn(x, 9)
119 #define SBYTE10(x) SBYTEn(x, 10)
120 #define SBYTE11(x) SBYTEn(x, 11)
121 #define SBYTE12(x) SBYTEn(x, 12)
122 #define SBYTE13(x) SBYTEn(x, 13)
123 #define SBYTE14(x) SBYTEn(x, 14)
124 #define SBYTE15(x) SBYTEn(x, 15)
125 #define SWORD1(x) SWORDn(x, 1)
126 #define SWORD2(x) SWORDn(x, 2)
127 #define SWORD3(x) SWORDn(x, 3)
128 #define SWORD4(x) SWORDn(x, 4)
129 #define SWORD5(x) SWORDn(x, 5)
130 #define SWORD6(x) SWORDn(x, 6)
131 #define SWORD7(x) SWORDn(x, 7)
132 
133 // Generate a reference to pair of operands
134 template <class T>
135 int16 __PAIR__(int8 high, T low) {
136  return (((int16)high) << sizeof(high) * 8) | uint8(low);
137 }
138 template <class T>
139 int32 __PAIR__(int16 high, T low) {
140  return (((int32)high) << sizeof(high) * 8) | uint16(low);
141 }
142 template <class T>
143 int64 __PAIR__(int32 high, T low) {
144  return (((int64)high) << sizeof(high) * 8) | uint32(low);
145 }
146 template <class T>
147 uint16 __PAIR__(uint8 high, T low) {
148  return (((uint16)high) << sizeof(high) * 8) | uint8(low);
149 }
150 template <class T>
151 uint32 __PAIR__(uint16 high, T low) {
152  return (((uint32)high) << sizeof(high) * 8) | uint16(low);
153 }
154 template <class T>
155 uint64 __PAIR__(uint32 high, T low) {
156  return (((uint64)high) << sizeof(high) * 8) | uint32(low);
157 }
158 
159 // rotate left
160 template <class T>
162  const uint nbits = sizeof(T) * 8;
163  count %= nbits;
164 
165  T high = value >> (nbits - count);
166  value <<= count;
167  value |= high;
168  return value;
169 }
170 
171 // rotate right
172 template <class T>
174  const uint nbits = sizeof(T) * 8;
175  count %= nbits;
176 
177  T low = value << (nbits - count);
178  value >>= count;
179  value |= low;
180  return value;
181 }
182 
183 // carry flag of left shift
184 template <class T>
186  const uint nbits = sizeof(T) * 8;
187  count %= nbits;
188 
189  return (value >> (nbits - count)) & 1;
190 }
191 
192 // carry flag of right shift
193 template <class T>
195  return (value >> (count - 1)) & 1;
196 }
197 
198 // sign flag
199 template <class T>
201  if (sizeof(T) == 1) return int8(x) < 0;
202  if (sizeof(T) == 2) return int16(x) < 0;
203  if (sizeof(T) == 4) return int32(x) < 0;
204  return int64(x) < 0;
205 }
206 
207 // overflow flag of subtraction (x-y)
208 template <class T, class U>
209 int8 __OFSUB__(T x, U y) {
210  if (sizeof(T) < sizeof(U)) {
211  U x2 = x;
212  int8 sx = __SETS__(x2);
213  return (sx ^ __SETS__(y)) & (sx ^ __SETS__(x2 - y));
214  } else {
215  T y2 = y;
216  int8 sx = __SETS__(x);
217  return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x - y2));
218  }
219 }
220 
221 // overflow flag of addition (x+y)
222 template <class T, class U>
223 int8 __OFADD__(T x, U y) {
224  if (sizeof(T) < sizeof(U)) {
225  U x2 = x;
226  int8 sx = __SETS__(x2);
227  return ((1 ^ sx) ^ __SETS__(y)) & (sx ^ __SETS__(x2 + y));
228  } else {
229  T y2 = y;
230  int8 sx = __SETS__(x);
231  return ((1 ^ sx) ^ __SETS__(y2)) & (sx ^ __SETS__(x + y2));
232  }
233 }
234 
235 // carry flag of subtraction (x-y)
236 template <class T, class U>
237 int8 __CFSUB__(T x, U y) {
238  int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U);
239  if (size == 1) return uint8(x) < uint8(y);
240  if (size == 2) return uint16(x) < uint16(y);
241  if (size == 4) return uint32(x) < uint32(y);
242  return uint64(x) < uint64(y);
243 }
244 
245 // carry flag of addition (x+y)
246 template <class T, class U>
247 int8 __CFADD__(T x, U y) {
248  int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U);
249  if (size == 1) return uint8(x) > uint8(x + y);
250  if (size == 2) return uint16(x) > uint16(x + y);
251  if (size == 4) return uint32(x) > uint32(x + y);
252  return uint64(x) > uint64(x + y);
253 }
254 
255 /* 297 */
256 enum SoundType {
260 };
261 
262 /* 362 */
263 #pragma pack(push, 1)
264 struct TravelInfo {
265  char uMapID;
266  char pSchedule[7];
268  char field_9[3];
269  int x;
270  int y;
271  int z;
273  int field_1C;
274 };
275 #pragma pack(pop)
276 
277 /* 374 */
278 #pragma pack(push, 1)
279 struct stat_coord {
280  __int16 x;
281  __int16 y;
282  __int16 width;
283  __int16 height;
284 };
285 #pragma pack(pop)
286 extern std::array<stat_coord, 26> stat_string_coord;
287 
288 /* 376 */
289 #pragma pack(push, 1)
290 struct stru336 {
291  int field_0;
292  int field_4;
293  int field_8;
294  int field_C;
295  int field_10;
296  int field_14;
297  __int16 field_18[480];
298  __int16 field_3D8[480];
299 };
300 #pragma pack(pop)
int32
int int32
Definition: MM7.h:54
stat_coord::height
__int16 height
Definition: MM7.h:283
TravelInfo::field_9
char field_9[3]
Definition: MM7.h:268
stru336::field_10
int field_10
Definition: MM7.h:295
__MKCSHR__
int8 __MKCSHR__(T value, uint count)
Definition: MM7.h:194
stat_coord::width
__int16 width
Definition: MM7.h:282
__ROL__
T __ROL__(T value, uint count)
Definition: MM7.h:161
stat_coord::y
__int16 y
Definition: MM7.h:281
__SETS__
int8 __SETS__(T x)
Definition: MM7.h:200
ll
long long ll
Definition: MM7.h:22
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
TravelInfo
Definition: MM7.h:264
sint32
signed int sint32
Definition: MM7.h:55
TravelInfo::pSchedule
char pSchedule[7]
Definition: MM7.h:266
ull
unsigned long long ull
Definition: MM7.h:23
TravelInfo::field_1C
int field_1C
Definition: MM7.h:273
stru336::field_18
__int16 field_18[480]
Definition: MM7.h:297
__CFADD__
int8 __CFADD__(T x, U y)
Definition: MM7.h:247
__MKCSHL__
int8 __MKCSHL__(T value, uint count)
Definition: MM7.h:185
stru336
Definition: MM7.h:290
stat_coord::x
__int16 x
Definition: MM7.h:280
ulong
unsigned long ulong
Definition: MM7.h:46
y
EGLSurface EGLint EGLint y
Definition: SDL_egl.h:1596
SoundType
SoundType
Definition: MM7.h:256
TravelInfo::z
int z
Definition: MM7.h:271
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11093
x
EGLSurface EGLint x
Definition: SDL_egl.h:1596
TravelInfo::y
int y
Definition: MM7.h:270
uchar
unsigned char uchar
Definition: MM7.h:44
uint16
unsigned short uint16
Definition: MM7.h:53
uint64
ull uint64
Definition: MM7.h:59
stru336::field_4
int field_4
Definition: MM7.h:292
TravelInfo::uDaysCount
char uDaysCount
Definition: MM7.h:267
TravelInfo::uMapID
char uMapID
Definition: MM7.h:265
__CFSUB__
int8 __CFSUB__(T x, U y)
Definition: MM7.h:237
sint16
signed short sint16
Definition: MM7.h:52
stru336::field_C
int field_C
Definition: MM7.h:294
int16
short int16
Definition: MM7.h:51
value
EGLSyncKHR EGLint EGLint * value
Definition: SDL_egl.h:899
int64
ll int64
Definition: MM7.h:57
__OFADD__
int8 __OFADD__(T x, U y)
Definition: MM7.h:223
stru336::field_8
int field_8
Definition: MM7.h:293
SOUND_EndTurnBasedMode
@ SOUND_EndTurnBasedMode
Definition: MM7.h:257
stru336::field_14
int field_14
Definition: MM7.h:296
SOUND_StartTurnBasedMode
@ SOUND_StartTurnBasedMode
Definition: MM7.h:258
memset32
void memset32(void *ptr, uint32_t value, int count)
Definition: MM7.h:14
sint8
signed char sint8
Definition: MM7.h:49
stru336::field_3D8
__int16 field_3D8[480]
Definition: MM7.h:298
SOUND_FlipOnExit
@ SOUND_FlipOnExit
Definition: MM7.h:259
TravelInfo::direction
int direction
Definition: MM7.h:272
x2
GLfixed GLfixed x2
Definition: SDL_opengl_glext.h:4586
sint64
ll sint64
Definition: MM7.h:58
uint
unsigned int uint
Definition: MM7.h:4
stru336::field_0
int field_0
Definition: MM7.h:291
__PAIR__
int16 __PAIR__(int8 high, T low)
Definition: MM7.h:135
y2
GLfixed GLfixed GLfixed y2
Definition: SDL_opengl_glext.h:4586
uint32
unsigned int uint32
Definition: MM7.h:56
TravelInfo::x
int x
Definition: MM7.h:269
__OFSUB__
int8 __OFSUB__(T x, U y)
Definition: MM7.h:209
size
GLsizeiptr size
Definition: SDL_opengl_glext.h:540
stat_coord
Definition: MM7.h:279
uint8
unsigned char uint8
Definition: MM7.h:50
uint32_t
unsigned __int32 uint32_t
Definition: SDL_config.h:39
int8
char int8
Definition: MM7.h:48
ushort
unsigned short ushort
Definition: MM7.h:45
__ROR__
T __ROR__(T value, uint count)
Definition: MM7.h:173
stat_string_coord
std::array< stat_coord, 26 > stat_string_coord
Definition: mm7_data.cpp:173