| 1 |
/** |
| 2 |
* Editor screen |
| 3 |
|
| 4 |
* Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson |
| 5 |
* Copyright (C) 2005, 2007, 2008, 2009, 2014, 2015 Sylvain Beucler |
| 6 |
|
| 7 |
* This file is part of GNU FreeDink |
| 8 |
|
| 9 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation; either version 3 of the |
| 12 |
* License, or (at your option) any later version. |
| 13 |
|
| 14 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* General Public License for more details. |
| 18 |
|
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see |
| 21 |
* <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "editor_screen.h" |
| 31 |
#include "live_screen.h" /* cur_ed_screen */ |
| 32 |
#include "hardness_tiles.h" |
| 33 |
#include "gfx.h" |
| 34 |
#include "sfx.h" |
| 35 |
#include "log.h" |
| 36 |
#include "paths.h" |
| 37 |
|
| 38 |
/** |
| 39 |
* Return hardness index for this screen tile, either its default |
| 40 |
* hardness, or the replaced/alternative hardness. Tile is in [0,95]. |
| 41 |
*/ |
| 42 |
int realhard(int tile) |
| 43 |
{ |
| 44 |
if (cur_ed_screen.t[tile].althard > 0) |
| 45 |
return(cur_ed_screen.t[tile].althard); |
| 46 |
else |
| 47 |
return(hmap.btile_default[cur_ed_screen.t[tile].square_full_idx0]); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Fills a int[MAX_SPRITES_EDITOR] with the index of the current |
| 52 |
* screen's sprites, sorted by ascending height/queue. |
| 53 |
*/ |
| 54 |
void screen_rank_editor_sprites(int* rank) |
| 55 |
{ |
| 56 |
memset(rank, 0, MAX_SPRITES_EDITOR * sizeof(int)); |
| 57 |
|
| 58 |
int r1 = 0; |
| 59 |
int already_checked[MAX_SPRITES_EDITOR+1]; |
| 60 |
memset(already_checked, 0, sizeof(already_checked)); |
| 61 |
for (r1 = 0; r1 < MAX_SPRITES_EDITOR; r1++) |
| 62 |
{ |
| 63 |
int highest_sprite = 22000; //more than it could ever be |
| 64 |
rank[r1] = 0; |
| 65 |
|
| 66 |
int h1; |
| 67 |
for (h1 = 1; h1 <= MAX_SPRITES_EDITOR; h1++) |
| 68 |
{ |
| 69 |
if (already_checked[h1] == 0 && cur_ed_screen.sprite[h1].active) |
| 70 |
{ |
| 71 |
int height; |
| 72 |
if (cur_ed_screen.sprite[h1].que != 0) |
| 73 |
height = cur_ed_screen.sprite[h1].que; |
| 74 |
else |
| 75 |
height = cur_ed_screen.sprite[h1].y; |
| 76 |
|
| 77 |
if (height < highest_sprite) |
| 78 |
{ |
| 79 |
highest_sprite = height; |
| 80 |
rank[r1] = h1; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
if (rank[r1] != 0) |
| 85 |
already_checked[rank[r1]] = 1; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Load 1 screen from specified map.dat in specified memory buffer |
| 91 |
*/ |
| 92 |
int load_screen_to(const char* path, const int num, struct editor_screen* screen) |
| 93 |
{ |
| 94 |
char skipbuf[10000]; // more than any fseek we do |
| 95 |
|
| 96 |
FILE *f = NULL; |
| 97 |
long holdme,lsize; |
| 98 |
f = paths_dmodfile_fopen(path, "rb"); |
| 99 |
if (!f) |
| 100 |
{ |
| 101 |
log_error("Cannot find %s file!!!", path); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
lsize = 31280; // sizeof(struct editor_screen); // under i386, not portable |
| 105 |
holdme = (lsize * (num-1)); |
| 106 |
fseek(f, holdme, SEEK_SET); |
| 107 |
//Msg("Trying to read %d bytes with offset of %d",lsize,holdme); |
| 108 |
|
| 109 |
/* Portably load screen structure from disk */ |
| 110 |
int i = 0; |
| 111 |
fread(skipbuf, 20, 1, f); // unused 'name' field |
| 112 |
for (i = 0; i < 97; i++) |
| 113 |
{ |
| 114 |
screen->t[i].square_full_idx0 = read_lsb_int(f); |
| 115 |
fread(skipbuf, 4, 1, f); // unused 'property' field |
| 116 |
screen->t[i].althard = read_lsb_int(f); |
| 117 |
fread(skipbuf, 6, 1, f); // unused 'more2', 'more3', 'more4' fields |
| 118 |
fread(skipbuf, 2, 1, f); // reproduce memory alignment |
| 119 |
fread(skipbuf, 60, 1, f); // unused 'buff' field |
| 120 |
} |
| 121 |
// offset 7780 |
| 122 |
|
| 123 |
fread(skipbuf, 160, 1, f); // unused 'v' field |
| 124 |
fread(skipbuf, 80, 1, f); // unused 's' field |
| 125 |
// offset 8020 |
| 126 |
|
| 127 |
/* struct sprite_placement sprite[101]; */ |
| 128 |
/* size = 220 */ |
| 129 |
for (i = 0; i < 101; i++) |
| 130 |
{ |
| 131 |
screen->sprite[i].x = read_lsb_int(f); |
| 132 |
screen->sprite[i].y = read_lsb_int(f); |
| 133 |
screen->sprite[i].seq = read_lsb_int(f); |
| 134 |
screen->sprite[i].frame = read_lsb_int(f); |
| 135 |
screen->sprite[i].type = read_lsb_int(f); |
| 136 |
screen->sprite[i].size = read_lsb_int(f); |
| 137 |
|
| 138 |
screen->sprite[i].active = fgetc(f); |
| 139 |
fread(skipbuf, 3, 1, f); // reproduce memory alignment |
| 140 |
// offset 28 |
| 141 |
|
| 142 |
screen->sprite[i].rotation = read_lsb_int(f); |
| 143 |
screen->sprite[i].special = read_lsb_int(f); |
| 144 |
screen->sprite[i].brain = read_lsb_int(f); |
| 145 |
|
| 146 |
fread(screen->sprite[i].script, 14, 1, f); |
| 147 |
screen->sprite[i].script[14-1] = '\0'; // safety |
| 148 |
fread(skipbuf, 38, 1, f); // unused hit/die/talk fields |
| 149 |
// offset 92 |
| 150 |
|
| 151 |
screen->sprite[i].speed = read_lsb_int(f); |
| 152 |
screen->sprite[i].base_walk = read_lsb_int(f); |
| 153 |
screen->sprite[i].base_idle = read_lsb_int(f); |
| 154 |
screen->sprite[i].base_attack = read_lsb_int(f); |
| 155 |
screen->sprite[i].base_hit = read_lsb_int(f); |
| 156 |
screen->sprite[i].timing = read_lsb_int(f); |
| 157 |
screen->sprite[i].que = read_lsb_int(f); |
| 158 |
screen->sprite[i].hard = read_lsb_int(f); |
| 159 |
// offset 124 |
| 160 |
|
| 161 |
screen->sprite[i].alt.left = read_lsb_int(f); |
| 162 |
screen->sprite[i].alt.top = read_lsb_int(f); |
| 163 |
screen->sprite[i].alt.right = read_lsb_int(f); |
| 164 |
screen->sprite[i].alt.bottom = read_lsb_int(f); |
| 165 |
// offset 140 |
| 166 |
|
| 167 |
screen->sprite[i].is_warp = read_lsb_int(f); |
| 168 |
screen->sprite[i].warp_map = read_lsb_int(f); |
| 169 |
screen->sprite[i].warp_x = read_lsb_int(f); |
| 170 |
screen->sprite[i].warp_y = read_lsb_int(f); |
| 171 |
screen->sprite[i].parm_seq = read_lsb_int(f); |
| 172 |
// offset 160 |
| 173 |
|
| 174 |
screen->sprite[i].base_die = read_lsb_int(f); |
| 175 |
screen->sprite[i].gold = read_lsb_int(f); |
| 176 |
screen->sprite[i].hitpoints = read_lsb_int(f); |
| 177 |
screen->sprite[i].strength = read_lsb_int(f); |
| 178 |
screen->sprite[i].defense = read_lsb_int(f); |
| 179 |
screen->sprite[i].exp = read_lsb_int(f); |
| 180 |
screen->sprite[i].sound = read_lsb_int(f); |
| 181 |
screen->sprite[i].vision = read_lsb_int(f); |
| 182 |
screen->sprite[i].nohit = read_lsb_int(f); |
| 183 |
screen->sprite[i].touch_damage = read_lsb_int(f); |
| 184 |
// offset 200 |
| 185 |
|
| 186 |
int j = 0; |
| 187 |
for (j = 0; j < 5; j++) |
| 188 |
read_lsb_int(f); |
| 189 |
} |
| 190 |
// offset 30204 |
| 191 |
|
| 192 |
fread(screen->script, 21, 1, f); |
| 193 |
screen->script[21-1] = '\0'; // safety |
| 194 |
fread(skipbuf, 1018, 1, f); // unused hit/die/talk fields |
| 195 |
fread(skipbuf, 1, 1, f); // reproduce memory alignment |
| 196 |
// offset 31280 |
| 197 |
|
| 198 |
fclose(f); |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Save screen number 'num' in the map. Only used by the editor. |
| 204 |
*/ |
| 205 |
void save_screen(const char* path, const int num) |
| 206 |
{ |
| 207 |
char skipbuf[10000]; // more than any fseek we do |
| 208 |
memset(skipbuf, 0, 10000); |
| 209 |
|
| 210 |
FILE *f = NULL; |
| 211 |
long holdme,lsize; |
| 212 |
|
| 213 |
log_info("Saving screen data.."); |
| 214 |
if (num > 0) |
| 215 |
{ |
| 216 |
f = paths_dmodfile_fopen(path, "r+b"); |
| 217 |
if (f == NULL) |
| 218 |
{ |
| 219 |
perror("Cannot save screen"); |
| 220 |
return; |
| 221 |
} |
| 222 |
lsize = 31280; // sizeof(struct editor_screen); // under ia32, not portable |
| 223 |
holdme = (lsize * (num-1)); |
| 224 |
fseek(f, holdme, SEEK_SET); |
| 225 |
|
| 226 |
|
| 227 |
/* Portably dump screen structure */ |
| 228 |
int i = 0; |
| 229 |
char name[20] = "Smallwood"; |
| 230 |
fwrite(name, 20, 1, f); |
| 231 |
for (i = 0; i < 97; i++) |
| 232 |
{ |
| 233 |
write_lsb_int(cur_ed_screen.t[i].square_full_idx0, f); |
| 234 |
fwrite(skipbuf, 4, 1, f); // unused 'property' field |
| 235 |
write_lsb_int(cur_ed_screen.t[i].althard, f); |
| 236 |
fwrite(skipbuf, 6, 1, f); // unused 'more2', 'more3', 'more4' fields |
| 237 |
fwrite(skipbuf, 2, 1, f); // reproduce memory alignment |
| 238 |
fwrite(skipbuf, 60, 1, f); // unused 'buff' field |
| 239 |
} |
| 240 |
// offset 7780 |
| 241 |
|
| 242 |
fwrite(skipbuf, 160, 1, f); // unused 'v' field |
| 243 |
fwrite(skipbuf, 80, 1, f); // unused 's' field |
| 244 |
// offset 8020 |
| 245 |
|
| 246 |
/* struct sprite_placement sprite[101]; */ |
| 247 |
/* size = 220 */ |
| 248 |
for (i = 0; i < 101; i++) |
| 249 |
{ |
| 250 |
write_lsb_int(cur_ed_screen.sprite[i].x, f); |
| 251 |
write_lsb_int(cur_ed_screen.sprite[i].y, f); |
| 252 |
write_lsb_int(cur_ed_screen.sprite[i].seq, f); |
| 253 |
write_lsb_int(cur_ed_screen.sprite[i].frame, f); |
| 254 |
write_lsb_int(cur_ed_screen.sprite[i].type, f); |
| 255 |
write_lsb_int(cur_ed_screen.sprite[i].size, f); |
| 256 |
|
| 257 |
fputc(cur_ed_screen.sprite[i].active, f); |
| 258 |
fwrite(skipbuf, 3, 1, f); // reproduce memory alignment |
| 259 |
// offset 28 |
| 260 |
|
| 261 |
write_lsb_int(cur_ed_screen.sprite[i].rotation, f); |
| 262 |
write_lsb_int(cur_ed_screen.sprite[i].special, f); |
| 263 |
write_lsb_int(cur_ed_screen.sprite[i].brain, f); |
| 264 |
|
| 265 |
fwrite(cur_ed_screen.sprite[i].script, 14, 1, f); |
| 266 |
fwrite(skipbuf, 38, 1, f); // reproduce memory alignment |
| 267 |
// offset 92 |
| 268 |
|
| 269 |
write_lsb_int(cur_ed_screen.sprite[i].speed, f); |
| 270 |
write_lsb_int(cur_ed_screen.sprite[i].base_walk, f); |
| 271 |
write_lsb_int(cur_ed_screen.sprite[i].base_idle, f); |
| 272 |
write_lsb_int(cur_ed_screen.sprite[i].base_attack, f); |
| 273 |
write_lsb_int(cur_ed_screen.sprite[i].base_hit, f); |
| 274 |
write_lsb_int(cur_ed_screen.sprite[i].timing, f); |
| 275 |
write_lsb_int(cur_ed_screen.sprite[i].que, f); |
| 276 |
write_lsb_int(cur_ed_screen.sprite[i].hard, f); |
| 277 |
// offset 124 |
| 278 |
|
| 279 |
write_lsb_int(cur_ed_screen.sprite[i].alt.left, f); |
| 280 |
write_lsb_int(cur_ed_screen.sprite[i].alt.top, f); |
| 281 |
write_lsb_int(cur_ed_screen.sprite[i].alt.right, f); |
| 282 |
write_lsb_int(cur_ed_screen.sprite[i].alt.bottom, f); |
| 283 |
// offset 140 |
| 284 |
|
| 285 |
write_lsb_int(cur_ed_screen.sprite[i].is_warp, f); |
| 286 |
write_lsb_int(cur_ed_screen.sprite[i].warp_map, f); |
| 287 |
write_lsb_int(cur_ed_screen.sprite[i].warp_x, f); |
| 288 |
write_lsb_int(cur_ed_screen.sprite[i].warp_y, f); |
| 289 |
write_lsb_int(cur_ed_screen.sprite[i].parm_seq, f); |
| 290 |
// offset 160 |
| 291 |
|
| 292 |
write_lsb_int(cur_ed_screen.sprite[i].base_die, f); |
| 293 |
write_lsb_int(cur_ed_screen.sprite[i].gold, f); |
| 294 |
write_lsb_int(cur_ed_screen.sprite[i].hitpoints, f); |
| 295 |
write_lsb_int(cur_ed_screen.sprite[i].strength, f); |
| 296 |
write_lsb_int(cur_ed_screen.sprite[i].defense, f); |
| 297 |
write_lsb_int(cur_ed_screen.sprite[i].exp, f); |
| 298 |
write_lsb_int(cur_ed_screen.sprite[i].sound, f); |
| 299 |
write_lsb_int(cur_ed_screen.sprite[i].vision, f); |
| 300 |
write_lsb_int(cur_ed_screen.sprite[i].nohit, f); |
| 301 |
write_lsb_int(cur_ed_screen.sprite[i].touch_damage, f); |
| 302 |
// offset 200 |
| 303 |
|
| 304 |
int j = 0; |
| 305 |
for (j = 0; j < 5; j++) |
| 306 |
write_lsb_int(0, f); |
| 307 |
} |
| 308 |
// offset 30204 |
| 309 |
|
| 310 |
fwrite(cur_ed_screen.script, 21, 1, f); |
| 311 |
fwrite(skipbuf, 1018, 1, f); // unused random/load/buffer fields |
| 312 |
fwrite(skipbuf, 1, 1, f); // reproduce memory alignment |
| 313 |
// offset 31280 |
| 314 |
|
| 315 |
fclose(f); |
| 316 |
} |
| 317 |
|
| 318 |
log_info("Done saving screen data.."); |
| 319 |
} |