00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "mech.h"
00016 #include "create.h"
00017
00018 #define CHELO(a,b,c,d) if ((tmp=fread(a,b,c,d)) != c) { fprintf (stderr, "Error loading mapdynamic for #%d - couldn't find enough entries! (found: %d, should: %d)\n", map->mynum, tmp, c); fflush(stderr); exit(1); }
00019 #define CHESA(a,b,c,d) if ((tmp=fwrite(a,b,c,d)) != c) { fprintf (stderr, "Error writing mapdynamic for #%d - couldn't find enough entries! (found: %d, should: %d)\n", map->mynum, tmp, c); fflush(stderr); exit(1); }
00020
00021 #define realnum(x) ((x) / 4 + ((x) % 4 ? 1 : 0))
00022 #define boffs(x) (2 * ((x) % 4))
00023 #define boffsbit(x,n) ((1<<boffs(x))*n)
00024 #define btsetbit(arr,x,y,n) \
00025 create_if_neccessary(arr,map,y);arr[y][realnum(x)] |= boffsbit(x,n)
00026 #define btunsetbit(arr,x,y,n) if (arr[y]) arr[y][realnum(x)] &= ~(boffsbit(x,n))
00027 #define btissetbit(arr,x,y,n) (arr[y][realnum(x)] & boffsbit(x,n))
00028
00029 #define BIT_MINE 1
00030 #define BIT_HANGAR 2
00031
00032
00033
00034
00035
00036 static void create_if_neccessary(unsigned char **foo, MAP * map, int y)
00037 {
00038 int xs = map->map_width;
00039
00040 if(!foo[y])
00041 Create(foo[y], unsigned char, realnum(xs));
00042 }
00043
00044
00045 void map_load_bits(FILE * f, MAP * map)
00046 {
00047 int xs = map->map_width;
00048 int ys = map->map_height;
00049 unsigned char **foo;
00050 int tmp, i;
00051
00052 Create(foo, unsigned char *, ys);
00053 CHELO(foo, sizeof(unsigned char *), ys, f);
00054
00055 for(i = 0; i < ys; i++)
00056 if(foo[i]) {
00057 Create(foo[i], unsigned char, realnum(xs));
00058 CHELO(foo[i], sizeof(unsigned char), realnum(xs), f);
00059 }
00060 }
00061
00062 void map_save_bits(FILE * f, MAP * map, mapobj * obj)
00063 {
00064 int tmp;
00065 int i, j, c, tc = 0;
00066 unsigned char **foo;
00067 int xs = map->map_width;
00068 int ys = map->map_height;
00069 unsigned char tmpb;
00070
00071 #define outbyte(a) tmpb=(a);fwrite(&tmpb, 1, 1, f);
00072 foo = (unsigned char **) ((void *) obj->datai);
00073
00074 for(i = 0; i < ys; i++) {
00075 c = 0;
00076 if(foo[i]) {
00077 for(j = 0; j < realnum(xs); j++)
00078 if(foo[i][j])
00079 c++;
00080 if(!c) {
00081 free((void *) foo[i]);
00082 foo[i] = NULL;
00083 } else
00084 tc += c;
00085 }
00086 }
00087 if(!tc) {
00088
00089
00090
00091
00092 return;
00093 }
00094 outbyte(TYPE_BITS + 1);
00095 CHESA(foo, sizeof(unsigned char *), ys, f);
00096
00097 for(i = 0; i < ys; i++)
00098 if(foo[i])
00099 CHESA(foo[i], sizeof(unsigned char), realnum(xs), f);
00100 }
00101
00102
00103
00104
00105
00106
00107 static unsigned char **grab_us_an_array(MAP * map)
00108 {
00109 unsigned char **foo;
00110 mapobj foob;
00111 int ys = map->map_height;
00112
00113 if(!map->mapobj[TYPE_BITS]) {
00114 Create(foo, unsigned char *, ys);
00115
00116 foob.datai = (int) ((void *) foo);
00117 add_mapobj(map, &map->mapobj[TYPE_BITS], &foob, 0);
00118 } else
00119 foo = (unsigned char **) ((void *) map->mapobj[TYPE_BITS]->datai);
00120 return foo;
00121 }
00122
00123 void set_hex_enterable(MAP * map, int x, int y)
00124 {
00125 unsigned char **foo;
00126
00127 foo = grab_us_an_array(map);
00128 btsetbit(foo, x, y, BIT_HANGAR);
00129 }
00130
00131 void set_hex_mine(MAP * map, int x, int y)
00132 {
00133 unsigned char **foo;
00134
00135 foo = grab_us_an_array(map);
00136 btsetbit(foo, x, y, BIT_MINE);
00137 }
00138
00139 void unset_hex_enterable(MAP * map, int x, int y)
00140 {
00141 unsigned char **foo;
00142
00143 foo = grab_us_an_array(map);
00144 btunsetbit(foo, x, y, BIT_HANGAR);
00145 }
00146
00147 void unset_hex_mine(MAP * map, int x, int y)
00148 {
00149 unsigned char **foo;
00150
00151 foo = grab_us_an_array(map);
00152 btunsetbit(foo, x, y, BIT_MINE);
00153 }
00154
00155 int is_mine_hex(MAP * map, int x, int y)
00156 {
00157 unsigned char **foo;
00158
00159 if(!map)
00160 return 0;
00161 if(!map->mapobj[TYPE_BITS])
00162 return 0;
00163 foo = grab_us_an_array(map);
00164 if(!foo[y])
00165 return 0;
00166 return (btissetbit(foo, x, y, BIT_MINE));
00167 }
00168
00169 int is_hangar_hex(MAP * map, int x, int y)
00170 {
00171 unsigned char **foo;
00172
00173 if(!map)
00174 return 0;
00175 if(!map->mapobj[TYPE_BITS])
00176 return 0;
00177 foo = grab_us_an_array(map);
00178 if(!foo[y])
00179 return 0;
00180 return (btissetbit(foo, x, y, BIT_HANGAR));
00181 }
00182
00183 void clear_hex_bits(MAP * map, int bits)
00184 {
00185 int xs = map->map_width;
00186 int ys = map->map_height;
00187 int i, j;
00188 unsigned char **foo;
00189
00190 if(!map->mapobj[TYPE_BITS])
00191 return;
00192 foo = grab_us_an_array(map);
00193 for(i = 0; i < ys; i++)
00194 if(foo[i])
00195 for(j = 0; j < xs; j++) {
00196 switch (bits) {
00197 case 1:
00198 case 2:
00199 if(btissetbit(foo, j, i, bits))
00200 btunsetbit(foo, j, i, bits);
00201 break;
00202 case 0:
00203 if(btissetbit(foo, j, i, 1))
00204 btunsetbit(foo, j, i, 1);
00205 if(btissetbit(foo, j, i, 2))
00206 btunsetbit(foo, j, i, 2);
00207 break;
00208 }
00209 }
00210 }
00211
00212 int bit_size(MAP * map)
00213 {
00214 int xs = map->map_width;
00215 int ys = map->map_height;
00216 int i, s = 0;
00217 unsigned char **foo;
00218
00219 if(!map->mapobj[TYPE_BITS])
00220 return 0;
00221 foo = grab_us_an_array(map);
00222 for(i = 0; i < ys; i++)
00223 if(foo[i])
00224 s += realnum(xs);
00225 return s;
00226 }