Make map generation a bit more flexible

This commit is contained in:
UnknownShadow200 2023-11-19 08:57:16 +11:00
parent b437554fa7
commit 3adc437dbe
6 changed files with 69 additions and 38 deletions

View File

@ -1068,7 +1068,7 @@ void PhysicsComp_DoEntityPush(struct Entity* entity) {
/*########################################################################################################################*
*----------------------------------------------------SoundsComponent------------------------------------------------------*
*#########################################################################################################################*/
static Vec3 sounds_lastPos = { -1e25f, -1e25f, -1e25f };
static Vec3 sounds_lastPos = { -87.1234f, -99.5678f, -100.91237f };
static cc_bool sounds_anyNonAir;
static cc_uint8 sounds_type;

View File

@ -6,18 +6,38 @@
#include "World.h"
#include "Utils.h"
#include "Game.h"
#include "Window.h"
volatile float Gen_CurrentProgress;
volatile const char* Gen_CurrentState;
volatile cc_bool Gen_Done;
int Gen_Seed;
cc_bool Gen_Vanilla;
BlockRaw* Gen_Blocks;
BlockRaw* Gen_Blocks;
const struct MapGenerator* Gen_Active;
static void Gen_Init(void) {
static void Gen_Reset(void) {
Gen_CurrentProgress = 0.0f;
Gen_CurrentState = "";
Gen_Done = false;
Gen_Done = false;
}
static void Gen_DoGen(void) {
Gen_Active->Generate();
}
void Gen_Start(void) {
void* thread;
Gen_Reset();
Gen_Blocks = (BlockRaw*)Mem_TryAlloc(World.Volume, 1);
if (!Gen_Blocks || !Gen_Active->Prepare()) {
Window_ShowDialog("Out of memory", "Not enough free memory to generate a map that large.\nTry a smaller size.");
Gen_Done = true;
} else {
thread = Thread_Create(Gen_DoGen);
Thread_Start2(thread, Gen_DoGen);
Thread_Detach(thread);
}
}
@ -39,9 +59,11 @@ static void FlatgrassGen_MapSet(int yBeg, int yEnd, BlockRaw block) {
}
}
void FlatgrassGen_Generate(void) {
Gen_Init();
static cc_bool FlatgrassGen_Prepare(void) {
return true;
}
static void FlatgrassGen_Generate(void) {
Gen_CurrentState = "Setting air blocks";
FlatgrassGen_MapSet(World.Height / 2, World.MaxY, BLOCK_AIR);
@ -54,6 +76,11 @@ void FlatgrassGen_Generate(void) {
Gen_Done = true;
}
const struct MapGenerator FlatgrassGen = {
FlatgrassGen_Prepare,
FlatgrassGen_Generate
};
/*########################################################################################################################*
*---------------------------------------------------Noise generation------------------------------------------------------*
@ -610,10 +637,12 @@ static void NotchyGen_PlantTrees(void) {
}
}
void NotchyGen_Generate(void) {
Gen_Init();
Heightmap = (cc_int16*)Mem_Alloc(World.Width * World.Length, 2, "gen heightmap");
static cc_bool NotchyGen_Prepare(void) {
Heightmap = (cc_int16*)Mem_TryAlloc(World.Width * World.Length, 2);
return Heightmap != NULL;
}
static void NotchyGen_Generate(void) {
Random_Seed(&rnd, Gen_Seed);
waterLevel = World.Height / 2;
minHeight = World.Height;
@ -639,6 +668,11 @@ void NotchyGen_Generate(void) {
Gen_Done = true;
}
const struct MapGenerator NotchyGen = {
NotchyGen_Prepare,
NotchyGen_Generate
};
/*########################################################################################################################*
*----------------------------------------------------Tree generation------------------------------------------------------*

View File

@ -15,11 +15,20 @@ extern volatile const char* Gen_CurrentState;
/* Whether map generation has completed */
extern volatile cc_bool Gen_Done;
extern int Gen_Seed;
extern cc_bool Gen_Vanilla;
extern BlockRaw* Gen_Blocks;
/* Starts generating a map using the Gen_Active generator */
void Gen_Start(void);
struct MapGenerator {
const cc_bool (*Prepare)(void);
const void (*Generate)(void);
};
extern const struct MapGenerator* Gen_Active;
extern const struct MapGenerator FlatgrassGen;
extern const struct MapGenerator NotchyGen;
void FlatgrassGen_Generate(void);
void NotchyGen_Generate(void);
extern BlockRaw* Tree_Blocks;
extern RNGState* Tree_Rnd;

View File

@ -187,6 +187,8 @@ static void Menu_Remove(void* screen, int i) {
static void Menu_BeginGen(int width, int height, int length) {
World_NewMap();
World_SetDimensions(width, height, length);
Gen_Start();
GeneratingScreen_Show();
}
@ -1098,7 +1100,7 @@ CC_NOINLINE static int GenLevelScreen_GetSeedInt(struct GenLevelScreen* s, int i
return GenLevelScreen_GetInt(s, index);
}
static void GenLevelScreen_Gen(void* screen, cc_bool vanilla) {
static void GenLevelScreen_Gen(void* screen, const struct MapGenerator* gen) {
struct GenLevelScreen* s = (struct GenLevelScreen*)screen;
int width = GenLevelScreen_GetInt(s, 0);
int height = GenLevelScreen_GetInt(s, 1);
@ -1111,14 +1113,15 @@ static void GenLevelScreen_Gen(void* screen, cc_bool vanilla) {
} else if (!width || !height || !length) {
Chat_AddRaw("&cOne of the map dimensions is invalid.");
} else {
Gen_Vanilla = vanilla; Gen_Seed = seed;
Gen_Active = gen;
Gen_Seed = seed;
Gui_Remove((struct Screen*)s);
Menu_BeginGen(width, height, length);
}
}
static void GenLevelScreen_Flatgrass(void* a, void* b) { GenLevelScreen_Gen(a, false); }
static void GenLevelScreen_Notchy(void* a, void* b) { GenLevelScreen_Gen(a, true); }
static void GenLevelScreen_Flatgrass(void* a, void* b) { GenLevelScreen_Gen(a, &FlatgrassGen); }
static void GenLevelScreen_Notchy(void* a, void* b) { GenLevelScreen_Gen(a, &NotchyGen); }
static void GenLevelScreen_Make(struct GenLevelScreen* s, int i, int def) {
cc_string tmp; char tmpBuffer[STRING_SIZE];
@ -1272,8 +1275,8 @@ static struct Widget* classicgen_widgets[] = {
static void ClassicGenScreen_Gen(int size) {
RNGState rnd; Random_SeedFromCurrentTime(&rnd);
Gen_Vanilla = true;
Gen_Seed = Random_Next(&rnd, Int32_MaxValue);
Gen_Active = &NotchyGen;
Gen_Seed = Random_Next(&rnd, Int32_MaxValue);
Gui_Remove((struct Screen*)&ClassicGenScreen);
Menu_BeginGen(size, 64, size);

View File

@ -1903,23 +1903,7 @@ static void GeneratingScreen_AtlasChanged(void* obj) {
}
static void GeneratingScreen_Init(void* screen) {
void* thread;
Gen_Done = false;
LoadingScreen_Init(screen);
Gen_Blocks = (BlockRaw*)Mem_TryAlloc(World.Volume, 1);
if (!Gen_Blocks) {
Window_ShowDialog("Out of memory", "Not enough free memory to generate a map that large.\nTry a smaller size.");
Gen_Done = true;
} else if (Gen_Vanilla) {
thread = Thread_Create(NotchyGen_Generate);
Thread_Start2(thread, NotchyGen_Generate);
Thread_Detach(thread);
} else {
thread = Thread_Create(FlatgrassGen_Generate);
Thread_Start2(thread, FlatgrassGen_Generate);
Thread_Detach(thread);
}
Event_Register_(&TextureEvents.AtlasChanged, NULL, GeneratingScreen_AtlasChanged);
}
static void GeneratingScreen_Free(void* screen) {
@ -1928,7 +1912,6 @@ static void GeneratingScreen_Free(void* screen) {
}
static void GeneratingScreen_EndGeneration(void) {
Gen_Done = false;
World_SetNewMap(Gen_Blocks, World.Width, World.Height, World.Length);
if (!Gen_Blocks) { Chat_AddRaw("&cFailed to generate the map."); return; }

View File

@ -133,8 +133,10 @@ static void SPConnection_BeginConnect(void) {
World_SetDimensions(128, 64, 128);
#endif
Gen_Vanilla = true;
Gen_Seed = Random_Next(&rnd, Int32_MaxValue);
Gen_Active = &NotchyGen;
Gen_Seed = Random_Next(&rnd, Int32_MaxValue);
Gen_Start();
GeneratingScreen_Show();
}