-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSiegeMap.java
More file actions
160 lines (130 loc) · 5.73 KB
/
SiegeMap.java
File metadata and controls
160 lines (130 loc) · 5.73 KB
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
153
154
155
156
157
158
159
160
package io.github.restioson.siege.game.map;
import io.github.restioson.siege.Siege;
import io.github.restioson.siege.entity.SiegeKitStandEntity;
import io.github.restioson.siege.game.SiegeSpawnLogic;
import io.github.restioson.siege.game.SiegeTeams;
import io.github.restioson.siege.game.active.SiegeActive;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import xyz.nucleoid.map_templates.BlockBounds;
import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.plasmid.game.common.team.GameTeam;
import xyz.nucleoid.plasmid.game.world.generator.TemplateChunkGenerator;
import java.util.ArrayList;
import java.util.List;
public class SiegeMap {
private final SiegeMapConfig config;
private final MapTemplate template;
public final List<SiegeFlag> flags = new ArrayList<>();
public final List<SiegeKitStandLocation> kitStands = new ArrayList<>();
public SiegeSpawn waitingSpawn = null;
public List<BlockBounds> noBuildRegions = new ArrayList<>();
public List<SiegeGate> gates = new ArrayList<>();
public SiegeSpawn attackerFirstSpawn;
public SiegeSpawn defenderFirstSpawn;
public long time;
private final LongSet protectedBlocks = new LongOpenHashSet();
public SiegeMap(SiegeMapConfig config, MapTemplate template) {
this.config = config;
this.template = template;
this.time = 1000;
}
private static SiegeMap merge(SiegeActive active, SiegeMap oldMap, SiegeMap newMap) {
var world = active.world;
var gameSpace = active.gameSpace;
for (var newFlag : newMap.flags) {
oldMap.flags.stream().filter(f -> f.id.equals(newFlag.id)).findFirst()
.ifPresent(oldFlag -> {
newFlag.captureProgressTicks = oldFlag.captureProgressTicks;
newFlag.team = oldFlag.team;
newFlag.setTeamBlocks(world, newFlag.team);
});
}
for (var oldFlag : oldMap.flags) {
oldFlag.captureBar.clearPlayers();
}
for (var newGate : newMap.gates) {
oldMap.gates.stream().filter(g -> g.id.equals(newGate.id)).findFirst()
.ifPresent(oldGate -> {
newGate.bashedOpen = oldGate.bashedOpen;
newGate.health = oldGate.health;
newGate.openSlide = oldGate.openSlide;
if (newGate.bashedOpen) {
newGate.slider.setOpen(world);
} else {
newGate.slider.set(world, newGate.openSlide);
}
});
}
newMap.spawnKitStands(active);
for (var player : gameSpace.getPlayers()) {
if (!newMap.template.getBounds().contains(player.getBlockPos()) || player.isInsideWall()) {
SiegeSpawnLogic.spawnPlayer(player, newMap.waitingSpawn, world);
}
}
gameSpace.getPlayers().sendMessage(Text.literal("[Siege] Map reloaded!"));
return newMap;
}
public void reload(SiegeActive active) {
var world = active.world;
var gameSpace = active.gameSpace;
var server = world.getServer();
long time = System.currentTimeMillis();
var newMap = SiegeMapLoader.load(server, this.config);
System.out.printf("Loading took %sms%n", System.currentTimeMillis() - time);
time = System.currentTimeMillis();
var diff = newMap.template.diff(this.template);
System.out.printf("Diffing took %sms%n", System.currentTimeMillis() - time);
Siege.LOGGER.info("Loaded new template with diff {}", diff);
if (diff.needsRegeneration()) {
Siege.LOGGER.info("Regenerating world");
gameSpace.getWorlds().regenerate(world, newMap.asGenerator(server), newMap.template.getBounds().union(this.template.getBounds()));
} else {
Siege.LOGGER.debug("Diff does not warrant regeneration");
}
if (newMap.time != this.time) {
Siege.LOGGER.info("Changing time from {} to {}", this.time, newMap.time);
world.setTimeOfDay(newMap.time);
} else {
Siege.LOGGER.debug("Skipping changing time, as they are the same");
}
var merged = merge(active, this, newMap);
Siege.LOGGER.info("Done reloading map!");
active.map = merged;
}
public SiegeSpawn getFirstSpawn(GameTeam team) {
if (team == SiegeTeams.ATTACKERS) {
return this.attackerFirstSpawn;
} else {
return this.defenderFirstSpawn;
}
}
public void setWaitingSpawn(SiegeSpawn spawn) {
this.waitingSpawn = spawn;
}
public void addProtectedBlock(long pos) {
this.protectedBlocks.add(pos);
}
public boolean isProtectedBlock(BlockPos pos) {
return this.isProtectedBlock(pos.asLong());
}
public boolean isProtectedBlock(long pos) {
return this.protectedBlocks.contains(pos);
}
public ChunkGenerator asGenerator(MinecraftServer server) {
return new TemplateChunkGenerator(server, this.template);
}
public void spawnKitStands(SiegeActive active) {
for (SiegeKitStandLocation stand : this.kitStands) {
SiegeKitStandEntity standEntity = new SiegeKitStandEntity(active.world, active, stand);
active.world.spawnEntity(standEntity);
if (standEntity.controllingFlag != null) {
standEntity.controllingFlag.kitStands.add(standEntity);
}
}
}
}