From 271b5faa4a3e72eb4b1e1b30b46670374289003b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 31 May 2022 20:46:14 +1000 Subject: [PATCH] Save level name now defaults to name of last map loaded in singleplayer or saved (Thanks Neonium) Whenever a new level is generated or loaded in multiplayer, this is reset back to empty string --- src/Menus.c | 9 +++++++-- src/Protocol.c | 7 ++++--- src/World.c | 5 ++++- src/World.h | 2 ++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Menus.c b/src/Menus.c index ba83877d7..8d9fb3e8f 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1401,6 +1401,7 @@ static void SaveLevelScreen_Save(void* screen, void* widget, const char* fmt) { } String_InitArray(path, pathBuffer); String_Format1(&path, fmt, &file); + String_Copy(&World.Name, &file); if (File_Exists(&path) && !btn->optName) { btn->optName = ""; @@ -1521,7 +1522,7 @@ static void SaveLevelScreen_Init(void* screen) { #endif ButtonWidget_Init(&s->cancel, 400, Menu_SwitchPause); - TextInputWidget_Create(&s->input, 500, &String_Empty, &desc); + TextInputWidget_Create(&s->input, 500, &World.Name, &desc); TextWidget_Init(&s->desc); s->input.onscreenPlaceholder = "Map name"; } @@ -1743,13 +1744,17 @@ void HotkeyListScreen_Show(void) { static void LoadLevelScreen_EntryClick(void* screen, void* widget) { cc_string path; char pathBuffer[FILENAME_SIZE]; struct ListScreen* s = (struct ListScreen*)screen; + cc_string relPath, fileName, fileExt; cc_result res; - cc_string relPath = ListScreen_UNSAFE_GetCur(s, widget); + relPath = ListScreen_UNSAFE_GetCur(s, widget); String_InitArray(path, pathBuffer); String_Format1(&path, "maps/%s", &relPath); res = Map_LoadFrom(&path); + String_UNSAFE_Separate(&relPath, '.', &fileName, &fileExt); + String_Copy(&World.Name, &fileName); + /* FileNotFound error may be because user deleted maps from disc */ if (res != ReturnCode_FileNotFound) return; Chat_AddRaw("&eReloading level list as it may be out of date"); diff --git a/src/Protocol.c b/src/Protocol.c index 7fd2a9b11..205b7ebbb 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -359,9 +359,10 @@ static cc_result MapState_Read(struct MapState* m) { m->sizeIndex += read; if (res) return res; + + /* 0.01% chance to happen, but still possible */ + if (m->sizeIndex < MAP_SIZE_LEN) return 0; } - /* 0.01% chance to happen, but still possible */ - if (m->sizeIndex < MAP_SIZE_LEN) return 0; if (!map_volume) map_volume = Stream_GetU32_BE(m->size); @@ -540,7 +541,7 @@ static void Classic_LevelDataChunk(cc_uint8* data) { if (res) { DisconnectInvalidMap(res); return; } } - progress = !map1.blocks ? 0.0f : (float)map1.index / map_volume; + progress = !map_volume ? 0.0f : (float)map1.index / map_volume; Event_RaiseFloat(&WorldEvents.Loading, progress); } diff --git a/src/World.c b/src/World.c index e9df101d1..cead7de9c 100644 --- a/src/World.c +++ b/src/World.c @@ -12,6 +12,7 @@ #include "Window.h" struct _WorldData World; +static char nameBuffer[STRING_SIZE]; /*########################################################################################################################* *----------------------------------------------------------World----------------------------------------------------------* *#########################################################################################################################*/ @@ -44,6 +45,7 @@ void World_Reset(void) { #endif Mem_Free(World.Blocks); World.Blocks = NULL; + String_InitArray(World.Name, nameBuffer); World_SetDimensions(0, 0, 0); World.Loaded = false; @@ -61,7 +63,8 @@ void World_SetNewMap(BlockRaw* blocks, int width, int height, int length) { if (!blocks) { width = 0; height = 0; length = 0; } World_SetDimensions(width, height, length); - World.Blocks = blocks; + World.Blocks = blocks; + World.Name.length = 0; if (!World.Volume) World.Blocks = NULL; #ifdef EXTENDED_BLOCKS diff --git a/src/World.h b/src/World.h index 46ec63bd7..76e05b8b3 100644 --- a/src/World.h +++ b/src/World.h @@ -46,6 +46,8 @@ CC_VAR extern struct _WorldData { cc_bool Loaded; /* Point in time the current world was last saved at */ double LastSave; + /* Default name of the world when saving */ + cc_string Name; } World; /* Frees the blocks array, sets dimensions to 0, resets environment to default. */