From: DeaDDooMER Date: Tue, 21 Apr 2020 11:09:08 +0000 (+0400) Subject: save: move save code to separated file X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=563dffaf02ff36da4054f937fcc9f99a072e15bf;p=flatwaifu.git save: move save code to separated file --- diff --git a/src/dots.c b/src/dots.c index b9ca8ec..cd89a2b 100644 --- a/src/dots.c +++ b/src/dots.c @@ -51,47 +51,6 @@ 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 < MAXDOT; ++i) { - if (dot[i].t) { - ++n; - } - } - myfwrite32(n, h); - for (i = 0; i < MAXDOT; ++i) { - if (dot[i].t) { - myfwrite32(dot[i].o.x, h); - myfwrite32(dot[i].o.y, h); - myfwrite32(dot[i].o.xv, h); - myfwrite32(dot[i].o.yv, h); - myfwrite32(dot[i].o.vx, h); - myfwrite32(dot[i].o.vy, h); - myfwrite32(dot[i].o.r, h); - myfwrite32(dot[i].o.h, h); - myfwrite8(dot[i].c, h); - myfwrite8(dot[i].t, h); - } - } -} - -void DOT_loadgame (FILE *h) { - int i, n; - n = myfread32(h); - for (i = 0; i < n; i++) { - dot[i].o.x = myfread32(h); - dot[i].o.y = myfread32(h); - dot[i].o.xv = myfread32(h); - dot[i].o.yv = myfread32(h); - dot[i].o.vx = myfread32(h); - dot[i].o.vy = myfread32(h); - dot[i].o.r = myfread32(h); - dot[i].o.h = myfread32(h); - dot[i].c = myfread8(h); - dot[i].t = myfread8(h); - } -} - void DOT_init(void) { int i; diff --git a/src/dots.h b/src/dots.h index c3a930a..ae40ce5 100644 --- a/src/dots.h +++ b/src/dots.h @@ -24,7 +24,6 @@ #include "glob.h" #include "view.h" // obj_t -#include // FILE #define MAXDOT 400 @@ -35,8 +34,6 @@ typedef struct { extern dot_t dot[MAXDOT]; -void DOT_savegame (FILE *h); -void DOT_loadgame (FILE *h); void DOT_init (void); void DOT_alloc (void); void DOT_act (void); diff --git a/src/files.c b/src/files.c index dc897ed..47aaa71 100644 --- a/src/files.c +++ b/src/files.c @@ -46,8 +46,6 @@ typedef struct { } dmv; int d_start, d_end; -byte savname[7][24]; -byte savok[7]; mwad_t wad[MAX_WAD]; map_block_t blk; @@ -75,87 +73,6 @@ void F_startup (void) { memset(wads,0,sizeof(wads)); } -static char *getsavfpname (int n, int ro) { - static char fn[]="savgame0.dat"; - fn[7]=n+'0'; -#ifndef WIN32 - static char p[100]; - char *e = getenv("HOME"); - strncpy(p,e,60); - strcat(p,"/.doom2d-rembo"); - if (!ro) mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - strcat(p,"/"); - strcat(p,fn); -#else - strcpy(p,fn); -#endif - return p; -} - -void F_getsavnames (void) { - int i; - FILE *h; - short ver; - char *p; - for (i = 0; i < 7; ++i) { - p = getsavfpname(i, 1); - memset(savname[i], 0, 24); - savok[i] = 0; - h = fopen(p, "rb"); - if (h != NULL) { - ver = -1; - myfread(savname[i], 24, 1, h); - ver = myfread16(h); - savname[i][23] = 0; - savok[i] = (ver == 3) ? 1 : 0; - fclose(h); - } - } -} - -void F_savegame (int n, char *s) { - char *p = getsavfpname(n, 0); - FILE *h = fopen(p, "wb"); - if (h != NULL) { - myfwrite(s, 24, 1, h); // slot name - myfwrite16(3, h); // version - G_savegame(h); - W_savegame(h); - DOT_savegame(h); - SMK_savegame(h); - FX_savegame(h); - IT_savegame(h); - MN_savegame(h); - PL_savegame(h); - SW_savegame(h); - WP_savegame(h); - fclose(h); - } -} - -void F_loadgame (int n) { - short ver; - char *p = getsavfpname(n, 1); - FILE *h = fopen(p, "rb"); - if (h != NULL) { - fseek(h, 24, SEEK_SET); // skip name - ver = myfread16(h); // version - if (ver == 3) { - G_loadgame(h); - W_loadgame(h); - DOT_loadgame(h); - SMK_loadgame(h); - FX_loadgame(h); - IT_loadgame(h); - MN_loadgame(h); - PL_loadgame(h); - SW_loadgame(h); - WP_loadgame(h); - } - fclose(h); - } -} - void F_addwad (const char *fn) { int i; diff --git a/src/files.h b/src/files.h index 01f2087..ee1b8bb 100644 --- a/src/files.h +++ b/src/files.h @@ -43,15 +43,10 @@ typedef struct { } mwad_t; extern int d_start, d_end; -extern byte savname[7][24]; -extern byte savok[7]; extern mwad_t wad[MAX_WAD]; extern map_block_t blk; void F_startup (void); -void F_getsavnames (void); -void F_savegame (int n, char *s); -void F_loadgame (int n); void F_addwad (const char *fn); void F_initwads (void); void F_allocres (void); diff --git a/src/fx.c b/src/fx.c index cea21f8..252eb8a 100644 --- a/src/fx.c +++ b/src/fx.c @@ -97,39 +97,6 @@ static void init_fx1sin(void) { } } -void FX_savegame (FILE *h) { - int i, n; - for (i = n = 0; i < MAXFX; ++i) { - if (fx[i].t) { - ++n; - } - } - myfwrite32(n, h); - for (i = 0; i < MAXFX; ++i) { - if (fx[i].t) { - myfwrite32(fx[i].x, h); - myfwrite32(fx[i].y, h); - myfwrite32(fx[i].xv, h); - myfwrite32(fx[i].yv, h); - myfwrite8(fx[i].t, h); - myfwrite8(fx[i].s, h); - } - } -} - -void FX_loadgame (FILE *h) { - int i, n; - n = myfread32(h); - for (i = 0; i < n; i++) { - fx[i].x = myfread32(h); - fx[i].y = myfread32(h); - fx[i].xv = myfread32(h); - fx[i].yv = myfread32(h); - fx[i].t = myfread8(h); - fx[i].s = myfread8(h); - } -} - void FX_alloc (void) { bsnd[0]=Z_getsnd("BUBL1"); bsnd[1]=Z_getsnd("BUBL2"); diff --git a/src/fx.h b/src/fx.h index 2ec7ff4..f86ca9d 100644 --- a/src/fx.h +++ b/src/fx.h @@ -22,8 +22,6 @@ #ifndef FX_H_INLUDED #define FX_H_INLUDED -#include // FILE - #define MAXFX 300 typedef struct { @@ -33,8 +31,6 @@ typedef struct { extern fx_t fx[MAXFX]; -void FX_savegame (FILE *h); -void FX_loadgame (FILE *h); void FX_alloc (void); void FX_init (void); void FX_act (void); diff --git a/src/game.c b/src/game.c index eba57b8..3ba9f41 100644 --- a/src/game.c +++ b/src/game.c @@ -48,6 +48,7 @@ #include "error.h" #include "input.h" +#include "save.h" #define LT_DELAY 8 #define LT_HITTIME 6 @@ -94,47 +95,6 @@ static void set_trans(int st) { g_trans=1;g_transt=0; } -void G_savegame (FILE* h) { - myfwrite8(_2pl, h); - myfwrite8(g_dm, h); - myfwrite8(g_exit, h); - myfwrite8(g_map, h); - myfwrite32(g_time, h); - myfwrite32(dm_pl1p, h); - myfwrite32(dm_pl2p, h); - myfwrite32(dm_pnum, h); - int i = 0; - while (i < dm_pnum) { - myfwrite32(dm_pos[i].x, h); - myfwrite32(dm_pos[i].y, h); - myfwrite8(dm_pos[i].d, h); - i += 1; - } - myfwrite8(cheat, h); - myfwrite(g_music, 8, 1, h); -} - -void G_loadgame (FILE* h) { - _2pl = myfread8(h); - g_dm = myfread8(h); - g_exit = myfread8(h); - g_map = myfread8(h); - g_time = myfread32(h); - dm_pl1p = myfread32(h); - dm_pl2p = myfread32(h); - dm_pnum = myfread32(h); - int i = 0; - while (i < dm_pnum) { - dm_pos[i].x = myfread32(h); - dm_pos[i].y = myfread32(h); - dm_pos[i].d = myfread8(h); - i += 1; - } - cheat = myfread8(h); - myfread(g_music, 8, 1, h); - F_loadmus(g_music); -} - int G_load (FILE *h) { switch (blk.t) { case MB_MUSIC: diff --git a/src/game.h b/src/game.h index 03aff95..f4f1a70 100644 --- a/src/game.h +++ b/src/game.h @@ -27,8 +27,6 @@ extern int lt_ypos; extern int g_trans; -void G_savegame (FILE* h); -void G_loadgame (FILE* h); int G_load (FILE *h); void load_game (int n); void G_start (void); diff --git a/src/items.c b/src/items.c index b0f079a..65f39e2 100644 --- a/src/items.c +++ b/src/items.c @@ -41,46 +41,6 @@ static int tsndtm, rsndtm; int itm_rtime = 1092; -void IT_savegame (FILE *h) { - int i, n; - for (n = MAXITEM - 1; n >= 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 i, n; - n = myfread32(h); - for (i = 0; i < n; i++) { - it[i].o.x = myfread32(h); - it[i].o.y = myfread32(h); - it[i].o.xv = myfread32(h); - it[i].o.yv = myfread32(h); - it[i].o.vx = myfread32(h); - it[i].o.vy = myfread32(h); - it[i].o.r = myfread32(h); - it[i].o.h = myfread32(h); - it[i].t = myfread32(h); - it[i].s = myfread32(h); - } - itm_rtime = myfread32(h); -} - void IT_alloc (void) { int i, j, n; static char nm[][6] = { diff --git a/src/items.h b/src/items.h index c2be467..011188a 100644 --- a/src/items.h +++ b/src/items.h @@ -23,7 +23,6 @@ #define ITEMS_H_INCLUDED #include "view.h" // obj_t -#include // FILE #define MAXITEM 300 @@ -43,8 +42,6 @@ typedef struct { extern item_t it[MAXITEM]; extern int itm_rtime; -void IT_savegame (FILE *h); -void IT_loadgame (FILE *h); void IT_alloc (void); void IT_init (void); int IT_load (FILE *h); diff --git a/src/menu.c b/src/menu.c index 4a15ede..4205285 100644 --- a/src/menu.c +++ b/src/menu.c @@ -39,6 +39,8 @@ #include "input.h" #include "system.h" +#include "save.h" + #include #include #include diff --git a/src/monster.c b/src/monster.c index e895a38..3a5efd4 100644 --- a/src/monster.c +++ b/src/monster.c @@ -90,12 +90,12 @@ static char *sleepanim[MN_TN]={ int hit_xv, hit_yv; mn_t mn[MAXMN]; +int mnum, gsndt; static void *fsnd,*pauksnd,*trupsnd; static void *snd[MN_TN][5],*impsitsnd[2],*impdthsnd[2],*firsnd,*slopsnd,*gsnd[4]; static void *swgsnd,*pchsnd,*telesnd; static void *positsnd[3],*podthsnd[3]; -static int mnum,gsndt; static mnsz_t mnsz[MN_TN+1]={ //rad ht life pain rv jv slop min_pn 0, 0, 0, 0, 0, 0, 0, 0, // none @@ -121,7 +121,7 @@ static mnsz_t mnsz[MN_TN+1]={ 8, 26, 400, 70, 8,10, 30, 50 // man }; -static void setst(int i,int st) { +void setst (int i, int st) { char *a; int t; @@ -163,81 +163,6 @@ static void setst(int i,int st) { mn[i].ap=a; } -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(0, h); // mn[i].ap 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); -} - -void MN_loadgame (FILE *h) { - int i, n, c; - n = myfread32(h); - for (i = 0; i < n; i++) { - mn[i].o.x = myfread32(h); - mn[i].o.y = myfread32(h); - mn[i].o.xv = myfread32(h); - mn[i].o.yv = myfread32(h); - mn[i].o.vx = myfread32(h); - mn[i].o.vy = myfread32(h); - mn[i].o.r = myfread32(h); - mn[i].o.h = myfread32(h); - mn[i].t = myfread8(h); - mn[i].d = myfread8(h); - mn[i].st = myfread8(h); - mn[i].ftime = myfread8(h); - mn[i].fobj = myfread32(h); - mn[i].s = myfread32(h); - mn[i].ap = NULL; myfread32(h); // useless, changed after loading - mn[i].aim = myfread32(h); - mn[i].life = myfread32(h); - mn[i].pain = myfread32(h); - mn[i].ac = myfread32(h); - mn[i].tx = myfread32(h); - mn[i].ty = myfread32(h); - mn[i].ammo = myfread32(h); - mn[i].atm = myfread16(h); - } - mnum = myfread32(h); - gsndt = myfread32(h); - for (n = 0; n < MAXMN; ++n) { - if (mn[n].t) { - c = mn[n].ac; - setst(n, mn[n].st); - mn[n].ac = c; - } - } -} - #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10) void MN_alloc (void) { diff --git a/src/monster.h b/src/monster.h index 022962f..30edf1d 100644 --- a/src/monster.h +++ b/src/monster.h @@ -51,9 +51,10 @@ typedef struct { extern byte nomon; extern int hit_xv, hit_yv; extern mn_t mn[MAXMN]; +extern int mnum, gsndt; + +void setst (int i, int st); -void MN_savegame (FILE *h); -void MN_loadgame (FILE *h); void MN_alloc (void); void MN_init (void); int MN_spawn (int x, int y, byte d, int t); diff --git a/src/player.c b/src/player.c index 2d781a3..79a5a98 100644 --- a/src/player.c +++ b/src/player.c @@ -68,114 +68,6 @@ byte plr_goanim[]="BDACDA"; byte plr_dieanim[]="HHHHIIIIJJJJKKKKLLLLMMMM"; byte plr_slopanim[]="OOPPQQRRSSTTUUVVWW"; -static 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_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); -} - -static void PL_load_player (player_t *p, FILE *h) { - p->o.x = myfread32(h); - p->o.y = myfread32(h); - p->o.xv = myfread32(h); - p->o.yv = myfread32(h); - p->o.vx = myfread32(h); - p->o.vy = myfread32(h); - p->o.r = myfread32(h); - p->o.h = myfread32(h); - p->looky = myfread32(h); - p->st = myfread32(h); - p->s = myfread32(h); - p->life = myfread32(h); - p->armor = myfread32(h); - p->hit = myfread32(h); - p->hito = myfread32(h); - p->pain = myfread32(h); - p->air = myfread32(h); - p->invl = myfread32(h); - p->suit = myfread32(h); - p->d = myfread8(h); - p->frag = myfread32(h); - p->ammo = myfread32(h); - p->shel = myfread32(h); - p->rock = myfread32(h); - p->cell = myfread32(h); - p->fuel = myfread32(h); - p->kills = myfread32(h); - p->secrets = myfread32(h); - p->fire = myfread8(h); - p->cwpn = myfread8(h); - p->csnd = myfread8(h); - p->amul = myfread8(h); - p->wpns = myfread16(h); - p->wpn = myfread8(h); - p->f = myfread8(h); - p->drawst = myfread8(h); - p->color = myfread8(h); - p->id = myfread32(h); - p->keys = myfread8(h); - p->lives = myfread8(h); - // k* not saved -} - -void PL_loadgame (FILE *h) { - PL_load_player(&pl1, h); - if (_2pl) { - PL_load_player(&pl2, h); - } - PL_JUMP = myfread32(h); - PL_RUN = myfread32(h); - p_immortal = myfread8(h); -} - static int nonz (int a) { return a ? a : 1; } diff --git a/src/player.h b/src/player.h index 48c49ab..056397a 100644 --- a/src/player.h +++ b/src/player.h @@ -25,7 +25,6 @@ #include "glob.h" #include "view.h" // obj_t -#include // FILE #define PL_DRAWLIFE 1 #define PL_DRAWARMOR 2 @@ -88,8 +87,6 @@ extern byte plr_goanim[]; extern byte plr_dieanim[]; extern byte plr_slopanim[]; -void PL_savegame (FILE *h); -void PL_loadgame (FILE *h); int PL_isdead (player_t *p); void PL_init (void); void PL_alloc (void); diff --git a/src/save.c b/src/save.c new file mode 100644 index 0000000..4ffb554 --- /dev/null +++ b/src/save.c @@ -0,0 +1,603 @@ +#include "save.h" + +#include "dots.h" +#include "fx.h" +#include "game.h" +#include "items.h" +#include "monster.h" +#include "player.h" +#include "smoke.h" +#include "switch.h" +#include "view.h" +#include "weapons.h" + +#include "render.h" +#include "music.h" + +#include // FILE +#include +#include +#include "files.h" +#include "my.h" + +char savname[SAVE_MAX][SAVE_MAXLEN]; +char savok[SAVE_MAX]; + +static void DOT_savegame (FILE *h) { + int i, n; + for (i = n = 0; i < MAXDOT; ++i) { + if (dot[i].t) { + ++n; + } + } + myfwrite32(n, h); + for (i = 0; i < MAXDOT; ++i) { + if (dot[i].t) { + myfwrite32(dot[i].o.x, h); + myfwrite32(dot[i].o.y, h); + myfwrite32(dot[i].o.xv, h); + myfwrite32(dot[i].o.yv, h); + myfwrite32(dot[i].o.vx, h); + myfwrite32(dot[i].o.vy, h); + myfwrite32(dot[i].o.r, h); + myfwrite32(dot[i].o.h, h); + myfwrite8(dot[i].c, h); + myfwrite8(dot[i].t, h); + } + } +} + +static void DOT_loadgame (FILE *h) { + int i, n; + n = myfread32(h); + for (i = 0; i < n; i++) { + dot[i].o.x = myfread32(h); + dot[i].o.y = myfread32(h); + dot[i].o.xv = myfread32(h); + dot[i].o.yv = myfread32(h); + dot[i].o.vx = myfread32(h); + dot[i].o.vy = myfread32(h); + dot[i].o.r = myfread32(h); + dot[i].o.h = myfread32(h); + dot[i].c = myfread8(h); + dot[i].t = myfread8(h); + } +} + +static void FX_savegame (FILE *h) { + int i, n; + for (i = n = 0; i < MAXFX; ++i) { + if (fx[i].t) { + ++n; + } + } + myfwrite32(n, h); + for (i = 0; i < MAXFX; ++i) { + if (fx[i].t) { + myfwrite32(fx[i].x, h); + myfwrite32(fx[i].y, h); + myfwrite32(fx[i].xv, h); + myfwrite32(fx[i].yv, h); + myfwrite8(fx[i].t, h); + myfwrite8(fx[i].s, h); + } + } +} + +static void FX_loadgame (FILE *h) { + int i, n; + n = myfread32(h); + for (i = 0; i < n; i++) { + fx[i].x = myfread32(h); + fx[i].y = myfread32(h); + fx[i].xv = myfread32(h); + fx[i].yv = myfread32(h); + fx[i].t = myfread8(h); + fx[i].s = myfread8(h); + } +} + +static void G_savegame (FILE *h) { + myfwrite8(_2pl, h); + myfwrite8(g_dm, h); + myfwrite8(g_exit, h); + myfwrite8(g_map, h); + myfwrite32(g_time, h); + myfwrite32(dm_pl1p, h); + myfwrite32(dm_pl2p, h); + myfwrite32(dm_pnum, h); + int i = 0; + while (i < dm_pnum) { + myfwrite32(dm_pos[i].x, h); + myfwrite32(dm_pos[i].y, h); + myfwrite8(dm_pos[i].d, h); + i += 1; + } + myfwrite8(cheat, h); + myfwrite(g_music, 8, 1, h); +} + +static void G_loadgame (FILE *h) { + _2pl = myfread8(h); + g_dm = myfread8(h); + g_exit = myfread8(h); + g_map = myfread8(h); + g_time = myfread32(h); + dm_pl1p = myfread32(h); + dm_pl2p = myfread32(h); + dm_pnum = myfread32(h); + int i = 0; + while (i < dm_pnum) { + dm_pos[i].x = myfread32(h); + dm_pos[i].y = myfread32(h); + dm_pos[i].d = myfread8(h); + i += 1; + } + cheat = myfread8(h); + myfread(g_music, 8, 1, h); + F_loadmus(g_music); +} + +static void IT_savegame (FILE *h) { + int i, n; + for (n = MAXITEM - 1; n >= 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); +} + +static void IT_loadgame (FILE *h) { + int i, n; + n = myfread32(h); + for (i = 0; i < n; i++) { + it[i].o.x = myfread32(h); + it[i].o.y = myfread32(h); + it[i].o.xv = myfread32(h); + it[i].o.yv = myfread32(h); + it[i].o.vx = myfread32(h); + it[i].o.vy = myfread32(h); + it[i].o.r = myfread32(h); + it[i].o.h = myfread32(h); + it[i].t = myfread32(h); + it[i].s = myfread32(h); + } + itm_rtime = myfread32(h); +} + +static 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(0, h); // mn[i].ap 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 MN_loadgame (FILE *h) { + int i, n, c; + n = myfread32(h); + for (i = 0; i < n; i++) { + mn[i].o.x = myfread32(h); + mn[i].o.y = myfread32(h); + mn[i].o.xv = myfread32(h); + mn[i].o.yv = myfread32(h); + mn[i].o.vx = myfread32(h); + mn[i].o.vy = myfread32(h); + mn[i].o.r = myfread32(h); + mn[i].o.h = myfread32(h); + mn[i].t = myfread8(h); + mn[i].d = myfread8(h); + mn[i].st = myfread8(h); + mn[i].ftime = myfread8(h); + mn[i].fobj = myfread32(h); + mn[i].s = myfread32(h); + mn[i].ap = NULL; myfread32(h); // useless, changed after loading + mn[i].aim = myfread32(h); + mn[i].life = myfread32(h); + mn[i].pain = myfread32(h); + mn[i].ac = myfread32(h); + mn[i].tx = myfread32(h); + mn[i].ty = myfread32(h); + mn[i].ammo = myfread32(h); + mn[i].atm = myfread16(h); + } + mnum = myfread32(h); + gsndt = myfread32(h); + for (n = 0; n < MAXMN; n++) { + if (mn[n].t) { + c = mn[n].ac; + setst(n, mn[n].st); + mn[n].ac = c; + } + } +} + +static 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 +} + +static 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); +} + +static void PL_load_player (player_t *p, FILE *h) { + p->o.x = myfread32(h); + p->o.y = myfread32(h); + p->o.xv = myfread32(h); + p->o.yv = myfread32(h); + p->o.vx = myfread32(h); + p->o.vy = myfread32(h); + p->o.r = myfread32(h); + p->o.h = myfread32(h); + p->looky = myfread32(h); + p->st = myfread32(h); + p->s = myfread32(h); + p->life = myfread32(h); + p->armor = myfread32(h); + p->hit = myfread32(h); + p->hito = myfread32(h); + p->pain = myfread32(h); + p->air = myfread32(h); + p->invl = myfread32(h); + p->suit = myfread32(h); + p->d = myfread8(h); + p->frag = myfread32(h); + p->ammo = myfread32(h); + p->shel = myfread32(h); + p->rock = myfread32(h); + p->cell = myfread32(h); + p->fuel = myfread32(h); + p->kills = myfread32(h); + p->secrets = myfread32(h); + p->fire = myfread8(h); + p->cwpn = myfread8(h); + p->csnd = myfread8(h); + p->amul = myfread8(h); + p->wpns = myfread16(h); + p->wpn = myfread8(h); + p->f = myfread8(h); + p->drawst = myfread8(h); + p->color = myfread8(h); + p->id = myfread32(h); + p->keys = myfread8(h); + p->lives = myfread8(h); + // k* not saved +} + +static void PL_loadgame (FILE *h) { + PL_load_player(&pl1, h); + if (_2pl) { + PL_load_player(&pl2, h); + } + PL_JUMP = myfread32(h); + PL_RUN = myfread32(h); + p_immortal = myfread8(h); +} + +static void SMK_savegame (FILE *h) { + int i, n; + for (i = n = 0; i < MAXSMOK; ++i) { + if (sm[i].t) { + ++n; + } + } + myfwrite32(n, h); + for (i = 0; i < MAXSMOK; ++i) { + if (sm[i].t) { + myfwrite32(sm[i].x, h); + myfwrite32(sm[i].y, h); + myfwrite32(sm[i].xv, h); + myfwrite32(sm[i].xv, h); + myfwrite8(sm[i].t, h); + myfwrite8(sm[i].s, h); + myfwrite16(sm[i].o, h); + } + } +} + +static void SMK_loadgame (FILE *h) { + int i, n; + n = myfread32(h); + for (i = 0; i < n; ++i) { + sm[i].x = myfread32(h); + sm[i].y = myfread32(h); + sm[i].xv = myfread32(h); + sm[i].xv = myfread32(h); + sm[i].t = myfread8(h); + sm[i].s = myfread8(h); + sm[i].o = myfread16(h); + } +} + +static void SW_savegame (FILE *h) { + int i, n; + for (n = MAXSW - 1; n >= 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); +} + +static void SW_loadgame (FILE *h) { + int i, n; + n = myfread32(h); + for (i = 0; i < n; i++) { + sw[i].x = myfread8(h); + sw[i].y = myfread8(h); + sw[i].t = myfread8(h); + sw[i].tm = myfread8(h); + sw[i].a = myfread8(h); + sw[i].b = myfread8(h); + sw[i].c = myfread8(h); + sw[i].d = myfread8(h); + sw[i].f = myfread8(h); + } + sw_secrets = myfread32(h); +} + +static void W_savegame (FILE* h) { + char s[8]; + int i; + myfwrite32(sky_type, h); + for(i = 1; i < 256; ++i) { + R_get_name(i, s); + myfwrite(s, 8, 1, h); + } + for (i = 0; i < 256; i++) { + myfwrite32(walf[i], h); + } + for (i = 0; i < 256; i++) { + myfwrite8(R_get_swp(i), h); + } + myfwrite(fldb, FLDW*FLDH, 1, h); + myfwrite(fld, FLDW*FLDH, 1, h); + myfwrite(fldf, FLDW*FLDH, 1, h); +} + +static void W_loadgame (FILE* h) { + int i; + char s[8]; + sky_type = myfread32(h); + R_loadsky(sky_type); + R_begin_load(); + for (i = 1; i < 256; ++i) { + myfread(s, 8, 1, h); + if (s[0]) { + R_load(s); + } + } + R_end_load(); + for (i = 0; i < 256; i++) { + myfread32(h); // useless + } + for (i = 0; i < 256; i++) { + walf[i] = myfread8(h); + } + myfread(fldb, FLDW*FLDH, 1, h); + myfread(fld, FLDW*FLDH, 1, h); + myfread(fldf, FLDW*FLDH, 1, h); +} + +static 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); + } +} + +static void WP_loadgame (FILE *h) { + int i, n; + n = myfread32(h); + for (i = 0; i < n; i++) { + wp[i].o.x = myfread32(h); + wp[i].o.y = myfread32(h); + wp[i].o.xv = myfread32(h); + wp[i].o.yv = myfread32(h); + wp[i].o.vx = myfread32(h); + wp[i].o.vy = myfread32(h); + wp[i].o.r = myfread32(h); + wp[i].o.h = myfread32(h); + wp[i].t = myfread8(h); + wp[i].s = myfread8(h); + wp[i].own = myfread32(h); + wp[i].target = myfread16(h); + } +} + +static char *getsavfpname (int n, int ro) { + static char fn[] = "savgame0.dat"; + fn[7] = n + '0'; +#ifndef WIN32 + static char p[100]; + char *e = getenv("HOME"); + strncpy(p, e, 60); + strcat(p, "/.doom2d-rembo"); + if (!ro) { + mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + } + strcat(p, "/"); + strcat(p, fn); +#else + strcpy(p, fn); +#endif + return p; +} + +void F_getsavnames (void) { + int i; + FILE *h; + short ver; + char *p; + for (i = 0; i < 7; ++i) { + p = getsavfpname(i, 1); + memset(savname[i], 0, 24); + savok[i] = 0; + h = fopen(p, "rb"); + if (h != NULL) { + ver = -1; + myfread(savname[i], 24, 1, h); + ver = myfread16(h); + savname[i][23] = 0; + savok[i] = (ver == 3) ? 1 : 0; + fclose(h); + } + } +} + +void F_savegame (int n, char *s) { + char *p = getsavfpname(n, 0); + FILE *h = fopen(p, "wb"); + if (h != NULL) { + myfwrite(s, 24, 1, h); // slot name + myfwrite16(3, h); // version + G_savegame(h); + W_savegame(h); + DOT_savegame(h); + SMK_savegame(h); + FX_savegame(h); + IT_savegame(h); + MN_savegame(h); + PL_savegame(h); + SW_savegame(h); + WP_savegame(h); + fclose(h); + } +} + +void F_loadgame (int n) { + short ver; + char *p = getsavfpname(n, 1); + FILE *h = fopen(p, "rb"); + if (h != NULL) { + fseek(h, 24, SEEK_SET); // skip name + ver = myfread16(h); // version + if (ver == 3) { + G_loadgame(h); + W_loadgame(h); + DOT_loadgame(h); + SMK_loadgame(h); + FX_loadgame(h); + IT_loadgame(h); + MN_loadgame(h); + PL_loadgame(h); + SW_loadgame(h); + WP_loadgame(h); + } + fclose(h); + } +} diff --git a/src/save.h b/src/save.h new file mode 100644 index 0000000..a303333 --- /dev/null +++ b/src/save.h @@ -0,0 +1,16 @@ +#ifndef SAVE_H_INCLUDED +#define SAVE_H_INCLUDED + +#include "glob.h" + +#define SAVE_MAX 7 +#define SAVE_MAXLEN 24 + +extern char savname[SAVE_MAX][SAVE_MAXLEN]; +extern char savok[SAVE_MAX]; + +void F_getsavnames (void); +void F_savegame (int n, char *s); +void F_loadgame (int n); + +#endif /* SAVE_H_INCLUDED */ diff --git a/src/smoke.c b/src/smoke.c index 19bf714..8512e48 100644 --- a/src/smoke.c +++ b/src/smoke.c @@ -40,41 +40,6 @@ static int lsm; static void *burnsnd; static int burntm=0; -void SMK_savegame (FILE *h) { - int i, n; - for (i = n = 0; i < MAXSMOK; ++i) { - if (sm[i].t) { - ++n; - } - } - myfwrite32(n, h); - for (i = 0; i < MAXSMOK; ++i) { - if (sm[i].t) { - myfwrite32(sm[i].x, h); - myfwrite32(sm[i].y, h); - myfwrite32(sm[i].xv, h); - myfwrite32(sm[i].xv, h); - myfwrite8(sm[i].t, h); - myfwrite8(sm[i].s, h); - myfwrite16(sm[i].o, h); - } - } -} - -void SMK_loadgame (FILE *h) { - int i, n; - n = myfread32(h); - for (i = 0; i < n; ++i) { - sm[i].x = myfread32(h); - sm[i].y = myfread32(h); - sm[i].xv = myfread32(h); - sm[i].xv = myfread32(h); - sm[i].t = myfread8(h); - sm[i].s = myfread8(h); - sm[i].o = myfread16(h); - } -} - void SMK_init (void) { int i; diff --git a/src/smoke.h b/src/smoke.h index f68ae94..04087bc 100644 --- a/src/smoke.h +++ b/src/smoke.h @@ -23,7 +23,6 @@ #define SMOKE_H_INLUDED #include "glob.h" -#include // FILE #define MAXSMOK 500 @@ -38,8 +37,6 @@ typedef struct { extern smoke_t sm[MAXSMOK]; -void SMK_savegame (FILE *h); -void SMK_loadgame (FILE *h); void SMK_init (void); void SMK_alloc (void); void SMK_act (void); diff --git a/src/switch.c b/src/switch.c index 79f15c0..d95e152 100644 --- a/src/switch.c +++ b/src/switch.c @@ -32,61 +32,15 @@ #include "game.h" #include "my.h" #include "monster.h" - -#define MAXSW 100 - -typedef struct { - byte x, y; - byte t, tm; - byte a, b, c, d; - byte f; -} sw_t; +#include "render.h" int sw_secrets; +sw_t sw[MAXSW]; -static sw_t sw[MAXSW]; static void *sndswn, *sndswx, *sndnoway, *sndbdo, *sndbdc, *sndnotele; static int swsnd; static byte cht, chto, chf, f_ch; -void SW_savegame (FILE *h) { - int i, n; - for (n = MAXSW - 1; n >= 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 i, n; - n = myfread32(h); - for (i = 0; i < n; i++) { - sw[i].x = myfread8(h); - sw[i].y = myfread8(h); - sw[i].t = myfread8(h); - sw[i].tm = myfread8(h); - sw[i].a = myfread8(h); - sw[i].b = myfread8(h); - sw[i].c = myfread8(h); - sw[i].d = myfread8(h); - sw[i].f = myfread8(h); - } - sw_secrets = myfread32(h); -} - int SW_load (FILE *h) { int i; switch(blk.t) { diff --git a/src/switch.h b/src/switch.h index 621df40..d0d586c 100644 --- a/src/switch.h +++ b/src/switch.h @@ -25,16 +25,24 @@ #include // FILE #include "view.h" // obj_t +#define MAXSW 100 + enum { SW_NONE, SW_EXIT, SW_EXITS, SW_OPENDOOR, SW_SHUTDOOR, SW_SHUTTRAP, SW_DOOR, SW_DOOR5, SW_PRESS, SW_TELE, SW_SECRET, SW_LIFTUP, SW_LIFTDOWN, SW_TRAP, SW_LIFT }; +typedef struct { + byte x, y; + byte t, tm; + byte a, b, c, d; + byte f; +} sw_t; + extern int sw_secrets; +extern sw_t sw[MAXSW]; -void SW_savegame (FILE *h); -void SW_loadgame (FILE *h); int SW_load (FILE *h); void SW_alloc (void); void SW_init (void); diff --git a/src/view.c b/src/view.c index f902e23..6a81329 100644 --- a/src/view.c +++ b/src/view.c @@ -47,49 +47,6 @@ byte fldb[FLDH][FLDW]; byte fldf[FLDH][FLDW]; byte fld[FLDH][FLDW]; -void W_savegame (FILE* h) { - char s[8]; - int i; - myfwrite32(sky_type, h); - for(i = 1; i < 256; ++i) { - R_get_name(i, s); - myfwrite(s, 8, 1, h); - } - for (i = 0; i < 256; i++) { - myfwrite32(walf[i], h); - } - for (i = 0; i < 256; i++) { - myfwrite8(R_get_swp(i), h); - } - myfwrite(fldb, FLDW*FLDH, 1, h); - myfwrite(fld, FLDW*FLDH, 1, h); - myfwrite(fldf, FLDW*FLDH, 1, h); -} - -void W_loadgame (FILE* h) { - int i; - char s[8]; - sky_type = myfread32(h); - R_loadsky(sky_type); - R_begin_load(); - for (i = 1; i < 256; ++i) { - myfread(s, 8, 1, h); - if (s[0]) { - R_load(s); - } - } - R_end_load(); - for (i = 0; i < 256; i++) { - myfread32(h); // useless - } - for (i = 0; i < 256; i++) { - walf[i] = myfread8(h); - } - myfread(fldb, FLDW*FLDH, 1, h); - myfread(fld, FLDW*FLDH, 1, h); - myfread(fldf, FLDW*FLDH, 1, h); -} - void W_init (void) { DOT_init(); SMK_init(); diff --git a/src/view.h b/src/view.h index d64512c..a88e0f6 100644 --- a/src/view.h +++ b/src/view.h @@ -65,8 +65,6 @@ extern byte fldb[FLDH][FLDW]; extern byte fldf[FLDH][FLDW]; extern byte fld[FLDH][FLDW]; -void W_savegame (FILE *h); -void W_loadgame (FILE *h); void W_init (void); int W_load (FILE *h); diff --git a/src/weapons.c b/src/weapons.c index b3b757d..4162ae2 100644 --- a/src/weapons.c +++ b/src/weapons.c @@ -43,48 +43,6 @@ weapon_t wp[MAXWPN]; static void *snd[14]; static void throw(int,int,int,int,int,int,int,int); -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 i, n; - n = myfread32(h); - for (i = 0; i < n; i++) { - wp[i].o.x = myfread32(h); - wp[i].o.y = myfread32(h); - wp[i].o.xv = myfread32(h); - wp[i].o.yv = myfread32(h); - wp[i].o.vx = myfread32(h); - wp[i].o.vy = myfread32(h); - wp[i].o.r = myfread32(h); - wp[i].o.h = myfread32(h); - wp[i].t = myfread8(h); - wp[i].s = myfread8(h); - wp[i].own = myfread32(h); - wp[i].target = myfread16(h); - } -} - void WP_alloc (void) { int i; static char nm[14][6]={ diff --git a/src/weapons.h b/src/weapons.h index fc4f7a3..62b2a34 100644 --- a/src/weapons.h +++ b/src/weapons.h @@ -22,8 +22,6 @@ #ifndef WEAPONS_H_INLUDED #define WEAPONS_H_INLUDED -#include // FILE - #define MAXWPN 300 typedef struct { @@ -35,8 +33,6 @@ typedef struct { extern weapon_t wp[MAXWPN]; -void WP_savegame (FILE *h); -void WP_loadgame (FILE *h); void WP_alloc (void); void WP_init (void); void WP_act (void);