added setup option to show invisible exit in BD engine
[rocksndiamonds.git] / src / game_bd / bd_gameplay.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
3  *
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.
7  *
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.
15  */
16
17 #include "main_bd.h"
18
19
20 void gd_game_free(GdGame *game)
21 {
22   // stop sounds
23   gd_sound_off();
24
25   if (game->element_buffer)
26     gd_cave_map_free(game->element_buffer);
27   if (game->last_element_buffer)
28     gd_cave_map_free(game->last_element_buffer);
29   if (game->dir_buffer)
30     gd_cave_map_free(game->dir_buffer);
31   if (game->gfx_buffer)
32     gd_cave_map_free(game->gfx_buffer);
33
34   game->player_lives = 0;
35
36   if (game->cave)
37     gd_cave_free(game->cave);
38
39   free(game);
40 }
41
42 // add bonus life. if sound enabled, play sound, too.
43 static void add_bonus_life(GdGame *game, boolean inform_user)
44 {
45   if (inform_user)
46   {
47     gd_sound_play_bonus_life();
48     game->bonus_life_flash = 100;
49   }
50
51   // really increment number of lifes? only in a real game, nowhere else.
52   if (game->player_lives &&
53       game->player_lives < gd_caveset_data->maximum_lives)
54   {
55     // only add a life, if lives is > 0.
56     // lives == 0 is a test run or a snapshot, no bonus life then.
57     // also, obey max number of bonus lives.
58     game->player_lives++;
59   }
60 }
61
62 // increment score of player.
63 // flash screen if bonus life
64 static void increment_score(GdGame *game, int increment)
65 {
66   int i;
67
68   i = game->player_score / gd_caveset_data->bonus_life_score;
69   game->player_score += increment;
70   game->cave_score += increment;
71
72   // if score crossed bonus_life_score point boundary, player won a bonus life
73   if (game->player_score / gd_caveset_data->bonus_life_score > i)
74     add_bonus_life(game, TRUE);
75 }
76
77 // do the things associated with loading a new cave. function creates gfx buffer and the like.
78 static void load_cave(GdGame *game)
79 {
80   int x, y;
81
82   // delete element buffer
83   if (game->element_buffer)
84     gd_cave_map_free(game->element_buffer);
85   game->element_buffer = NULL;
86
87   // delete last element buffer
88   if (game->last_element_buffer)
89     gd_cave_map_free(game->last_element_buffer);
90   game->last_element_buffer = NULL;
91
92   // delete direction buffer
93   if (game->dir_buffer)
94     gd_cave_map_free(game->dir_buffer);
95   game->dir_buffer = NULL;
96
97   // delete gfx buffer
98   if (game->gfx_buffer)
99     gd_cave_map_free(game->gfx_buffer);
100   game->gfx_buffer = NULL;
101
102   // load the cave
103   game->cave_score = 0;
104
105   // delete previous cave
106   gd_cave_free(game->cave);
107
108   if (native_bd_level.loaded_from_caveset)
109     game->original_cave = gd_get_original_cave_from_caveset(game->cave_num);
110   else
111     game->original_cave = native_bd_level.cave;
112
113   game->cave = gd_get_prepared_cave(game->original_cave, game->level_num);
114
115   // if requested, recolor cave (cave is a copy only, so no worries)
116   if (setup.bd_random_colors)
117     gd_cave_set_random_colors(game->cave, setup.bd_default_color_type);
118
119   if (game->cave->intermission && game->cave->intermission_instantlife)
120     add_bonus_life(game, FALSE);
121
122   game->milliseconds_anim = 0;
123   game->milliseconds_game = 0;        // set game timer to zero, too
124
125   // create new element buffer
126   game->element_buffer = gd_cave_map_new(game->cave, int);
127
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;
131
132   // create new last element buffer
133   game->last_element_buffer = gd_cave_map_new(game->cave, int);
134
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;
138
139   // create new direction buffer
140   game->dir_buffer = gd_cave_map_new(game->cave, int);
141
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;
145
146   // create new gfx buffer
147   game->gfx_buffer = gd_cave_map_new(game->cave, int);
148
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"
152 }
153
154 GdCave *gd_create_snapshot(GdGame *game)
155 {
156   GdCave *snapshot;
157
158   if (game->cave == NULL)
159     return NULL;
160
161   // make an exact copy
162   snapshot = gd_cave_new_from_cave(game->cave);
163
164   return snapshot;
165 }
166
167 // this starts a new game
168 GdGame *gd_game_new(const int cave, const int level)
169 {
170   GdGame *game;
171
172   game = checked_calloc(sizeof(GdGame));
173
174   game->cave_num = cave;
175   game->level_num = level;
176
177   game->player_lives = gd_caveset_data->initial_lives;
178   game->player_score = 0;
179
180   game->player_move = GD_MV_STILL;
181   game->player_move_stick = FALSE;
182   game->player_fire = FALSE;
183
184   game->state_counter = GAME_INT_LOAD_CAVE;
185
186   game->show_story = TRUE;
187
188   return game;
189 }
190
191 static void iterate_cave(GdGame *game, GdDirection player_move, boolean fire)
192 {
193   boolean suicide = FALSE;
194
195   // ANYTHING EXCEPT A TIMEOUT, WE ITERATE THE CAVE
196   if (game->cave->player_state != GD_PL_TIMEOUT)
197   {
198     if (TapeIsPlaying_ReplayBD())
199     {
200       byte *action_rnd = TapePlayAction_BD();
201
202       if (action_rnd != NULL)
203       {
204         int action_bd = map_action_RND_to_BD(action_rnd[0]);
205
206         player_move = (action_bd & GD_REPLAY_MOVE_MASK);
207         fire        = (action_bd & GD_REPLAY_FIRE_MASK) != 0;
208       }
209     }
210
211     // iterate cave
212     gd_cave_iterate(game->cave, player_move, fire, suicide);
213
214     if (game->cave->score)
215       increment_score(game, game->cave->score);
216
217     gd_sound_play_cave(game->cave);
218   }
219
220   if (game->cave->player_state == GD_PL_EXITED)
221   {
222     if (game->cave->intermission &&
223         game->cave->intermission_rewardlife &&
224         game->player_lives != 0)
225     {
226       // one life extra for completing intermission
227       add_bonus_life(game, FALSE);
228     }
229
230     // start adding points for remaining time
231     game->state_counter = GAME_INT_CHECK_BONUS_TIME;
232     gd_cave_clear_sounds(game->cave);
233
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);
237   }
238
239   if (game->cave->player_state == GD_PL_DIED ||
240       game->cave->player_state == GD_PL_TIMEOUT)
241   {
242     game_bd.game_over = TRUE;
243     game_bd.cover_screen = TRUE;
244   }
245 }
246
247 static GdGameState gd_game_main_int(GdGame *game, boolean allow_iterate, boolean fast_forward)
248 {
249   int millisecs_elapsed = 20;
250   boolean frame;    // set to true, if this will be an animation frame
251   GdGameState return_state;
252   int counter_next;
253   int x, y;
254
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
259
260   if (game->milliseconds_anim >= 40)
261   {
262     frame = TRUE;
263     game->milliseconds_anim -= 40;
264   }
265
266   // cannot be less than uncover start.
267   if (game->state_counter < GAME_INT_LOAD_CAVE)
268   {
269     ;
270   }
271   else if (game->state_counter == GAME_INT_LOAD_CAVE)
272   {
273     // do the things associated with loading a new cave. function creates gfx buffer and the like.
274     load_cave(game);
275
276     return_state = GD_GAME_NOTHING;
277     counter_next = GAME_INT_SHOW_STORY;
278   }
279   else if (game->state_counter == GAME_INT_SHOW_STORY)
280   {
281     // for normal game, every cave can have a long string of description/story. show that.
282
283     // if we have a story...
284 #if 0
285     if (game->show_story && game->original_cave && game->original_cave->story != NULL)
286       Info("Cave Story: %s", game->original_cave->story);
287 #endif
288
289     counter_next = GAME_INT_START_UNCOVER;
290     return_state = GD_GAME_NOTHING;
291   }
292   else if (game->state_counter == GAME_INT_START_UNCOVER)
293   {
294     // the very beginning.
295
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;
300
301     counter_next = game->state_counter + 1;
302
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;
306   }
307   else if (game->state_counter < GAME_INT_UNCOVER_ALL)
308   {
309     // uncover animation
310
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);
314
315     counter_next = game->state_counter;
316
317     if (frame)
318     {
319       int j;
320
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++)
326       {
327         y = gd_random_int_range(0, game->cave->h);
328         x = gd_random_int_range(0, game->cave->w);
329
330         game->cave->map[y][x] &= ~COVERED;
331       }
332
333       counter_next++;    // as we did something, advance the counter.
334     }
335
336     return_state = GD_GAME_NOTHING;
337   }
338   else if (game->state_counter == GAME_INT_UNCOVER_ALL)
339   {
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;
344
345     // to stop uncover sound.
346     gd_cave_clear_sounds(game->cave);
347     gd_sound_play_cave(game->cave);
348
349     counter_next = GAME_INT_CAVE_RUNNING;
350     return_state = GD_GAME_NOTHING;
351   }
352   else if (game->state_counter == GAME_INT_CAVE_RUNNING)
353   {
354     // normal.
355     int cavespeed;
356
357     if (!fast_forward)
358       cavespeed = game->cave->speed;   // cave speed in ms, like 175ms/frame
359     else
360       cavespeed = 40;    // if fast forward, ignore cave speed, and go as 25 iterations/sec
361
362     // ITERATION - cave is running.
363
364     // normally nothing happes. but if we iterate, this might change.
365     return_state = GD_GAME_NOTHING;
366
367     // if allowing cave movements, add elapsed time to timer. and then we can check what to do.
368     if (allow_iterate)
369       game->milliseconds_game += millisecs_elapsed;
370
371     // increment cycle (frame) counter for the current cave iteration
372     game->itercycle++;
373
374     if (game->milliseconds_game >= cavespeed)
375     {
376       GdPlayerState pl;
377
378       game->milliseconds_game -= cavespeed;
379       pl = game->cave->player_state;
380
381       // initialize buffers for last cave element and direction for next iteration
382       for (y = 0; y < game->cave->h; y++)
383       {
384         for (x = 0; x < game->cave->w; x++)
385         {
386           game->last_element_buffer[y][x] = game->element_buffer[y][x] & ~SKIPPED;
387           game->dir_buffer[y][x] = GD_MV_STILL;
388         }
389       }
390
391       // store last maximum number of cycles (to force redraw if changed)
392       game->itermax_last = game->itermax;
393
394       // update maximum number of cycles (frame) per cave iteration
395       game->itermax = game->itercycle;
396
397       // reset cycle (frame) counter for the next cave iteration
398       game->itercycle = 0;
399
400       iterate_cave(game, game->player_move, game->player_fire);
401
402       if (game->player_move == GD_MV_STILL)
403       {
404         game->player_move_stick = FALSE;
405       }
406       else
407       {
408         game->player_move_stick = TRUE;
409         game->player_move = GD_MV_STILL;
410       }
411
412       // as we iterated, the score and the like could have been changed.
413       return_state = GD_GAME_LABELS_CHANGED;
414
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;
418     }
419
420     // do not change counter state, as it is set by iterate_cave()
421     counter_next = game->state_counter;
422   }
423   else if (game->state_counter == GAME_INT_CHECK_BONUS_TIME)
424   {
425     // before covering, we check for time bonus score
426     if (frame)
427     {
428       // if time remaining, bonus points are added. do not start animation yet.
429       if (game->cave->time > 0)
430       {
431         // subtract number of "milliseconds" - nothing to do with gameplay->millisecs!
432         game->cave->time -= game->cave->timing_factor;
433
434         // higher levels get more bonus points per second remained
435         increment_score(game, game->cave->timevalue);
436
437         // if much time (> 60s) remained, fast counter :)
438         if (game->cave->time > 60 * game->cave->timing_factor)
439         {
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);
443         }
444
445         // just to be neat
446         if (game->cave->time < 0)
447           game->cave->time = 0;
448
449         counter_next = game->state_counter;    // do not change yet
450       }
451       else
452       {
453         game_bd.level_solved = TRUE;
454         game_bd.cover_screen = TRUE;
455
456         // if no more points, start waiting a bit, and later start covering.
457         counter_next = GAME_INT_WAIT_BEFORE_COVER;
458       }
459
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
462
463       // play bonus sound
464       gd_cave_set_seconds_sound(game->cave);
465       gd_sound_play_cave(game->cave);
466
467       return_state = GD_GAME_LABELS_CHANGED;
468     }
469     else
470     {
471       return_state = GD_GAME_NOTHING;
472
473       // do not change counter state, as it is set by iterate_cave()
474       counter_next = game->state_counter;
475     }
476   }
477   else if (game->state_counter == GAME_INT_WAIT_BEFORE_COVER)
478   {
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;
483
484     if (game->player_lives == 0)
485       return_state = GD_GAME_NO_MORE_LIVES;
486     else
487       return_state = GD_GAME_NOTHING;
488   }
489   else if (game->state_counter > GAME_INT_WAIT_BEFORE_COVER &&
490            game->state_counter < GAME_INT_COVER_START)
491   {
492     // after adding bonus points, we wait some time before starting to cover.
493     // ... and the other frames.
494     counter_next = game->state_counter;
495     if (frame)
496       counter_next++;    // 40 ms elapsed, advance counter
497
498     return_state = GD_GAME_NOTHING;
499   }
500   else if (game->state_counter == GAME_INT_COVER_START)
501   {
502     // starting to cover. start cover sound.
503
504     gd_cave_clear_sounds(game->cave);
505     gd_sound_play(game->cave, GD_S_COVERING, O_COVERED, -1, -1);
506
507     // to play cover sound
508     gd_sound_play_cave(game->cave);
509
510     counter_next = game->state_counter + 1;
511     return_state = GD_GAME_NOTHING;
512   }
513   else if (game->state_counter > GAME_INT_COVER_START &&
514            game->state_counter < GAME_INT_COVER_ALL)
515   {
516     // covering.
517     gd_sound_play(game->cave, GD_S_COVERING, O_COVERED, -1, -1);
518
519     counter_next = game->state_counter;
520
521     if (frame)
522     {
523       int j;
524
525       counter_next++;    // 40 ms elapsed, doing cover: advance counter
526
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;
530     }
531
532     return_state = GD_GAME_NOTHING;
533   }
534   else if (game->state_counter == GAME_INT_COVER_ALL)
535   {
536     // 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;
540
541     counter_next = game->state_counter + 1;
542     return_state = GD_GAME_NOTHING;
543
544     // to stop uncover sound.
545     gd_cave_clear_sounds(game->cave);
546     gd_sound_play_cave(game->cave);
547   }
548   else
549   {
550     // cover all + 1
551
552     if (game->player_lives != 0)
553       return_state = GD_GAME_NOTHING;    // and go to next level
554     else
555       return_state = GD_GAME_GAME_OVER;
556   }
557
558   // draw the cave
559   if (frame)
560   {
561     if (game->bonus_life_flash)    // bonus life - frames
562       game->bonus_life_flash--;
563
564     game->animcycle = (game->animcycle + 1) % 8;
565   }
566
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->last_element_buffer, game->gfx_buffer,
571                      game->bonus_life_flash != 0, game->animcycle, setup.bd_show_invisible_outbox);
572
573   game->state_counter = counter_next;
574
575   return return_state;
576 }
577
578 void play_game_func(GdGame *game, int action)
579 {
580   GdGameState state;
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);
586
587   if (game->player_move_stick || move_up || move_down || move_left || move_right) // no "fire"!
588   {
589     // get movement
590     game->player_move = gd_direction_from_keypress(move_up, move_down, move_left, move_right);
591
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;
595   }
596
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;
600
601   // tell the interrupt "20ms has passed"
602   state = gd_game_main_int(game, !game->out_of_window, gd_keystate[SDL_SCANCODE_F]);
603
604   // state of game, returned by gd_game_main_int
605   switch (state)
606   {
607     case GD_GAME_CAVE_LOADED:
608       // scroll to start position
609       gd_scroll_to_origin();
610
611       // fill whole screen with black - cave might be smaller than previous!
612       FillRectangle(gd_screen_bitmap, 0, 0, SXSIZE, SYSIZE, BLACK_PIXEL);
613       break;
614
615     default:
616       break;
617   }
618
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)
625   {
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);
629   }
630 }