From da79be9b6a4134aa9cd19e6f5909fedb20d318cb Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Tue, 10 Mar 2020 22:57:16 +0300 Subject: [PATCH] ppc: fix saves and more map loading improvements --- src/dots.c | 48 +++++++++--- src/files.c | 155 +++++++++++++++++++++------------------ src/fx.c | 40 +++++++--- src/game.c | 60 +++++++++++---- src/items.c | 178 ++++++++++++++++++++++++++++++--------------- src/misc.h | 6 +- src/miscc.c | 38 ++++++---- src/monster.c | 85 ++++++++++++++++++---- src/my.c | 38 +++++++++- src/my.h | 20 ++++- src/player.c | 112 ++++++++++++++++++++++++++-- src/smoke.c | 42 ++++++++--- src/switch.c | 75 +++++++++++++------ src/view.c | 198 +++++++++++++++++++++++++++++++------------------- src/weapons.c | 47 +++++++++--- 15 files changed, 817 insertions(+), 325 deletions(-) diff --git a/src/dots.c b/src/dots.c index d89b9d6..4f52130 100644 --- a/src/dots.c +++ b/src/dots.c @@ -58,19 +58,45 @@ static init_t bl_ini[MAXINI],sp_ini[MAXINI]; static int bl_r,sp_r,sr_r,sxr[MAXSR],syr[MAXSR]; static int ldot; -void DOT_savegame(FILE* h) { - int i,n; - - for(i=n=0;i= 0 && it[n].t == 0; n--) { + // empty + } + n += 1; + myfwrite32(n, h); + for (i = 0; i < n; i++) { + myfwrite32(it[i].o.x, h); + myfwrite32(it[i].o.y, h); + myfwrite32(it[i].o.xv, h); + myfwrite32(it[i].o.yv, h); + myfwrite32(it[i].o.vx, h); + myfwrite32(it[i].o.vy, h); + myfwrite32(it[i].o.r, h); + myfwrite32(it[i].o.h, h); + myfwrite32(it[i].t, h); + myfwrite32(it[i].s, h); + } + myfwrite32(itm_rtime, h); } -void IT_loadgame(FILE* h) { - int n; - - myfread(&n,1,4,h); - myfread(it,1,n*sizeof(it[0]),h); - myfread(&itm_rtime,1,4,h); +void IT_loadgame (FILE *h) { + int i, n; + myfread32(&n, h); + for (i = 0; i < n; i++) { + myfread32(&it[i].o.x, h); + myfread32(&it[i].o.y, h); + myfread32(&it[i].o.xv, h); + myfread32(&it[i].o.yv, h); + myfread32(&it[i].o.vx, h); + myfread32(&it[i].o.vy, h); + myfread32(&it[i].o.r, h); + myfread32(&it[i].o.h, h); + myfread32(&it[i].t, h); + myfread32(&it[i].s, h); + } + myfread32(&itm_rtime, h); } void IT_alloc(void) { @@ -112,56 +135,97 @@ void IT_init(void) { tsndtm=rsndtm=0; } -int IT_load(FILE* h) { - int m,i,j; +int IT_load (FILE *h) { + int m, i, j; old_thing_t t; - - switch(blk.t) { + switch (blk.t) { case MB_THING: - for(i=0;blk.sz>0;++i,blk.sz-=8) { - myfread(&t,1,sizeof(t),h); - t.x = short2host(t.x); - t.y = short2host(t.y); - t.t = short2host(t.t); - t.f = short2host(t.f); - it[i].o.x=t.x;it[i].o.y=t.y; - it[i].t=t.t;it[i].s=t.f; - if(!it[i].t) break; - if((it[i].s&THF_DM) && !g_dm) it[i].t=0; - }m=i; - for(i=0,j=-1;i 0; ++i, blk.sz -= 8) { + myfread16(&t.x, h); + myfread16(&t.y, h); + myfread16(&t.t, h); + myfread16(&t.f, h); + it[i].o.x = t.x; + it[i].o.y = t.y; + it[i].t = t.t; + it[i].s = t.f; + if (it[i].t && (it[i].s & THF_DM) && !g_dm) { + it[i].t=0; + } } - for(i=0,j=-1;i=TH_CLIP && it[i].t=I_KEYR && it[i].t<=I_KEYB) it[i].t|=0x8000; - }else if(it[i].t>=TH_DEMON) { - MN_spawn(it[i].o.x,it[i].o.y,it[i].s&THF_DIR,it[i].t-TH_DEMON+MN_DEMON); - it[i].t=0; - } + for (i = 0, j = 0; i < m; ++i) { + if (it[i].t == TH_DMSTART) { + if (g_dm) { + dm_pos[j].x = it[i].o.x; + dm_pos[j].y = it[i].o.y; + dm_pos[j].d = it[i].s & THF_DIR; + } + it[i].t = 0; + ++j; + } + } + if (g_dm && j < 2) { + ERR_fatal("Меньше 2-ух точек DM"); + } + if (g_dm) { + dm_pnum = j; + dm_pl1p = myrand(dm_pnum); + do { + dm_pl2p = myrand(dm_pnum); + } while (dm_pl2p == dm_pl1p); + } else { + dm_pl1p = 0; + dm_pl2p = 1; + dm_pnum = 2; + } + PL_spawn(&pl1, dm_pos[dm_pl1p].x, dm_pos[dm_pl1p].y, dm_pos[dm_pl1p].d); + if (_2pl) { + PL_spawn(&pl2, dm_pos[dm_pl2p].x, dm_pos[dm_pl2p].y, dm_pos[dm_pl2p].d); + } + for (i = 0; i < m; ++i) { + if (it[i].t >= TH_CLIP && it[i].t < TH_DEMON) { + it[i].s = 0; + it[i].t = it[i].t - TH_CLIP + I_CLIP; + if (it[i].t >= I_KEYR && it[i].t <= I_KEYB) { + it[i].t |= 0x8000; + } + } else if (it[i].t >= TH_DEMON) { + MN_spawn(it[i].o.x, it[i].o.y, it[i].s & THF_DIR, it[i].t - TH_DEMON + MN_DEMON); + it[i].t = 0; + } + } return 1; - }return 0; + } + return 0; } static void takesnd(int t) { diff --git a/src/misc.h b/src/misc.h index 60eaee5..3a96a5e 100644 --- a/src/misc.h +++ b/src/misc.h @@ -22,6 +22,8 @@ // Miscellaneous functions +#include + #define MAXDIST 2000000L enum{ @@ -83,5 +85,5 @@ int Z_getacid(int x,int y,int r,int h); void Z_clrst(); -unsigned short int short2host (unsigned short int x); -unsigned int int2host (unsigned int x); +uint16_t short2host (uint16_t x); +uint32_t int2host (uint32_t x); diff --git a/src/miscc.c b/src/miscc.c index 7123938..4f5a302 100644 --- a/src/miscc.c +++ b/src/miscc.c @@ -613,33 +613,43 @@ void Z_calc_time(dword t,word *h,word *m,word *s) *h = t; } -unsigned short int short2host (unsigned short int x) { -#if __BIG_ENDIAN__ +#define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0) + +uint16_t short2swap (uint16_t x) { union { - unsigned char a[2]; - unsigned short int x; + uint8_t a[2]; + uint16_t x; } y; y.x = x; - unsigned char t = y.a[0]; y.a[0] = y.a[1]; y.a[1] = t; + SWAP_VAR(y.a[0], y.a[1]); return y.x; -#else - return x; -#endif } -unsigned int int2host (unsigned int x) { -#if __BIG_ENDIAN__ +uint32_t int2swap (uint32_t x) { union { - unsigned char a[4]; - unsigned int x; + uint8_t a[4]; + uint32_t x; } y; y.x = x; - #define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0) SWAP_VAR(y.a[0], y.a[3]); SWAP_VAR(y.a[1], y.a[2]); - #undef SWAP_VAR return y.x; +} + +#undef SWAP_VAR + +uint16_t short2host (uint16_t x) { +#if __BIG_ENDIAN__ + return short2swap(x); #else return x; #endif } + +uint32_t int2host (uint32_t x) { +#if __BIG_ENDIAN__ + return int2swap(x); +#else + return x; +#endif +} \ No newline at end of file diff --git a/src/monster.c b/src/monster.c index 98b909e..b0d677f 100644 --- a/src/monster.c +++ b/src/monster.c @@ -137,27 +137,82 @@ static mnsz_t mnsz[MN_TN+1]={ 8, 26, 400, 70, 8,10, 30, 50 // man }; -void MN_savegame(FILE* h) { - int n; - - for(n=MAXMN;--n;) if(mn[n].t) break; - ++n;myfwrite(&n,1,4,h); - myfwrite(mn,1,n*sizeof(mn[0]),h); - myfwrite(&mnum,1,4,h);myfwrite(&gsndt,1,4,h); +void MN_savegame (FILE *h) { + int i, n; + for (n = MAXMN - 1; n >= 0 && mn[n].t == 0; n--) { + // empty + } + n += 1; + myfwrite32(n, h); + for (i = 0; i < n; i++) { + myfwrite32(mn[i].o.x, h); + myfwrite32(mn[i].o.y, h); + myfwrite32(mn[i].o.xv, h); + myfwrite32(mn[i].o.yv, h); + myfwrite32(mn[i].o.vx, h); + myfwrite32(mn[i].o.vy, h); + myfwrite32(mn[i].o.r, h); + myfwrite32(mn[i].o.h, h); + myfwrite8(mn[i].t, h); + myfwrite8(mn[i].d, h); + myfwrite8(mn[i].st, h); + myfwrite8(mn[i].ftime, h); + myfwrite32(mn[i].fobj, h); + myfwrite32(mn[i].s, h); + myfwrite32(mn[i].ap, h); // useless, changed after load + myfwrite32(mn[i].aim, h); + myfwrite32(mn[i].life, h); + myfwrite32(mn[i].pain, h); + myfwrite32(mn[i].ac, h); + myfwrite32(mn[i].tx, h); + myfwrite32(mn[i].ty, h); + myfwrite32(mn[i].ammo, h); + myfwrite16(mn[i].atm, h); + } + myfwrite32(mnum, h); + myfwrite32(gsndt, h); } static void setst(int,int); static int MN_hit(int n,int d,int o,int t); -void MN_loadgame(FILE* h) { - int n,c; - - myfread(&n,1,4,h); - myfread(mn,1,n*sizeof(mn[0]),h); - myfread(&mnum,1,4,h);myfread(&gsndt,1,4,h); - for(n=0;n +#include +#include void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext) { @@ -78,18 +80,46 @@ void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext) } } -size_t myfreadc(void *ptr, size_t size, size_t n, FILE *f) { +size_t myfreadc (void *ptr, size_t size, size_t n, FILE *f) { return fread(ptr, size, n, f); } -void myfread(void *ptr, size_t size, size_t n, FILE *f) { +void myfread (void *ptr, size_t size, size_t n, FILE *f) { if (myfreadc(ptr, size, n, f) != n) { ERR_fatal("File reading error\n"); } } -void myfwrite(void *ptr, size_t n, size_t size, FILE *f) { - size_t s = fwrite(ptr,n,size,f); +void myfread8 (uint8_t *x, FILE *f) { + myfread(x, 1, 1, f); +} + +void myfread16 (uint16_t *x, FILE *f) { + myfread(x, 2, 1, f); + *x = short2host(*x); +} + +void myfread32 (uint32_t *x, FILE *f) { + myfread(x, 4, 1, f); + *x = int2host(*x); +} + +void myfwrite (void *ptr, size_t size, size_t n, FILE *f) { + assert(fwrite(ptr, size, n, f) == n); +} + +void myfwrite8 (uint8_t x, FILE *f) { + myfwrite(&x, 1, 1, f); +} + +void myfwrite16 (uint16_t x, FILE *f) { + x = short2host(x); + myfwrite(&x, 2, 1, f); +} + +void myfwrite32 (uint32_t x, FILE *f) { + x = int2host(x); + myfwrite(&x, 4, 1, f); } void myrandomize(void) diff --git a/src/my.h b/src/my.h index 607cccd..37ba02b 100644 --- a/src/my.h +++ b/src/my.h @@ -23,7 +23,25 @@ extern "C" { #endif -int fexists(char * filename); +#include +#include + +void mysplitpath (const char *path, char *drv, char *dir, char *name, char *ext); + +size_t myfreadc(void *ptr, size_t size, size_t n, FILE *f); + +void myfread (void *ptr, size_t size, size_t n, FILE *f); +void myfread8 (uint8_t *x, FILE *f); +void myfread16 (uint16_t *x, FILE *f); +void myfread32 (uint32_t *x, FILE *f); + +void myfwrite(void *ptr, size_t size, size_t n, FILE *f); +void myfwrite8 (uint8_t x, FILE *f); +void myfwrite16 (uint16_t x, FILE *f); +void myfwrite32 (uint32_t x, FILE *f); + +void myrandomize (void); +int fexists (char *filename); #ifdef __cplusplus } diff --git a/src/player.c b/src/player.c index fd1080a..5a8a80e 100644 --- a/src/player.c +++ b/src/player.c @@ -76,16 +76,112 @@ struct { } _keys; #pragma pack() -void PL_savegame(FILE* h) { - myfwrite(&pl1,1,sizeof(pl1)-sizeof(_keys),h);//myfwrite(&pl1,1,sizeof(pl1),h); - if(_2pl) myfwrite(&pl2,1,sizeof(pl2)-sizeof(_keys),h);//myfwrite(&pl2,1,sizeof(pl2),h); - myfwrite(&PL_JUMP,1,4,h);myfwrite(&PL_RUN,1,4,h);myfwrite(&p_immortal,1,1,h); +void PL_save_player (player_t *p, FILE *h) { + myfwrite32(p->o.x, h); + myfwrite32(p->o.y, h); + myfwrite32(p->o.xv, h); + myfwrite32(p->o.yv, h); + myfwrite32(p->o.vx, h); + myfwrite32(p->o.vy, h); + myfwrite32(p->o.r, h); + myfwrite32(p->o.h, h); + myfwrite32(p->looky, h); + myfwrite32(p->st, h); + myfwrite32(p->s, h); + myfwrite32(p->life, h); + myfwrite32(p->armor, h); + myfwrite32(p->hit, h); + myfwrite32(p->hito, h); + myfwrite32(p->pain, h); + myfwrite32(p->air, h); + myfwrite32(p->invl, h); + myfwrite32(p->suit, h); + myfwrite8(p->d, h); + myfwrite32(p->frag, h); + myfwrite32(p->ammo, h); + myfwrite32(p->shel, h); + myfwrite32(p->rock, h); + myfwrite32(p->cell, h); + myfwrite32(p->fuel, h); + myfwrite32(p->kills, h); + myfwrite32(p->secrets, h); + myfwrite8(p->fire, h); + myfwrite8(p->cwpn, h); + myfwrite8(p->csnd, h); + myfwrite8(p->amul, h); + myfwrite16(p->wpns, h); + myfwrite8(p->wpn, h); + myfwrite8(p->f, h); + myfwrite8(p->drawst, h); + myfwrite8(p->color, h); + myfwrite32(p->id, h); + myfwrite8(p->keys, h); + myfwrite8(p->lives, h); + // k* not saved } -void PL_loadgame(FILE* h) { - myfread(&pl1,1,sizeof(pl1)-sizeof(_keys),h);//myfread(&pl1,1,sizeof(pl1),h); - if(_2pl) myfread(&pl2,1,sizeof(pl2)-sizeof(_keys),h);//myfread(&pl2,1,sizeof(pl2),h); - myfread(&PL_JUMP,1,4,h);myfread(&PL_RUN,1,4,h);myfread(&p_immortal,1,1,h); +void PL_savegame (FILE *h) { + PL_save_player(&pl1, h); + if (_2pl) { + PL_save_player(&pl2, h); + } + myfwrite32(PL_JUMP, h); + myfwrite32(PL_RUN, h); + myfwrite8(p_immortal, h); +} + +void PL_load_player (player_t *p, FILE *h) { + myfread32(&p->o.x, h); + myfread32(&p->o.y, h); + myfread32(&p->o.xv, h); + myfread32(&p->o.yv, h); + myfread32(&p->o.vx, h); + myfread32(&p->o.vy, h); + myfread32(&p->o.r, h); + myfread32(&p->o.h, h); + myfread32(&p->looky, h); + myfread32(&p->st, h); + myfread32(&p->s, h); + myfread32(&p->life, h); + myfread32(&p->armor, h); + myfread32(&p->hit, h); + myfread32(&p->hito, h); + myfread32(&p->pain, h); + myfread32(&p->air, h); + myfread32(&p->invl, h); + myfread32(&p->suit, h); + myfread8(&p->d, h); + myfread32(&p->frag, h); + myfread32(&p->ammo, h); + myfread32(&p->shel, h); + myfread32(&p->rock, h); + myfread32(&p->cell, h); + myfread32(&p->fuel, h); + myfread32(&p->kills, h); + myfread32(&p->secrets, h); + myfread8(&p->fire, h); + myfread8(&p->cwpn, h); + myfread8(&p->csnd, h); + myfread8(&p->amul, h); + myfread16(&p->wpns, h); + myfread8(&p->wpn, h); + myfread8(&p->f, h); + myfread8(&p->drawst, h); + myfread8(&p->color, h); + myfread32(&p->id, h); + myfread8(&p->keys, h); + myfread8(&p->lives, h); + // k* not saved +} + +void PL_loadgame (FILE *h) { + PL_load_player(&pl1, h); + if (_2pl) { + PL_load_player(&pl2, h); + } + myfread32(&PL_JUMP, h); + myfread32(&PL_RUN, h); + myfread8(&p_immortal, h); } static int nonz(int a) {return (a)?a:1;} diff --git a/src/smoke.c b/src/smoke.c index 4ec8117..6b01261 100644 --- a/src/smoke.c +++ b/src/smoke.c @@ -53,19 +53,39 @@ byte flametab[16]={ 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0 }; -void SMK_savegame(FILE* h) { - int i,n; - - for(i=n=0;i= 0 && sw[n].t == 0; n--) { + // empty + } + n += 1; + myfwrite32(n, h); + for (i = 0; i < n; i++) { + myfwrite8(sw[i].x, h); + myfwrite8(sw[i].y, h); + myfwrite8(sw[i].t, h); + myfwrite8(sw[i].tm, h); + myfwrite8(sw[i].a, h); + myfwrite8(sw[i].b, h); + myfwrite8(sw[i].c, h); + myfwrite8(sw[i].d, h); + myfwrite8(sw[i].f, h); + } + myfwrite32(sw_secrets, h); } -void SW_loadgame(FILE* h) { - int n; - - myfread(&n,1,4,h);myfread(sw,1,n*sizeof(sw[0]),h); - myfread(&sw_secrets,1,4,h); +void SW_loadgame (FILE *h) { + int i, n; + myfread32(&n, h); + for (i = 0; i < n; i++) { + myfread8(&sw[i].x, h); + myfread8(&sw[i].y, h); + myfread8(&sw[i].t, h); + myfread8(&sw[i].tm, h); + myfread8(&sw[i].a, h); + myfread8(&sw[i].b, h); + myfread8(&sw[i].c, h); + myfread8(&sw[i].d, h); + myfread8(&sw[i].f, h); + } + myfread32(&sw_secrets, h); } -int SW_load(FILE* h) { +int SW_load (FILE *h) { int i; - switch(blk.t) { case MB_SWITCH2: - sw_secrets=0; - for(i=0;i0;++i,blk.sz-=sizeof(sw_t)) { - myfread(sw+i,1,sizeof(sw_t),h); - sw[i].c = short2host(sw[i].c); - sw[i].tm=0;sw[i].d=0; - sw[i].f|=0x80; - if(sw[i].t==SW_SECRET) ++sw_secrets; + sw_secrets = 0; + for (i = 0; i < MAXSW && blk.sz > 0; ++i, blk.sz -= 9) { + myfread8(&sw[i].x, h); + myfread8(&sw[i].y, h); + myfread8(&sw[i].t, h); + myfread8(&sw[i].tm, h); // unused + myfread8(&sw[i].a, h); + myfread8(&sw[i].b, h); + myfread8(&sw[i].c, h); + myfread8(&sw[i].d, h); // unused + myfread8(&sw[i].f, h); + sw[i].tm = 0; + sw[i].d = 0; + sw[i].f |= 0x80; + if (sw[i].t == SW_SECRET) { + ++sw_secrets; + } } return 1; - }return 0; + } + return 0; } void SW_alloc(void) { diff --git a/src/view.c b/src/view.c index 3bf3677..034b0aa 100644 --- a/src/view.c +++ b/src/view.c @@ -95,43 +95,59 @@ static short getani(char *n) { void W_savegame(FILE* h) { char s[8]; int i; - - myfwrite(&sky_type,1,4,h); - for(i=1;i<256;++i) { - getname(i,s);myfwrite(s,1,8,h); + myfwrite32(sky_type, h); + for(i = 1; i < 256; ++i) { + getname(i, s); + myfwrite(s, 8, 1, h); + } + for (i = 0; i < 256; i++) { + myfwrite32(walf[i], h); + } + for (i = 0; i < 256; i++) { + myfwrite8(walswp[i], h); } - myfwrite(walf,1,sizeof(walf),h); - myfwrite(walswp,1,sizeof(walswp),h); - myfwrite(fldb,1,FLDW*FLDH,h); - myfwrite(fld,1,FLDW*FLDH,h); - myfwrite(fldf,1,FLDW*FLDH,h); + myfwrite(fldb, FLDW*FLDH, 1, h); + myfwrite(fld, FLDW*FLDH, 1, h); + myfwrite(fldf, FLDW*FLDH, 1, h); } void W_loadgame(FILE* h) { char s[8]; int i; - - myfread(&sky_type,1,4,h); - for(i=1;i<256;++i) { + myfread32(&sky_type, h); + for (i = 1; i < 256; ++i) { walani[i]=0; - myfread(s,1,8,h);if(!s[0]) {walh[i]=-1;walp[i]=NULL;continue;} - walani[i]=getani(s); - if(strncasecmp(s,"_WATER_",7)==0) { //if(memicmp(s,"_WATER_",7)==0) { - walh[i]=-2;walp[i]=(void*)(s[7]-'0'+1); + myfread(s, 8, 1, h); + if (!s[0]) { + walh[i] = -1; + walp[i] = NULL; } else { - walh[i]=F_getresid(s); - walp[i]=V_getvgaimg(walh[i]); + walani[i] = getani(s); + if (strncasecmp(s, "_WATER_", 7) == 0) { + walh[i] = -2; + walp[i] = (void*)(s[7] - '0' + 1); + } else { + walh[i] = F_getresid(s); + walp[i] = V_getvgaimg(walh[i]); + } } } - myfread(walf,1,sizeof(walf),h); - for(i=1;i<256;++i) if(walf[i]&1) walh[i]|=0x8000; - myfread(walswp,1,sizeof(walswp),h); - myfread(fldb,1,FLDW*FLDH,h); - myfread(fld,1,FLDW*FLDH,h); - myfread(fldf,1,FLDW*FLDH,h); - strcpy(s,"RSKY1");s[4]=sky_type+'0'; + for (i = 0; i < 256; i++) { + myfread32(&walf[i], h); + if (i > 0 && walf[i] & 1) { + walh[i] |= 0x8000; + } + } + for (i = 0; i < 256; i++) { + myfread8(&walswp[i], h); + } + myfread(fldb, FLDW*FLDH, 1, h); + myfread(fld, FLDW*FLDH, 1, h); + myfread(fldf, FLDW*FLDH, 1, h); + strcpy(s, "RSKY1"); + s[4] = '0' + sky_type; M_unlock(horiz); - horiz=V_loadvgaimg(s); + horiz = V_loadvgaimg(s); } void W_adjust(void) { @@ -258,59 +274,93 @@ static void unpack(void *buf, int len, void *obuf) { } } -int W_load(FILE* h) { - int i,j,k,g; - static wall_t w; - void *p,*buf; - - switch(blk.t) { - case MB_WALLNAMES: - for(i=0;i<256;++i) {walh[i]=-1;walswp[i]=i;walani[i]=0;} - for(i=1;i<256 && blk.sz>0;++i,blk.sz-=sizeof(w)) { - myfread(&w,1,sizeof(w),h); - - if(strncasecmp(w.n,"_WATER_",7)==0) //if(memicmp(w.n,"_WATER_",7)==0) - {walp[i]=(void*)(w.n[7]-'0'+1);walh[i]=-2;continue;} - - walh[i]=F_getresid(w.n); - walp[i]=V_getvgaimg(walh[i]); - if(w.n[0]=='S' && w.n[1]=='W' && w.n[4]=='_') walswp[i]=0; - walf[i]=(w.t)?1:0;if(w.t) walh[i]|=0x8000; - - if(strncasecmp(w.n,"VTRAP01",8)==0) walf[i]|=2; //if(memicmp(w.n,"VTRAP01",8)==0) walf[i]|=2; - walani[i]=getani(w.n); - } - for(j=i,i=1;i<256;++i) if(walswp[i]==0) { - if(j>=256) break; - F_getresname(w.n,walh[i]&0x7FFF); - w.n[5]^=1; - g=F_getresid(w.n)|(walh[i]&0x8000); - for(k=1;k<256;++k) if(walh[k]==g) break; - if(k>=256) { - walh[k=j++]=g;walp[k]=V_getvgaimg(g); - walf[k]=(g&0x8000)?1:0; - } - walswp[i]=k;walswp[k]=i; +int W_load (FILE *h) { + int i, j, k, g; + void *p, *buf; + wall_t w; + switch (blk.t) { + case MB_WALLNAMES: + for (i = 0; i < 256; ++i) { + walh[i] = -1; + walswp[i] = i; + walani[i] = 0; + } + for (i = 1; i < 256 && blk.sz > 0; ++i, blk.sz -= 9) { + myfread(w.n, 8, 1, h); + myfread8(&w.t, h); + if (strncasecmp(w.n, "_WATER_", 7) == 0) { + walp[i] = (void*)(w.n[7] - '0' + 1); + walh[i] = -2; + } else { + walh[i] = F_getresid(w.n); + walp[i] = V_getvgaimg(walh[i]); + if (w.n[0] == 'S' && w.n[1] == 'W' && w.n[4] == '_') { + walswp[i] = 0; + } + walf[i] = w.t ? 1 : 0; + if (w.t) { + walh[i] |= 0x8000; + } + if (strncasecmp(w.n, "VTRAP01", 8) == 0) { + walf[i] |= 2; + } + walani[i] = getani(w.n); + } } + for (j = i, i = 1; i < 256 && j < 256; ++i) { + if (walswp[i] == 0) { + F_getresname(w.n, walh[i] & 0x7FFF); + w.n[5] ^= 1; + g = F_getresid(w.n) | (walh[i] & 0x8000); + k = 1; + while (k < 256 && walh[k] != g) { + k += 1; + } + if(k >= 256) { + k = j; + j += 1; + walh[k] = g; + walp[k] = V_getvgaimg(g); + walf[k] = g & 0x8000 ? 1 : 0; + } + walswp[i] = k; + walswp[k] = i; + } + } return 1; - case MB_BACK: p=fldb;goto unp; - case MB_WTYPE: p=fld;goto unp; - case MB_FRONT: p=fldf; - unp: switch(blk.st) { - case 0: myfread(p,1,FLDW*FLDH,h);break; + case MB_BACK: + p = fldb; + goto unp; + case MB_WTYPE: + p = fld; + goto unp; + case MB_FRONT: + p = fldf; + unp: + switch (blk.st) { + case 0: + myfread(p, FLDW * FLDH, 1, h); + break; case 1: - if(!(buf=malloc(blk.sz))) + buf = malloc(blk.sz); + if(buf == NULL) { ERR_fatal("Не хватает памяти"); - myfread(buf,1,blk.sz,h); - unpack(buf,blk.sz,p);free(buf);break; - default: return 0; - }return 1; + } + myfread(buf, blk.sz, 1, h); + unpack(buf, blk.sz, p); + free(buf); + break; + default: + return 0; + } + return 1; case MB_SKY: - sky_type=0;myfread(&sky_type,2,1,h); - sky_type = short2host(sky_type); - strcpy(w.n,"RSKY1");w.n[4]=sky_type+'0'; + myfread16(&sky_type, h); + strcpy(w.n, "RSKY1"); + w.n[4] = '0' + sky_type; M_unlock(horiz); - horiz=V_loadvgaimg(w.n); + horiz = V_loadvgaimg(w.n); return 1; - }return 0; + } + return 0; } diff --git a/src/weapons.c b/src/weapons.c index b778a0e..4a5a063 100644 --- a/src/weapons.c +++ b/src/weapons.c @@ -51,17 +51,46 @@ static weapon_t wp[MAXWPN]; static void throw(int,int,int,int,int,int,int,int); -void WP_savegame(FILE* h) { - int n; - - for(n=MAXWPN;--n;) if(wp[n].t) break; - ++n;myfwrite(&n,1,4,h);myfwrite(wp,1,n*sizeof(wp[0]),h); +void WP_savegame (FILE *h) { + int i, n; + for (n = MAXWPN - 1; n >= 0 && wp[n].t == 0; n--) { + // empty + } + n += 1; + myfwrite32(n, h); + for (i = 0; i < n; i++) { + myfwrite32(wp[i].o.x, h); + myfwrite32(wp[i].o.y, h); + myfwrite32(wp[i].o.xv, h); + myfwrite32(wp[i].o.yv, h); + myfwrite32(wp[i].o.vx, h); + myfwrite32(wp[i].o.vy, h); + myfwrite32(wp[i].o.r, h); + myfwrite32(wp[i].o.h, h); + myfwrite8(wp[i].t, h); + myfwrite8(wp[i].s, h); + myfwrite32(wp[i].own, h); + myfwrite16(wp[i].target, h); + } } -void WP_loadgame(FILE* h) { - int n; - - myfread(&n,1,4,h);myfread(wp,1,n*sizeof(wp[0]),h); +void WP_loadgame (FILE *h) { + int i, n; + myfread32(&n, h); + for (i = 0; i < n; i++) { + myfread32(&wp[i].o.x, h); + myfread32(&wp[i].o.y, h); + myfread32(&wp[i].o.xv, h); + myfread32(&wp[i].o.yv, h); + myfread32(&wp[i].o.vx, h); + myfread32(&wp[i].o.vy, h); + myfread32(&wp[i].o.r, h); + myfread32(&wp[i].o.h, h); + myfread8(&wp[i].t, h); + myfread8(&wp[i].s, h); + myfread32(&wp[i].own, h); + myfread16(&wp[i].target, h); + } } void WP_alloc(void) { -- 2.29.2