Declare notchygen as C exported funcs

This commit is contained in:
UnknownShadow200 2017-04-11 19:42:33 +10:00
parent e4bbe8876f
commit d6e80fcf37
5 changed files with 34 additions and 31 deletions

View File

@ -1,7 +1,7 @@
#ifndef CS_BLOCK_H #ifndef CS_BLOCK_H
#define CS_BLOCK_H #define CS_BLOCK_H
// Classic blocks /* Classic blocks */
#define Block_Air 0 #define Block_Air 0
#define Block_Stone 1 #define Block_Stone 1
#define Block_Grass 2 #define Block_Grass 2
@ -53,7 +53,7 @@
#define Block_MossyRocks 48 #define Block_MossyRocks 48
#define Block_Obsidian 49 #define Block_Obsidian 49
// CPE blocks /* CPE blocks */
#define Block_CobblestoneSlab 50 #define Block_CobblestoneSlab 50
#define Block_Rope 51 #define Block_Rope 51
#define Block_Sandstone 52 #define Block_Sandstone 52

View File

@ -1,7 +1,7 @@
#include "Noise.h" #include "Noise.h"
void ImprovedNoise_Init(UInt8* p, Random* rnd) { void ImprovedNoise_Init(UInt8* p, Random* rnd) {
// shuffle randomly using fisher-yates /* shuffle randomly using fisher-yates */
for (Int32 i = 0; i < 256; i++) { for (Int32 i = 0; i < 256; i++) {
p[i] = (UInt8)i; p[i] = (UInt8)i;
} }
@ -22,26 +22,26 @@ Real64 ImprovedNoise_Compute(UInt8* p, Real64 x, Real64 y) {
Int32 X = xFloor & 0xFF, Y = yFloor & 0xFF; Int32 X = xFloor & 0xFF, Y = yFloor & 0xFF;
x -= xFloor; y -= yFloor; x -= xFloor; y -= yFloor;
Real64 u = x * x * x * (x * (x * 6 - 15) + 10); // Fade(x) Real64 u = x * x * x * (x * (x * 6 - 15) + 10); /* Fade(x) */
Real64 v = y * y * y * (y * (y * 6 - 15) + 10); // Fade(y) Real64 v = y * y * y * (y * (y * 6 - 15) + 10); /* Fade(y) */
Int32 A = p[X] + Y, B = p[X + 1] + Y; Int32 A = p[X] + Y, B = p[X + 1] + Y;
// Normally, calculating Grad involves a function call. However, we can directly pack this table /* Normally, calculating Grad involves a function call. However, we can directly pack this table
// (since each value indicates either -1, 0 1) into a set of bit flags. This way we avoid needing (since each value indicates either -1, 0 1) into a set of bit flags. This way we avoid needing
// to call another function that performs branching to call another function that performs branching */
#define xFlags 0x46552222 #define xFlags 0x46552222
#define yFlags 0x2222550A #define yFlags 0x2222550A
Int32 hash = (p[p[A]] & 0xF) << 1; Int32 hash = (p[p[A]] & 0xF) << 1;
Real64 g22 = (((xFlags >> hash) & 3) - 1) * x + (((yFlags >> hash) & 3) - 1) * y; // Grad(p[p[A], x, y) Real64 g22 = (((xFlags >> hash) & 3) - 1) * x + (((yFlags >> hash) & 3) - 1) * y; /* Grad(p[p[A], x, y) */
hash = (p[p[B]] & 0xF) << 1; hash = (p[p[B]] & 0xF) << 1;
Real64 g12 = (((xFlags >> hash) & 3) - 1) * (x - 1) + (((yFlags >> hash) & 3) - 1) * y; // Grad(p[p[B], x - 1, y) Real64 g12 = (((xFlags >> hash) & 3) - 1) * (x - 1) + (((yFlags >> hash) & 3) - 1) * y; /* Grad(p[p[B], x - 1, y) */
Real64 c1 = g22 + u * (g12 - g22); Real64 c1 = g22 + u * (g12 - g22);
hash = (p[p[A + 1]] & 0xF) << 1; hash = (p[p[A + 1]] & 0xF) << 1;
Real64 g21 = (((xFlags >> hash) & 3) - 1) * x + (((yFlags >> hash) & 3) - 1) * (y - 1); // Grad(p[p[A + 1], x, y - 1) Real64 g21 = (((xFlags >> hash) & 3) - 1) * x + (((yFlags >> hash) & 3) - 1) * (y - 1); /* Grad(p[p[A + 1], x, y - 1) */
hash = (p[p[B + 1]] & 0xF) << 1; hash = (p[p[B + 1]] & 0xF) << 1;
Real64 g11 = (((xFlags >> hash) & 3) - 1) * (x - 1) + (((yFlags >> hash) & 3) - 1) * (y - 1); // Grad(p[p[B + 1], x - 1, y - 1) Real64 g11 = (((xFlags >> hash) & 3) - 1) * (x - 1) + (((yFlags >> hash) & 3) - 1) * (y - 1); /* Grad(p[p[B + 1], x - 1, y - 1) */
Real64 c2 = g21 + u * (g11 - g21); Real64 c2 = g21 + u * (g11 - g21);
return c1 + v * (c2 - c1); return c1 + v * (c2 - c1);

View File

@ -8,6 +8,7 @@ void ImprovedNoise_Init(UInt8* p, Random* rnd);
Real64 ImprovedNoise_Compute(UInt8* p, Real64 x, Real64 y); Real64 ImprovedNoise_Compute(UInt8* p, Real64 x, Real64 y);
/* since we need structure to be a fixed size */
#define MAX_OCTAVES 8 #define MAX_OCTAVES 8
typedef struct { typedef struct {
UInt8 p[MAX_OCTAVES][NOISE_TABLE_SIZE]; UInt8 p[MAX_OCTAVES][NOISE_TABLE_SIZE];

View File

@ -1,15 +1,19 @@
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 /* Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */
// Based on: /* Based on:
// https://github.com/UnknownShadow200/ClassicalSharp/wiki/Minecraft-Classic-map-generation-algorithm https://github.com/UnknownShadow200/ClassicalSharp/wiki/Minecraft-Classic-map-generation-algorithm
// Thanks to Jerralish for originally reverse engineering classic's algorithm, then preparing a high level overview of the algorithm. Thanks to Jerralish for originally reverse engineering classic's algorithm, then preparing a high level overview of the algorithm.
// I believe this process adheres to clean room reverse engineering. I believe this process adheres to clean room reverse engineering.*/
#include "NotchyGenerator.h" #include "NotchyGenerator.h"
#include "Block.h"
#include "Funcs.h"
#include "Noise.h"
#include "Random.h" #include "Random.h"
// External variables /* External variables */
/* TODO: how do they even work? */
Real32 CurrentProgress; Real32 CurrentProgress;
// Internal variables /* Internal variables */
Int32 Width, Height, Length; Int32 Width, Height, Length;
Int32 waterLevel, oneY, minHeight; Int32 waterLevel, oneY, minHeight;
BlockID* Blocks; BlockID* Blocks;
@ -65,7 +69,7 @@ void NotchyGen_CreateStrata() {
OctaveNoise_Init(&n, &rnd, 8); OctaveNoise_Init(&n, &rnd, 8);
//CurrentState = "Creating strata"; //CurrentState = "Creating strata";
Int32 hMapIndex = 0, maxY = Height - 1, mapIndex = 0; Int32 hMapIndex = 0, maxY = Height - 1, mapIndex = 0;
// Try to bulk fill bottom of the map if possible /* Try to bulk fill bottom of the map if possible */
Int32 minStoneY = NotchyGen_CreateStrataFast(); Int32 minStoneY = NotchyGen_CreateStrataFast();
for (Int32 z = 0; z < Length; z++) { for (Int32 z = 0; z < Length; z++) {
@ -93,7 +97,7 @@ void NotchyGen_CreateStrata() {
} }
Int32 NotchyGen_CreateStrataFast() { Int32 NotchyGen_CreateStrataFast() {
// Make lava layer at bottom /* Make lava layer at bottom */
Int32 mapIndex = 0; Int32 mapIndex = 0;
for (Int32 z = 0; z < Length; z++) for (Int32 z = 0; z < Length; z++)
for (int x = 0; x < Width; x++) for (int x = 0; x < Width; x++)
@ -101,11 +105,11 @@ Int32 NotchyGen_CreateStrataFast() {
Blocks[mapIndex++] = Block_Lava; Blocks[mapIndex++] = Block_Lava;
} }
// Invariant: the lowest value dirtThickness can possible be is -14 /* Invariant: the lowest value dirtThickness can possible be is -14 */
Int32 stoneHeight = minHeight - 14; Int32 stoneHeight = minHeight - 14;
if (stoneHeight <= 0) return 1; // no layer is fully stone if (stoneHeight <= 0) return 1; /* no layer is fully stone */
// We can quickly fill in bottom solid layers /* We can quickly fill in bottom solid layers */
for (Int32 y = 1; y <= stoneHeight; y++) for (Int32 y = 1; y <= stoneHeight; y++)
for (Int32 z = 0; z < Length; z++) for (Int32 z = 0; z < Length; z++)
for (Int32 x = 0; x < Width; x++) for (Int32 x = 0; x < Width; x++)

View File

@ -1,14 +1,12 @@
#ifndef CS_NOTCHY_GEN_H #ifndef CS_NOTCHY_GEN_H
#define CS_NOTCHY_GEN_H #define CS_NOTCHY_GEN_H
#include "Compiler.h"
#include "Typedefs.h" #include "Typedefs.h"
#include "Noise.h"
#include "Funcs.h"
#include "Block.h"
void NotchyGen_Init(Int32 width, Int32 height, Int32 length, CLIENT_API void NotchyGen_Init(Int32 width, Int32 height, Int32 length,
Int32 seed, BlockID* blocks, Int16* heightmap); Int32 seed, BlockID* blocks, Int16* heightmap);
void NotchyGen_CreateHeightmap(); CLIENT_API void NotchyGen_CreateHeightmap();
void NotchyGen_CreateStrata(); CLIENT_API void NotchyGen_CreateStrata();
Int32 NotchyGen_CreateStrataFast(); Int32 NotchyGen_CreateStrataFast();
#endif #endif