| 1 |
/** |
| 2 |
* Game-specific processing |
| 3 |
|
| 4 |
* Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson |
| 5 |
* Copyright (C) 2008, 2009, 2010, 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 <stdlib.h> /* srand */ |
| 29 |
#include <time.h> /* time */ |
| 30 |
#include <string.h> /* memset */ |
| 31 |
#include "game_engine.h" |
| 32 |
#include "live_sprites_manager.h" |
| 33 |
#include "live_screen.h" |
| 34 |
#include "DMod.h" /* g_dmod.map */ |
| 35 |
#include "live_screen.h" /* screen_hitmap */ |
| 36 |
#include "dinkini.h" /* check_seq_status, hmap, cur_ed_screen */ |
| 37 |
#include "freedink.h" /* add_time_to_saved_game */ |
| 38 |
#include "input.h" |
| 39 |
#include "log.h" |
| 40 |
#include "sfx.h" |
| 41 |
#include "bgm.h" |
| 42 |
#include "IOGfxPrimitives.h" |
| 43 |
#include "IOGfxDisplay.h" |
| 44 |
#include "gfx.h" |
| 45 |
#include "gfx_sprites.h" |
| 46 |
#include "hardness_tiles.h" |
| 47 |
#include "savegame.h" |
| 48 |
#include "meminfo.h" |
| 49 |
#include "dinkc.h" |
| 50 |
#include "dinkc_bindings.h" |
| 51 |
#include "game_state.h" |
| 52 |
#include "status.h" |
| 53 |
|
| 54 |
|
| 55 |
int dversion = 108; |
| 56 |
|
| 57 |
int walk_off_screen = /*false*/0; |
| 58 |
|
| 59 |
int show_inventory = 0; // display inventory? |
| 60 |
int warp_editor_sprite = -500; |
| 61 |
|
| 62 |
int stop_entire_game; |
| 63 |
|
| 64 |
int screenlock = 0; |
| 65 |
|
| 66 |
struct wait_for_button wait4b; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
char* dversion_string; |
| 71 |
|
| 72 |
int last_saved_game = 0; |
| 73 |
char save_game_info[200] = "Level &level"; |
| 74 |
|
| 75 |
time_t time_start; |
| 76 |
|
| 77 |
|
| 78 |
int smooth_follow = 0; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
/** Fadedown / fadeup **/ |
| 83 |
/* Tell the engine that we're warping */ |
| 84 |
/* (quick fadedown + black + fadeup) */ |
| 85 |
int process_warp = 0; |
| 86 |
/* Tell the engine that we're fading down */ |
| 87 |
/* or fading up */ |
| 88 |
int process_downcycle = 0; |
| 89 |
int process_upcycle = 0; |
| 90 |
/* When the fadedown must finish: */ |
| 91 |
unsigned long cycle_clock = 0; |
| 92 |
/* What script to resume after fade: */ |
| 93 |
int cycle_script = 0; |
| 94 |
|
| 95 |
/* Engine is currently executing a screen's main() - display "Please |
| 96 |
Wait" when loading graphics and postpone other scripts */ |
| 97 |
/*bool*/int screen_main_is_running = /*false*/0; |
| 98 |
static int please_wait_toggle_frame = 7; |
| 99 |
|
| 100 |
|
| 101 |
static int high_speed = 0; |
| 102 |
struct player_info play; |
| 103 |
|
| 104 |
struct attackinfo_struct bow; |
| 105 |
|
| 106 |
/* Number of ms since an arbitrarily fixed point */ |
| 107 |
Uint32 thisTickCount; |
| 108 |
Uint32 lastTickCount; |
| 109 |
int fps_final = 0; |
| 110 |
int base_timing = 0; |
| 111 |
|
| 112 |
int keep_mouse = 0; |
| 113 |
int mode = 0; |
| 114 |
|
| 115 |
void game_restart() |
| 116 |
{ |
| 117 |
int mainscript; |
| 118 |
lsm_kill_all_nonlive_sprites(); |
| 119 |
kill_repeat_sounds_all(); |
| 120 |
kill_all_scripts_for_real(); |
| 121 |
set_mode(0); |
| 122 |
screenlock = 0; |
| 123 |
|
| 124 |
/* Reset all game state */ |
| 125 |
memset(&play, 0, sizeof(play)); |
| 126 |
|
| 127 |
memset(&screen_hitmap, 0, sizeof(screen_hitmap)); |
| 128 |
input_set_default_buttons(); |
| 129 |
|
| 130 |
mainscript = load_script("main", 0); |
| 131 |
|
| 132 |
locate(mainscript, "main"); |
| 133 |
run_script(mainscript); |
| 134 |
//lets attach our vars to the scripts |
| 135 |
attach(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
void game_compute_speed() { |
| 140 |
//Use to test at 30 fps |
| 141 |
//Sleep(66); |
| 142 |
|
| 143 |
lastTickCount = thisTickCount; |
| 144 |
thisTickCount = game_GetTicks(); |
| 145 |
|
| 146 |
fps_final = thisTickCount - lastTickCount; |
| 147 |
|
| 148 |
//redink1 changed to 12-12 from 10-15... maybe work better on faster computers? |
| 149 |
if (fps_final < 12) |
| 150 |
fps_final = 12; |
| 151 |
if (fps_final > 68) |
| 152 |
fps_final = 68; |
| 153 |
|
| 154 |
|
| 155 |
base_timing = fps_final / 3; |
| 156 |
if (base_timing < 4) |
| 157 |
base_timing = 4; |
| 158 |
|
| 159 |
|
| 160 |
int new_dinkspeed; |
| 161 |
|
| 162 |
//redink1 added these changes to set Dink's speed correctly, |
| 163 |
//even on fast machines. |
| 164 |
//new_dinkspeed = (fps_average / dinkspeed) - (fps_average / 8); |
| 165 |
if (dinkspeed <= 0) |
| 166 |
new_dinkspeed = 0; |
| 167 |
else if (dinkspeed == 1) |
| 168 |
new_dinkspeed = 12; |
| 169 |
else if (dinkspeed == 2) |
| 170 |
new_dinkspeed = 6; |
| 171 |
else if (dinkspeed == 3) |
| 172 |
new_dinkspeed = 3; |
| 173 |
else |
| 174 |
new_dinkspeed = 1; |
| 175 |
|
| 176 |
//redink1... weird bug, only do this for normal mode, as it |
| 177 |
//breaks mouse support |
| 178 |
//if (mode == 2) |
| 179 |
new_dinkspeed *= (base_timing / 4); |
| 180 |
|
| 181 |
spr[1].speed = new_dinkspeed; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Fake SDL_GetTicks if the player is in high-speed mode. Make sure |
| 186 |
* you call it once per frame. |
| 187 |
*/ |
| 188 |
Uint32 game_GetTicks() |
| 189 |
{ |
| 190 |
static Uint32 last_sdl_ticks = 0; |
| 191 |
static Uint32 high_ticks = 0; |
| 192 |
|
| 193 |
Uint32 cur_sdl_ticks = SDL_GetTicks(); |
| 194 |
/* Work-around incorrect initial value */ |
| 195 |
if (last_sdl_ticks == 0) |
| 196 |
last_sdl_ticks = cur_sdl_ticks - 1; |
| 197 |
|
| 198 |
/* If high speed, then count every tick as triple (so add 2x) */ |
| 199 |
if (high_speed) |
| 200 |
{ |
| 201 |
high_ticks += 2 * (cur_sdl_ticks - last_sdl_ticks); |
| 202 |
} |
| 203 |
|
| 204 |
last_sdl_ticks = cur_sdl_ticks; |
| 205 |
return cur_sdl_ticks + high_ticks; |
| 206 |
} |
| 207 |
|
| 208 |
void game_set_high_speed() |
| 209 |
{ |
| 210 |
if (high_speed == 0) |
| 211 |
{ |
| 212 |
SDL_setFramerate(&framerate_manager, 3*FPS); |
| 213 |
high_speed = 1; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void game_set_normal_speed() |
| 218 |
{ |
| 219 |
if (high_speed == 1) |
| 220 |
{ |
| 221 |
SDL_setFramerate(&framerate_manager, FPS); |
| 222 |
high_speed = 0; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
int next_raise(void) |
| 228 |
{ |
| 229 |
int crap = *plevel; |
| 230 |
int num = ((100 * crap) * crap); |
| 231 |
|
| 232 |
if (num > 99999) num = 99999; |
| 233 |
return(num); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
/** |
| 238 |
* Add experience - no "did the player really kill this enemy?" |
| 239 |
* checks |
| 240 |
*/ |
| 241 |
void add_exp_force(int num, int source_sprite) |
| 242 |
{ |
| 243 |
if (num > 0) |
| 244 |
{ |
| 245 |
//add experience |
| 246 |
*pexp += num; |
| 247 |
|
| 248 |
int crap2 = add_sprite(spr[source_sprite].x, spr[source_sprite].y, 8, 0, 0); |
| 249 |
spr[crap2].y -= k[seq[spr[source_sprite].pseq].frame[spr[source_sprite].pframe]].yoffset; |
| 250 |
spr[crap2].x -= k[seq[spr[source_sprite].pseq].frame[spr[source_sprite].pframe]].xoffset; |
| 251 |
spr[crap2].y -= k[seq[spr[source_sprite].pseq].frame[spr[source_sprite].pframe]].box.bottom / 3; |
| 252 |
spr[crap2].x += k[seq[spr[source_sprite].pseq].frame[spr[source_sprite].pframe]].box.right / 5; |
| 253 |
spr[crap2].y -= 30; |
| 254 |
spr[crap2].speed = 1; |
| 255 |
spr[crap2].hard = 1; |
| 256 |
spr[crap2].brain_parm = 5000; |
| 257 |
spr[crap2].my = -1; |
| 258 |
spr[crap2].kill_ttl = 1000; |
| 259 |
spr[crap2].dir = 8; |
| 260 |
spr[crap2].damage = num; |
| 261 |
|
| 262 |
if (*pexp > 99999) |
| 263 |
*pexp = 99999; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void add_exp(int num, int killed_sprite) |
| 268 |
{ |
| 269 |
if (spr[killed_sprite].last_hit != 1) |
| 270 |
return; |
| 271 |
|
| 272 |
add_exp_force(num, killed_sprite); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Resurrect sprites that were temporarily disabled |
| 278 |
* (editor_type(6/7/8)) |
| 279 |
*/ |
| 280 |
void fix_dead_sprites() |
| 281 |
{ |
| 282 |
int i; |
| 283 |
|
| 284 |
for (i = 1; i < 100; i++) |
| 285 |
{ |
| 286 |
int type = play.spmap[*pplayer_map].type[i]; |
| 287 |
|
| 288 |
// Resurrect sprites after 5mn |
| 289 |
if (type == 6) |
| 290 |
{ |
| 291 |
if ((thisTickCount > (play.spmap[*pplayer_map].last_time + 300000)) |
| 292 |
|| (thisTickCount + 400000 < play.spmap[*pplayer_map].last_time + 300000)) |
| 293 |
{ |
| 294 |
//this sprite can come back online now |
| 295 |
play.spmap[*pplayer_map].type[i] = 0; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// Resurrect sprites after 3mn |
| 300 |
if (type == 7) |
| 301 |
{ |
| 302 |
if (thisTickCount > (play.spmap[*pplayer_map].last_time + 180000)) |
| 303 |
{ |
| 304 |
//this sprite can come back online now |
| 305 |
play.spmap[*pplayer_map].type[i] = 0; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
// Resurrect sprites after 1mn |
| 310 |
if (type == 8) |
| 311 |
{ |
| 312 |
if (thisTickCount > (play.spmap[*pplayer_map].last_time + 60000)) |
| 313 |
{ |
| 314 |
//this sprite can come back online now |
| 315 |
play.spmap[*pplayer_map].type[i] = 0; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* When entering a screen, play the appropriate MIDI |
| 323 |
*/ |
| 324 |
void check_midi(void) |
| 325 |
{ |
| 326 |
// TODO: use a better constant (like max_file_path) |
| 327 |
char midi_filename[20]; |
| 328 |
|
| 329 |
if ((!midi_active) || (g_dmod.map.music[*pplayer_map] == 0)) |
| 330 |
return; |
| 331 |
|
| 332 |
/* There is music information associated with this screen */ |
| 333 |
if (g_dmod.map.music[*pplayer_map] != -1) { |
| 334 |
if (g_dmod.map.music[*pplayer_map] > 1000) |
| 335 |
/* Try to play a CD track (unsupported) - fall back to MIDI */ |
| 336 |
{ |
| 337 |
sprintf(midi_filename, "%d.mid", g_dmod.map.music[*pplayer_map] - 1000); |
| 338 |
PlayMidi(midi_filename); |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
/* Just play the specified MIDI */ |
| 343 |
sprintf(midi_filename, "%d.mid", g_dmod.map.music[*pplayer_map]); |
| 344 |
PlayMidi(midi_filename); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Load 1 screen from map.dat, which contains all 768 game screens |
| 351 |
*/ |
| 352 |
int game_load_screen(int mapdat_num) |
| 353 |
{ |
| 354 |
if (g_dmod.map.ts_loc_mem[mapdat_num] != NULL) { |
| 355 |
memcpy(&cur_ed_screen, g_dmod.map.ts_loc_mem[mapdat_num], sizeof(struct editor_screen)); |
| 356 |
} else if (load_screen_to(g_dmod.map.map_dat.c_str(), mapdat_num, &cur_ed_screen) < 0) { |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
spr[1].move_active = 0; |
| 361 |
if (dversion >= 108) |
| 362 |
spr[1].move_nohard = 0; |
| 363 |
spr[1].freeze = 0; |
| 364 |
screenlock = 0; |
| 365 |
|
| 366 |
fill_whole_hard(); |
| 367 |
|
| 368 |
fix_dead_sprites(); |
| 369 |
check_midi(); |
| 370 |
|
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* Remember last time we entered this screen (so we can disable |
| 377 |
* sprites for some minutes, e.g. monsters) |
| 378 |
*/ |
| 379 |
void update_screen_time() |
| 380 |
{ |
| 381 |
//Msg("Cur time is %d", play.spmap[*pplayer_map].last_time); |
| 382 |
//Msg("Map is %d..", *pplayer_map); |
| 383 |
play.spmap[*pplayer_map].last_time = thisTickCount; |
| 384 |
//Msg("Time was saved as %d", play.spmap[*pplayer_map].last_time); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
void update_play_changes( void ) |
| 389 |
{ |
| 390 |
int j; |
| 391 |
for (j = 1; j < 100; j++) |
| 392 |
{ |
| 393 |
if (cur_ed_screen.sprite[j].active) |
| 394 |
if (play.spmap[*pplayer_map].type[j] != 0) |
| 395 |
{ |
| 396 |
//lets make some changes, player has extra info |
| 397 |
if (play.spmap[*pplayer_map].type[j] == 1) |
| 398 |
{ |
| 399 |
cur_ed_screen.sprite[j].active = 0; |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
if (play.spmap[*pplayer_map].type[j] == 2) |
| 404 |
{ |
| 405 |
cur_ed_screen.sprite[j].type = 1; |
| 406 |
cur_ed_screen.sprite[j].hard = 1; |
| 407 |
} |
| 408 |
if (play.spmap[*pplayer_map].type[j] == 3) |
| 409 |
{ |
| 410 |
|
| 411 |
// Msg("Changing sprite %d", j); |
| 412 |
cur_ed_screen.sprite[j].type = 0; |
| 413 |
cur_ed_screen.sprite[j].hard = 1; |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
if (play.spmap[*pplayer_map].type[j] == 4) |
| 418 |
{ |
| 419 |
cur_ed_screen.sprite[j].type = 1; |
| 420 |
cur_ed_screen.sprite[j].hard = 0; |
| 421 |
} |
| 422 |
|
| 423 |
if (play.spmap[*pplayer_map].type[j] == 5) |
| 424 |
{ |
| 425 |
cur_ed_screen.sprite[j].type = 0; |
| 426 |
cur_ed_screen.sprite[j].hard = 0; |
| 427 |
} |
| 428 |
|
| 429 |
if (play.spmap[*pplayer_map].type[j] == 6) |
| 430 |
{ |
| 431 |
cur_ed_screen.sprite[j].active = 0; |
| 432 |
|
| 433 |
} |
| 434 |
if (play.spmap[*pplayer_map].type[j] == 7) |
| 435 |
{ |
| 436 |
cur_ed_screen.sprite[j].active = 0; |
| 437 |
|
| 438 |
} |
| 439 |
if (play.spmap[*pplayer_map].type[j] == 8) |
| 440 |
{ |
| 441 |
cur_ed_screen.sprite[j].active = 0; |
| 442 |
|
| 443 |
} |
| 444 |
|
| 445 |
cur_ed_screen.sprite[j].seq = play.spmap[*pplayer_map].seq[j]; |
| 446 |
cur_ed_screen.sprite[j].frame = play.spmap[*pplayer_map].frame[j]; |
| 447 |
strcpy(cur_ed_screen.sprite[j].script, ""); |
| 448 |
|
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
/** |
| 458 |
* Load screen sprites: draw background sprites, ordered by queue and |
| 459 |
* configure the others (sounds, scripts, etc.). |
| 460 |
* |
| 461 |
* Also cf. game_place_sprites_background(...) and editor's |
| 462 |
* place_sprites(...). |
| 463 |
*/ |
| 464 |
void game_place_sprites() |
| 465 |
{ |
| 466 |
update_play_changes(); |
| 467 |
|
| 468 |
int rank[MAX_SPRITES_EDITOR]; |
| 469 |
screen_rank_editor_sprites(rank); |
| 470 |
|
| 471 |
int r1 = 0; |
| 472 |
for (; r1 < MAX_SPRITES_EDITOR && rank[r1] > 0; r1++) |
| 473 |
{ |
| 474 |
//Msg("Ok, rank[%d] is %d.",oo,rank[oo]); |
| 475 |
int j = rank[r1]; |
| 476 |
|
| 477 |
if (cur_ed_screen.sprite[j].active == 1 |
| 478 |
&& (cur_ed_screen.sprite[j].vision == 0 || cur_ed_screen.sprite[j].vision == *pvision)) |
| 479 |
{ |
| 480 |
check_seq_status(cur_ed_screen.sprite[j].seq); |
| 481 |
|
| 482 |
//we have instructions to make a sprite |
| 483 |
if (cur_ed_screen.sprite[j].type == 0 || cur_ed_screen.sprite[j].type == 2) |
| 484 |
{ |
| 485 |
//make it part of the background (much faster) |
| 486 |
int sprite = add_sprite_dumb(cur_ed_screen.sprite[j].x,cur_ed_screen.sprite[j].y, 0, |
| 487 |
cur_ed_screen.sprite[j].seq,cur_ed_screen.sprite[j].frame, |
| 488 |
cur_ed_screen.sprite[j].size); |
| 489 |
|
| 490 |
spr[sprite].hard = cur_ed_screen.sprite[j].hard; |
| 491 |
spr[sprite].sp_index = j; |
| 492 |
rect_copy(&spr[sprite].alt , &cur_ed_screen.sprite[j].alt); |
| 493 |
|
| 494 |
check_sprite_status_full(sprite); |
| 495 |
|
| 496 |
if (cur_ed_screen.sprite[j].type == 0) |
| 497 |
draw_sprite_game(IOGFX_background, sprite); |
| 498 |
|
| 499 |
if (spr[sprite].hard == 0) |
| 500 |
{ |
| 501 |
/*if (cur_ed_screen.sprite[j].is_warp == 0) |
| 502 |
add_hardness(sprite, 1); else */ |
| 503 |
add_hardness(sprite, 100 + j); |
| 504 |
} |
| 505 |
lsm_remove_sprite(sprite); |
| 506 |
} |
| 507 |
|
| 508 |
if (cur_ed_screen.sprite[j].type == 1) |
| 509 |
{ |
| 510 |
//make it a living sprite |
| 511 |
int sprite = add_sprite_dumb(cur_ed_screen.sprite[j].x,cur_ed_screen.sprite[j].y, 0, |
| 512 |
cur_ed_screen.sprite[j].seq,cur_ed_screen.sprite[j].frame, |
| 513 |
cur_ed_screen.sprite[j].size); |
| 514 |
|
| 515 |
spr[sprite].hard = cur_ed_screen.sprite[j].hard; |
| 516 |
|
| 517 |
//assign addition parms to the new sprite |
| 518 |
spr[sprite].sp_index = j; |
| 519 |
|
| 520 |
spr[sprite].brain = cur_ed_screen.sprite[j].brain; |
| 521 |
spr[sprite].speed = cur_ed_screen.sprite[j].speed; |
| 522 |
spr[sprite].base_walk = cur_ed_screen.sprite[j].base_walk; |
| 523 |
spr[sprite].base_idle = cur_ed_screen.sprite[j].base_idle; |
| 524 |
spr[sprite].base_attack = cur_ed_screen.sprite[j].base_attack; |
| 525 |
spr[sprite].base_hit = cur_ed_screen.sprite[j].base_hit; |
| 526 |
spr[sprite].hard = cur_ed_screen.sprite[j].hard; |
| 527 |
spr[sprite].timing = cur_ed_screen.sprite[j].timing; |
| 528 |
spr[sprite].que = cur_ed_screen.sprite[j].que; |
| 529 |
|
| 530 |
|
| 531 |
spr[sprite].sp_index = j; |
| 532 |
|
| 533 |
rect_copy(&spr[sprite].alt , &cur_ed_screen.sprite[j].alt); |
| 534 |
|
| 535 |
spr[sprite].base_die = cur_ed_screen.sprite[j].base_die; |
| 536 |
spr[sprite].strength = cur_ed_screen.sprite[j].strength; |
| 537 |
spr[sprite].defense = cur_ed_screen.sprite[j].defense; |
| 538 |
spr[sprite].gold = cur_ed_screen.sprite[j].gold; |
| 539 |
spr[sprite].exp = cur_ed_screen.sprite[j].exp; |
| 540 |
spr[sprite].nohit = cur_ed_screen.sprite[j].nohit; |
| 541 |
spr[sprite].touch_damage = cur_ed_screen.sprite[j].touch_damage; |
| 542 |
spr[sprite].hitpoints = cur_ed_screen.sprite[j].hitpoints; |
| 543 |
spr[sprite].sound = cur_ed_screen.sprite[j].sound; |
| 544 |
check_sprite_status_full(sprite); |
| 545 |
if (cur_ed_screen.sprite[j].is_warp == 0 && spr[sprite].sound != 0) |
| 546 |
{ |
| 547 |
//make looping sound |
| 548 |
log_debug("making sound with sprite %d..", sprite); |
| 549 |
SoundPlayEffect( spr[sprite].sound,22050, 0,sprite, 1); |
| 550 |
} |
| 551 |
if (spr[sprite].brain == 3) |
| 552 |
{ |
| 553 |
// Duck |
| 554 |
check_seq_status(21); |
| 555 |
check_seq_status(23); |
| 556 |
check_seq_status(24); |
| 557 |
check_seq_status(26); |
| 558 |
check_seq_status(27); |
| 559 |
check_seq_status(29); |
| 560 |
// Headless duck |
| 561 |
check_seq_status(111); |
| 562 |
check_seq_status(113); |
| 563 |
check_seq_status(117); |
| 564 |
check_seq_status(119); |
| 565 |
// Duck head |
| 566 |
check_seq_status(121); |
| 567 |
check_seq_status(123); |
| 568 |
check_seq_status(127); |
| 569 |
check_seq_status(129); |
| 570 |
} |
| 571 |
|
| 572 |
if (spr[sprite].hard == 0) |
| 573 |
{ |
| 574 |
/* if (cur_ed_screen.sprite[j].is_warp == 0) |
| 575 |
add_hardness(sprite, 1); |
| 576 |
else */ |
| 577 |
add_hardness(sprite, 100+j); |
| 578 |
} |
| 579 |
|
| 580 |
//does it need a script loaded? |
| 581 |
if (strlen(cur_ed_screen.sprite[j].script) > 1) |
| 582 |
{ |
| 583 |
spr[sprite].script = load_script(cur_ed_screen.sprite[j].script, sprite); |
| 584 |
} |
| 585 |
} |
| 586 |
//Msg("I just made sprite %d because rank[%d] told me to..",sprite,j); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* Draw background sprites and background (not looking at |
| 593 |
* non-background sprites), ordered by queue. |
| 594 |
* |
| 595 |
* Also cf. game_place_sprites(...) and editor's place_sprites(...). |
| 596 |
*/ |
| 597 |
void game_place_sprites_background() |
| 598 |
{ |
| 599 |
int rank[MAX_SPRITES_EDITOR]; |
| 600 |
screen_rank_editor_sprites(rank); |
| 601 |
|
| 602 |
int r1 = 0; |
| 603 |
for (; r1 < MAX_SPRITES_EDITOR && rank[r1] > 0; r1++) |
| 604 |
{ |
| 605 |
//Msg("Ok, rank[%d] is %d.",oo,rank[oo]); |
| 606 |
int j = rank[r1]; |
| 607 |
|
| 608 |
if (cur_ed_screen.sprite[j].active == 1 |
| 609 |
&& (cur_ed_screen.sprite[j].vision == 0 || cur_ed_screen.sprite[j].vision == *pvision)) |
| 610 |
{ |
| 611 |
if (cur_ed_screen.sprite[j].type == 0) |
| 612 |
{ |
| 613 |
//we have instructions to make a sprite |
| 614 |
check_seq_status(cur_ed_screen.sprite[j].seq); |
| 615 |
|
| 616 |
//make it part of the background (much faster) |
| 617 |
int sprite = add_sprite_dumb(cur_ed_screen.sprite[j].x,cur_ed_screen.sprite[j].y, 0, |
| 618 |
cur_ed_screen.sprite[j].seq,cur_ed_screen.sprite[j].frame, |
| 619 |
cur_ed_screen.sprite[j].size); |
| 620 |
|
| 621 |
check_sprite_status_full(sprite); |
| 622 |
draw_sprite_game(IOGFX_background, sprite); |
| 623 |
lsm_remove_sprite(sprite); |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
void fill_back_sprites() |
| 630 |
{ |
| 631 |
int rank[MAX_SPRITES_EDITOR]; |
| 632 |
screen_rank_editor_sprites(rank); |
| 633 |
|
| 634 |
int r1 = 0; |
| 635 |
for (; r1 < MAX_SPRITES_EDITOR && rank[r1] > 0; r1++) |
| 636 |
{ |
| 637 |
//Msg("Ok, rank[%d] is %d.",oo,rank[oo]); |
| 638 |
int j = rank[r1]; |
| 639 |
|
| 640 |
if (cur_ed_screen.sprite[j].active == 1 |
| 641 |
&& (cur_ed_screen.sprite[j].vision == 0 || cur_ed_screen.sprite[j].vision == *pvision)) |
| 642 |
{ |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if (cur_ed_screen.sprite[j].type != 1 && cur_ed_screen.sprite[j].hard == 0) |
| 647 |
{ |
| 648 |
//make it part of the background (much faster) |
| 649 |
int sprite = add_sprite_dumb(cur_ed_screen.sprite[j].x,cur_ed_screen.sprite[j].y, 0, |
| 650 |
cur_ed_screen.sprite[j].seq,cur_ed_screen.sprite[j].frame, |
| 651 |
cur_ed_screen.sprite[j].size); |
| 652 |
|
| 653 |
spr[sprite].hard = cur_ed_screen.sprite[j].hard; |
| 654 |
spr[sprite].sp_index = j; |
| 655 |
rect_copy(&spr[sprite].alt , &cur_ed_screen.sprite[j].alt); |
| 656 |
|
| 657 |
check_sprite_status_full(sprite); |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
if (spr[sprite].hard == 0) |
| 663 |
{ |
| 664 |
/*if (cur_ed_screen.sprite[j].is_warp == 0) |
| 665 |
add_hardness(sprite, 1); else */ |
| 666 |
add_hardness(sprite,100+j); |
| 667 |
} |
| 668 |
lsm_remove_sprite(sprite); |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
/** |
| 676 |
* Run main() for all active sprites on screen |
| 677 |
*/ |
| 678 |
void game_screen_init_scripts() |
| 679 |
{ |
| 680 |
int k = 1; |
| 681 |
for (; k < MAX_SCRIPTS; k++) |
| 682 |
{ |
| 683 |
if (rinfo[k] != NULL && rinfo[k]->sprite != 0 |
| 684 |
/* don't go out of bounds in spr[300], e.g. when sprite == 1000: */ |
| 685 |
&& rinfo[k]->sprite < MAX_SPRITES_AT_ONCE |
| 686 |
&& spr[rinfo[k]->sprite].active) |
| 687 |
{ |
| 688 |
if (locate(k, "main")) |
| 689 |
{ |
| 690 |
log_debug("Screendraw: running main of script %s..", rinfo[k]->name); |
| 691 |
run_script(k); |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Activates a screen: draw it and run scripts |
| 699 |
*/ |
| 700 |
void draw_screen_game(void) |
| 701 |
{ |
| 702 |
*pvision = 0; |
| 703 |
|
| 704 |
lsm_kill_all_nonlive_sprites(); |
| 705 |
kill_repeat_sounds(); |
| 706 |
kill_all_scripts(); |
| 707 |
|
| 708 |
gfx_tiles_draw_screen(g_dmod.bgTilesets.slots, cur_ed_screen.t); |
| 709 |
|
| 710 |
int script_id = 0; |
| 711 |
if (cur_ed_screen.ts_script_id > 0) |
| 712 |
script_id = cur_ed_screen.ts_script_id; |
| 713 |
else if (strlen(cur_ed_screen.script) > 1) |
| 714 |
script_id = load_script(cur_ed_screen.script, 0); |
| 715 |
|
| 716 |
if (script_id > 0) |
| 717 |
{ |
| 718 |
locate(script_id, "main"); |
| 719 |
screen_main_is_running = /*true*/1; |
| 720 |
run_script(script_id); |
| 721 |
screen_main_is_running = /*false*/0; |
| 722 |
} |
| 723 |
|
| 724 |
// lets add the sprites hardness to the real hardness, adding it's |
| 725 |
// own uniqueness to our collective. |
| 726 |
game_place_sprites(); |
| 727 |
|
| 728 |
thisTickCount = game_GetTicks(); |
| 729 |
|
| 730 |
// Run active sprites' scripts |
| 731 |
game_screen_init_scripts(); |
| 732 |
|
| 733 |
// Display some memory stats after loading a screen |
| 734 |
if (debug_mode) { |
| 735 |
meminfo_log_mallinfo(); |
| 736 |
gfx_log_meminfo(); |
| 737 |
sfx_log_meminfo(); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
/* It's used at: freedink.cpp:restoreAll(), DinkC's draw_background(), |
| 742 |
stop_entire_game(). What's the difference with draw_screen_game()?? */ |
| 743 |
void draw_screen_game_background(void) |
| 744 |
{ |
| 745 |
gfx_tiles_draw_screen(g_dmod.bgTilesets.slots, cur_ed_screen.t); |
| 746 |
game_place_sprites_background(); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
/** |
| 751 |
* Display a flashing "Please Wait" anim directly on the screen, just |
| 752 |
* before switching to a screen that requires loading new graphics |
| 753 |
* from the disk. |
| 754 |
*/ |
| 755 |
static void draw_wait() { |
| 756 |
if (screen_main_is_running) { |
| 757 |
if (seq[423].frame[please_wait_toggle_frame] != 0) { |
| 758 |
SDL_Rect dst = { 232, 0, -1, -1 }; |
| 759 |
IOGFX_backbuffer->blit(GFX_k[seq[423].frame[please_wait_toggle_frame]].k, NULL, &dst); |
| 760 |
g_display->flipStretch(IOGFX_backbuffer); |
| 761 |
} |
| 762 |
if (please_wait_toggle_frame == 7) |
| 763 |
please_wait_toggle_frame = 8; |
| 764 |
else |
| 765 |
please_wait_toggle_frame = 7; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
void set_mode(int new_mode) { |
| 771 |
mode = new_mode; |
| 772 |
if (mode == 3 && !keep_mouse) { |
| 773 |
SDL_SetWindowGrab(g_display->window, SDL_FALSE); |
| 774 |
} else { |
| 775 |
/* Jail window cursor (Alt+Tab still works) */ |
| 776 |
SDL_SetWindowGrab(g_display->window, SDL_TRUE); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
/** |
| 782 |
* Prepare mode 1 (mouse interaction / title screen / START.c) |
| 783 |
*/ |
| 784 |
void apply_mode0() { |
| 785 |
memset(&spr[1], 0, sizeof(spr[1])); |
| 786 |
spr[1].speed = 3; |
| 787 |
spr[1].timing = 0; |
| 788 |
spr[1].brain = 1; |
| 789 |
spr[1].hard = 1; |
| 790 |
spr[1].pseq = 2; |
| 791 |
spr[1].pframe = 1; |
| 792 |
spr[1].seq = 2; |
| 793 |
spr[1].dir = 2; |
| 794 |
spr[1].damage = 0; |
| 795 |
spr[1].strength = 10; |
| 796 |
spr[1].defense = 0; |
| 797 |
spr[1].skip = 0; |
| 798 |
rect_set(&spr[1].alt, 0, 0, 0, 0); |
| 799 |
spr[1].base_idle = 10; |
| 800 |
spr[1].base_walk = -1; |
| 801 |
spr[1].size = 100; |
| 802 |
spr[1].base_hit = 100; |
| 803 |
spr[1].active = true; |
| 804 |
spr[1].custom = new std::map<std::string, int>; |
| 805 |
|
| 806 |
SDL_WarpMouseInWindow(g_display->window, spr[1].x, spr[1].y); |
| 807 |
|
| 808 |
int version_text = add_sprite(0, 450, 8, 0, 0); |
| 809 |
spr[version_text].hard = 1; |
| 810 |
spr[version_text].noclip = 1; |
| 811 |
strcpy(spr[version_text].text, dversion_string); |
| 812 |
spr[version_text].damage = -1; |
| 813 |
spr[version_text].text_owner = 1000; |
| 814 |
|
| 815 |
int scr = load_script("START", 1000); |
| 816 |
if (locate(scr, "MAIN") == /*false*/0) { |
| 817 |
log_error("Can't locate MAIN in script START!"); |
| 818 |
} |
| 819 |
|
| 820 |
run_script(scr); |
| 821 |
} |
| 822 |
|
| 823 |
void apply_mode() { |
| 824 |
if (mode == 0) { |
| 825 |
apply_mode0(); |
| 826 |
set_mode(1); |
| 827 |
} |
| 828 |
|
| 829 |
if (mode == 2) { |
| 830 |
set_mode(3); |
| 831 |
game_load_screen(g_dmod.map.loc[*pplayer_map]); |
| 832 |
draw_screen_game(); |
| 833 |
flife = *plife; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
void set_keep_mouse(int on) { |
| 839 |
keep_mouse = on; |
| 840 |
if (!on) |
| 841 |
SDL_SetWindowGrab(g_display->window, SDL_FALSE); |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
void game_init(int version) |
| 846 |
{ |
| 847 |
dversion = version; |
| 848 |
/* Clean the game state structure - done by C++ but not |
| 849 |
automatically done by C, and this causes errors. TODO: fix the |
| 850 |
errors properly instead of using this dirty trick. */ |
| 851 |
memset(&play, 0, sizeof(play)); |
| 852 |
live_sprites_manager_init(); |
| 853 |
|
| 854 |
gfx_sprites_loading_listener = draw_wait; |
| 855 |
|
| 856 |
if (dversion >= 108) |
| 857 |
dversion_string = "v1.08 FreeDink"; |
| 858 |
else |
| 859 |
dversion_string = "v1.07 FreeDink"; |
| 860 |
|
| 861 |
srand((unsigned)time(NULL)); |
| 862 |
|
| 863 |
dinkc_init(); |
| 864 |
dinkc_bindings_init(); |
| 865 |
} |
| 866 |
|
| 867 |
void game_quit() |
| 868 |
{ |
| 869 |
kill_all_scripts_for_real(); |
| 870 |
|
| 871 |
int i = 0; |
| 872 |
for (i = 1; i < MAX_SPRITES_AT_ONCE; i++) |
| 873 |
{ |
| 874 |
if (spr[i].custom != NULL) |
| 875 |
delete(spr[i].custom); |
| 876 |
spr[i].custom = NULL; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
dinkc_bindings_quit(); |
| 881 |
dinkc_quit(); |
| 882 |
|
| 883 |
if (last_saved_game > 0) |
| 884 |
{ |
| 885 |
log_info("Modifying saved game."); |
| 886 |
|
| 887 |
if (!add_time_to_saved_game(last_saved_game)) |
| 888 |
log_error("Error modifying saved game."); |
| 889 |
last_saved_game = 0; |
| 890 |
} |
| 891 |
} |