-
Notifications
You must be signed in to change notification settings - Fork 46
/
WorldClimateEngine.java
152 lines (133 loc) · 5.09 KB
/
WorldClimateEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package net.porillo.engine.api;
import lombok.Getter;
import net.porillo.GlobalWarming;
import net.porillo.config.WorldConfig;
import net.porillo.database.tables.WorldTable;
import net.porillo.effect.api.ClimateEffectType;
import net.porillo.engine.models.*;
import net.porillo.objects.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class WorldClimateEngine {
@Getter private WorldConfig config;
// Models
@Getter private ScoreTempModel scoreTempModel;
private FuelModel fuelModel;
private EntityMethaneModel methaneModel;
private ReductionModel reductionModel;
@Getter private EntityFitnessModel entityFitnessModel;
@Getter private CarbonIndexModel carbonIndexModel;
public WorldClimateEngine(WorldConfig config) {
this.config = config;
// Worlds load their own model file
World world = Bukkit.getWorld(config.getWorldId());
if (world != null) {
String worldName = Bukkit.getWorld(config.getWorldId()).getName();
this.scoreTempModel = new ScoreTempModel(worldName, config.getSensitivity());
this.fuelModel = new FuelModel(worldName);
this.methaneModel = new EntityMethaneModel(worldName);
this.reductionModel = new ReductionModel(worldName);
this.entityFitnessModel = new EntityFitnessModel(worldName);
this.carbonIndexModel = new CarbonIndexModel(worldName);
} else {
GlobalWarming.getInstance().getLogger().warning(
String.format("Could not load climate engine for world id [%s]", config.getWorldId()));
}
}
public Reduction treeGrow(Tree tree, List<BlockState> blocks, boolean bonemealUsed) {
int reductionValue = 0;
int numBlocks = 0;
if (bonemealUsed && !config.isBonemealReductionAllowed()) {
return null;
} else if (bonemealUsed) {
for (BlockState bs : blocks) {
double reduction = reductionModel.getReduction(bs.getType());
if (reduction > 0.0) {
reductionValue += (config.getBonemealReductionModifier() * reduction);
numBlocks++;
}
}
} else {
for (BlockState bs : blocks) {
double reduction = reductionModel.getReduction(bs.getType());
if (reduction > 0.0) {
reductionValue += reduction;
numBlocks++;
}
}
}
Integer uniqueId = GlobalWarming.getInstance().getRandom().nextInt(Integer.MAX_VALUE);
Reduction reduction = new Reduction();
reduction.setUniqueID(uniqueId);
reduction.setWorldId(config.getWorldId());
reduction.setReductioner(tree.getOwnerId());
reduction.setReductionKey(tree.getUniqueId());
reduction.setReductionValue(reductionValue);
reduction.setNumBlocks(numBlocks);
return reduction;
}
public Contribution furnaceBurn(Furnace furnace, Material furnaceType, ItemStack fuel) {
Contribution contribution = null;
if (furnace == null) {
GlobalWarming.getInstance().getLogger().severe("Furnace null");
} else if (fuel == null) {
GlobalWarming.getInstance().getLogger().severe("Fuel null");
} else {
Integer uniqueId = GlobalWarming.getInstance().getRandom().nextInt(Integer.MAX_VALUE);
contribution = new Contribution();
contribution.setUniqueID(uniqueId);
contribution.setWorldId(config.getWorldId());
contribution.setContributer(furnace.getOwnerId());
contribution.setContributionKey(furnace.getUniqueId());
double contribValue = fuelModel.getContribution(fuel.getType());
if (furnaceType == Material.BLAST_FURNACE) {
contribValue *= config.getBlastFurnaceMultiplier();
}
contribution.setContributionValue((int) contribValue);
}
return contribution;
}
public Contribution methaneRelease(TrackedEntity entity) {
Contribution contribution = null;
if (entity == null) {
GlobalWarming.getInstance().getLogger().severe("Entity null!");
} else {
Integer uniqueId = GlobalWarming.getInstance().getRandom().nextInt(Integer.MAX_VALUE);
contribution = new Contribution();
contribution.setUniqueID(uniqueId);
contribution.setWorldId(config.getWorldId());
contribution.setContributer(entity.getBreederId());
contribution.setContributionKey(entity.getUniqueId());
double contribValue = methaneModel.getContribution(entity.getEntityType());
double modifier = config.getMethaneTicksLivedModifier();
contribValue += (int)(entity.getTicksLived()/20/60 * modifier);
contribution.setContributionValue((int) contribValue);
}
return contribution;
}
public double getTemperature() {
double temperature = 14.0;
WorldTable worldTable = GlobalWarming.getInstance().getTableManager().getWorldTable();
GWorld gWorld = worldTable.getWorld(config.getWorldId());
if (gWorld == null) {
Thread.dumpStack();
GlobalWarming.getInstance().getLogger().severe(String.format(
"World ID not found in GWorld table [%s]: [%s]",
WorldConfig.getDisplayName(config.getWorldId()),
config.getWorldId()));
} else {
temperature = scoreTempModel.getTemperature(gWorld.getCarbonValue());
}
return temperature;
}
public boolean isEffectEnabled(ClimateEffectType type) {
return isEnabled() && config.getEnabledEffects().contains(type);
}
public boolean isEnabled() {
return this.config.isEnabled();
}
}