diff --git a/src/p_user.c b/src/p_user.c index 4aa2e58..4f28e61 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -588,3 +588,133 @@ void P_PlayerThink (player_t* player) player->powers[pw_invulnerability] & 8) ? INVERSECOLORMAP : player->powers[pw_infrared] > 4*32 || player->powers[pw_infrared] & 8; } + +#ifdef __EMSCRIPTEN__ +// +// Dwasm-Verse8: rewarded-ad revive support. +// JS_PlayerIsDead lets the page poll whether the revive offer still applies. +// JS_RevivePlayer resurrects the console player in place after a rewarded ad. +// + +#include +#include "m_menu.h" + +extern int initial_health; // from p_inter.h / g_game.c +extern int maxhealth; + +EMSCRIPTEN_KEEPALIVE int JS_PlayerIsDead(void) +{ + return playeringame[consoleplayer] && + players[consoleplayer].playerstate == PST_DEAD; +} + +EMSCRIPTEN_KEEPALIVE int JS_RevivePlayer(void) +{ + player_t* player = &players[consoleplayer]; + mobj_t* mo = player->mo; + + if (!playeringame[consoleplayer] || !mo || + player->playerstate != PST_DEAD || mo->type != MT_PLAYER) + return 0; + + player->playerstate = PST_LIVE; + player->health = initial_health; + mo->health = initial_health; + mo->flags |= MF_SOLID | MF_SHOOTABLE; + mo->flags &= ~MF_CORPSE; + mo->height = mo->info->height; + mo->radius = mo->info->radius; + mo->pitch = 0; + P_SetMobjState(mo, S_PLAY); + player->viewheight = VIEWHEIGHT; + player->deltaviewheight = 0; + player->damagecount = 0; + player->bonuscount = 0; + player->attacker = NULL; + player->extralight = 0; + player->fixedcolormap = 0; + player->refire = 0; + player->powers[pw_invulnerability] = 3*TICRATE; // 3s of mercy after ad revive + P_SetupPsprites(player); + return 1; +} +// Rewarded-ad full heal (cooldown handled by the page). +EMSCRIPTEN_KEEPALIVE int JS_GetHealth(void) +{ + if (!playeringame[consoleplayer] || !players[consoleplayer].mo) + return -1; + return players[consoleplayer].health; +} + +EMSCRIPTEN_KEEPALIVE int JS_FullHeal(void) +{ + player_t* player = &players[consoleplayer]; + + if (!playeringame[consoleplayer] || !player->mo || + player->playerstate != PST_LIVE) + return 0; + if (player->health >= maxhealth) + return 0; + + player->health = maxhealth; + player->mo->health = maxhealth; + player->bonuscount += 6; // brief pickup flash as feedback + return 1; +} +// 1 if any owned ammo type is below max (page disables the button otherwise). +EMSCRIPTEN_KEEPALIVE int JS_NeedsAmmo(void) +{ + player_t* player = &players[consoleplayer]; + int i; + + if (!playeringame[consoleplayer] || !player->mo || + player->playerstate != PST_LIVE) + return 0; + for (i = 0; i < NUMAMMO; i++) + if (player->ammo[i] < player->maxammo[i]) + return 1; + return 0; +} + +// Rewarded-ad full ammo refill (cooldown handled by the page). +EMSCRIPTEN_KEEPALIVE int JS_FullAmmo(void) +{ + player_t* player = &players[consoleplayer]; + int i, gave = 0; + + if (!playeringame[consoleplayer] || !player->mo || + player->playerstate != PST_LIVE) + return 0; + + for (i = 0; i < NUMAMMO; i++) + if (player->ammo[i] < player->maxammo[i]) + { + player->ammo[i] = player->maxammo[i]; + gave = 1; + } + + if (gave) + player->bonuscount += 6; + return gave; +} + +// JS_SetPaused: pause or unpause the game from JS (e.g. during an ad break). +// Only takes effect during actual gameplay (GS_LEVEL). +EMSCRIPTEN_KEEPALIVE void JS_SetPaused(int p) +{ + if (gamestate == GS_LEVEL) + paused = p ? 1 : 0; +} + +// JS_OpenMenu: called from JS to open the main menu programmatically. +// Works from the title screen, in-game, and intermission. +EMSCRIPTEN_KEEPALIVE int JS_OpenMenu(void) +{ + if (!menuactive) + { + M_StartControlPanel(); + return 1; + } + return 0; +} +#endif // __EMSCRIPTEN__