From 265a3225357284e6ceb68f623dec1b81eebd5125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schweiger?= Date: Mon, 5 Feb 2018 19:06:48 +0100 Subject: [PATCH] Allow to exit Game when a connection drops --- source/Ingame.c | 86 ++++++++++++++++++++++++++++++++++++++++++- source/Ingame.h | 2 + source/IngameMenu.c | 10 +---- source/Menu.c | 4 +- source/Synchronizer.c | 4 ++ source/Synchronizer.h | 1 + 6 files changed, 96 insertions(+), 11 deletions(-) diff --git a/source/Ingame.c b/source/Ingame.c index 1263f47..6430be8 100644 --- a/source/Ingame.c +++ b/source/Ingame.c @@ -10,6 +10,11 @@ #include "SaveLoad.h" #include "Network.h" +#define STALL_TIME 120 + +int stallCounter; +bool stallAreYouSure; + //generates stairs up and creates compass data void generatePass2() { int level, x, y; @@ -120,6 +125,8 @@ void startGame(bool load, char *filename) { for(int i=0; i most likely a player disconnected -> present option to exit game + if(stallCounter>=STALL_TIME) { + if(stallCounter==STALL_TIME) stallAreYouSure = false; + + //scan local inputs, because synchronizer only updates them when not stalled + hidScanInput(); + tickKeys(&localInputs, hidKeysHeld(), hidKeysDown()); + + if (localInputs.k_accept.clicked) { + if(stallAreYouSure) { + //create backup save + if(playerLocalID==0) { + char backupSaveFileName[256+32]; + backupSaveFileName[0] = '\0'; + + strncat(backupSaveFileName, currentFileName, strlen(currentFileName)-4); + strcat(backupSaveFileName, ".exit.msv"); + + saveWorld(backupSaveFileName, &eManager, &worldData, players, playerCount); + } + + exitGame(); + } else { + stallAreYouSure = true; + } + } + if (localInputs.k_decline.clicked) { + stallAreYouSure = false; + } + } + } } //for rendering -> move to a better place @@ -278,6 +322,36 @@ void renderGame() { ingameMenuRender(getLocalPlayer(), getLocalPlayer()->ingameMenu); } + //game stalled -> most likely a player disconnected -> present option to exit game + if(stallCounter>STALL_TIME) { + renderFrame(1,1,24,14,0xFF1010AF); + drawText("Waiting for a long time", (400 - (23 * 12))/2, 32); + + char text[50]; + sprintf(text, "Last response %is ago", stallCounter/60); + drawText(text, (400 - (strlen(text) * 12))/2, 64); + + if(playerLocalID==0) { + drawText("Press to leave the game", (400 - (25 * 12))/2, 160); + renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 120, 157, 1); + + drawText("A backup save will be created", (400 - (29 * 12))/2, 192); + } else { + drawText("Press to leave the game", (400 - (25 * 12))/2, 192); + renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 120, 189, 1); + } + + if(stallAreYouSure){ + renderFrame(6,5,19,10,0xFF10108F); + + drawText("Are you sure?",122,96); + drawText(" Yes", 164, 117); + renderButtonIcon(localInputs.k_accept.input & -localInputs.k_accept.input, 166, 114, 1); + drawText(" No", 170, 133); + renderButtonIcon(localInputs.k_decline.input & -localInputs.k_decline.input, 166, 130, 1); + } + } + sf2d_end_frame(); sf2d_start_frame(GFX_BOTTOM, GFX_LEFT); @@ -290,3 +364,13 @@ void renderGame() { sf2d_end_frame(); } +void exitGame() { + networkDisconnect(); + synchronizerReset(); + + sf2d_set_clear_color(0xFF); + currentSelection = 0; + currentMenu = MENU_TITLE; + + playMusic(&music_menu); +} diff --git a/source/Ingame.h b/source/Ingame.h index 538fa04..f930d2b 100644 --- a/source/Ingame.h +++ b/source/Ingame.h @@ -5,3 +5,5 @@ void startGame(bool load, char *filename); void tickGame(); void renderGame(); + +void exitGame(); diff --git a/source/IngameMenu.c b/source/IngameMenu.c index d8159f6..ff5737f 100644 --- a/source/IngameMenu.c +++ b/source/IngameMenu.c @@ -2,6 +2,7 @@ #include "Globals.h" #include "Menu.h" +#include "Ingame.h" #include "Player.h" #include "SaveLoad.h" #include "Synchronizer.h" @@ -47,14 +48,7 @@ void ingameMenuTick(PlayerData *pd, int menu) { pd->ingameMenuAreYouSure = false; pd->ingameMenuAreYouSureSave = false; - networkDisconnect(); - synchronizerReset(); - - sf2d_set_clear_color(0xFF); - currentSelection = 0; - currentMenu = MENU_TITLE; - - playMusic(&music_menu); + exitGame(); } else if (pd->inputs.k_decline.clicked){ pd->ingameMenuAreYouSure = false; pd->ingameMenuAreYouSureSave = false; diff --git a/source/Menu.c b/source/Menu.c index 3a26899..1763edd 100644 --- a/source/Menu.c +++ b/source/Menu.c @@ -1166,7 +1166,7 @@ void renderMenu(int menu,int xscr,int yscr){ memset(text, 0, (50+8+1) * sizeof(char)); networkGetNodeName(j, text); - drawText(text,(400-(strlen(text)*12))/2,i*32+32); + drawText(text,(400-(strlen(text)*12))/2,i*26+32); free(text); lastj = j; @@ -1234,7 +1234,7 @@ void renderMenu(int menu,int xscr,int yscr){ memset(text, 0, (50+8+1) * sizeof(char)); networkGetNodeName(j, text); - drawText(text,(400-(strlen(text)*12))/2,i*32+32); + drawText(text,(400-(strlen(text)*12))/2,i*26+32); free(text); lastj = j; diff --git a/source/Synchronizer.c b/source/Synchronizer.c index 4896c97..74d3fbb 100644 --- a/source/Synchronizer.c +++ b/source/Synchronizer.c @@ -217,6 +217,10 @@ void synchronizerReset() { synchronizerCurrentTicks = 0; } +bool synchronizerIsRunning() { + return synchronizerRunning; +} + // helpers for random numbers #define PI 3.141592654 double gaussrand(bool reset) diff --git a/source/Synchronizer.h b/source/Synchronizer.h index 1815cf9..bafc544 100644 --- a/source/Synchronizer.h +++ b/source/Synchronizer.h @@ -12,6 +12,7 @@ void synchronizerSetPlayerUID(int playerID, u32 uid); void synchronizerSendIfReady(); void synchronizerSetPlayerReady(int playerID); bool synchronizerAllReady(); +bool synchronizerIsRunning(); void synchronizerStart();