Path: blob/main/games/alex4/files/patch-map.c
16461 views
--- map.c.orig 2016-06-14 16:25:53 UTC1+++ map.c2@@ -24,6 +24,7 @@3#include <stdlib.h>4#include <stdio.h>5#include <string.h>6+#include <machine/endian.h>7#include "allegro.h"8#include "map.h"9#include "timer.h"10@@ -64,7 +65,41 @@ Tmap *create_map(int w, int h) {1112return m;13}14-15+16+static void mem_to_int(int *dest, unsigned char *mem)17+{18+#if __BYTE_ORDER == __LITTLE_ENDIAN19+ memcpy(dest, mem, 4);20+#else21+ *dest = mem[0] | (((int)mem[1]) << 8) | (((int)mem[2]) << 16) |22+ (((int)mem[3]) << 24);23+#endif24+}25+26+static void fread_int(int *dest, FILE *fp)27+{28+#if __BYTE_ORDER == __LITTLE_ENDIAN29+ fread(dest, 4, 1, fp);30+#else31+ unsigned char buf[4];32+ fread(buf, 1, 4, fp);33+ mem_to_int(dest, buf);34+#endif35+}36+37+static void fwrite_int(const int *src, FILE *fp)38+{39+#if __BYTE_ORDER == __LITTLE_ENDIAN40+ fwrite(src, 4, 1, fp);41+#else42+ unsigned char buf[4];43+ buf[0] = *src;44+ buf[1] = *src >> 8;45+ buf[2] = *src >> 16;46+ buf[3] = *src >> 24;47+ fwrite(buf, 1, 4, fp);48+#endif49+}5051// loads one splendind map from disk52Tmap *load_map(char *fname) {53@@ -93,7 +128,19 @@ Tmap *load_map(char *fname) {54}5556// read datastruct57- fread(m, sizeof(Tmap), 1, fp);58+ // a mapfile contain a raw dump of the Tmap struct made on an i38659+ // the code below reads these struct dumps in an arch neutral manner60+ // Note this dumps contains pointers, these are not used because these61+ // ofcourse point to some no longer valid address.62+ fread(m, 64, 1, fp); // first 64 bytes data63+ fread_int(&(m->width), fp);64+ fread_int(&(m->height), fp);65+ fread(header, 4, 1, fp); // skip the first pointer66+ fread_int(&(m->offset_x), fp);67+ fread_int(&(m->offset_y), fp);68+ fread(header, 4, 1, fp); // skip the second pointer69+ fread_int(&(m->start_x), fp);70+ fread_int(&(m->start_y), fp);7172// read map data73m->dat = malloc(m->width * m->height * sizeof(Tmappos));74@@ -116,8 +163,8 @@ Tmap *load_map(char *fname) {75// loads one splendind map from memory76Tmap *load_map_from_memory(void *mem) {77Tmap *m;78- char header[6];79- char *c = (char *)mem;80+ unsigned char header[6];81+ unsigned char *c = (unsigned char *)mem;828384// does the header match?85@@ -137,9 +184,19 @@ Tmap *load_map_from_memory(void *mem) {86}8788// read datastruct89- // fread(m, sizeof(Tmap), 1, fp);90- memcpy(m, c, sizeof(Tmap));91- c += sizeof(Tmap);92+ // a mapfile contain a raw dump of the Tmap struct made on an i38693+ // the code below reads these struct dumps in an arch neutral manner94+ // Note this dumps contains pointers, these are not used because these95+ // ofcourse point to some no longer valid address.96+ memcpy(m, c, 64); c += 64; // first 64 bytes data97+ mem_to_int(&(m->width), c); c += 4;98+ mem_to_int(&(m->height), c); c += 4;99+ c += 4; // skip the first pointer100+ mem_to_int(&(m->offset_x), c); c += 4;101+ mem_to_int(&(m->offset_y), c); c += 4;102+ c += 4; // skip the second pointer103+ mem_to_int(&(m->start_x), c); c+= 4;104+ mem_to_int(&(m->start_y), c); c+= 4;105106// read map data107m->dat = malloc(m->width * m->height * sizeof(Tmappos));108@@ -174,7 +231,18 @@ int save_map(Tmap *m, char *fname) {109fwrite(header, 6, 1, fp);110111// write datastruct112- fwrite(m, sizeof(Tmap), 1, fp);113+ // a mapfile should contain a raw dump of the Tmap struct as made on an114+ // i386 the code below writes a struct dump as an i386 in an arch115+ // neutral manner116+ fwrite(m, 64, 1, fp); // first 64 bytes data117+ fwrite_int(&(m->width), fp);118+ fwrite_int(&(m->height), fp);119+ fwrite(header, 4, 1, fp); // skip the first pointer120+ fwrite_int(&(m->offset_x), fp);121+ fwrite_int(&(m->offset_y), fp);122+ fwrite(header, 4, 1, fp); // skip the second pointer123+ fwrite_int(&(m->start_x), fp);124+ fwrite_int(&(m->start_y), fp);125126// write map data127fwrite(m->dat, sizeof(Tmappos), m->width * m->height, fp);128@@ -409,7 +477,7 @@ int is_ground(Tmap *sm , int x, int y) {129}130131if (mask == 5 && oy > 31 - ox) return mask; // 45 degree slope /132- if (mask == 6 && oy > ox) return mask; // 45 degree slope \133+ if (mask == 6 && oy > ox) return mask; // 45 degree slope \ .134135// the not so simple ones136if (mask == 3 && oy > 31 - ox / 2) return mask; // 22 degree slope / (low)137138139