#include "stdafx.h"
#include "net.minecraft.world.level.newbiome.layer.h"
#include "net.minecraft.world.level.h"
#include "BiomeOverrideLayer.h"


LayerArray Layer::getDefaultLayers(__int64 seed, LevelType *levelType)
{
	// 4J - Some changes moved here from 1.2.3. Temperature & downfall layers are no longer created & returned, and a debug layer is isn't.
	// For reference with regard to future merging, things NOT brought forward from the 1.2.3 version are new layer types that we
	// don't have yet (shores, swamprivers, region hills etc.)
	shared_ptr<Layer>islandLayer = shared_ptr<Layer>(new IslandLayer(1));
	islandLayer = shared_ptr<Layer>(new FuzzyZoomLayer(2000, islandLayer));
	islandLayer = shared_ptr<Layer>(new AddIslandLayer(1, islandLayer));
	islandLayer = shared_ptr<Layer>(new ZoomLayer(2001, islandLayer));
	islandLayer = shared_ptr<Layer>(new AddIslandLayer(2, islandLayer));
	islandLayer = shared_ptr<Layer>(new AddSnowLayer(2, islandLayer));
	islandLayer = shared_ptr<Layer>(new ZoomLayer(2002, islandLayer));
	islandLayer = shared_ptr<Layer>(new AddIslandLayer(3, islandLayer));
	islandLayer = shared_ptr<Layer>(new ZoomLayer(2003, islandLayer));
	islandLayer = shared_ptr<Layer>(new AddIslandLayer(4, islandLayer));
//	islandLayer = shared_ptr<Layer>(new AddMushroomIslandLayer(5, islandLayer));		// 4J - old position of mushroom island layer

	int zoomLevel = 4;
	if (levelType == LevelType::lvl_largeBiomes)
	{
		zoomLevel = 6;
	}

	shared_ptr<Layer> riverLayer = islandLayer;
	riverLayer = ZoomLayer::zoom(1000, riverLayer, 0);
	riverLayer = shared_ptr<Layer>(new RiverInitLayer(100, riverLayer));
	riverLayer = ZoomLayer::zoom(1000, riverLayer, zoomLevel + 2);
	riverLayer = shared_ptr<Layer>(new RiverLayer(1, riverLayer));
	riverLayer = shared_ptr<Layer>(new SmoothLayer(1000, riverLayer));

	shared_ptr<Layer> biomeLayer = islandLayer;
	biomeLayer = ZoomLayer::zoom(1000, biomeLayer, 0);
	biomeLayer = shared_ptr<Layer>(new BiomeInitLayer(200, biomeLayer, levelType));

	biomeLayer = ZoomLayer::zoom(1000, biomeLayer, 2);
	biomeLayer = shared_ptr<Layer>(new RegionHillsLayer(1000, biomeLayer));

	for (int i = 0; i < zoomLevel; i++)
	{
		biomeLayer = shared_ptr<Layer>(new ZoomLayer(1000 + i, biomeLayer));

		if (i == 0) biomeLayer = shared_ptr<Layer>(new AddIslandLayer(3, biomeLayer));
		
		if (i == 0)
		{
			// 4J - moved mushroom islands to here. This skips 3 zooms that the old location of the add was, making them about 1/8 of the original size. Adding
			// them at this scale actually lets us place them near enough other land, if we add them at the same scale as java then they have to be too far out to see for
			// the scale of our maps
			biomeLayer = shared_ptr<Layer>(new AddMushroomIslandLayer(5, biomeLayer));
		}

		if (i == 1 )
		{	
			// 4J - now expand mushroom islands up again. This does a simple region grow to add a new mushroom island element when any of the neighbours are also mushroom islands.
			// This helps make the islands into nice compact shapes of the type that are actually likely to be able to make an island out of the sea in a small space. Also
			// helps the shore layer from doing too much damage in shrinking the islands we are making
			biomeLayer = shared_ptr<Layer>(new GrowMushroomIslandLayer(5, biomeLayer));	
			// Note - this reduces the size of mushroom islands by turning their edges into shores. We are doing this at i == 1 rather than i == 0 as the original does
			biomeLayer = shared_ptr<Layer>(new ShoreLayer(1000, biomeLayer));		

			biomeLayer = shared_ptr<Layer>(new SwampRiversLayer(1000, biomeLayer));
		}
	}

	biomeLayer = shared_ptr<Layer>(new SmoothLayer(1000, biomeLayer));

	biomeLayer = shared_ptr<Layer>(new RiverMixerLayer(100, biomeLayer, riverLayer));

#ifndef _CONTENT_PACKAGE
#ifdef _BIOME_OVERRIDE
	if(app.DebugSettingsOn() && app.GetGameSettingsDebugMask(ProfileManager.GetPrimaryPad())&(1L<<eDebugSetting_EnableHeightWaterBiomeOverride))
	{
		biomeLayer = shared_ptr<BiomeOverrideLayer>(new BiomeOverrideLayer(1));
	}
#endif
#endif

	shared_ptr<Layer> debugLayer = biomeLayer;

	shared_ptr<Layer>zoomedLayer = shared_ptr<Layer>(new VoronoiZoom(10, biomeLayer));

	biomeLayer->init(seed);
	zoomedLayer->init(seed);

	LayerArray result(3);
	result[0] = biomeLayer;
	result[1] = zoomedLayer;
	result[2] = debugLayer;
	return result;
}

Layer::Layer(__int64 seedMixup)
{
	parent = nullptr;

	this->seedMixup = seedMixup;
	this->seedMixup *= this->seedMixup * 6364136223846793005l + 1442695040888963407l;
	this->seedMixup += seedMixup;
	this->seedMixup *= this->seedMixup * 6364136223846793005l + 1442695040888963407l;
	this->seedMixup += seedMixup;
	this->seedMixup *= this->seedMixup * 6364136223846793005l + 1442695040888963407l;
	this->seedMixup += seedMixup;
}

void Layer::init(__int64 seed)
{
	this->seed = seed;
	if (parent != NULL) parent->init(seed);
	this->seed *= this->seed * 6364136223846793005l + 1442695040888963407l;
	this->seed += seedMixup;
	this->seed *= this->seed * 6364136223846793005l + 1442695040888963407l;
	this->seed += seedMixup;
	this->seed *= this->seed * 6364136223846793005l + 1442695040888963407l;
	this->seed += seedMixup;
}

void Layer::initRandom(__int64 x, __int64 y)
{
	rval = seed;
	rval *= rval * 6364136223846793005l + 1442695040888963407l;
	rval += x;
	rval *= rval * 6364136223846793005l + 1442695040888963407l;
	rval += y;
	rval *= rval * 6364136223846793005l + 1442695040888963407l;
	rval += x;
	rval *= rval * 6364136223846793005l + 1442695040888963407l;
	rval += y;
}

int Layer::nextRandom(int max)
{

	int result = (int) ((rval >> 24) % max);

	if (result < 0) result += max;
	rval *= rval * 6364136223846793005l + 1442695040888963407l;
	rval += seed;
	return result;
}
