Skip to content

Commit f45a011

Browse files
authored
Merge pull request #24 from RKN-bot-discord/main
Big think
2 parents 316d3b4 + 3a97724 commit f45a011

10 files changed

Lines changed: 302 additions & 87 deletions

File tree

src/main/java/castle/CastleCommandAI.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public void updateUnit() {
1616
if (!hasCommand() && onEnemySide(unit)) {
1717
target = attackTarget = unit.closestEnemyCore();
1818

19-
if (unit.type.flying) moveTo(target, unit.type.range * 0.8f);
19+
if (unit.type.flying){
20+
if (unit.health>unit.type.health*0.5) moveTo(target, unit.type.range * 0.8f);
21+
else moveTo(target, 0f);
22+
}
2023
else pathfind(Pathfinder.fieldCore);
2124

2225
if (!invalid(target) && unit.type.circleTarget)

src/main/java/castle/CastleGenerator.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import arc.math.Mathf;
55
import arc.math.geom.Point2;
66
import arc.struct.Seq;
7+
import arc.util.Time;
78
import mindustry.content.*;
89
import mindustry.game.Team;
910
import mindustry.gen.Call;
@@ -14,15 +15,23 @@
1415
import mindustry.world.blocks.distribution.Sorter.SorterBuild;
1516
import mindustry.world.blocks.environment.SpawnBlock;
1617
import mindustry.world.blocks.storage.CoreBlock;
17-
import useful.Bundle;
18-
18+
import mindustry.world.blocks.logic.LogicBlock.LogicBuild;
19+
import mindustry.world.blocks.logic.LogicBlock.LogicLink;
20+
import static mindustry.Vars.*;
21+
import castle.CastleRooms.BlockRoom;
22+
import castle.CastleRooms.EffectRoom;
23+
import castle.CastleRooms.MinerRoom;
24+
import castle.CastleRooms.Room;
25+
import castle.CastleRooms.UnitRoom;
1926
import static castle.CastleRooms.*;
2027
import static castle.CastleUtils.drill;
2128
import static castle.CastleUtils.refreshMeta;
2229
import static castle.CastleUtils.revealedUnits;
2330
import static castle.CastleUtils.shopFloor;
2431
import static castle.Main.*;
25-
import static mindustry.Vars.*;
32+
import useful.Bundle;
33+
34+
2635

2736
public class CastleGenerator {
2837
public static final int unitLimitX = 5, unitLimitY = 3, effectLimitX = 4;
@@ -95,6 +104,38 @@ public static void generate() {
95104
() -> new MinerRoom(drill, sorter.config(), CastleCosts.items.get(sorter.config())));
96105
}
97106

107+
if (tile.build instanceof LogicBuild logicBlock) {
108+
109+
var code = logicBlock.code;
110+
var linksProcessor = logicBlock.links.copy();
111+
var tileEdit = world.tile(x, y);
112+
var tileNew = world.tile(x, world.height() - y);
113+
114+
tileEdit.setNet(tile.block(), tile.build.team(), 0);
115+
tileNew.setNet(tile.block(), Team.blue, 180);
116+
117+
int yCoordinate = (int) tile.build.getY()/8;
118+
Seq<LogicLink> mirroredLinks = new Seq<>();
119+
120+
for (LogicLink link : logicBlock.links) {
121+
int xLink = link.x;
122+
int yLink = world.height() - yCoordinate -(link.y - yCoordinate);
123+
LogicLink mirrored = new LogicLink(xLink, yLink, link.name, link.valid);
124+
mirroredLinks.add(mirrored);
125+
}
126+
127+
if (tileEdit.build instanceof LogicBuild logicBlockEdit) {
128+
logicBlockEdit.updateCode(code);
129+
logicBlockEdit.links.set(linksProcessor);
130+
logicBlockEdit.updateTile();
131+
}
132+
if (tileNew.build instanceof LogicBuild newLogicBlock) {
133+
newLogicBlock.updateCode(code);
134+
newLogicBlock.links.set(mirroredLinks);
135+
newLogicBlock.updateTile();
136+
}
137+
}
138+
98139
if (tile.overlay() instanceof SpawnBlock)
99140
spawns.add(x, y);
100141
});
@@ -210,7 +251,7 @@ public void spawn(Player summonner, Team team, UnitType type) {
210251
spawn = get(team).cpy().add(Mathf.range(tilesize), Mathf.range(tilesize));
211252
} while (!validFor(type, spawn));
212253
var prevLimit = state.rules.unitCap;
213-
state.rules.unitCap = Integer.MAX_VALUE;
254+
state.rules.unitCap = 500;
214255
var unit = type.spawn(team, spawn.x * tilesize, spawn.y * tilesize);
215256
state.rules.unitCap = prevLimit;
216257
Bundle.label(1f, unit.getX(), unit.getY(), "rooms.unit.bought", summonner.coloredName());

src/main/java/castle/CastleRooms.java

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
package castle;
22

3+
import arc.math.geom.Point2;
4+
import arc.struct.Seq;
35
import arc.math.Mathf;
46
import arc.util.*;
57
import castle.CastleCosts.*;
68
import mindustry.Vars;
79
import mindustry.content.*;
10+
import mindustry.world.*;
811
import mindustry.game.Team;
912
import mindustry.gen.*;
1013
import mindustry.type.*;
1114
import mindustry.world.*;
15+
import mindustry.gen.Player;
16+
import mindustry.type.UnitType;
1217
import mindustry.world.blocks.ConstructBlock;
1318
import mindustry.world.blocks.storage.CoreBlock;
14-
import useful.Bundle;
19+
import useful.Bundle;
20+
import mindustry.Vars;
1521

1622
import static castle.CastleUtils.boatSpawnX;
1723
import static castle.CastleUtils.boatSpawnY;
24+
import static castle.CastleUtils.landSpawnX;
25+
import static castle.CastleUtils.landSpawnY;
26+
import static castle.CastleUtils.airSpawnX;
27+
import static castle.CastleUtils.airSpawnY;
1828
import static castle.CastleUtils.generatePlatforms;
1929
import static castle.CastleUtils.platformSource;
30+
import static castle.CastleUtils.defenseCap;
31+
import static castle.CastleUtils.attackCap;
2032
import static castle.Main.*;
2133

2234
public class CastleRooms {
@@ -180,44 +192,80 @@ public UnitRoom(UnitType type, UnitData data, boolean attack) {
180192
this.income = attack ? data.income() : -data.income();
181193
}
182194

195+
private boolean validFor(UnitType type, int x,int y) {
196+
197+
var tile = Vars.world.tile(x, y);
198+
if (tile == null ||
199+
(!type.flying && !tile.block().isAir()) ||
200+
(type.naval && !tile.floor().isLiquid) )
201+
return false;
202+
return true;
203+
}
204+
205+
private void spawnUnit(PlayerData data, float x, float y, UnitType type, boolean core_spawn) {
206+
Unit unit = null;
207+
var i = 0;
208+
var y_coordinate = 0.0;
209+
if (core_spawn == true){
210+
y_coordinate = Math.round((data.team().team == Team.blue ? Vars.world.height() - y : y) * 8 + Mathf.range(48f));
211+
}
212+
else{
213+
y_coordinate = y + Mathf.range(48f);
214+
}
215+
while(i < 10 && !validFor(type,Math.round((int)x),Math.round((int)y_coordinate/8))){
216+
if (core_spawn == true){
217+
y_coordinate = Math.round((data.team().team == Team.blue ? Vars.world.height() - y : y) * 8 + Mathf.range(48f));
218+
}
219+
else{
220+
y_coordinate = y + Mathf.range(48f);
221+
}
222+
i++;
223+
}
224+
unit = type.spawn(
225+
data.player.team(),
226+
x * 8 + 48f,Math.round(y_coordinate));
227+
Bundle.label(1f, unit.getX(), unit.getY(), "rooms.unit.bought", data.player.coloredName());
228+
}
229+
230+
183231
@Override
184232
public void buy(PlayerData data) {
185233
super.buy(data);
186234
data.income += income;
187235

188236
if (attack) spawns.spawn(data.player, data.player.team(), type);
189237
else if ((boatSpawnX > 0 && boatSpawnY > 0) && type.naval) {
190-
var prevLimit = Vars.state.rules.unitCap;
191-
Vars.state.rules.unitCap = Integer.MAX_VALUE;
192-
var unit = type.spawn(
193-
data.player.team(),
194-
boatSpawnX * 8f,
195-
(data.team().team == Team.blue ? Vars.world.height() - boatSpawnY : boatSpawnY) * 8f + Mathf.range(48f));
196-
Bundle.label(1f, unit.getX(), unit.getY(), "rooms.unit.bought", data.player.coloredName());
197-
Vars.state.rules.unitCap = prevLimit;
238+
spawnUnit(data, boatSpawnX, boatSpawnY, type, true);
239+
}
240+
else if ((landSpawnX > 0 && landSpawnY > 0) && !type.naval && !type.flying) {
241+
spawnUnit(data, landSpawnX, landSpawnY, type, true);
242+
}
243+
else if ((airSpawnX > 0 && airSpawnY > 0) && type.flying) {
244+
spawnUnit(data, airSpawnX, airSpawnY, type, true);
198245
}
199246
else if (data.player.core() != null) {
200247
var core = data.player.core();
201-
var prevLimit = Vars.state.rules.unitCap;
202-
Vars.state.rules.unitCap = Integer.MAX_VALUE;
203-
var unit = type.spawn(data.player.team(), core.x + 48f, core.y + Mathf.range(48f));
204-
Bundle.label(1f, unit.getX(), unit.getY(), "rooms.unit.bought", data.player.coloredName());
205-
Vars.state.rules.unitCap = prevLimit;
206-
}
248+
spawnUnit(data, core.x/8, core.y, type, false);
249+
};
207250
}
208251

209252
@Override
210253
public boolean canBuy(PlayerData data) {
211254
if (!super.canBuy(data)) return false;
212-
213-
if (data.team().getUnitCount() >= Vars.state.rules.unitCap) {
214-
Bundle.announce(data.player, "rooms.unit.limit");
215-
return false;
255+
if (attack){
256+
if(data.team().getUnitCountAttack()>=attackCap) {
257+
Bundle.announce(data.player, "rooms.unit.limit");
258+
return false;
259+
}
260+
}
261+
else{
262+
if(data.team().getUnitCountDefense()>=defenseCap){
263+
Bundle.announce(data.player, "rooms.unit.limit");
264+
return false;
265+
}
216266
}
217-
218267
return true;
219268
}
220-
221269
@Override
222270
public String toString() {
223271
return type.emoji() + " " + (attack ? "[accent]\uE865" : "[scarlet]\uE84D") +

src/main/java/castle/CastleUtils.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package castle;
22

3+
import arc.Core;
34
import mindustry.Vars;
45
import mindustry.content.Blocks;
56
import mindustry.content.Items;
67
import mindustry.content.Planets;
78
import mindustry.game.*;
89
import mindustry.game.MapObjectives.FlagObjective;
910
import mindustry.gen.Teamc;
11+
import mindustry.gen.Building;
1012
import mindustry.type.Item;
1113
import mindustry.type.UnitType;
1214
import mindustry.type.unit.ErekirUnitType;
@@ -20,9 +22,13 @@
2022
import mindustry.world.blocks.units.UnitFactory;
2123
import mindustry.world.meta.BlockGroup;
2224
import mindustry.world.meta.Env;
25+
import mindustry.gen.Call;
26+
import arc.util.io.Writes;
2327

2428
import static mindustry.Vars.*;
2529

30+
import java.io.DataOutputStream;
31+
import arc.util.io.ReusableByteOutStream;
2632
import arc.struct.Seq;
2733
import arc.util.Log;
2834
import arc.util.Nullable;
@@ -39,6 +45,8 @@ public class CastleUtils {
3945
public static short landSpawnY = -1;
4046
public static short airSpawnX = -1;
4147
public static short airSpawnY = -1;
48+
public static short defenseCap = 150;
49+
public static short attackCap = 350;
4250

4351
public static boolean any(String[] array, String value) {
4452
for (var test : array)
@@ -59,8 +67,6 @@ public static void refreshMeta() {
5967
generatePlatforms = true;
6068
platformSource.clear();
6169
shopFloor = Blocks.space.asFloor();
62-
boatSpawnX = -1;
63-
boatSpawnY = -1;
6470

6571
for (var objective : state.rules.objectives.all) {
6672
if (objective instanceof FlagObjective flag) {
@@ -126,6 +132,24 @@ public static void refreshMeta() {
126132
airSpawnY = -1;
127133
}
128134
}
135+
if (flag.flag.startsWith("defenseCap ")) {
136+
try {
137+
String[] args = flag.flag.split(" ");
138+
defenseCap = Short.valueOf(args[1]);
139+
} catch (Exception error) {
140+
Log.warn("Failed to set Defense Cap!\n" + error);
141+
defenseCap = 150;
142+
}
143+
}
144+
if (flag.flag.startsWith("attackCap ")) {
145+
try {
146+
String[] args = flag.flag.split(" ");
147+
attackCap = Short.valueOf(args[1]);
148+
} catch (Exception error) {
149+
Log.warn("Failed to set Attack Cap!\n" + error);
150+
attackCap = 350;
151+
}
152+
}
129153
}
130154
}
131155

@@ -137,6 +161,26 @@ public static void refreshMeta() {
137161
}
138162
}
139163

164+
public static void syncBlock(Building block_sync, ReusableByteOutStream syncStream, DataOutputStream dataStream){
165+
try{
166+
final Building block = block_sync;
167+
Core.app.post(() -> {
168+
try {
169+
syncStream.reset();
170+
dataStream.writeInt(block.pos());
171+
dataStream.writeShort(block.block().id);
172+
block.writeAll(Writes.get(dataStream));
173+
dataStream.close();
174+
Call.blockSnapshot((short) 1, syncStream.toByteArray());
175+
syncStream.reset();
176+
} catch (Exception ohshit) {
177+
throw new RuntimeException(ohshit);
178+
}});
179+
}catch(Exception e){
180+
e.printStackTrace();
181+
}
182+
}
183+
140184
public static void applyRules(Rules rules) {
141185
rules.waveTimer = rules.waves = rules.waveSending = false;
142186
rules.pvp = rules.attackMode = rules.polygonCoreProtection = true;

0 commit comments

Comments
 (0)