2010-10-12 10:55:23 +02:00
|
|
|
/*
|
2011-03-05 13:44:21 +01:00
|
|
|
* This software is licensed under the terms of the MIT-License
|
2017-02-10 23:05:22 +01:00
|
|
|
* See COPYING for further information.
|
2011-03-05 13:44:21 +01:00
|
|
|
* ---
|
2019-01-23 21:10:43 +01:00
|
|
|
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@alienslab.net>.
|
2010-10-12 10:55:23 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
|
|
|
|
2017-03-06 01:25:59 +01:00
|
|
|
#include "util.h"
|
2010-10-12 10:55:23 +02:00
|
|
|
#include "stage.h"
|
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
#include <time.h>
|
2010-10-12 10:55:23 +02:00
|
|
|
#include "global.h"
|
2012-07-28 22:53:53 +02:00
|
|
|
#include "video.h"
|
2017-01-24 14:40:57 +01:00
|
|
|
#include "resource/bgm.h"
|
2012-07-14 16:37:52 +02:00
|
|
|
#include "replay.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "player.h"
|
2011-06-13 18:48:36 +02:00
|
|
|
#include "menu/ingamemenu.h"
|
2012-08-17 20:58:23 +02:00
|
|
|
#include "menu/gameovermenu.h"
|
2019-03-05 20:43:01 +01:00
|
|
|
#include "audio/audio.h"
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
#include "log.h"
|
2017-04-02 16:06:18 +02:00
|
|
|
#include "stagetext.h"
|
2017-04-07 14:20:45 +02:00
|
|
|
#include "stagedraw.h"
|
2017-12-13 20:05:12 +01:00
|
|
|
#include "stageobjects.h"
|
2010-10-12 10:55:23 +02:00
|
|
|
|
2018-08-14 00:48:18 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define DPSTEST
|
|
|
|
#include "stages/dpstest.h"
|
|
|
|
#endif
|
|
|
|
|
2017-02-27 15:27:48 +01:00
|
|
|
static size_t numstages = 0;
|
|
|
|
StageInfo *stages = NULL;
|
|
|
|
|
2017-10-02 23:23:02 +02:00
|
|
|
static void add_stage(uint16_t id, StageProcs *procs, StageType type, const char *title, const char *subtitle, AttackInfo *spell, Difficulty diff) {
|
2017-02-27 15:27:48 +01:00
|
|
|
++numstages;
|
|
|
|
stages = realloc(stages, numstages * sizeof(StageInfo));
|
|
|
|
StageInfo *stg = stages + (numstages - 1);
|
|
|
|
memset(stg, 0, sizeof(StageInfo));
|
|
|
|
|
|
|
|
stg->id = id;
|
|
|
|
stg->procs = procs;
|
|
|
|
stg->type = type;
|
|
|
|
stralloc(&stg->title, title);
|
|
|
|
stralloc(&stg->subtitle, subtitle);
|
|
|
|
stg->spell = spell;
|
|
|
|
stg->difficulty = diff;
|
|
|
|
}
|
|
|
|
|
2017-03-06 01:03:06 +01:00
|
|
|
static void end_stages(void) {
|
2017-10-02 23:23:02 +02:00
|
|
|
add_stage(0, NULL, 0, NULL, NULL, NULL, 0);
|
2017-02-27 15:27:48 +01:00
|
|
|
}
|
2012-07-14 09:40:37 +02:00
|
|
|
|
2018-01-21 11:35:48 +01:00
|
|
|
static void add_spellpractice_stage(StageInfo *s, AttackInfo *a, int *spellnum, uint16_t spellbits, Difficulty diff) {
|
|
|
|
uint16_t id = spellbits | a->idmap[diff - D_Easy] | (s->id << 8);
|
|
|
|
|
|
|
|
char *title = strfmt("Spell %d", ++(*spellnum));
|
|
|
|
char *subtitle = strjoin(a->name, " ~ ", difficulty_name(diff), NULL);
|
|
|
|
|
|
|
|
add_stage(id, s->procs->spellpractice_procs, STAGE_SPELL, title, subtitle, a, diff);
|
|
|
|
|
|
|
|
free(title);
|
|
|
|
free(subtitle);
|
|
|
|
}
|
|
|
|
|
2017-02-27 16:36:43 +01:00
|
|
|
static void add_spellpractice_stages(int *spellnum, bool (*filter)(AttackInfo*), uint16_t spellbits) {
|
|
|
|
for(int i = 0 ;; ++i) {
|
2017-02-27 15:27:48 +01:00
|
|
|
StageInfo *s = stages + i;
|
2017-02-15 14:04:47 +01:00
|
|
|
|
2018-08-14 00:48:18 +02:00
|
|
|
if(s->type == STAGE_SPELL || !s->spell) {
|
2017-02-27 16:36:43 +01:00
|
|
|
break;
|
2017-02-10 11:39:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 15:27:48 +01:00
|
|
|
for(AttackInfo *a = s->spell; a->rule; ++a) {
|
2017-02-27 16:36:43 +01:00
|
|
|
if(!filter(a)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-27 15:27:48 +01:00
|
|
|
for(Difficulty diff = D_Easy; diff < D_Easy + NUM_SELECTABLE_DIFFICULTIES; ++diff) {
|
|
|
|
if(a->idmap[diff - D_Easy] >= 0) {
|
2018-01-21 11:35:48 +01:00
|
|
|
add_spellpractice_stage(s, a, spellnum, spellbits, diff);
|
2017-02-27 15:27:48 +01:00
|
|
|
s = stages + i; // stages just got realloc'd, so we must update the pointer
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 11:39:42 +01:00
|
|
|
}
|
2017-02-27 15:27:48 +01:00
|
|
|
}
|
2017-02-27 16:36:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool spellfilter_normal(AttackInfo *spell) {
|
|
|
|
return spell->type != AT_ExtraSpell;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool spellfilter_extra(AttackInfo *spell) {
|
|
|
|
return spell->type == AT_ExtraSpell;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stage_init_array(void) {
|
|
|
|
int spellnum = 0;
|
|
|
|
|
2018-01-21 11:35:48 +01:00
|
|
|
// id procs type title subtitle spells diff
|
2017-10-02 23:23:02 +02:00
|
|
|
add_stage(1, &stage1_procs, STAGE_STORY, "Stage 1", "Misty Lake", (AttackInfo*)&stage1_spells, D_Any);
|
|
|
|
add_stage(2, &stage2_procs, STAGE_STORY, "Stage 2", "Walk Along the Border", (AttackInfo*)&stage2_spells, D_Any);
|
|
|
|
add_stage(3, &stage3_procs, STAGE_STORY, "Stage 3", "Through the Tunnel of Light", (AttackInfo*)&stage3_spells, D_Any);
|
|
|
|
add_stage(4, &stage4_procs, STAGE_STORY, "Stage 4", "Forgotten Mansion", (AttackInfo*)&stage4_spells, D_Any);
|
|
|
|
add_stage(5, &stage5_procs, STAGE_STORY, "Stage 5", "Climbing the Tower of Babel", (AttackInfo*)&stage5_spells, D_Any);
|
|
|
|
add_stage(6, &stage6_procs, STAGE_STORY, "Stage 6", "Roof of the World", (AttackInfo*)&stage6_spells, D_Any);
|
2017-02-27 16:36:43 +01:00
|
|
|
|
2018-08-14 00:48:18 +02:00
|
|
|
#ifdef DPSTEST
|
|
|
|
add_stage(0x40|0, &stage_dpstest_single_procs, STAGE_SPECIAL, "DPS Test", "Single target", NULL, D_Normal);
|
|
|
|
add_stage(0x40|1, &stage_dpstest_multi_procs, STAGE_SPECIAL, "DPS Test", "Multiple targets", NULL, D_Normal);
|
|
|
|
add_stage(0x40|2, &stage_dpstest_boss_procs, STAGE_SPECIAL, "DPS Test", "Boss", NULL, D_Normal);
|
|
|
|
#endif
|
|
|
|
|
2017-02-27 16:36:43 +01:00
|
|
|
// generate spellpractice stages
|
|
|
|
add_spellpractice_stages(&spellnum, spellfilter_normal, STAGE_SPELL_BIT);
|
|
|
|
add_spellpractice_stages(&spellnum, spellfilter_extra, STAGE_SPELL_BIT | STAGE_EXTRASPELL_BIT);
|
2017-02-27 15:27:48 +01:00
|
|
|
|
2018-01-21 11:35:48 +01:00
|
|
|
#ifdef SPELL_BENCHMARK
|
|
|
|
add_spellpractice_stage(stages, &stage1_spell_benchmark, &spellnum, STAGE_SPELL_BIT, D_Extra);
|
|
|
|
#endif
|
|
|
|
|
2017-02-27 15:27:48 +01:00
|
|
|
end_stages();
|
2017-02-10 11:39:42 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2017-02-27 15:27:48 +01:00
|
|
|
for(int i = 0; stages[i].procs; ++i) {
|
2017-02-10 11:39:42 +01:00
|
|
|
if(stages[i].type == STAGE_SPELL && !(stages[i].id & STAGE_SPELL_BIT)) {
|
2017-03-13 17:03:51 +01:00
|
|
|
log_fatal("Spell stage has an ID without the spell bit set: 0x%04x", stages[i].id);
|
2017-02-10 11:39:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
for(int j = 0; stages[j].procs; ++j) {
|
2017-02-10 11:39:42 +01:00
|
|
|
if(i != j && stages[i].id == stages[j].id) {
|
2017-12-31 09:08:07 +01:00
|
|
|
log_fatal("Duplicate ID %X in stages array, indices: %i, %i", stages[i].id, i, j);
|
2017-02-10 11:39:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-27 15:27:48 +01:00
|
|
|
#endif
|
2017-02-10 11:39:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 13:53:24 +01:00
|
|
|
void stage_free_array(void) {
|
2017-02-26 13:17:48 +01:00
|
|
|
for(StageInfo *stg = stages; stg->procs; ++stg) {
|
2017-02-27 15:27:48 +01:00
|
|
|
free(stg->title);
|
|
|
|
free(stg->subtitle);
|
2017-02-23 15:05:55 +01:00
|
|
|
free(stg->progress);
|
2017-02-15 13:53:24 +01:00
|
|
|
}
|
2017-02-27 15:27:48 +01:00
|
|
|
|
|
|
|
free(stages);
|
2017-02-15 13:53:24 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
// NOTE: This returns the stage BY ID, not by the array index!
|
2017-02-10 11:39:42 +01:00
|
|
|
StageInfo* stage_get(uint16_t n) {
|
2017-02-26 13:17:48 +01:00
|
|
|
for(StageInfo *stg = stages; stg->procs; ++stg)
|
2017-02-10 11:39:42 +01:00
|
|
|
if(stg->id == n)
|
|
|
|
return stg;
|
2012-07-14 09:40:37 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-10 23:34:48 +01:00
|
|
|
StageInfo* stage_get_by_spellcard(AttackInfo *spell, Difficulty diff) {
|
|
|
|
if(!spell)
|
|
|
|
return NULL;
|
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
for(StageInfo *stg = stages; stg->procs; ++stg)
|
2017-02-10 23:34:48 +01:00
|
|
|
if(stg->spell == spell && stg->difficulty == diff)
|
|
|
|
return stg;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-11 13:22:14 +01:00
|
|
|
StageProgress* stage_get_progress_from_info(StageInfo *stage, Difficulty diff, bool allocate) {
|
2017-02-11 12:38:50 +01:00
|
|
|
// D_Any stages will have a separate StageProgress for every selectable difficulty.
|
|
|
|
// Stages with a fixed difficulty setting (spellpractice, extra stage...) obviously get just one and the diff parameter is ignored.
|
|
|
|
|
|
|
|
// This stuff must stay around until progress_save(), which happens on shutdown.
|
|
|
|
// So do NOT try to free any pointers this function returns, that will fuck everything up.
|
|
|
|
|
|
|
|
if(!stage) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fixed_diff = (stage->difficulty != D_Any);
|
|
|
|
|
|
|
|
if(!fixed_diff && (diff < D_Easy || diff > D_Lunatic)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!stage->progress) {
|
2017-02-11 13:22:14 +01:00
|
|
|
if(!allocate) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-11 12:38:50 +01:00
|
|
|
size_t allocsize = sizeof(StageProgress) * (fixed_diff ? 1 : NUM_SELECTABLE_DIFFICULTIES);
|
|
|
|
stage->progress = malloc(allocsize);
|
|
|
|
memset(stage->progress, 0, allocsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stage->progress + (fixed_diff ? 0 : diff - D_Easy);
|
|
|
|
}
|
|
|
|
|
2017-02-11 13:22:14 +01:00
|
|
|
StageProgress* stage_get_progress(uint16_t id, Difficulty diff, bool allocate) {
|
|
|
|
return stage_get_progress_from_info(stage_get(id), diff, allocate);
|
2017-02-11 12:38:50 +01:00
|
|
|
}
|
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
static void stage_start(StageInfo *stage) {
|
2011-05-08 13:48:25 +02:00
|
|
|
global.timer = 0;
|
2011-06-13 18:48:36 +02:00
|
|
|
global.frames = 0;
|
2019-02-22 00:56:03 +01:00
|
|
|
global.gameover = 0;
|
2012-08-07 17:01:26 +02:00
|
|
|
global.shake_view = 0;
|
2019-02-22 00:56:03 +01:00
|
|
|
global.voltage_threshold = 0;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-10-08 13:30:51 +02:00
|
|
|
player_stage_pre_init(&global.plr);
|
2017-02-26 13:17:48 +01:00
|
|
|
|
|
|
|
if(stage->type == STAGE_SPELL) {
|
2018-01-21 10:52:54 +01:00
|
|
|
global.is_practice_mode = true;
|
2017-03-21 11:09:32 +01:00
|
|
|
global.plr.lives = 0;
|
2017-02-26 13:17:48 +01:00
|
|
|
global.plr.bombs = 0;
|
2017-09-11 21:09:30 +02:00
|
|
|
} else if(global.is_practice_mode) {
|
|
|
|
global.plr.lives = PLR_STGPRACTICE_LIVES;
|
|
|
|
global.plr.bombs = PLR_STGPRACTICE_BOMBS;
|
2018-01-20 16:54:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(global.is_practice_mode) {
|
|
|
|
global.plr.power = config_get_int(CONFIG_PRACTICE_POWER);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(global.plr.power < 0) {
|
|
|
|
global.plr.power = 0;
|
|
|
|
} else if(global.plr.power > PLR_MAX_POWER) {
|
|
|
|
global.plr.power = PLR_MAX_POWER;
|
2017-02-26 13:17:48 +01:00
|
|
|
}
|
2017-03-02 11:23:30 +01:00
|
|
|
|
|
|
|
reset_sounds();
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 16:06:46 +02:00
|
|
|
static bool ingame_menu_interrupts_bgm(void) {
|
|
|
|
return global.stage->type != STAGE_SPELL;
|
|
|
|
}
|
|
|
|
|
2017-12-17 01:00:06 +01:00
|
|
|
static void stage_fade_bgm(void) {
|
|
|
|
fade_bgm((FPS * FADE_TIME) / 2000.0);
|
|
|
|
}
|
|
|
|
|
2018-01-10 20:50:01 +01:00
|
|
|
static void stage_ingame_menu_loop(MenuData *menu) {
|
2017-10-10 16:06:46 +02:00
|
|
|
if(ingame_menu_interrupts_bgm()) {
|
|
|
|
stop_bgm(false);
|
|
|
|
}
|
|
|
|
|
2017-10-08 07:15:43 +02:00
|
|
|
pause_sounds();
|
2018-01-10 20:50:01 +01:00
|
|
|
menu_loop(menu);
|
2017-10-10 16:06:46 +02:00
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover > 0) {
|
2017-10-10 16:06:46 +02:00
|
|
|
stop_sounds();
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(ingame_menu_interrupts_bgm() || global.gameover != GAMEOVER_RESTART) {
|
2017-12-17 01:00:06 +01:00
|
|
|
stage_fade_bgm();
|
2017-10-10 16:06:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resume_sounds();
|
|
|
|
resume_bgm();
|
|
|
|
}
|
2012-07-14 16:37:52 +02:00
|
|
|
}
|
|
|
|
|
2018-01-10 20:50:01 +01:00
|
|
|
void stage_pause(void) {
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover == GAMEOVER_TRANSITIONING) {
|
2018-07-24 20:30:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-10 20:50:01 +01:00
|
|
|
MenuData menu;
|
|
|
|
|
|
|
|
if(global.replaymode == REPLAY_PLAY) {
|
|
|
|
create_ingame_menu_replay(&menu);
|
|
|
|
} else {
|
|
|
|
create_ingame_menu(&menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
stage_ingame_menu_loop(&menu);
|
|
|
|
}
|
|
|
|
|
2012-08-17 20:58:23 +02:00
|
|
|
void stage_gameover(void) {
|
2017-02-28 00:07:03 +01:00
|
|
|
if(global.stage->type == STAGE_SPELL && config_get_int(CONFIG_SPELLSTAGE_AUTORESTART)) {
|
2019-02-22 00:56:03 +01:00
|
|
|
global.gameover = GAMEOVER_RESTART;
|
2017-02-28 00:07:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-24 22:58:27 +01:00
|
|
|
MenuData menu;
|
|
|
|
create_gameover_menu(&menu);
|
2018-01-10 20:50:01 +01:00
|
|
|
stage_ingame_menu_loop(&menu);
|
2012-08-17 20:58:23 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 02:35:53 +01:00
|
|
|
static bool stage_input_common(SDL_Event *event, void *arg) {
|
|
|
|
TaiseiEvent type = TAISEI_EVENT(event->type);
|
|
|
|
int32_t code = event->user.code;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case TE_GAME_KEY_DOWN:
|
|
|
|
switch(code) {
|
|
|
|
case KEY_STOP:
|
|
|
|
// global.game_over = GAMEOVER_DEFEAT;
|
|
|
|
stage_finish(GAMEOVER_DEFEAT);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case KEY_RESTART:
|
|
|
|
// global.game_over = GAMEOVER_RESTART;
|
|
|
|
stage_finish(GAMEOVER_RESTART);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TE_GAME_PAUSE:
|
|
|
|
stage_pause();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
static bool stage_input_key_filter(KeyIndex key, bool is_release) {
|
|
|
|
if(key == KEY_HAHAIWIN) {
|
|
|
|
IF_DEBUG(
|
|
|
|
if(!is_release) {
|
|
|
|
stage_finish(GAMEOVER_WIN);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IF_NOT_DEBUG(
|
|
|
|
if(
|
|
|
|
key == KEY_IDDQD ||
|
|
|
|
key == KEY_POWERUP ||
|
|
|
|
key == KEY_POWERDOWN
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if(stage_is_cleared()) {
|
|
|
|
if(key == KEY_SHOT) {
|
|
|
|
if(
|
|
|
|
global.gameover == GAMEOVER_SCORESCREEN &&
|
|
|
|
global.frames - global.gameover_time > GAMEOVER_SCORE_DELAY * 2
|
|
|
|
) {
|
|
|
|
if(!is_release) {
|
|
|
|
stage_finish(GAMEOVER_WIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(key == KEY_BOMB || key == KEY_SPECIAL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-24 21:21:08 +01:00
|
|
|
static bool stage_input_handler_gameplay(SDL_Event *event, void *arg) {
|
2017-09-29 21:03:49 +02:00
|
|
|
TaiseiEvent type = TAISEI_EVENT(event->type);
|
|
|
|
int32_t code = event->user.code;
|
|
|
|
|
2017-12-28 02:35:53 +01:00
|
|
|
if(stage_input_common(event, arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
switch(type) {
|
2017-09-29 21:03:49 +02:00
|
|
|
case TE_GAME_KEY_DOWN:
|
2019-02-22 00:56:03 +01:00
|
|
|
if(stage_input_key_filter(code, false)) {
|
|
|
|
player_event_with_replay(&global.plr, EV_PRESS, code);
|
2017-02-15 18:10:56 +01:00
|
|
|
}
|
2012-08-13 17:50:28 +02:00
|
|
|
break;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-09-29 21:03:49 +02:00
|
|
|
case TE_GAME_KEY_UP:
|
2019-02-22 00:56:03 +01:00
|
|
|
if(stage_input_key_filter(code, true)) {
|
|
|
|
player_event_with_replay(&global.plr, EV_RELEASE, code);
|
|
|
|
}
|
2012-08-07 05:28:41 +02:00
|
|
|
break;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-09-29 21:03:49 +02:00
|
|
|
case TE_GAME_AXIS_LR:
|
|
|
|
player_event_with_replay(&global.plr, EV_AXIS_LR, (uint16_t)code);
|
2012-08-15 16:36:39 +02:00
|
|
|
break;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-09-29 21:03:49 +02:00
|
|
|
case TE_GAME_AXIS_UD:
|
|
|
|
player_event_with_replay(&global.plr, EV_AXIS_UD, (uint16_t)code);
|
2012-08-15 16:36:39 +02:00
|
|
|
break;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-08-13 17:50:28 +02:00
|
|
|
default: break;
|
|
|
|
}
|
2017-09-29 21:03:49 +02:00
|
|
|
|
|
|
|
return false;
|
2012-08-13 17:50:28 +02:00
|
|
|
}
|
|
|
|
|
2019-01-24 21:21:08 +01:00
|
|
|
static bool stage_input_handler_replay(SDL_Event *event, void *arg) {
|
2017-12-28 02:35:53 +01:00
|
|
|
stage_input_common(event, arg);
|
2017-09-29 21:03:49 +02:00
|
|
|
return false;
|
2012-08-13 17:50:28 +02:00
|
|
|
}
|
|
|
|
|
2019-01-24 21:21:08 +01:00
|
|
|
static void replay_input(void) {
|
2017-02-10 00:24:19 +01:00
|
|
|
ReplayStage *s = global.replay_stage;
|
2012-07-14 19:46:03 +02:00
|
|
|
int i;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-09-29 21:03:49 +02:00
|
|
|
events_poll((EventHandler[]){
|
|
|
|
{ .proc = stage_input_handler_replay },
|
|
|
|
{NULL}
|
|
|
|
}, EFLAG_GAME);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-02-09 05:06:46 +01:00
|
|
|
for(i = s->playpos; i < s->numevents; ++i) {
|
2017-03-21 03:28:35 +01:00
|
|
|
ReplayEvent *e = s->events + i;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-07-29 15:35:40 +02:00
|
|
|
if(e->frame != global.frames)
|
|
|
|
break;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-07-29 15:35:40 +02:00
|
|
|
switch(e->type) {
|
2012-07-14 19:46:03 +02:00
|
|
|
case EV_OVER:
|
2019-02-22 00:56:03 +01:00
|
|
|
global.gameover = GAMEOVER_DEFEAT;
|
2012-07-14 19:46:03 +02:00
|
|
|
break;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-03-21 03:28:35 +01:00
|
|
|
case EV_CHECK_DESYNC:
|
|
|
|
s->desync_check = e->value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EV_FPS:
|
|
|
|
s->fps = e->value;
|
|
|
|
break;
|
|
|
|
|
2017-10-29 23:45:24 +01:00
|
|
|
default: {
|
2017-10-31 10:48:30 +01:00
|
|
|
player_event(&global.plr, e->type, (int16_t)e->value, NULL, NULL);
|
2012-07-14 19:46:03 +02:00
|
|
|
break;
|
2017-10-29 23:45:24 +01:00
|
|
|
}
|
2012-07-14 19:46:03 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-08-07 05:28:41 +02:00
|
|
|
s->playpos = i;
|
2012-07-14 19:46:03 +02:00
|
|
|
player_applymovement(&global.plr);
|
|
|
|
}
|
|
|
|
|
2019-01-24 21:21:08 +01:00
|
|
|
static void stage_input(void) {
|
2017-09-29 21:03:49 +02:00
|
|
|
events_poll((EventHandler[]){
|
|
|
|
{ .proc = stage_input_handler_gameplay },
|
|
|
|
{NULL}
|
|
|
|
}, EFLAG_GAME);
|
2017-09-19 20:46:28 +02:00
|
|
|
player_fix_input(&global.plr);
|
2017-03-07 00:57:14 +01:00
|
|
|
player_applymovement(&global.plr);
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
static void stage_logic(void) {
|
2010-10-12 10:55:23 +02:00
|
|
|
player_logic(&global.plr);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
Give projectiles EVENT_BIRTH and call them with t==0 more consistently
The only case of t==0 being skipped is if the projectile was somehow
created after the projectile processing loop for this frame has been
finished. Currently that is only possible if a particle spawns a
non-particle projectile, which, ideally, should never happen. The same
problem exists with other types of entities. For example, if you have a
funny projectile rule that spawns an Enemy, that Enemy will have the 0th
frame skipped. One way to fix this is to maintain a list of all entities
in the game and process them all in a single loop, where newly spawned
entities would be appended to the tail of the list. This is also
probably the only good way to fix this, too.
Handling of all projectile events has been made mandatory to facilitate
easier debugging of subtle and/or hard to track bugs. If t<0, then the
projectile rule MUST return ACTION_ACK, signifying that it acknowledged
the event and handled it appropriately. Otherwise, it SHOULD return
ACTION_NONE or ACTION_DESTROY. In a perfect world, those just wouldn't
be conflated in the same function with update logic.
I've also cleaned up the stage_logic routine a bit. Moved most of the
dialog handling into dialog.c and gave higher priority to boss
processing. The later is currently necessary to let boss-spawned
projectiles and particles to get called with t==0.
2018-05-16 01:38:47 +02:00
|
|
|
process_boss(&global.boss);
|
2011-04-26 12:04:45 +02:00
|
|
|
process_enemies(&global.enemies);
|
2017-02-11 04:52:08 +01:00
|
|
|
process_projectiles(&global.projs, true);
|
2011-04-29 10:26:37 +02:00
|
|
|
process_items();
|
2011-04-24 15:39:17 +02:00
|
|
|
process_lasers();
|
2017-02-11 04:52:08 +01:00
|
|
|
process_projectiles(&global.particles, false);
|
Give projectiles EVENT_BIRTH and call them with t==0 more consistently
The only case of t==0 being skipped is if the projectile was somehow
created after the projectile processing loop for this frame has been
finished. Currently that is only possible if a particle spawns a
non-particle projectile, which, ideally, should never happen. The same
problem exists with other types of entities. For example, if you have a
funny projectile rule that spawns an Enemy, that Enemy will have the 0th
frame skipped. One way to fix this is to maintain a list of all entities
in the game and process them all in a single loop, where newly spawned
entities would be appended to the tail of the list. This is also
probably the only good way to fix this, too.
Handling of all projectile events has been made mandatory to facilitate
easier debugging of subtle and/or hard to track bugs. If t<0, then the
projectile rule MUST return ACTION_ACK, signifying that it acknowledged
the event and handled it appropriately. Otherwise, it SHOULD return
ACTION_NONE or ACTION_DESTROY. In a perfect world, those just wouldn't
be conflated in the same function with update logic.
I've also cleaned up the stage_logic routine a bit. Moved most of the
dialog handling into dialog.c and gave higher priority to boss
processing. The later is currently necessary to let boss-spawned
projectiles and particles to get called with t==0.
2018-05-16 01:38:47 +02:00
|
|
|
process_dialog(&global.dialog);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-09-30 19:33:07 +02:00
|
|
|
update_sounds();
|
|
|
|
|
2010-10-12 10:55:23 +02:00
|
|
|
global.frames++;
|
2017-02-10 23:05:22 +01:00
|
|
|
|
Give projectiles EVENT_BIRTH and call them with t==0 more consistently
The only case of t==0 being skipped is if the projectile was somehow
created after the projectile processing loop for this frame has been
finished. Currently that is only possible if a particle spawns a
non-particle projectile, which, ideally, should never happen. The same
problem exists with other types of entities. For example, if you have a
funny projectile rule that spawns an Enemy, that Enemy will have the 0th
frame skipped. One way to fix this is to maintain a list of all entities
in the game and process them all in a single loop, where newly spawned
entities would be appended to the tail of the list. This is also
probably the only good way to fix this, too.
Handling of all projectile events has been made mandatory to facilitate
easier debugging of subtle and/or hard to track bugs. If t<0, then the
projectile rule MUST return ACTION_ACK, signifying that it acknowledged
the event and handled it appropriately. Otherwise, it SHOULD return
ACTION_NONE or ACTION_DESTROY. In a perfect world, those just wouldn't
be conflated in the same function with update logic.
I've also cleaned up the stage_logic routine a bit. Moved most of the
dialog handling into dialog.c and gave higher priority to boss
processing. The later is currently necessary to let boss-spawned
projectiles and particles to get called with t==0.
2018-05-16 01:38:47 +02:00
|
|
|
if(!global.dialog && (!global.boss || boss_is_fleeing(global.boss))) {
|
2012-07-14 19:46:03 +02:00
|
|
|
global.timer++;
|
Give projectiles EVENT_BIRTH and call them with t==0 more consistently
The only case of t==0 being skipped is if the projectile was somehow
created after the projectile processing loop for this frame has been
finished. Currently that is only possible if a particle spawns a
non-particle projectile, which, ideally, should never happen. The same
problem exists with other types of entities. For example, if you have a
funny projectile rule that spawns an Enemy, that Enemy will have the 0th
frame skipped. One way to fix this is to maintain a list of all entities
in the game and process them all in a single loop, where newly spawned
entities would be appended to the tail of the list. This is also
probably the only good way to fix this, too.
Handling of all projectile events has been made mandatory to facilitate
easier debugging of subtle and/or hard to track bugs. If t<0, then the
projectile rule MUST return ACTION_ACK, signifying that it acknowledged
the event and handled it appropriately. Otherwise, it SHOULD return
ACTION_NONE or ACTION_DESTROY. In a perfect world, those just wouldn't
be conflated in the same function with update logic.
I've also cleaned up the stage_logic routine a bit. Moved most of the
dialog handling into dialog.c and gave higher priority to boss
processing. The later is currently necessary to let boss-spawned
projectiles and particles to get called with t==0.
2018-05-16 01:38:47 +02:00
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-02-26 20:27:10 +01:00
|
|
|
if(global.replaymode == REPLAY_PLAY &&
|
|
|
|
global.frames == global.replay_stage->events[global.replay_stage->numevents-1].frame - FADE_TIME &&
|
2019-02-22 00:56:03 +01:00
|
|
|
global.gameover != GAMEOVER_TRANSITIONING) {
|
2017-02-26 13:17:48 +01:00
|
|
|
stage_finish(GAMEOVER_DEFEAT);
|
2017-02-10 00:24:19 +01:00
|
|
|
}
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 19:58:50 +02:00
|
|
|
void stage_clear_hazards_predicate(bool (*predicate)(EntityInterface *ent, void *arg), void *arg, ClearHazardsFlags flags) {
|
2018-01-06 10:24:46 +01:00
|
|
|
if(flags & CLEAR_HAZARDS_BULLETS) {
|
2019-01-26 02:54:57 +01:00
|
|
|
for(Projectile *p = global.projs.first, *next; p; p = next) {
|
|
|
|
next = p->next;
|
2018-08-05 19:58:50 +02:00
|
|
|
|
|
|
|
if(!predicate || predicate(&p->ent, arg)) {
|
2019-02-22 00:56:03 +01:00
|
|
|
clear_projectile(p, flags);
|
2018-08-05 19:58:50 +02:00
|
|
|
}
|
2018-01-06 09:54:13 +01:00
|
|
|
}
|
2017-04-06 00:46:00 +02:00
|
|
|
}
|
|
|
|
|
2018-01-06 10:24:46 +01:00
|
|
|
if(flags & CLEAR_HAZARDS_LASERS) {
|
2018-06-01 20:40:18 +02:00
|
|
|
for(Laser *l = global.lasers.first, *next; l; l = next) {
|
2018-01-06 19:23:38 +01:00
|
|
|
next = l->next;
|
2018-08-05 19:58:50 +02:00
|
|
|
|
|
|
|
if(!predicate || predicate(&l->ent, arg)) {
|
2019-02-22 00:56:03 +01:00
|
|
|
clear_laser(l, flags);
|
2018-08-05 19:58:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stage_clear_hazards(ClearHazardsFlags flags) {
|
|
|
|
stage_clear_hazards_predicate(NULL, NULL, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool proximity_predicate(EntityInterface *ent, void *varg) {
|
|
|
|
Circle *area = varg;
|
|
|
|
|
|
|
|
switch(ent->type) {
|
|
|
|
case ENT_PROJECTILE: {
|
|
|
|
Projectile *p = ENT_CAST(ent, Projectile);
|
|
|
|
return cabs(p->pos - area->origin) < area->radius;
|
2018-01-06 09:54:13 +01:00
|
|
|
}
|
2018-08-05 19:58:50 +02:00
|
|
|
|
|
|
|
case ENT_LASER: {
|
|
|
|
Laser *l = ENT_CAST(ent, Laser);
|
|
|
|
return laser_intersects_circle(l, *area);
|
|
|
|
}
|
|
|
|
|
|
|
|
default: UNREACHABLE;
|
2017-10-31 14:47:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:58:50 +02:00
|
|
|
void stage_clear_hazards_at(complex origin, double radius, ClearHazardsFlags flags) {
|
|
|
|
Circle area = { origin, radius };
|
|
|
|
stage_clear_hazards_predicate(proximity_predicate, &area, flags);
|
|
|
|
}
|
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
static void stage_free(void) {
|
2011-04-26 22:39:50 +02:00
|
|
|
delete_enemies(&global.enemies);
|
2017-05-05 03:44:05 +02:00
|
|
|
delete_enemies(&global.plr.slaves);
|
2011-04-29 10:26:37 +02:00
|
|
|
delete_items();
|
2011-04-26 22:47:13 +02:00
|
|
|
delete_lasers();
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-08-07 17:07:29 +02:00
|
|
|
delete_projectiles(&global.projs);
|
|
|
|
delete_projectiles(&global.particles);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2011-06-13 18:48:36 +02:00
|
|
|
if(global.dialog) {
|
|
|
|
delete_dialog(global.dialog);
|
|
|
|
global.dialog = NULL;
|
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2011-06-13 18:48:36 +02:00
|
|
|
if(global.boss) {
|
|
|
|
free_boss(global.boss);
|
|
|
|
global.boss = NULL;
|
|
|
|
}
|
2017-04-02 16:06:18 +02:00
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
projectiles_free();
|
2018-04-12 16:08:48 +02:00
|
|
|
lasers_free();
|
2017-04-02 16:06:18 +02:00
|
|
|
stagetext_free();
|
2010-10-12 10:55:23 +02:00
|
|
|
}
|
2011-06-25 12:41:40 +02:00
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
static void stage_finalize(void *arg) {
|
2019-02-22 00:56:03 +01:00
|
|
|
global.gameover = (intptr_t)arg;
|
2017-02-26 13:17:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void stage_finish(int gameover) {
|
2019-02-22 00:56:03 +01:00
|
|
|
assert(global.gameover != GAMEOVER_TRANSITIONING);
|
2018-01-16 12:55:13 +01:00
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
int prev_gameover = global.gameover;
|
|
|
|
global.gameover_time = global.frames;
|
2017-04-06 02:58:57 +02:00
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(gameover == GAMEOVER_SCORESCREEN) {
|
|
|
|
global.gameover = GAMEOVER_SCORESCREEN;
|
|
|
|
} else {
|
|
|
|
global.gameover = GAMEOVER_TRANSITIONING;
|
|
|
|
set_transition_callback(TransFadeBlack, FADE_TIME, FADE_TIME*2, stage_finalize, (void*)(intptr_t)gameover);
|
|
|
|
stage_fade_bgm();
|
2017-04-06 02:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(
|
|
|
|
global.replaymode != REPLAY_PLAY &&
|
|
|
|
prev_gameover != GAMEOVER_SCORESCREEN &&
|
|
|
|
(gameover == GAMEOVER_SCORESCREEN || gameover == GAMEOVER_WIN)
|
|
|
|
) {
|
|
|
|
StageProgress *p = stage_get_progress_from_info(global.stage, global.diff, true);
|
2017-04-06 02:58:57 +02:00
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(p) {
|
|
|
|
++p->num_cleared;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("Stage cleared %u times now", p->num_cleared);
|
2017-04-06 02:58:57 +02:00
|
|
|
}
|
2017-02-26 13:17:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 04:41:57 +01:00
|
|
|
static void stage_preload(void) {
|
|
|
|
difficulty_preload();
|
|
|
|
projectiles_preload();
|
|
|
|
player_preload();
|
|
|
|
items_preload();
|
|
|
|
boss_preload();
|
2018-04-12 16:08:48 +02:00
|
|
|
lasers_preload();
|
2017-03-11 04:41:57 +01:00
|
|
|
|
2018-04-12 16:08:48 +02:00
|
|
|
if(global.stage->type != STAGE_SPELL) {
|
2017-03-11 04:41:57 +01:00
|
|
|
enemies_preload();
|
2018-04-12 16:08:48 +02:00
|
|
|
}
|
2017-03-11 04:41:57 +01:00
|
|
|
|
|
|
|
global.stage->procs->preload();
|
|
|
|
}
|
|
|
|
|
2017-04-07 14:20:45 +02:00
|
|
|
static void display_stage_title(StageInfo *info) {
|
2018-07-23 19:07:59 +02:00
|
|
|
stagetext_add(info->title, VIEWPORT_W/2 + I * (VIEWPORT_H/2-40), ALIGN_CENTER, get_font("big"), RGB(1, 1, 1), 50, 85, 35, 35);
|
|
|
|
stagetext_add(info->subtitle, VIEWPORT_W/2 + I * (VIEWPORT_H/2), ALIGN_CENTER, get_font("standard"), RGB(1, 1, 1), 60, 85, 35, 35);
|
2017-10-02 23:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void stage_start_bgm(const char *bgm) {
|
2017-12-17 01:13:10 +01:00
|
|
|
char *old_title = NULL;
|
|
|
|
|
|
|
|
if(current_bgm.title && global.stage->type == STAGE_SPELL) {
|
|
|
|
old_title = strdup(current_bgm.title);
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:23:02 +02:00
|
|
|
start_bgm(bgm);
|
2017-04-07 14:20:45 +02:00
|
|
|
|
2017-12-17 01:13:10 +01:00
|
|
|
if(current_bgm.title && current_bgm.started_at >= 0 && (!old_title || strcmp(current_bgm.title, old_title))) {
|
2017-10-02 02:48:34 +02:00
|
|
|
char txt[strlen(current_bgm.title) + 6];
|
2017-10-02 23:23:02 +02:00
|
|
|
snprintf(txt, sizeof(txt), "BGM: %s", current_bgm.title);
|
2018-07-23 19:07:59 +02:00
|
|
|
stagetext_add(txt, VIEWPORT_W-15 + I * (VIEWPORT_H-20), ALIGN_RIGHT, get_font("standard"), RGB(1, 1, 1), 30, 180, 35, 35);
|
2017-04-07 14:20:45 +02:00
|
|
|
}
|
2017-12-17 01:13:10 +01:00
|
|
|
|
|
|
|
free(old_title);
|
2017-04-07 14:20:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
void stage_set_voltage_thresholds(uint easy, uint normal, uint hard, uint lunatic) {
|
|
|
|
switch(global.diff) {
|
|
|
|
case D_Easy: global.voltage_threshold = easy; return;
|
|
|
|
case D_Normal: global.voltage_threshold = normal; return;
|
|
|
|
case D_Hard: global.voltage_threshold = hard; return;
|
|
|
|
case D_Lunatic: global.voltage_threshold = lunatic; return;
|
|
|
|
default: UNREACHABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stage_is_cleared(void) {
|
|
|
|
return global.gameover == GAMEOVER_SCORESCREEN || global.gameover == GAMEOVER_TRANSITIONING;
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:07:04 +02:00
|
|
|
typedef struct StageFrameState {
|
|
|
|
StageInfo *stage;
|
|
|
|
int transition_delay;
|
|
|
|
uint16_t last_replay_fps;
|
|
|
|
} StageFrameState;
|
|
|
|
|
2017-12-26 09:56:21 +01:00
|
|
|
static void stage_update_fps(StageFrameState *fstate) {
|
|
|
|
if(global.replaymode == REPLAY_RECORD) {
|
2017-12-26 12:07:40 +01:00
|
|
|
uint16_t replay_fps = (uint16_t)rint(global.fps.logic.fps);
|
2017-12-26 09:56:21 +01:00
|
|
|
|
|
|
|
if(replay_fps != fstate->last_replay_fps) {
|
|
|
|
replay_stage_event(global.replay_stage, global.frames, EV_FPS, replay_fps);
|
|
|
|
fstate->last_replay_fps = replay_fps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
static void stage_give_clear_bonus(const StageInfo *stage, StageClearBonus *bonus) {
|
|
|
|
memset(bonus, 0, sizeof(*bonus));
|
|
|
|
|
|
|
|
// FIXME: this is clunky...
|
|
|
|
if(!global.is_practice_mode && stage->type == STAGE_STORY) {
|
|
|
|
StageInfo *next = stage_get(stage->id + 1);
|
|
|
|
|
|
|
|
if(next == NULL || next->type != STAGE_STORY) {
|
|
|
|
bonus->all_clear = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stage->type == STAGE_STORY) {
|
|
|
|
bonus->base = stage->id * 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bonus->all_clear) {
|
|
|
|
bonus->base += global.plr.point_item_value * 100;
|
|
|
|
bonus->graze = global.plr.graze * (global.plr.point_item_value / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
bonus->voltage = imax(0, (int)global.plr.voltage - (int)global.voltage_threshold) * (global.plr.point_item_value / 100);
|
|
|
|
bonus->lives = global.plr.lives * global.plr.point_item_value * 5;
|
|
|
|
|
|
|
|
// TODO: maybe a difficulty multiplier?
|
|
|
|
|
|
|
|
bonus->total = bonus->base + bonus->voltage + bonus->lives;
|
|
|
|
player_add_points(&global.plr, bonus->total);
|
|
|
|
}
|
|
|
|
|
2017-12-26 12:07:40 +01:00
|
|
|
static FrameAction stage_logic_frame(void *arg) {
|
2017-10-04 07:07:04 +02:00
|
|
|
StageFrameState *fstate = arg;
|
|
|
|
StageInfo *stage = fstate->stage;
|
|
|
|
|
2017-12-26 09:56:21 +01:00
|
|
|
stage_update_fps(fstate);
|
2018-10-02 02:03:51 +02:00
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
if(global.shake_view > 30) {
|
|
|
|
global.shake_view = 30;
|
|
|
|
}
|
|
|
|
|
2018-10-02 02:03:51 +02:00
|
|
|
if(global.shake_view_fade) {
|
|
|
|
global.shake_view -= global.shake_view_fade;
|
|
|
|
|
|
|
|
if(global.shake_view <= 0) {
|
|
|
|
global.shake_view = global.shake_view_fade = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:07:04 +02:00
|
|
|
((global.replaymode == REPLAY_PLAY) ? replay_input : stage_input)();
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover != GAMEOVER_TRANSITIONING) {
|
2017-10-04 07:07:04 +02:00
|
|
|
if((!global.boss || boss_is_fleeing(global.boss)) && !global.dialog) {
|
|
|
|
stage->procs->event();
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover == GAMEOVER_SCORESCREEN && global.frames - global.gameover_time == GAMEOVER_SCORE_DELAY) {
|
|
|
|
StageClearBonus b;
|
|
|
|
stage_give_clear_bonus(stage, &b);
|
|
|
|
stage_display_clear_screen(&b);
|
|
|
|
}
|
|
|
|
|
2018-07-24 20:30:45 +02:00
|
|
|
if(stage->type == STAGE_SPELL && !global.boss && !fstate->transition_delay) {
|
|
|
|
fstate->transition_delay = 120;
|
2017-10-04 07:07:04 +02:00
|
|
|
}
|
2017-11-15 04:45:41 +01:00
|
|
|
|
|
|
|
stage->procs->update();
|
2017-10-04 07:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
replay_stage_check_desync(global.replay_stage, global.frames, (tsrand() ^ global.plr.points) & 0xFFFF, global.replaymode);
|
|
|
|
stage_logic();
|
|
|
|
|
2017-12-28 11:46:08 +01:00
|
|
|
if(fstate->transition_delay) {
|
2018-07-24 20:30:45 +02:00
|
|
|
if(!--fstate->transition_delay) {
|
|
|
|
stage_finish(GAMEOVER_WIN);
|
|
|
|
}
|
2017-12-28 11:46:08 +01:00
|
|
|
} else {
|
|
|
|
update_transition();
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:07:04 +02:00
|
|
|
if(global.replaymode == REPLAY_RECORD && global.plr.points > progress.hiscore) {
|
|
|
|
progress.hiscore = global.plr.points;
|
|
|
|
}
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover > 0) {
|
2017-12-26 12:07:40 +01:00
|
|
|
return LFRAME_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(global.frameskip || (global.replaymode == REPLAY_PLAY && gamekeypressed(KEY_SKIP))) {
|
|
|
|
return LFRAME_SKIP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LFRAME_WAIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FrameAction stage_render_frame(void *arg) {
|
|
|
|
StageFrameState *fstate = arg;
|
|
|
|
StageInfo *stage = fstate->stage;
|
|
|
|
|
2017-10-04 07:07:04 +02:00
|
|
|
tsrand_lock(&global.rand_game);
|
|
|
|
tsrand_switch(&global.rand_visual);
|
2017-11-15 07:05:47 +01:00
|
|
|
BEGIN_DRAW_CODE();
|
2017-10-04 07:07:04 +02:00
|
|
|
stage_draw_scene(stage);
|
2017-11-15 07:05:47 +01:00
|
|
|
END_DRAW_CODE();
|
2017-10-04 07:07:04 +02:00
|
|
|
tsrand_unlock(&global.rand_game);
|
|
|
|
tsrand_switch(&global.rand_game);
|
|
|
|
draw_transition();
|
|
|
|
|
2017-12-26 12:07:40 +01:00
|
|
|
return RFRAME_SWAP;
|
2017-10-04 07:07:04 +02:00
|
|
|
}
|
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
void stage_loop(StageInfo *stage) {
|
|
|
|
assert(stage);
|
|
|
|
assert(stage->procs);
|
2017-03-11 03:12:51 +01:00
|
|
|
assert(stage->procs->preload);
|
2017-02-26 13:17:48 +01:00
|
|
|
assert(stage->procs->begin);
|
|
|
|
assert(stage->procs->end);
|
|
|
|
assert(stage->procs->draw);
|
|
|
|
assert(stage->procs->event);
|
2017-11-15 04:45:41 +01:00
|
|
|
assert(stage->procs->update);
|
2017-02-26 13:17:48 +01:00
|
|
|
assert(stage->procs->shader_rules);
|
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover == GAMEOVER_WIN) {
|
|
|
|
global.gameover = 0;
|
|
|
|
} else if(global.gameover) {
|
2012-01-06 21:52:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-02-10 11:39:42 +01:00
|
|
|
// I really want to separate all of the game state from the global struct sometime
|
2017-02-26 13:17:48 +01:00
|
|
|
global.stage = stage;
|
2017-02-10 11:39:42 +01:00
|
|
|
|
2019-01-04 23:59:39 +01:00
|
|
|
ent_init();
|
2017-12-13 20:05:12 +01:00
|
|
|
stage_objpools_alloc();
|
2017-03-11 04:41:57 +01:00
|
|
|
stage_preload();
|
2018-07-04 10:36:16 +02:00
|
|
|
stage_draw_init();
|
2017-03-11 03:12:51 +01:00
|
|
|
|
2012-08-07 02:45:38 +02:00
|
|
|
tsrand_switch(&global.rand_game);
|
2017-02-26 13:17:48 +01:00
|
|
|
stage_start(stage);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-07-14 19:46:03 +02:00
|
|
|
if(global.replaymode == REPLAY_RECORD) {
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t start_time = (uint64_t)time(0);
|
|
|
|
uint64_t seed = makeseed();
|
|
|
|
tsrand_seed_p(&global.rand_game, seed);
|
|
|
|
|
|
|
|
global.replay_stage = replay_create_stage(&global.replay, stage, start_time, seed, global.diff, &global.plr);
|
2017-12-18 17:34:10 +01:00
|
|
|
|
|
|
|
// make sure our player state is consistent with what goes into the replay
|
|
|
|
player_init(&global.plr);
|
|
|
|
replay_stage_sync_player_state(global.replay_stage, &global.plr);
|
2017-02-10 00:24:19 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
log_debug("Start time: %"PRIu64, start_time);
|
|
|
|
log_debug("Random seed: 0x%"PRIx64, seed);
|
2017-04-06 02:58:57 +02:00
|
|
|
|
|
|
|
StageProgress *p = stage_get_progress_from_info(stage, global.diff, true);
|
|
|
|
|
|
|
|
if(p) {
|
|
|
|
log_debug("You played this stage %u times", p->num_played);
|
|
|
|
log_debug("You cleared this stage %u times", p->num_cleared);
|
|
|
|
|
|
|
|
++p->num_played;
|
2018-10-23 05:33:16 +02:00
|
|
|
p->unlocked = true;
|
2017-04-06 02:58:57 +02:00
|
|
|
}
|
2012-07-14 19:46:03 +02:00
|
|
|
} else {
|
2017-02-10 00:24:19 +01:00
|
|
|
ReplayStage *stg = global.replay_stage;
|
2019-02-02 12:25:06 +01:00
|
|
|
|
|
|
|
assert(stg != NULL);
|
|
|
|
assert(stage_get(stg->stage) == stage);
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
tsrand_seed_p(&global.rand_game, stg->rng_seed);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
log_debug("REPLAY_PLAY mode: %d events, stage: \"%s\"", stg->numevents, stage->title);
|
|
|
|
log_debug("Start time: %"PRIu64, stg->start_time);
|
|
|
|
log_debug("Random seed: 0x%"PRIx64, stg->rng_seed);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-02-15 18:34:47 +01:00
|
|
|
global.diff = stg->diff;
|
2017-10-08 13:30:51 +02:00
|
|
|
player_init(&global.plr);
|
2017-02-15 18:34:47 +01:00
|
|
|
replay_stage_sync_player_state(stg, &global.plr);
|
2012-08-07 05:28:41 +02:00
|
|
|
stg->playpos = 0;
|
2012-07-14 19:46:03 +02:00
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
stage->procs->begin();
|
2018-10-23 05:49:19 +02:00
|
|
|
player_stage_post_init(&global.plr);
|
2018-01-10 20:43:29 +01:00
|
|
|
|
|
|
|
if(global.stage->type != STAGE_SPELL) {
|
|
|
|
display_stage_title(stage);
|
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-10-04 07:07:04 +02:00
|
|
|
StageFrameState fstate = { .stage = stage };
|
2017-12-26 12:07:40 +01:00
|
|
|
loop_at_fps(stage_logic_frame, stage_render_frame, &fstate, FPS);
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2012-07-16 17:47:06 +02:00
|
|
|
if(global.replaymode == REPLAY_RECORD) {
|
2017-02-10 00:24:19 +01:00
|
|
|
replay_stage_event(global.replay_stage, global.frames, EV_OVER, 0);
|
2017-10-29 23:45:24 +01:00
|
|
|
|
2019-02-22 00:56:03 +01:00
|
|
|
if(global.gameover == GAMEOVER_WIN) {
|
2017-10-29 23:45:24 +01:00
|
|
|
global.replay_stage->flags |= REPLAY_SFLAG_CLEAR;
|
|
|
|
}
|
2012-07-16 17:47:06 +02:00
|
|
|
}
|
2017-02-10 23:05:22 +01:00
|
|
|
|
2017-02-26 13:17:48 +01:00
|
|
|
stage->procs->end();
|
2018-07-04 10:36:16 +02:00
|
|
|
stage_draw_shutdown();
|
2017-02-26 13:17:48 +01:00
|
|
|
stage_free();
|
2017-11-25 16:51:43 +01:00
|
|
|
player_free(&global.plr);
|
2012-07-27 19:11:45 +02:00
|
|
|
tsrand_switch(&global.rand_visual);
|
2017-02-28 15:38:02 +01:00
|
|
|
free_all_refs();
|
2018-04-13 21:13:48 +02:00
|
|
|
ent_shutdown();
|
2017-12-13 20:05:12 +01:00
|
|
|
stage_objpools_free();
|
2017-10-08 07:15:43 +02:00
|
|
|
stop_sounds();
|
2018-05-19 04:01:16 +02:00
|
|
|
|
|
|
|
if(taisei_quit_requested()) {
|
2019-02-22 00:56:03 +01:00
|
|
|
global.gameover = GAMEOVER_ABORT;
|
2018-05-19 04:01:16 +02:00
|
|
|
}
|
2011-06-25 12:41:40 +02:00
|
|
|
}
|