-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWaveSpawner.java
244 lines (195 loc) · 8.14 KB
/
WaveSpawner.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package mindustry.ai;
import arc.*;
import arc.func.*;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import mindustry.annotations.Annotations.*;
import mindustry.content.*;
import mindustry.core.*;
import mindustry.entities.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import static mindustry.Vars.*;
public class WaveSpawner{
private static final float margin = 0f, coreMargin = tilesize * 2f, maxSteps = 30;
private int tmpCount;
private Seq<Tile> spawns = new Seq<>();
private boolean spawning = false;
private boolean any = false;
private Tile firstSpawn = null;
public WaveSpawner(){
Events.on(WorldLoadEvent.class, e -> reset());
}
@Nullable
public Tile getFirstSpawn(){
firstSpawn = null;
eachGroundSpawn((cx, cy) -> firstSpawn = world.tile(cx, cy));
return firstSpawn;
}
public int countSpawns(){
return spawns.size;
}
public Seq<Tile> getSpawns(){
return spawns;
}
/** @return true if the player is near a ground spawn point. */
public boolean playerNear(){
return state.hasSpawns() && !player.dead() && spawns.contains(g -> Mathf.dst(g.x * tilesize, g.y * tilesize, player.x, player.y) < state.rules.dropZoneRadius && player.team() != state.rules.waveTeam);
}
public void spawnEnemies(){
spawning = true;
eachGroundSpawn(-1, (spawnX, spawnY, doShockwave) -> {
if(doShockwave){
doShockwave(spawnX, spawnY);
}
});
for(SpawnGroup group : state.rules.spawns){
if(group.type == null) continue;
int spawned = group.getSpawned(state.wave - 1);
if(spawned == 0) continue;
if(state.isCampaign()){
//when spawning a boss, round down, so 1.5x (hard) * 1 boss does not result in 2 bosses
spawned = Math.max(1, group.effect == StatusEffects.boss ?
(int)(spawned * state.getPlanet().campaignRules.difficulty.enemySpawnMultiplier) :
Mathf.round(spawned * state.getPlanet().campaignRules.difficulty.enemySpawnMultiplier));
}
int spawnedf = spawned;
if(group.type.flying){
float spread = margin / 1.5f;
eachFlyerSpawn(group.spawn, (spawnX, spawnY) -> {
for(int i = 0; i < spawnedf; i++){
Unit unit = group.createUnit(state.rules.waveTeam, state.wave - 1);
unit.set(spawnX + Mathf.range(spread), spawnY + Mathf.range(spread));
spawnEffect(unit);
}
});
}else{
float spread = tilesize * 2;
eachGroundSpawn(group.spawn, (spawnX, spawnY, doShockwave) -> {
for(int i = 0; i < spawnedf; i++){
Tmp.v1.rnd(spread);
Unit unit = group.createUnit(state.rules.waveTeam, state.wave - 1);
unit.set(spawnX + Tmp.v1.x, spawnY + Tmp.v1.y);
spawnEffect(unit);
}
});
}
}
Time.run(121f, () -> spawning = false);
}
public void doShockwave(float x, float y){
Fx.spawnShockwave.at(x, y, state.rules.dropZoneRadius);
Damage.damage(state.rules.waveTeam, x, y, state.rules.dropZoneRadius, 99999999f, true);
}
public void eachGroundSpawn(Intc2 cons){
eachGroundSpawn(-1, (x, y, shock) -> cons.get(World.toTile(x), World.toTile(y)));
}
private void eachGroundSpawn(int filterPos, SpawnConsumer cons){
if(state.hasSpawns()){
for(Tile spawn : spawns){
if(filterPos != -1 && filterPos != spawn.pos()) continue;
cons.accept(spawn.worldx(), spawn.worldy(), true);
}
}
if(state.rules.attackMode && state.teams.isActive(state.rules.waveTeam) && !state.teams.playerCores().isEmpty()){
Building firstCore = state.teams.playerCores().first();
for(Building core : state.rules.waveTeam.cores()){
if(filterPos != -1 && filterPos != core.pos()) continue;
Tmp.v1.set(firstCore).sub(core).limit(coreMargin + core.block.size * tilesize /2f * Mathf.sqrt2);
boolean valid = false;
int steps = 0;
//keep moving forward until the max step amount is reached
while(steps++ < maxSteps){
int tx = World.toTile(core.x + Tmp.v1.x), ty = World.toTile(core.y + Tmp.v1.y);
any = false;
Geometry.circle(tx, ty, world.width(), world.height(), 3, (x, y) -> {
if(world.solid(x, y)){
any = true;
}
});
//nothing is in the way, spawn it
if(!any){
valid = true;
break;
}else{
//make the vector longer
Tmp.v1.setLength(Tmp.v1.len() + tilesize*1.1f);
}
}
if(valid){
cons.accept(core.x + Tmp.v1.x, core.y + Tmp.v1.y, false);
}
}
}
}
private void eachFlyerSpawn(int filterPos, Floatc2 cons){
boolean airUseSpawns = state.rules.airUseSpawns;
for(Tile tile : spawns){
if(filterPos != -1 && filterPos != tile.pos()) continue;
if(!airUseSpawns){
float angle = Angles.angle(world.width() / 2f, world.height() / 2f, tile.x, tile.y);
float trns = Math.max(world.width(), world.height()) * Mathf.sqrt2 * tilesize;
float spawnX = Mathf.clamp(world.width() * tilesize / 2f + Angles.trnsx(angle, trns), -margin, world.width() * tilesize + margin);
float spawnY = Mathf.clamp(world.height() * tilesize / 2f + Angles.trnsy(angle, trns), -margin, world.height() * tilesize + margin);
cons.get(spawnX, spawnY);
}else{
cons.get(tile.worldx(), tile.worldy());
}
}
if(state.rules.attackMode && state.teams.isActive(state.rules.waveTeam)){
for(Building core : state.rules.waveTeam.data().cores){
if(filterPos != -1 && filterPos != core.pos()) continue;
cons.get(core.x, core.y);
}
}
}
public int countGroundSpawns(){
tmpCount = 0;
eachGroundSpawn((x, y) -> tmpCount ++);
return tmpCount;
}
public int countFlyerSpawns(){
tmpCount = 0;
eachFlyerSpawn(-1, (x, y) -> tmpCount ++);
return tmpCount;
}
public boolean isSpawning(){
return spawning && !net.client();
}
public void reset(){
spawning = false;
spawns.clear();
for(Tile tile : world.tiles){
if(tile.overlay() == Blocks.spawn){
spawns.add(tile);
}
}
}
/** Applies the standard wave spawn effects to a unit - invincibility, unmoving. */
public void spawnEffect(Unit unit){
spawnEffect(unit, unit.angleTo(world.width()/2f * tilesize, world.height()/2f * tilesize));
}
/** Applies the standard wave spawn effects to a unit - invincibility, unmoving. */
public void spawnEffect(Unit unit, float rotation){
unit.rotation = rotation;
unit.apply(StatusEffects.unmoving, 30f);
unit.apply(StatusEffects.invincible, 60f);
unit.add();
unit.unloaded();
Events.fire(new UnitSpawnEvent(unit));
Call.spawnEffect(unit.x, unit.y, unit.rotation, unit.type);
}
private interface SpawnConsumer{
void accept(float x, float y, boolean shockwave);
}
@Remote(called = Loc.server, unreliable = true)
public static void spawnEffect(float x, float y, float rotation, UnitType u){
Fx.unitSpawn.at(x, y, rotation, u);
Time.run(30f, () -> Fx.spawn.at(x, y));
}
}