int save_s_player (s_player * s, PACKFILE * f) { int i; pack_fwrite (s->name, sizeof (s->name), f); pack_putc (0, f); /* alignment */ pack_putc (0, f); /* alignment */ pack_putc (0, f); /* alignment */ pack_iputl (s->xp, f); pack_iputl (s->next, f); pack_iputl (s->lvl, f); pack_iputl (s->mrp, f); pack_iputl (s->hp, f); pack_iputl (s->mhp, f); pack_iputl (s->mp, f); pack_iputl (s->mmp, f); for (i = 0; i < NUM_STATS; ++i) { pack_iputl (s->stats[i], f); } for (i = 0; i < 16; ++i) { pack_putc (s->res[i], f); } for (i = 0; i < 24; ++i) { pack_putc (s->sts[i], f); } for (i = 0; i < 6; ++i) { pack_putc (s->eqp[i], f); } for (i = 0; i < 60; ++i) { pack_putc (s->spells[i], f); } pack_putc (0, f); /* alignment */ pack_putc (0, f); /* alignment */ return 0; }
static void write_palette (PACKFILE *file, GIF_PALETTE *palette, int bits) { int i; for (i = 0; i < (1 << bits); i++) { pack_putc (palette->colors[i].r, file); pack_putc (palette->colors[i].g, file); pack_putc (palette->colors[i].b, file); } }
void save_level(PACKFILE *file,LEVEL *data) { int i; pack_iputw(data->sizey,file); pack_iputw(data->sizex,file); pack_putc(data->bgtype,file); if (data->bgtype == 1) for (i=0;i<600;i++) {pack_putc(getr24(data->lines[i]),file); pack_putc(getg24(data->lines[i]),file); pack_putc(getb24(data->lines[i]),file);} pack_iputw(data->entitycnt,file); pack_fwrite(data->map,data->length,file); for(i=0;i<data->entitycnt;i++) { save_entity(file,&data->entities[i]); } }
int rename_file(char *oldname, char *newname) { PACKFILE *oldfile, *newfile; int c; oldfile = pack_fopen(oldname, F_READ); if (!oldfile) return -1; newfile = pack_fopen(newname, F_WRITE); if (!newfile) { pack_fclose(oldfile); return -1; } c = pack_getc(oldfile); while (c != EOF) { pack_putc(c, newfile); c = pack_getc(oldfile); } pack_fclose(oldfile); pack_fclose(newfile); delete_file(oldname); return 0; }
void dump_buffer(BUFFER *b, PACKFILE *f) { int size; size = b->pos; if(b->bit_pos != 0) size ++; pack_putc(size, f); pack_fwrite(b->data, size, f); }
void save_entity(PACKFILE *file, ENTITY* data) { pack_iputl(data->x,file); pack_iputl(data->y,file); pack_iputl(data->speed,file); pack_putc(data->type,file); pack_iputw(data->data1,file); pack_iputw(data->data2,file); pack_iputw(data->data3,file); pack_iputw(data->data4,file); pack_iputw(data->health,file); pack_iputl(data->maxcooldown,file); }
int eof_copy_file(const char * src, const char * dest) { PACKFILE * src_fp = NULL; PACKFILE * dest_fp = NULL; void *ptr = NULL; //Used to buffer memory unsigned long src_size = 0; unsigned long i; eof_log("eof_copy_file() entered", 1); if((src == NULL) || (dest == NULL)) return 0; if(!ustricmp(src,dest)) //Special case: The input and output file are the same return 0; //Return success without copying any files src_size = (unsigned long)file_size_ex(src); if(src_size > LONG_MAX) return 0; //Unable to validate I/O due to Allegro's usage of signed values src_fp = pack_fopen(src, "r"); if(!src_fp) { return 0; } dest_fp = pack_fopen(dest, "w"); if(!dest_fp) { (void) pack_fclose(src_fp); return 0; } //Attempt to buffer the input file into memory for faster read and write ptr = malloc((size_t)src_size); if(ptr != NULL) { //If a buffer large enough to store the input file was created long long_src_size = src_size; if((pack_fread(ptr, long_src_size, src_fp) != long_src_size) || (pack_fwrite(ptr, long_src_size, dest_fp) != long_src_size)) { //If there was an error reading from file or writing from memory free(ptr); //Release buffer return 0; //Return error } free(ptr); //Release buffer } else { //Otherwise copy the slow way (one byte at a time) for(i = 0; i < src_size; i++) { (void) pack_putc(pack_getc(src_fp), dest_fp); } } (void) pack_fclose(src_fp); (void) pack_fclose(dest_fp); return 1; }
void extract_data(void) { PACKFILE *f, *df; int c; get_stats(); if (!err) { if (!stat_hasdata) { fprintf(stderr, "%s has no appended data: cannot extract it\n", opt_filename); err = 1; return; } f = pack_fopen(opt_filename, F_READ); if (!f) { fprintf(stderr, "Error reading %s\n", opt_filename); err = 1; return; } pack_fseek(f, stat_exe_size); f = pack_fopen_chunk(f, FALSE); if (!f) { fprintf(stderr, "Error reading %s\n", opt_filename); err = 1; return; } if (opt_allegro) df = pack_fopen(opt_dataname, (opt_compress ? F_WRITE_PACKED : F_WRITE_NOPACK)); else df = pack_fopen(opt_dataname, F_WRITE); if (!df) { pack_fclose(f); fprintf(stderr, "Error writing %s\n", opt_dataname); err = 1; return; } while ((c = pack_getc(f)) != EOF) pack_putc(c, df); printf("%d bytes extracted from %s into %s\n", stat_uncompressed_size, opt_filename, opt_dataname); pack_fclose(f); pack_fclose(df); } }
int save_s_map (s_map * sm, PACKFILE * f) { int i; /* pack_putc (sm->map_no, f); */ pack_putc (0, f); /* To maintain compatibility. */ pack_putc (sm->zero_zone, f); pack_putc (sm->map_mode, f); pack_putc (sm->can_save, f); pack_putc (sm->tileset, f); pack_putc (sm->use_sstone, f); pack_putc (sm->can_warp, f); pack_putc (sm->extra_byte, f); pack_iputl (sm->xsize, f); pack_iputl (sm->ysize, f); pack_iputl (sm->pmult, f); pack_iputl (sm->pdiv, f); pack_iputl (sm->stx, f); pack_iputl (sm->sty, f); pack_iputl (sm->warpx, f); pack_iputl (sm->warpy, f); //pack_iputl (1, f); /* Revision 1 */ sm->revision = 2; // Force new revision: 2 pack_iputl (sm->revision, f); /* Revision 2 */ pack_iputl (sm->extra_sdword2, f); pack_fwrite (sm->song_file, sizeof (sm->song_file), f); pack_fwrite (sm->map_desc, sizeof (sm->map_desc), f); /* Markers */ pack_iputw (sm->num_markers, f); for (i = 0; i < sm->num_markers; ++i) { save_s_marker (&sm->markers[i], f); } /* Bounding boxes */ pack_iputw (sm->num_bound_boxes, f); for (i = 0; i < sm->num_bound_boxes; ++i) { save_s_bound (&sm->bound_box[i], f); } return 0; }
int save_s_map(s_map *sm, PACKFILE *f) { size_t i; assert(sm && "sm == NULL"); assert(f && "f == NULL"); /* pack_putc (sm->map_no, f); */ pack_putc(0, f); /* To maintain compatibility. */ pack_putc(sm->zero_zone, f); pack_putc(sm->map_mode, f); pack_putc(sm->can_save, f); pack_putc(sm->tileset, f); pack_putc(sm->use_sstone, f); pack_putc(sm->can_warp, f); pack_putc(sm->extra_byte, f); pack_iputl(sm->xsize, f); pack_iputl(sm->ysize, f); pack_iputl(sm->pmult, f); pack_iputl(sm->pdiv, f); pack_iputl(sm->stx, f); pack_iputl(sm->sty, f); pack_iputl(sm->warpx, f); pack_iputl(sm->warpy, f); //pack_iputl (1, f); /* Revision 1 */ sm->revision = 2; // Force new revision: 2 pack_iputl(sm->revision, f); /* Revision 2 */ pack_iputl(sm->extra_sdword2, f); pack_fwrite(sm->song_file, sizeof(sm->song_file), f); pack_fwrite(sm->map_desc, sizeof(sm->map_desc), f); /* Markers */ save_markers(&sm->markers, f); /* Bounding boxes */ save_bounds(&sm->bounds, f); return 0; }
/* * Compresses all the frames in the animation and writes to a gif file. * Nothing else like extensions or comments is written. * * Returns 0 on success. * * Note: All bitmaps must have a color depth of 8. */ int algif_save_raw_animation (const char *filename, GIF_ANIMATION *gif) { int frame; int i, j; PACKFILE *file; file = pack_fopen (filename, "w"); if (!file) return -1; pack_fwrite ("GIF89a", 6, file); pack_iputw (gif->width, file); pack_iputw (gif->height, file); /* 7 global palette * 456 color richness * 3 sorted * 012 palette bits */ for (i = 1, j = 0; i < gif->palette.colors_count; i *= 2, j++); pack_putc ((j ? 128 : 0) + 64 + 32 + 16 + (j ? j - 1 : 0), file); pack_putc (gif->background_index, file); pack_putc (0, file); /* No aspect ratio. */ if (j) write_palette (file, &gif->palette, j); if (gif->loop != -1) /* Loop count extension. */ { pack_putc (0x21, file); /* Extension Introducer. */ pack_putc (0xff, file); /* Application Extension. */ pack_putc (11, file); /* Size. */ pack_fwrite ("NETSCAPE2.0", 11, file); pack_putc (3, file); /* Size. */ pack_putc (1, file); pack_iputw (gif->loop, file); pack_putc (0, file); } for (frame = 0; frame < gif->frames_count; frame++) { int w = gif->frames[frame].bitmap_8_bit->w; int h = gif->frames[frame].bitmap_8_bit->h; pack_putc (0x21, file); /* Extension Introducer. */ pack_putc (0xf9, file); /* Graphic Control Extension. */ pack_putc (4, file); /* Size. */ /* Disposal method, and enable transparency. */ i = gif->frames[frame].disposal_method << 2; if (gif->frames[frame].transparent_index != -1) i |= 1; pack_putc (i, file); pack_iputw (gif->frames[frame].duration, file); /* In 1/100th seconds. */ if (gif->frames[frame].transparent_index != -1) pack_putc (gif->frames[frame].transparent_index, file); /* Transparent color index. */ else pack_putc (0, file); pack_putc (0x00, file); /* Terminator. */ pack_putc (0x2c, file); /* Image Descriptor. */ pack_iputw (gif->frames[frame].xoff, file); pack_iputw (gif->frames[frame].yoff, file); pack_iputw (w, file); pack_iputw (h, file); /* 7: local palette * 6: interlaced * 5: sorted * 012: palette bits */ for (i = 1, j = 0; i < gif->frames[frame].palette.colors_count; i *= 2, j++); pack_putc ((j ? 128 : 0) + (j ? j - 1 : 0), file); if (j) write_palette (file, &gif->frames[frame].palette, j); LZW_encode (file, gif->frames[frame].bitmap_8_bit); pack_putc (0x00, file); /* Terminator. */ } pack_putc (0x3b, file); /* Trailer. */ pack_fclose (file); return 0; }
/*! \brief Save game 92 * * Save the game, using KQ Save Game Format 92 (Beta) * Author: Winter Knight * * \returns 0 if save failed, 1 if success */ static int save_game_92 (void) { size_t a, b, c, d; PACKFILE *sdat; for (b = 0; b < PSIZE; b++) { sid[save_ptr][b] = 0; shp[save_ptr][b] = 0; smp[save_ptr][b] = 0; slv[save_ptr][b] = 0; } for (b = 0; b < numchrs; b++) { sid[save_ptr][b] = pidx[b]; shp[save_ptr][b] = party[pidx[b]].hp * 100 / party[pidx[b]].mhp; if (party[pidx[b]].mmp > 0) smp[save_ptr][b] = party[pidx[b]].mp * 100 / party[pidx[b]].mmp; slv[save_ptr][b] = party[pidx[b]].lvl; } snc[save_ptr] = numchrs; sgp[save_ptr] = gp; smin[save_ptr] = kmin; shr[save_ptr] = khr; sprintf (strbuf, "sg%d.sav", save_ptr); sdat = pack_fopen (kqres (SAVE_DIR, strbuf), F_WRITE_PACKED); if (!sdat) { message (_("Could not save game data."), 255, 0, 0, 0); return 0; } pack_putc (kq_version, sdat); pack_iputl (gp, sdat); pack_iputw (shr[save_ptr], sdat); pack_iputw (smin[save_ptr], sdat); /* Save number of, and which characters are in the party */ pack_iputw (numchrs, sdat); for (a = 0; a < numchrs; a++) { pack_iputw (pidx[a], sdat); } /* Save number of, and data on all characters in game */ pack_iputw (MAXCHRS, sdat); for (a = 0; a < MAXCHRS; a++) { save_s_player (&party[a], sdat); } /* Save map name and location */ pack_iputw (strlen (curmap), sdat); pack_fwrite (curmap, strlen (curmap), sdat); pack_iputw (g_ent[0].tilex, sdat); pack_iputw (g_ent[0].tiley, sdat); /* Save quest info */ for (a = sizeof (progress); a > 0; a--) { if (progress[a - 1] > 0) break; } pack_iputw (a, sdat); for (b = 0; b < a; b++) pack_putc (progress[b], sdat); /* Save treasure info */ for (a = sizeof (treasure); a > 0; a--) { if (treasure[a - 1] > 0) break; } pack_iputw (a, sdat); for (b = 0; b < a; b++) pack_putc (treasure[b], sdat); /* Save spell info (P_REPULSE is 48) */ pack_iputw (sizeof (save_spells), sdat); for (a = 0; a < sizeof (save_spells); a++) { /* sizeof(save_spells) is 50 */ pack_putc (save_spells[a], sdat); } /* Save player inventory */ pack_iputw (MAX_INV, sdat); for (a = 0; a < MAX_INV; a++) { pack_iputw (g_inv[a][0], sdat); pack_iputw (g_inv[a][1], sdat); } /* Save special items */ for (a = MAX_SPECIAL_ITEMS + 1; a > 0; a--) { if (player_special_items[a - 1]) break; } pack_iputw (a, sdat); for (b = 0; b < a; b++) { pack_putc (player_special_items[b], sdat); } /* Save shop info (last visit time and number of items) */ /* Find last index of shop that the player has visited. */ for (a = num_shops; a > 0; a--) { if (shop_time[a - 1] > 0) break; } pack_iputw (a, sdat); for (b = 0; b < a; b++) { pack_iputw (shop_time[b], sdat); /* Find last valid (non-zero) shop item for this shop */ for (c = SHOPITEMS; c > 0; c--) if (shops[b].items[c - 1] > 0) break; pack_iputw (c, sdat); for (d = 0; d < c; d++) pack_iputw (shops[b].items_current[d], sdat); } pack_fclose (sdat); return 1; }
/*! \brief Save game * * You guessed it... save the game. * * \returns 0 if save failed, 1 if success */ static int save_game (void) { PACKFILE *sdat; size_t a, b; return save_game_92 (); /* Rest of this function is no longer used */ for (b = 0; b < PSIZE; b++) { sid[save_ptr][b] = 0; shp[save_ptr][b] = 0; smp[save_ptr][b] = 0; slv[save_ptr][b] = 0; } for (b = 0; b < numchrs; b++) { sid[save_ptr][b] = pidx[b]; shp[save_ptr][b] = party[pidx[b]].hp * 100 / party[pidx[b]].mhp; if (party[pidx[b]].mmp > 0) smp[save_ptr][b] = party[pidx[b]].mp * 100 / party[pidx[b]].mmp; slv[save_ptr][b] = party[pidx[b]].lvl; } snc[save_ptr] = numchrs; sgp[save_ptr] = gp; smin[save_ptr] = kmin; shr[save_ptr] = khr; sprintf (strbuf, "sg%d.sav", save_ptr); sdat = pack_fopen (kqres (SAVE_DIR, strbuf), F_WRITE_PACKED); if (!sdat) { message (_("Could not save game data."), 255, 0, 0, 0); return 0; } pack_putc (kq_version, sdat); pack_iputl (numchrs, sdat); pack_iputl (gp, sdat); pack_iputl (shr[save_ptr], sdat); pack_iputl (smin[save_ptr], sdat); for (a = 0; a < PSIZE; a++) { pack_iputl (pidx[a], sdat); } for (a = 0; a < MAXCHRS; a++) { save_s_player (&party[a], sdat); } pack_fwrite (curmap, 16, sdat); for (a = 0; a < sizeof (progress); a++) { /* sizeof(progress) is 1750 */ pack_putc (progress[a], sdat); } for (a = 0; a < NUMSHOPS; a++) { /* NUMSHOPS is 50 */ pack_putc (shop_time[a], sdat); } for (a = 0; a < SIZE_SAVE_RESERVE1; a++) { /* SAVE_RESERVE_SIZE1 is 150 */ pack_putc (0, sdat); } for (a = 0; a < sizeof (save_spells); a++) { /* sizeof(save_spells) is 50 */ pack_putc (save_spells[a], sdat); } for (a = 0; a < sizeof (treasure); a++) { /* sizeof(treasure) is 1000 */ pack_putc (treasure[a], sdat); } for (a = 0; a < NUMSHOPS; a++) { for (b = 0; b < SHOPITEMS; b++) { pack_iputw (shops[a].items_current[b], sdat); } } for (a = 0; a < MAX_INV; a++) { pack_iputw (g_inv[a][0], sdat); pack_iputw (g_inv[a][1], sdat); } /* PH FIXME: do we _really_ want things like controls and screen */ /* mode to be saved/loaded ? */ /* WK: No. */ pack_iputl (gsvol, sdat); pack_iputl (gmvol, sdat); pack_putc (windowed, sdat); pack_putc (stretch_view, sdat); pack_putc (wait_retrace, sdat); pack_iputl (kup, sdat); pack_iputl (kdown, sdat); pack_iputl (kleft, sdat); pack_iputl (kright, sdat); pack_iputl (kalt, sdat); pack_iputl (kctrl, sdat); pack_iputl (kenter, sdat); pack_iputl (kesc, sdat); pack_iputl (jbalt, sdat); pack_iputl (jbctrl, sdat); pack_iputl (jbenter, sdat); pack_iputl (jbesc, sdat); /* End worthless */ pack_iputw (g_ent[0].tilex, sdat); pack_iputw (g_ent[0].tiley, sdat); pack_fclose (sdat); return 1; }
int save_s_entity(s_entity *s, PACKFILE *f) { pack_putc(s->chrx, f); pack_putc(0, f); /* alignment */ pack_iputw(s->x, f); pack_iputw(s->y, f); pack_iputw(s->tilex, f); pack_iputw(s->tiley, f); pack_putc(s->eid, f); pack_putc(s->active, f); pack_putc(s->facing, f); pack_putc(s->moving, f); pack_putc(s->movcnt, f); pack_putc(s->framectr, f); pack_putc(s->movemode, f); pack_putc(s->obsmode, f); pack_putc(s->delay, f); pack_putc(s->delayctr, f); pack_putc(s->speed, f); pack_putc(s->scount, f); pack_putc(s->cmd, f); pack_putc(s->sidx, f); pack_putc(s->extra, f); pack_putc(s->chasing, f); pack_iputw(0, f); /* alignment */ pack_iputl(s->cmdnum, f); pack_putc(s->atype, f); pack_putc(s->snapback, f); pack_putc(s->facehero, f); pack_putc(s->transl, f); pack_fwrite(s->script, sizeof(s->script), f); return 0; }
void update_file(char *filename, char *dataname) { PACKFILE *f, *ef, *df = NULL; char *tmpname; int c; #ifdef ALLEGRO_HAVE_MKSTEMP char tmp_buf[] = "XXXXXX"; char tmp[512]; int tmp_fd; tmp_fd = mkstemp(tmp_buf); close(tmp_fd); tmpname = uconvert_ascii(tmp_buf, tmp); #else tmpname = tmpnam(NULL); #endif get_stats(); if (!err) { if ((!stat_hasdata) && (!dataname)) { fprintf(stderr, "%s has no appended data: cannot remove it\n", filename); err = 1; return; } rename_file(filename, tmpname); ef = pack_fopen(tmpname, F_READ); if (!ef) { delete_file(tmpname); fprintf(stderr, "Error reading temporary file\n"); err = 1; return; } f = pack_fopen(filename, F_WRITE); if (!f) { pack_fclose(ef); rename_file(tmpname, filename); fprintf(stderr, "Error writing to %s\n", filename); err = 1; return; } #ifdef ALLEGRO_UNIX chmod(filename, stat_stat.st_mode); #endif for (c=0; c<stat_exe_size; c++) pack_putc(pack_getc(ef), f); pack_fclose(ef); delete_file(tmpname); if (dataname) { if (stat_hasdata) printf("replacing %d bytes of currently appended data\n", stat_uncompressed_size); if (opt_allegro) { printf("reading %s as Allegro format compressed data\n", dataname); df = pack_fopen(dataname, F_READ_PACKED); } else if (opt_binary) { printf("reading %s as raw binary data\n", dataname); df = pack_fopen(dataname, F_READ); } else { printf("autodetecting format of %s: ", dataname); df = pack_fopen(dataname, F_READ_PACKED); if (df) { printf("Allegro compressed data\n"); } else { df = pack_fopen(dataname, F_READ); if (df) printf("raw binary data\n"); else printf("\n"); } } if (!df) { pack_fclose(f); fprintf(stderr, "Error opening %s\n", dataname); err = 1; return; } f = pack_fopen_chunk(f, opt_compress); while ((c = pack_getc(df)) != EOF) pack_putc(c, f); f = pack_fclose_chunk(f); pack_mputl(F_EXE_MAGIC, f); pack_mputl(_packfile_filesize+16, f); printf("%s has been appended onto %s\n" "original executable size: %d bytes (%dk)\n" "appended data size: %d bytes (%dk)\n" "compressed into: %d bytes (%dk)\n" "ratio: %d%%\n", dataname, filename, stat_exe_size, stat_exe_size/1024, _packfile_datasize, _packfile_datasize/1024, _packfile_filesize, _packfile_filesize/1024, (_packfile_datasize) ? _packfile_filesize*100/_packfile_datasize : 0); } else { printf("%d bytes of appended data removed from %s\n", stat_uncompressed_size, filename); } pack_fclose(f); pack_fclose(df); } }