Skip to content

Commit 27b8d38

Browse files
committed
improve
1 parent d0da75a commit 27b8d38

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/main/java/com/teammoeg/frostedheart/content/climate/player/SurroundingTemperatureSimulator.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public static void init() {
158158

159159
}
160160

161-
public SurroundingTemperatureSimulator(ServerPlayer player) {
162-
int sourceX = Mth.floor(player.getX()), sourceY = Mth.floor(player.getEyeY()), sourceZ = Mth.floor(player.getZ());
161+
public SurroundingTemperatureSimulator(ServerLevel world,double sx,double sy,double sz,boolean threadSafe) {
162+
int sourceX = Mth.floor(sx), sourceY = Mth.floor(sy), sourceZ = Mth.floor(sz);
163163
// these are block position offset
164164
int offsetN = sourceZ - range;
165165
int offsetW = sourceX - range;
@@ -168,12 +168,10 @@ public SurroundingTemperatureSimulator(ServerPlayer player) {
168168
int chunkOffsetW = offsetW >> 4;
169169
int chunkOffsetN = offsetN >> 4;
170170
int chunkOffsetD = offsetD >> 4;
171-
172171
// get origin point(center of 8 sections)
173172
origin = new BlockPos((chunkOffsetW + 1) << 4, (chunkOffsetD + 1) << 4, (chunkOffsetN + 1) << 4);
174173
// fetch all sections to lower calculation cost
175174
int i = 0;
176-
ServerLevel world = player.serverLevel();
177175

178176
for (int x = chunkOffsetW; x <= chunkOffsetW + 1; x++)
179177
for (int z = chunkOffsetN; z <= chunkOffsetN + 1; z++) {
@@ -182,16 +180,23 @@ public SurroundingTemperatureSimulator(ServerPlayer player) {
182180
int index0=cnk.getSectionIndexFromSectionY(chunkOffsetD);
183181
int index1=index0+1;
184182
maps[i / 2] = cnk.getOrCreateHeightmapUnprimed(Types.MOTION_BLOCKING_NO_LEAVES);
185-
//copy to avoid threading issue
183+
186184
if(index0>=0&&index0<maxIndex)
187-
sections[i] = cnk.getSection(index0).getStates().copy();
185+
sections[i] = cnk.getSection(index0).getStates();
188186
//System.out.println(sections[i].get(0, 0, 0));
189187
if(index1>=0&&index1<maxIndex)
190-
sections[i + 1] = cnk.getSection(index1).getStates().copy();
188+
sections[i + 1] = cnk.getSection(index1).getStates();
191189
//System.out.println(sections[i+1].get(0, 0, 0));
192190
i += 2;
193191
}
194-
rnd = new Random(player.blockPosition().asLong() ^ (world.getGameTime() >> 6));
192+
//copy to avoid threading issue
193+
if(threadSafe) {
194+
for(int j=0;j<sections.length;j++)
195+
if(sections[j]!=null)
196+
sections[j]=sections[j].copy();
197+
198+
}
199+
rnd = new Random(new BlockPos(sourceX,sourceY,sourceZ).asLong() ^ (world.getGameTime() >> 6));
195200
}
196201

197202
/**

src/main/java/com/teammoeg/frostedheart/content/climate/player/TemperatureThreadingPool.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
import com.teammoeg.chorda.util.CUtils;
1515
import com.teammoeg.frostedheart.content.climate.player.SurroundingTemperatureSimulator.SimulationResult;
1616

17+
import lombok.Getter;
1718
import net.minecraft.server.level.ServerPlayer;
1819

1920
public class TemperatureThreadingPool {
2021
Map<UUID,Future<SimulationResult>> resultMap;
2122
ExecutorService scheduler;
23+
/**
24+
* Used to benchmark temperature calculation performance
25+
* */
26+
@Getter
27+
int tasksRemain;
2228
public TemperatureThreadingPool(int threadNum) {
2329
if(threadNum!=0) {
2430
scheduler=Executors.newFixedThreadPool(threadNum, CUtils.makeThreadFactory("block-temperature-calculation", true));
@@ -31,20 +37,20 @@ public boolean tryCommitWork(ServerPlayer player) {
3137
double y=player.getEyeY()-0.7;
3238
double z=player.getZ();
3339
if(scheduler==null) {
34-
SurroundingTemperatureSimulator sts=new SurroundingTemperatureSimulator(player);
40+
SurroundingTemperatureSimulator sts=new SurroundingTemperatureSimulator(player.serverLevel(),player.getX(),player.getEyeY(),player.getZ(),false);
3541
submitPlayerData(player, sts.getBlockTemperatureAndWind(x, y, z));
3642
return true;
3743
}else if(!resultMap.containsKey(player.getUUID())){
3844
//System.out.println("committing work for "+player.getName().getString());
39-
SurroundingTemperatureSimulator sts=new SurroundingTemperatureSimulator(player);
45+
SurroundingTemperatureSimulator sts=new SurroundingTemperatureSimulator(player.serverLevel(),player.getX(),player.getEyeY(),player.getZ(),true);
4046
resultMap.put(player.getUUID(), scheduler.submit(()->sts.getBlockTemperatureAndWind(x, y, z)));
4147
return true;
4248
}
4349
return false;
4450
}
4551
public void tick() {
4652
if(resultMap!=null) {
47-
53+
int tasksRemain=0;
4854
for(Iterator<Entry<UUID, Future<SimulationResult>>> it=resultMap.entrySet().iterator();it.hasNext();) {
4955
Entry<UUID, Future<SimulationResult>> entry=it.next();
5056
if(entry.getValue().isDone()) {
@@ -61,8 +67,9 @@ public void tick() {
6167
throw new RuntimeException(e.getCause());
6268
}
6369
}
64-
}
70+
}else tasksRemain++;
6571
}
72+
this.tasksRemain=tasksRemain;
6673
}
6774
}
6875
public void close() {
@@ -71,7 +78,7 @@ public void close() {
7178

7279
}
7380
private void submitPlayerData(ServerPlayer player,SimulationResult result) {
74-
System.out.println(result);
81+
//System.out.println(result);
7582
PlayerTemperatureData.getCapability(player).ifPresent(t->{
7683
t.blockTemp=result.blockTemp();
7784
t.windStrengh=result.windStrengh();

0 commit comments

Comments
 (0)