Skip to content

Commit 4852ed8

Browse files
committed
Added a simple weather & day/night system
1 parent c1940fb commit 4852ed8

File tree

4 files changed

+106
-12
lines changed

4 files changed

+106
-12
lines changed

gameserver/src/main/java/brainwine/gameserver/GameServer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public class GameServer {
2727
private final ZoneManager zoneManager;
2828
private final PlayerManager playerManager;
2929
private final Server server;
30-
private long lastSave = System.currentTimeMillis();
30+
private long lastTick = System.currentTimeMillis();
31+
private long lastSave = lastTick;
3132
private boolean shutdownRequested;
3233

3334
public GameServer() {
@@ -56,6 +57,10 @@ public static GameServer getInstance() {
5657
}
5758

5859
public void tick() {
60+
long now = System.currentTimeMillis();
61+
float deltaTime = (now - lastTick) / 1000.0F; // in seconds
62+
lastTick = now;
63+
5964
while(!tasks.isEmpty()) {
6065
tasks.poll().run();
6166
}
@@ -66,7 +71,7 @@ public void tick() {
6671
lastSave = System.currentTimeMillis();
6772
}
6873

69-
zoneManager.tick();
74+
zoneManager.tick(deltaTime);
7075
playerManager.tick();
7176
}
7277

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package brainwine.gameserver.zone;
2+
3+
import brainwine.gameserver.util.MathUtils;
4+
import io.netty.util.internal.ThreadLocalRandom;
5+
6+
/**
7+
* TODO save weather in zone config
8+
*/
9+
public class WeatherManager {
10+
11+
private static final ThreadLocalRandom random = ThreadLocalRandom.current();
12+
private long rainStart;
13+
private long rainDuration;
14+
private float rainPower;
15+
private float precipitation;
16+
private float wind;
17+
private float cloudiness;
18+
19+
public WeatherManager() {
20+
createRandomRain(random.nextBoolean());
21+
}
22+
23+
public void tick(float deltaTime) {
24+
long now = System.currentTimeMillis();
25+
26+
if(now > rainStart + rainDuration) {
27+
createRandomRain(rainPower > 0 ? true : false);
28+
}
29+
30+
float lerp = (float)(deltaTime * MathUtils.lerp(0.02F, 0.1F, (now - rainStart) / (float)rainDuration));
31+
// Why are these separate, again?
32+
precipitation = (float)MathUtils.lerp(precipitation, rainPower, lerp);
33+
wind = precipitation;
34+
cloudiness = precipitation;
35+
}
36+
37+
public void setRain(float power, long duration) {
38+
rainStart = System.currentTimeMillis();
39+
rainPower = power;
40+
rainDuration = duration;
41+
}
42+
43+
public void createRandomRain(boolean dry) {
44+
rainStart = System.currentTimeMillis();
45+
46+
if(dry) {
47+
rainDuration = (long)(random.nextDouble(12, 17) * 60000);
48+
rainPower = 0;
49+
} else {
50+
rainDuration = (long)(random.nextDouble(2.5, 4) * 60000);
51+
rainPower = (float)random.nextDouble(0.33, 1.0);
52+
}
53+
}
54+
55+
public float getPrecipitation() {
56+
return precipitation;
57+
}
58+
59+
public float getWind() {
60+
return wind;
61+
}
62+
63+
public float getCloudiness() {
64+
return cloudiness;
65+
}
66+
}

gameserver/src/main/java/brainwine/gameserver/zone/Zone.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ public class Zone {
6262
private int[] surface;
6363
private int[] sunlight;
6464
private boolean[] chunksExplored;
65-
private float time = 5000;
65+
private float time = (float)Math.random(); // TODO temporary
6666
private float temperature = 0;
67-
private float wind = 0;
68-
private float cloudiness = 5000;
69-
private float precipitation = 0;
7067
private float acidity = 0;
7168
private final ChunkManager chunkManager;
72-
private final Queue<DugBlock> digQueue = new ArrayDeque<>(); // TODO should be saved
69+
private final WeatherManager weatherManager = new WeatherManager();
70+
private final Queue<DugBlock> digQueue = new ArrayDeque<>();
7371
private final Set<Integer> pendingSunlight = new HashSet<>();
7472
private final Map<Integer, Entity> entities = new HashMap<>();
7573
private final List<Player> players = new ArrayList<>();
@@ -78,6 +76,7 @@ public class Zone {
7876
private final Map<Integer, MetaBlock> metaBlocks = new HashMap<>();
7977
private final Map<Integer, MetaBlock> globalMetaBlocks = new HashMap<>();
8078
private final Map<Integer, MetaBlock> fieldBlocks = new HashMap<>();
79+
private long lastStatusUpdate = System.currentTimeMillis();
8180

8281
protected Zone(String documentId, ZoneConfig config, ZoneData data) {
8382
this(documentId, config.getName(), config.getBiome(), config.getWidth(), config.getHeight());
@@ -107,15 +106,32 @@ private static Zone fromId(String id) {
107106
return GameServer.getInstance().getZoneManager().getZone(id);
108107
}
109108

110-
public void tick() {
109+
public void tick(float deltaTime) {
110+
long now = System.currentTimeMillis();
111+
weatherManager.tick(deltaTime);
112+
111113
for(Entity entity : getEntities()) {
112114
entity.tick();
113115
}
114116

117+
// One full cycle = 1200 seconds = 20 minutes
118+
time += deltaTime * (1.0F / 1200.0F);
119+
120+
if(time >= 1.0F) {
121+
time -= 1.0F;
122+
}
123+
124+
if(!players.isEmpty()) {
125+
if(now >= lastStatusUpdate + 4000) {
126+
sendMessage(new ZoneStatusMessage(getStatusConfig()));
127+
lastStatusUpdate = now;
128+
}
129+
}
130+
115131
if(!digQueue.isEmpty()) {
116132
DugBlock dugBlock = digQueue.peek();
117133

118-
if(System.currentTimeMillis() >= dugBlock.getTime()) {
134+
if(now >= dugBlock.getTime()) {
119135
digQueue.poll();
120136
int x = dugBlock.getX();
121137
int y = dugBlock.getY();
@@ -928,7 +944,14 @@ public Map<String, Object> getClientConfig(Player player) {
928944
*/
929945
public Map<String, Object> getStatusConfig() {
930946
Map<String, Object> config = new HashMap<>();
931-
config.put("w", new float[]{time, temperature, wind, cloudiness, precipitation, acidity});
947+
config.put("w", new int[] {
948+
(int)(time * 10000),
949+
(int)(temperature * 10000),
950+
(int)(weatherManager.getWind() * 10000),
951+
(int)(weatherManager.getCloudiness() * 10000),
952+
(int)(weatherManager.getPrecipitation() * 10000),
953+
(int)(acidity * 10000)
954+
});
932955
return config;
933956
}
934957

gameserver/src/main/java/brainwine/gameserver/zone/ZoneManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public ZoneManager() {
5858
asyncGenerator.start();
5959
}
6060

61-
public void tick() {
61+
public void tick(float deltaTime) {
6262
for(Zone zone : getZones()) {
63-
zone.tick();
63+
zone.tick(deltaTime);
6464
}
6565
}
6666

0 commit comments

Comments
 (0)