Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/games/alex4/files/patch-map.c
16461 views
1
--- map.c.orig 2016-06-14 16:25:53 UTC
2
+++ map.c
3
@@ -24,6 +24,7 @@
4
#include <stdlib.h>
5
#include <stdio.h>
6
#include <string.h>
7
+#include <machine/endian.h>
8
#include "allegro.h"
9
#include "map.h"
10
#include "timer.h"
11
@@ -64,7 +65,41 @@ Tmap *create_map(int w, int h) {
12
13
return m;
14
}
15
-
16
+
17
+static void mem_to_int(int *dest, unsigned char *mem)
18
+{
19
+#if __BYTE_ORDER == __LITTLE_ENDIAN
20
+ memcpy(dest, mem, 4);
21
+#else
22
+ *dest = mem[0] | (((int)mem[1]) << 8) | (((int)mem[2]) << 16) |
23
+ (((int)mem[3]) << 24);
24
+#endif
25
+}
26
+
27
+static void fread_int(int *dest, FILE *fp)
28
+{
29
+#if __BYTE_ORDER == __LITTLE_ENDIAN
30
+ fread(dest, 4, 1, fp);
31
+#else
32
+ unsigned char buf[4];
33
+ fread(buf, 1, 4, fp);
34
+ mem_to_int(dest, buf);
35
+#endif
36
+}
37
+
38
+static void fwrite_int(const int *src, FILE *fp)
39
+{
40
+#if __BYTE_ORDER == __LITTLE_ENDIAN
41
+ fwrite(src, 4, 1, fp);
42
+#else
43
+ unsigned char buf[4];
44
+ buf[0] = *src;
45
+ buf[1] = *src >> 8;
46
+ buf[2] = *src >> 16;
47
+ buf[3] = *src >> 24;
48
+ fwrite(buf, 1, 4, fp);
49
+#endif
50
+}
51
52
// loads one splendind map from disk
53
Tmap *load_map(char *fname) {
54
@@ -93,7 +128,19 @@ Tmap *load_map(char *fname) {
55
}
56
57
// read datastruct
58
- fread(m, sizeof(Tmap), 1, fp);
59
+ // a mapfile contain a raw dump of the Tmap struct made on an i386
60
+ // the code below reads these struct dumps in an arch neutral manner
61
+ // Note this dumps contains pointers, these are not used because these
62
+ // ofcourse point to some no longer valid address.
63
+ fread(m, 64, 1, fp); // first 64 bytes data
64
+ fread_int(&(m->width), fp);
65
+ fread_int(&(m->height), fp);
66
+ fread(header, 4, 1, fp); // skip the first pointer
67
+ fread_int(&(m->offset_x), fp);
68
+ fread_int(&(m->offset_y), fp);
69
+ fread(header, 4, 1, fp); // skip the second pointer
70
+ fread_int(&(m->start_x), fp);
71
+ fread_int(&(m->start_y), fp);
72
73
// read map data
74
m->dat = malloc(m->width * m->height * sizeof(Tmappos));
75
@@ -116,8 +163,8 @@ Tmap *load_map(char *fname) {
76
// loads one splendind map from memory
77
Tmap *load_map_from_memory(void *mem) {
78
Tmap *m;
79
- char header[6];
80
- char *c = (char *)mem;
81
+ unsigned char header[6];
82
+ unsigned char *c = (unsigned char *)mem;
83
84
85
// does the header match?
86
@@ -137,9 +184,19 @@ Tmap *load_map_from_memory(void *mem) {
87
}
88
89
// read datastruct
90
- // fread(m, sizeof(Tmap), 1, fp);
91
- memcpy(m, c, sizeof(Tmap));
92
- c += sizeof(Tmap);
93
+ // a mapfile contain a raw dump of the Tmap struct made on an i386
94
+ // the code below reads these struct dumps in an arch neutral manner
95
+ // Note this dumps contains pointers, these are not used because these
96
+ // ofcourse point to some no longer valid address.
97
+ memcpy(m, c, 64); c += 64; // first 64 bytes data
98
+ mem_to_int(&(m->width), c); c += 4;
99
+ mem_to_int(&(m->height), c); c += 4;
100
+ c += 4; // skip the first pointer
101
+ mem_to_int(&(m->offset_x), c); c += 4;
102
+ mem_to_int(&(m->offset_y), c); c += 4;
103
+ c += 4; // skip the second pointer
104
+ mem_to_int(&(m->start_x), c); c+= 4;
105
+ mem_to_int(&(m->start_y), c); c+= 4;
106
107
// read map data
108
m->dat = malloc(m->width * m->height * sizeof(Tmappos));
109
@@ -174,7 +231,18 @@ int save_map(Tmap *m, char *fname) {
110
fwrite(header, 6, 1, fp);
111
112
// write datastruct
113
- fwrite(m, sizeof(Tmap), 1, fp);
114
+ // a mapfile should contain a raw dump of the Tmap struct as made on an
115
+ // i386 the code below writes a struct dump as an i386 in an arch
116
+ // neutral manner
117
+ fwrite(m, 64, 1, fp); // first 64 bytes data
118
+ fwrite_int(&(m->width), fp);
119
+ fwrite_int(&(m->height), fp);
120
+ fwrite(header, 4, 1, fp); // skip the first pointer
121
+ fwrite_int(&(m->offset_x), fp);
122
+ fwrite_int(&(m->offset_y), fp);
123
+ fwrite(header, 4, 1, fp); // skip the second pointer
124
+ fwrite_int(&(m->start_x), fp);
125
+ fwrite_int(&(m->start_y), fp);
126
127
// write map data
128
fwrite(m->dat, sizeof(Tmappos), m->width * m->height, fp);
129
@@ -409,7 +477,7 @@ int is_ground(Tmap *sm , int x, int y) {
130
}
131
132
if (mask == 5 && oy > 31 - ox) return mask; // 45 degree slope /
133
- if (mask == 6 && oy > ox) return mask; // 45 degree slope \
134
+ if (mask == 6 && oy > ox) return mask; // 45 degree slope \ .
135
136
// the not so simple ones
137
if (mask == 3 && oy > 31 - ox / 2) return mask; // 22 degree slope / (low)
138
139