2 * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 static boolean gd_no_invisible_outbox = FALSE;
24 void gd_game_free(GdGame *game)
29 if (game->element_buffer)
30 gd_cave_map_free(game->element_buffer);
31 if (game->last_element_buffer)
32 gd_cave_map_free(game->last_element_buffer);
34 gd_cave_map_free(game->dir_buffer);
36 gd_cave_map_free(game->gfx_buffer);
38 game->player_lives = 0;
41 gd_cave_free(game->cave);
46 // add bonus life. if sound enabled, play sound, too.
47 static void add_bonus_life(GdGame *game, boolean inform_user)
51 gd_sound_play_bonus_life();
52 game->bonus_life_flash = 100;
55 // really increment number of lifes? only in a real game, nowhere else.
56 if (game->player_lives &&
57 game->player_lives < gd_caveset_data->maximum_lives)
59 // only add a life, if lives is > 0.
60 // lives == 0 is a test run or a snapshot, no bonus life then.
61 // also, obey max number of bonus lives.
66 // increment score of player.
67 // flash screen if bonus life
68 static void increment_score(GdGame *game, int increment)
72 i = game->player_score / gd_caveset_data->bonus_life_score;
73 game->player_score += increment;
74 game->cave_score += increment;
76 // if score crossed bonus_life_score point boundary, player won a bonus life
77 if (game->player_score / gd_caveset_data->bonus_life_score > i)
78 add_bonus_life(game, TRUE);
81 // do the things associated with loading a new cave. function creates gfx buffer and the like.
82 static void load_cave(GdGame *game)
86 // delete element buffer
87 if (game->element_buffer)
88 gd_cave_map_free(game->element_buffer);
89 game->element_buffer = NULL;
91 // delete last element buffer
92 if (game->last_element_buffer)
93 gd_cave_map_free(game->last_element_buffer);
94 game->last_element_buffer = NULL;
96 // delete direction buffer
98 gd_cave_map_free(game->dir_buffer);
99 game->dir_buffer = NULL;
102 if (game->gfx_buffer)
103 gd_cave_map_free(game->gfx_buffer);
104 game->gfx_buffer = NULL;
107 game->cave_score = 0;
109 // delete previous cave
110 gd_cave_free(game->cave);
112 if (native_bd_level.loaded_from_caveset)
113 game->original_cave = gd_get_original_cave_from_caveset(game->cave_num);
115 game->original_cave = native_bd_level.cave;
117 game->cave = gd_get_prepared_cave(game->original_cave, game->level_num);
119 if (game->cave->intermission && game->cave->intermission_instantlife)
120 add_bonus_life(game, FALSE);
122 game->milliseconds_anim = 0;
123 game->milliseconds_game = 0; // set game timer to zero, too
125 // create new element buffer
126 game->element_buffer = gd_cave_map_new(game->cave, int);
128 for (y = 0; y < game->cave->h; y++)
129 for (x = 0; x < game->cave->w; x++)
130 game->element_buffer[y][x] = O_NONE;
132 // create new last element buffer
133 game->last_element_buffer = gd_cave_map_new(game->cave, int);
135 for (y = 0; y < game->cave->h; y++)
136 for (x = 0; x < game->cave->w; x++)
137 game->last_element_buffer[y][x] = O_NONE;
139 // create new direction buffer
140 game->dir_buffer = gd_cave_map_new(game->cave, int);
142 for (y = 0; y < game->cave->h; y++)
143 for (x = 0; x < game->cave->w; x++)
144 game->dir_buffer[y][x] = GD_MV_STILL;
146 // create new gfx buffer
147 game->gfx_buffer = gd_cave_map_new(game->cave, int);
149 for (y = 0; y < game->cave->h; y++)
150 for (x = 0; x < game->cave->w; x++)
151 game->gfx_buffer[y][x] = -1; // fill with "invalid"
154 GdCave *gd_create_snapshot(GdGame *game)
158 if (game->cave == NULL)
161 // make an exact copy
162 snapshot = gd_cave_new_from_cave(game->cave);
167 // this starts a new game
168 GdGame *gd_game_new(const int cave, const int level)
172 game = checked_calloc(sizeof(GdGame));
174 game->cave_num = cave;
175 game->level_num = level;
177 game->player_lives = gd_caveset_data->initial_lives;
178 game->player_score = 0;
180 game->player_move = GD_MV_STILL;
181 game->player_move_stick = FALSE;
182 game->player_fire = FALSE;
184 game->state_counter = GAME_INT_LOAD_CAVE;
186 game->show_story = TRUE;
191 static void iterate_cave(GdGame *game, GdDirection player_move, boolean fire)
193 boolean suicide = FALSE;
195 // ANYTHING EXCEPT A TIMEOUT, WE ITERATE THE CAVE
196 if (game->cave->player_state != GD_PL_TIMEOUT)
198 if (TapeIsPlaying_ReplayBD())
200 byte *action_rnd = TapePlayAction_BD();
202 if (action_rnd != NULL)
204 int action_bd = map_action_RND_to_BD(action_rnd[0]);
206 player_move = (action_bd & GD_REPLAY_MOVE_MASK);
207 fire = (action_bd & GD_REPLAY_FIRE_MASK) != 0;
212 gd_cave_iterate(game->cave, player_move, fire, suicide);
214 if (game->cave->score)
215 increment_score(game, game->cave->score);
217 gd_sound_play_cave(game->cave);
220 if (game->cave->player_state == GD_PL_EXITED)
222 if (game->cave->intermission &&
223 game->cave->intermission_rewardlife &&
224 game->player_lives != 0)
226 // one life extra for completing intermission
227 add_bonus_life(game, FALSE);
230 // start adding points for remaining time
231 game->state_counter = GAME_INT_CHECK_BONUS_TIME;
232 gd_cave_clear_sounds(game->cave);
234 // play cave finished sound
235 gd_sound_play(game->cave, GD_S_FINISHED, O_NONE, -1, -1);
236 gd_sound_play_cave(game->cave);
239 if (game->cave->player_state == GD_PL_DIED ||
240 game->cave->player_state == GD_PL_TIMEOUT)
242 game_bd.game_over = TRUE;
243 game_bd.cover_screen = TRUE;
247 static GdGameState gd_game_main_int(GdGame *game, boolean allow_iterate, boolean fast_forward)
249 int millisecs_elapsed = 20;
250 boolean frame; // set to true, if this will be an animation frame
251 GdGameState return_state;
255 counter_next = GAME_INT_INVALID;
256 return_state = GD_GAME_INVALID_STATE;
257 game->milliseconds_anim += millisecs_elapsed; // keep track of time
258 frame = FALSE; // set to true, if this will be an animation frame
260 if (game->milliseconds_anim >= 40)
263 game->milliseconds_anim -= 40;
266 // cannot be less than uncover start.
267 if (game->state_counter < GAME_INT_LOAD_CAVE)
271 else if (game->state_counter == GAME_INT_LOAD_CAVE)
273 // do the things associated with loading a new cave. function creates gfx buffer and the like.
276 return_state = GD_GAME_NOTHING;
277 counter_next = GAME_INT_SHOW_STORY;
279 else if (game->state_counter == GAME_INT_SHOW_STORY)
281 // for normal game, every cave can have a long string of description/story. show that.
283 // if we have a story...
285 if (game->show_story && game->original_cave && game->original_cave->story != NULL)
286 Info("Cave Story: %s", game->original_cave->story);
289 counter_next = GAME_INT_START_UNCOVER;
290 return_state = GD_GAME_NOTHING;
292 else if (game->state_counter == GAME_INT_START_UNCOVER)
294 // the very beginning.
296 // cover all cells of cave
297 for (y = 0; y < game->cave->h; y++)
298 for (x = 0; x < game->cave->w; x++)
299 game->cave->map[y][x] |= COVERED;
301 counter_next = game->state_counter + 1;
303 // very important: tell the caller that we loaded a new cave.
304 // size of the cave might be new, colors might be new, and so on.
305 return_state = GD_GAME_CAVE_LOADED;
307 else if (game->state_counter < GAME_INT_UNCOVER_ALL)
311 // to play cover sound
312 gd_sound_play(game->cave, GD_S_COVERING, O_COVERED, -1, -1);
313 gd_sound_play_cave(game->cave);
315 counter_next = game->state_counter;
321 // original game uncovered one cell per line each frame.
322 // we have different cave sizes, so uncover width * height / 40 random
323 // cells each frame. (original was width = 40).
324 // this way the uncovering is the same speed also for intermissions.
325 for (j = 0; j < game->cave->w * game->cave->h / 40; j++)
327 y = gd_random_int_range(0, game->cave->h);
328 x = gd_random_int_range(0, game->cave->w);
330 game->cave->map[y][x] &= ~COVERED;
333 counter_next++; // as we did something, advance the counter.
336 return_state = GD_GAME_NOTHING;
338 else if (game->state_counter == GAME_INT_UNCOVER_ALL)
340 // time to uncover the whole cave.
341 for (y = 0; y < game->cave->h; y++)
342 for (x = 0; x < game->cave->w; x++)
343 game->cave->map[y][x] &= ~COVERED;
345 // to stop uncover sound.
346 gd_cave_clear_sounds(game->cave);
347 gd_sound_play_cave(game->cave);
349 counter_next = GAME_INT_CAVE_RUNNING;
350 return_state = GD_GAME_NOTHING;
352 else if (game->state_counter == GAME_INT_CAVE_RUNNING)
358 cavespeed = game->cave->speed; // cave speed in ms, like 175ms/frame
360 cavespeed = 40; // if fast forward, ignore cave speed, and go as 25 iterations/sec
362 // ITERATION - cave is running.
364 // normally nothing happes. but if we iterate, this might change.
365 return_state = GD_GAME_NOTHING;
367 // if allowing cave movements, add elapsed time to timer. and then we can check what to do.
369 game->milliseconds_game += millisecs_elapsed;
371 // increment cycle (frame) counter for the current cave iteration
374 if (game->milliseconds_game >= cavespeed)
378 game->milliseconds_game -= cavespeed;
379 pl = game->cave->player_state;
381 // initialize buffers for last cave element and direction for next iteration
382 for (y = 0; y < game->cave->h; y++)
384 for (x = 0; x < game->cave->w; x++)
386 game->last_element_buffer[y][x] = game->element_buffer[y][x] & ~SKIPPED;
387 game->dir_buffer[y][x] = GD_MV_STILL;
391 // store last maximum number of cycles (to force redraw if changed)
392 game->itermax_last = game->itermax;
394 // update maximum number of cycles (frame) per cave iteration
395 game->itermax = game->itercycle;
397 // reset cycle (frame) counter for the next cave iteration
400 iterate_cave(game, game->player_move, game->player_fire);
402 if (game->player_move == GD_MV_STILL)
404 game->player_move_stick = FALSE;
408 game->player_move_stick = TRUE;
409 game->player_move = GD_MV_STILL;
412 // as we iterated, the score and the like could have been changed.
413 return_state = GD_GAME_LABELS_CHANGED;
415 // and if the cave timeouted at this moment, that is a special case.
416 if (pl != GD_PL_TIMEOUT && game->cave->player_state == GD_PL_TIMEOUT)
417 return_state = GD_GAME_TIMEOUT_NOW;
420 // do not change counter state, as it is set by iterate_cave()
421 counter_next = game->state_counter;
423 else if (game->state_counter == GAME_INT_CHECK_BONUS_TIME)
425 // before covering, we check for time bonus score
428 // if time remaining, bonus points are added. do not start animation yet.
429 if (game->cave->time > 0)
431 // subtract number of "milliseconds" - nothing to do with gameplay->millisecs!
432 game->cave->time -= game->cave->timing_factor;
434 // higher levels get more bonus points per second remained
435 increment_score(game, game->cave->timevalue);
437 // if much time (> 60s) remained, fast counter :)
438 if (game->cave->time > 60 * game->cave->timing_factor)
440 // decrement by nine each frame, so it also looks like a fast counter. 9 is 8 + 1!
441 game->cave->time -= 8 * game->cave->timing_factor;
442 increment_score(game, game->cave->timevalue * 8);
446 if (game->cave->time < 0)
447 game->cave->time = 0;
449 counter_next = game->state_counter; // do not change yet
453 game_bd.level_solved = TRUE;
454 game_bd.cover_screen = TRUE;
456 // if no more points, start waiting a bit, and later start covering.
457 counter_next = GAME_INT_WAIT_BEFORE_COVER;
460 if (game->cave->time / game->cave->timing_factor > 8)
461 gd_sound_play(game->cave, GD_S_FINISHED, O_NONE, -1, -1); // play cave finished sound
464 gd_cave_set_seconds_sound(game->cave);
465 gd_sound_play_cave(game->cave);
467 return_state = GD_GAME_LABELS_CHANGED;
471 return_state = GD_GAME_NOTHING;
473 // do not change counter state, as it is set by iterate_cave()
474 counter_next = game->state_counter;
477 else if (game->state_counter == GAME_INT_WAIT_BEFORE_COVER)
479 // after adding bonus points, we wait some time before starting to cover.
480 // this is the FIRST frame... so we check for game over and maybe jump there
481 // if no more lives, game is over.
482 counter_next = game->state_counter;
484 if (game->player_lives == 0)
485 return_state = GD_GAME_NO_MORE_LIVES;
487 return_state = GD_GAME_NOTHING;
489 else if (game->state_counter > GAME_INT_WAIT_BEFORE_COVER &&
490 game->state_counter < GAME_INT_COVER_START)
492 // after adding bonus points, we wait some time before starting to cover.
493 // ... and the other frames.
494 counter_next = game->state_counter;
496 counter_next++; // 40 ms elapsed, advance counter
498 return_state = GD_GAME_NOTHING;
500 else if (game->state_counter == GAME_INT_COVER_START)
502 // starting to cover. start cover sound.
504 gd_cave_clear_sounds(game->cave);
505 gd_sound_play(game->cave, GD_S_COVERING, O_COVERED, -1, -1);
507 // to play cover sound
508 gd_sound_play_cave(game->cave);
510 counter_next = game->state_counter + 1;
511 return_state = GD_GAME_NOTHING;
513 else if (game->state_counter > GAME_INT_COVER_START &&
514 game->state_counter < GAME_INT_COVER_ALL)
517 gd_sound_play(game->cave, GD_S_COVERING, O_COVERED, -1, -1);
519 counter_next = game->state_counter;
525 counter_next++; // 40 ms elapsed, doing cover: advance counter
527 // covering eight times faster than uncovering.
528 for (j = 0; j < game->cave->w * game->cave->h * 8 / 40; j++)
529 game->cave->map[gd_random_int_range(0, game->cave->h)][gd_random_int_range (0, game->cave->w)] |= COVERED;
532 return_state = GD_GAME_NOTHING;
534 else if (game->state_counter == GAME_INT_COVER_ALL)
537 for (y = 0; y < game->cave->h; y++)
538 for (x = 0; x < game->cave->w; x++)
539 game->cave->map[y][x] |= COVERED;
541 counter_next = game->state_counter + 1;
542 return_state = GD_GAME_NOTHING;
544 // to stop uncover sound.
545 gd_cave_clear_sounds(game->cave);
546 gd_sound_play_cave(game->cave);
552 if (game->player_lives != 0)
553 return_state = GD_GAME_NOTHING; // and go to next level
555 return_state = GD_GAME_GAME_OVER;
561 if (game->bonus_life_flash) // bonus life - frames
562 game->bonus_life_flash--;
564 game->animcycle = (game->animcycle + 1) % 8;
567 // always render the cave to the gfx buffer;
568 // however it may do nothing if animcycle was not changed.
569 if (game->element_buffer && game->gfx_buffer)
570 gd_drawcave_game(game->cave, game->element_buffer, game->gfx_buffer,
571 game->bonus_life_flash != 0, game->animcycle, gd_no_invisible_outbox);
573 game->state_counter = counter_next;
578 void play_game_func(GdGame *game, int action)
581 boolean move_up = ((action & JOY_UP) != 0);
582 boolean move_down = ((action & JOY_DOWN) != 0);
583 boolean move_left = ((action & JOY_LEFT) != 0);
584 boolean move_right = ((action & JOY_RIGHT) != 0);
585 boolean fire = ((action & (JOY_BUTTON_1 | JOY_BUTTON_2)) != 0);
587 if (game->player_move_stick || move_up || move_down || move_left || move_right) // no "fire"!
590 game->player_move = gd_direction_from_keypress(move_up, move_down, move_left, move_right);
592 // when storing last action, only update fire action with direction
593 // (prevents clearing direction if snapping stopped before action is performed)
594 game->player_fire = fire;
597 // if no direction was stored before, allow setting fire to current state
598 if (game->player_move == GD_MV_STILL)
599 game->player_fire = fire;
601 // tell the interrupt "20ms has passed"
602 state = gd_game_main_int(game, !game->out_of_window, gd_keystate[SDL_SCANCODE_F]);
604 // state of game, returned by gd_game_main_int
607 case GD_GAME_CAVE_LOADED:
608 // scroll to start position
609 gd_scroll_to_origin();
611 // fill whole screen with black - cave might be smaller than previous!
612 FillRectangle(gd_screen_bitmap, 0, 0, SXSIZE, SYSIZE, BLACK_PIXEL);
619 // for the sdl version, it seems nicer if we first scroll, and then draw.
620 // scrolling for the sdl version will merely invalidate the whole gfx buffer.
621 // if drawcave was before scrolling, it would draw, scroll would invalidate,
622 // and then it should be drawn again
623 // only do the drawing if the cave already exists.
624 if (game->cave && game->element_buffer && game->gfx_buffer)
626 // if fine scrolling, scroll at 50hz. if not, only scroll at every second call, so 25hz.
627 // do the scrolling. scroll exactly, if player is not yet alive
628 game->out_of_window = gd_scroll(game, game->cave->player_state == GD_PL_NOT_YET, FALSE);