Hugely simplified the temperature system to be far more logical

This commit is contained in:
Adubbz 2013-12-10 21:04:45 +11:00
parent 087d567654
commit 1e997725a5
9 changed files with 200 additions and 135 deletions

View file

@ -2,7 +2,7 @@ package tan;
import net.minecraftforge.common.MinecraftForge;
import tan.core.TANPlayerStats;
import tan.core.TANTemperatureSources;
import tan.core.TANTemperature;
import tan.handler.ConnectionHandler;
import tan.handler.RenderOverlayEventHandler;
import tan.handler.TickHandlerServer;
@ -33,7 +33,7 @@ public class ToughAsNails
public void preInit(FMLPreInitializationEvent event)
{
TANPlayerStats.init();
TANTemperatureSources.init();
TANTemperature.init();
}
@EventHandler

View file

@ -1,40 +0,0 @@
package tan.api;
import java.util.HashMap;
import net.minecraft.item.ItemStack;
public class TemperatureRegistry
{
public static HashMap<String, TemperatureSource> temperatureSources = new HashMap<String, TemperatureSource>();
public static TemperatureSource getTemperatureSource(int id, int metadata)
{
return temperatureSources.get(id + ";" + metadata);
}
public static TemperatureSource getTemperatureSource(int id)
{
return getTemperatureSource(id, 0);
}
public static void registerTemperatureSource(int id, int metadata, TemperatureSource temperatureSource)
{
if (metadata == -1)
{
for (int i = 0; i < 16; i++)
{
temperatureSources.put(id + ";" + i, temperatureSource);
}
}
else
{
temperatureSources.put(id + ";" + metadata, temperatureSource);
}
}
public static void registerTemperatureSource(int id, TemperatureSource temperatureSource)
{
registerTemperatureSource(id, 0, temperatureSource);
}
}

View file

@ -1,13 +0,0 @@
package tan.api;
public class TemperatureSource
{
public float temperature;
public float rate;
public TemperatureSource(float temperature, float rate)
{
this.temperature = temperature;
this.rate = rate;
}
}

View file

@ -0,0 +1,9 @@
package tan.api.temperature;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.world.World;
public interface ITemperatureModifier
{
public float modifyTemperature(World world, EntityPlayerMP player);
}

View file

@ -0,0 +1,55 @@
package tan.api.temperature;
import java.util.ArrayList;
import java.util.HashMap;
public class TemperatureRegistry
{
public static HashMap<String, Float> temperatureSources = new HashMap<String, Float>();
public static ArrayList<ITemperatureModifier> temperatureModifiers = new ArrayList<ITemperatureModifier>();
public static float getTemperatureSourceModifier(int id, int metadata)
{
float modifier = 0;
try
{
modifier = temperatureSources.get(id + ";" + metadata);
}
catch (Exception e)
{
}
return modifier;
}
public static float getTemperatureSourceModifier(int id)
{
return getTemperatureSourceModifier(id, 0);
}
public static void registerTemperatureModifier(ITemperatureModifier temperatureModifier)
{
temperatureModifiers.add(temperatureModifier);
}
public static void registerTemperatureSource(int id, int metadata, float modifier)
{
if (metadata == -1)
{
for (int i = 0; i < 16; i++)
{
temperatureSources.put(id + ";" + i, modifier);
}
}
else
{
temperatureSources.put(id + ";" + metadata, modifier);
}
}
public static void registerTemperatureSource(int id, float modifier)
{
registerTemperatureSource(id, 0, modifier);
}
}

View file

@ -0,0 +1,24 @@
package tan.core;
import net.minecraft.block.Block;
import tan.api.temperature.TemperatureRegistry;
import tan.temperaturemodifiers.TemperatureSourceModifier;
public class TANTemperature
{
public static void init()
{
registerTemperatureModifiers();
registerTemperatureSources();
}
private static void registerTemperatureModifiers()
{
TemperatureRegistry.registerTemperatureModifier(new TemperatureSourceModifier());
}
private static void registerTemperatureSources()
{
TemperatureRegistry.registerTemperatureSource(Block.fire.blockID, -1, 4F);
}
}

View file

@ -1,18 +0,0 @@
package tan.core;
import net.minecraft.block.Block;
import tan.api.TemperatureRegistry;
import tan.api.TemperatureSource;
public class TANTemperatureSources
{
public static void init()
{
registerTemperatureSources();
}
private static void registerTemperatureSources()
{
TemperatureRegistry.registerTemperatureSource(Block.fire.blockID, -1, new TemperatureSource(43F, 0.80F));
}
}

View file

@ -1,19 +1,15 @@
package tan.stats;
import java.text.DecimalFormat;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.util.MathHelper;
import net.minecraft.world.biome.BiomeGenBase;
import tan.api.TANStat;
import tan.api.TemperatureRegistry;
import tan.api.TemperatureSource;
import tan.api.temperature.ITemperatureModifier;
import tan.api.temperature.TemperatureRegistry;
public class TemperatureStat extends TANStat
{
private ArrayList<Float> averageAimedTemperatures = new ArrayList<Float>();
private ArrayList<Float> averageRates = new ArrayList<Float>();
@Override
public void update()
{
@ -24,19 +20,18 @@ public class TemperatureStat extends TANStat
float originalTemperature = tanData.getFloat(getStatName());
float temperature = originalTemperature;
float aimedTemperature = 0F;
float rate = 0F;
float[] temperatureRainfall = getEnvironmentTemperatureRainfall(x, y, z);
calculateSourceAndEnvironment(x, y, z);
float aimedTemperature = temperatureRainfall[0];
for (float averageAimedTemperature : averageAimedTemperatures)
for (ITemperatureModifier temperatureModifier : TemperatureRegistry.temperatureModifiers)
{
aimedTemperature += averageAimedTemperature;
float modifier = temperatureModifier.modifyTemperature(world, player);
aimedTemperature += modifier;
}
aimedTemperature /= averageAimedTemperatures.size();
DecimalFormat twoDForm = new DecimalFormat("#.#");
DecimalFormat twoDForm = new DecimalFormat("#.##");
try
{
@ -47,27 +42,26 @@ public class TemperatureStat extends TANStat
}
for (float averageRate : averageRates)
{
rate += averageRate;
}
rate = (rate / averageRates.size()) / 10;
if (world.rand.nextFloat() <= rate)
if (world.rand.nextFloat() <= temperatureRainfall[1])
{
if (temperature > aimedTemperature)
{
temperature -= 0.1F;
temperature -= 0.01F;
}
else if (temperature < aimedTemperature)
{
temperature += 0.1F;
temperature += 0.01F;
}
}
averageAimedTemperatures.clear();
averageRates.clear();
try
{
temperature = Float.parseFloat(twoDForm.format(temperature));
}
catch (Exception e)
{
}
if (temperature != originalTemperature)
{
@ -77,15 +71,12 @@ public class TemperatureStat extends TANStat
}
}
private void calculateSourceAndEnvironment(int x, int y, int z)
private float[] getEnvironmentTemperatureRainfall(int x, int y, int z)
{
float averageAimedSourceTemperature = 0F;
float averageSourceRate = 0F;
int sourceDivider = 0;
float[] temperatureRainfall = new float[2];
float averageAimedEnvironmentTemperature = 0F;
float averageEnvironmentRate = 0F;
float rainfall = 0.25F;
int environmentDivider = 0;
@ -98,39 +89,39 @@ public class TemperatureStat extends TANStat
int blockID = world.getBlockId(x + ix, y + iy, z + iz);
int metadata = world.getBlockMetadata(x + ix, y + iy, z + iz);
TemperatureSource temperatureSource = TemperatureRegistry.getTemperatureSource(blockID, metadata);
BiomeGenBase biome = world.getBiomeGenForCoords(x + ix, z + iz);
if (temperatureSource != null)
{
averageAimedSourceTemperature += temperatureSource.temperature;
averageSourceRate += temperatureSource.rate;
averageAimedEnvironmentTemperature += ((biome.temperature / 2) * 20) + 27;
sourceDivider++;
}
else
{
averageAimedEnvironmentTemperature += ((world.getBiomeGenForCoords(x + ix, z + iz).temperature / 2) * 20) + 27;
averageEnvironmentRate += 0.25F;
rainfall = (biome.rainfall / 2) / 10;
environmentDivider++;
}
}
}
temperatureRainfall[0] = averageAimedEnvironmentTemperature / environmentDivider;
temperatureRainfall[1] = rainfall;
return temperatureRainfall;
}
if (sourceDivider != 0)
public boolean isDay()
{
float aimedSourceTemperature = averageAimedSourceTemperature /= sourceDivider;
averageAimedTemperatures.add(aimedSourceTemperature);
}
if (environmentDivider != 0)
{
float aimedEnvironmentTemperature = averageAimedEnvironmentTemperature /= environmentDivider;
averageAimedTemperatures.add(aimedEnvironmentTemperature);
float celestialAngle = world.getCelestialAngle(0.0F);
if (celestialAngle >= 0.75F && celestialAngle <= 1.0F || celestialAngle >= 0.0F && celestialAngle <= 0.25F) return true;
return false;
}
if (sourceDivider != 0) averageRates.add(averageSourceRate /= sourceDivider);
if (environmentDivider != 0) averageRates.add(averageEnvironmentRate /= environmentDivider);
public boolean isNight()
{
float celestialAngle = world.getCelestialAngle(0.0F);
if (celestialAngle >= 0.25F && celestialAngle <= 0.75F) return true;
return false;
}
@Override

View file

@ -0,0 +1,57 @@
package tan.temperaturemodifiers;
import java.util.ArrayList;
import java.util.Collections;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import tan.api.temperature.ITemperatureModifier;
import tan.api.temperature.TemperatureRegistry;
public class TemperatureSourceModifier implements ITemperatureModifier
{
@Override
public float modifyTemperature(World world, EntityPlayerMP player)
{
ArrayList<Float> temperatureModifiers = new ArrayList<Float>();
int x = MathHelper.floor_double(player.posX);
int y = MathHelper.floor_double(player.posY);
int z = MathHelper.floor_double(player.posZ);
for (int ix = -2; ix <= 2; ix++)
{
for (int iy = -1; iy <= 1; iy++)
{
for (int iz = -2; iz <= 2; iz++)
{
int blockID = world.getBlockId(x + ix, y + iy, z + iz);
int metadata = world.getBlockMetadata(x + ix, y + iy, z + iz);
float temperatureModifier = TemperatureRegistry.getTemperatureSourceModifier(blockID, metadata);
temperatureModifiers.add(temperatureModifier);
}
}
}
float total = 0F;
int divider = 0;
for (float temperatureModifier : temperatureModifiers)
{
total += temperatureModifier;
divider++;
}
if ((total / divider) > 0)
{
return Collections.max(temperatureModifiers);
}
else
{
return Collections.min(temperatureModifiers);
}
}
}