Skip to content

Commit 40d10f7

Browse files
committed
feat: Port CN tower defense to MOMO
1 parent dd3b6a8 commit 40d10f7

15 files changed

+897
-6
lines changed

build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies {
4646
compileOnlyApi(libs.jspecify)
4747
annotationProcessor(libs.nullaway)
4848
errorprone(libs.errorprone.core)
49+
implementation(libs.configurate.yaml)
4950
}
5051

5152
signing {
@@ -142,5 +143,5 @@ val downloadDistributorCommon by tasks.registering(GithubAssetDownload::class) {
142143
}
143144

144145
tasks.runMindustryServer {
145-
mods.from(downloadSlf4md, downloadDistributorCommon)
146+
mods.from(downloadSlf4md, files("libs/distributor-common.jar"))
146147
}

gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ errorprone-gradle = "4.1.0"
99
# utilities
1010
slf4md = "1.0.1"
1111
distributor = "4.2.0-SNAPSHOT"
12+
configurate = "4.1.2"
1213

1314
# static analysis
1415
errorprone-core = "2.36.0"
@@ -18,6 +19,7 @@ jspecify = "1.0.0"
1819
[libraries]
1920
# utilities
2021
distributor-api = { module = "com.xpdustry:distributor-common-api", version.ref = "distributor" }
22+
configurate-yaml = { module = "org.spongepowered:configurate-yaml", version.ref = "configurate" }
2123

2224
# static analysis
2325
nullaway = { module = "com.uber.nullaway:nullaway", version.ref = "nullaway" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This file is part of MOMO. A plugin providing more gamemodes for Mindustry servers.
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) 2024 Xpdustry
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.xpdustry.momo;
27+
28+
public enum MoGameMode {
29+
TOWER_DEFENSE
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of MOMO. A plugin providing more gamemodes for Mindustry servers.
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) 2024 Xpdustry
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.xpdustry.momo;
27+
28+
import mindustry.Vars;
29+
30+
public interface MoMoAPI {
31+
32+
static MoMoAPI get() {
33+
return (MoMoAPI) Vars.mods.getMod(MoMoPlugin.class).main;
34+
}
35+
36+
boolean isActive(final MoGameMode mode);
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of MOMO. A plugin providing more gamemodes for Mindustry servers.
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) 2024 Xpdustry
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.xpdustry.momo;
27+
28+
import com.xpdustry.momo.tower.TowerConfig;
29+
import org.jspecify.annotations.Nullable;
30+
31+
public record MoMoConfig(@Nullable MoGameMode mode, TowerConfig tower) {}

src/main/java/com/xpdustry/momo/MoMoPlugin.java

+122-5
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,131 @@
2525
*/
2626
package com.xpdustry.momo;
2727

28-
import arc.util.Log;
29-
import mindustry.mod.Plugin;
28+
import com.xpdustry.distributor.api.annotation.PluginAnnotationProcessor;
29+
import com.xpdustry.distributor.api.plugin.AbstractMindustryPlugin;
30+
import com.xpdustry.distributor.api.plugin.PluginListener;
31+
import com.xpdustry.momo.tower.TowerConfig;
32+
import com.xpdustry.momo.tower.TowerDrop;
33+
import com.xpdustry.momo.tower.TowerLogic;
34+
import java.io.IOException;
35+
import java.nio.file.Files;
36+
import java.util.List;
37+
import java.util.Map;
38+
import java.util.Objects;
39+
import java.util.stream.Collectors;
40+
import mindustry.Vars;
41+
import mindustry.ctype.ContentType;
42+
import mindustry.type.Item;
43+
import mindustry.type.UnitType;
44+
import org.jspecify.annotations.Nullable;
45+
import org.spongepowered.configurate.ConfigurationNode;
46+
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
3047

3148
@SuppressWarnings("unused")
32-
public final class MoMoPlugin extends Plugin {
49+
public final class MoMoPlugin extends AbstractMindustryPlugin implements MoMoAPI {
50+
51+
private final PluginAnnotationProcessor<List<Object>> processor = PluginAnnotationProcessor.compose(
52+
PluginAnnotationProcessor.events(this),
53+
PluginAnnotationProcessor.tasks(this),
54+
PluginAnnotationProcessor.triggers(this),
55+
PluginAnnotationProcessor.playerActions(this));
56+
57+
private @Nullable MoMoConfig config;
58+
59+
@Override
60+
public void onInit() {
61+
try {
62+
this.config = load();
63+
} catch (final Exception e) {
64+
throw new RuntimeException("Failed to load MoMo", e);
65+
}
66+
this.addListener(new TowerLogic(this));
67+
}
3368

3469
@Override
35-
public void init() {
36-
Log.info("Hello MOMO");
70+
public void addListener(final PluginListener listener) {
71+
super.addListener(listener);
72+
processor.process(listener);
73+
}
74+
75+
@Override
76+
public boolean isActive(final MoGameMode mode) {
77+
return getMoConfig().mode() == mode;
78+
}
79+
80+
public MoMoConfig getMoConfig() {
81+
return Objects.requireNonNull(config);
82+
}
83+
84+
private MoMoConfig load() throws IOException {
85+
final var file = this.getDirectory().resolve("config.yaml");
86+
if (Files.notExists(file)) {
87+
try (final var stream = Objects.requireNonNull(
88+
this.getClass().getClassLoader().getResourceAsStream("com/xpdustry/momo/config.yaml"))) {
89+
Files.copy(stream, file);
90+
}
91+
}
92+
final var node = YamlConfigurationLoader.builder().path(file).build().load();
93+
94+
final TowerConfig tower;
95+
{
96+
final var root = node.node("tower-defense");
97+
final var drops = root.node("drops").childrenMap().entrySet().stream()
98+
.collect(Collectors.toUnmodifiableMap(
99+
entry -> entry.getKey().toString(), entry -> entry.getValue().childrenList().stream()
100+
.map(this::parseDrop)
101+
.toList()));
102+
final var units = root.node("units").childrenMap().entrySet().stream()
103+
.collect(Collectors.toUnmodifiableMap(
104+
entry -> {
105+
final var name = entry.getKey().toString();
106+
return Objects.requireNonNull(
107+
Vars.content.<UnitType>getByName(ContentType.unit, name),
108+
"Unknown unit " + name);
109+
},
110+
entry -> parseUnit(drops, entry.getValue())));
111+
tower = new TowerConfig(drops, units);
112+
}
113+
114+
MoGameMode mode = null;
115+
{
116+
final var root = node.node("game-mode");
117+
if (!root.virtual()) {
118+
final var name = Objects.requireNonNull(root.getString());
119+
mode = switch (name) {
120+
case "tower-defense" -> MoGameMode.TOWER_DEFENSE;
121+
default -> throw new IllegalArgumentException("Unknown gamemode " + name);};
122+
}
123+
}
124+
125+
return new MoMoConfig(mode, tower);
126+
}
127+
128+
private TowerDrop parseDrop(final ConfigurationNode node) {
129+
final var type = node.node("type").getString("simple");
130+
switch (type) {
131+
case "simple" -> {
132+
final var itemName =
133+
Objects.requireNonNull(node.node("item").getString(), "item field missing for " + node.path());
134+
final var item = Objects.requireNonNull(
135+
Vars.content.<Item>getByName(ContentType.item, itemName), "Unknown item " + itemName);
136+
final var amountNode = node.node("amount");
137+
if (amountNode.virtual()) throw new RuntimeException("The amount of item is missing");
138+
return new TowerDrop.Simple(item, amountNode.getInt());
139+
}
140+
case "random" -> {
141+
return new TowerDrop.Random(node.node("items").childrenList().stream()
142+
.map(this::parseDrop)
143+
.toList());
144+
}
145+
default -> throw new RuntimeException("Unknown bounty type: " + type);
146+
}
147+
}
148+
149+
private TowerConfig.UnitData parseUnit(final Map<String, List<TowerDrop>> drops, final ConfigurationNode node) {
150+
final var dropName =
151+
Objects.requireNonNull(node.node("drop").getString(), "drop field missing for " + node.path());
152+
final var drop = Objects.requireNonNull(drops.get(dropName), "Unknown drop bundle " + dropName);
153+
return new TowerConfig.UnitData(drop);
37154
}
38155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.xpdustry.momo;
3+
4+
import org.jspecify.annotations.NullMarked;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of MOMO. A plugin providing more gamemodes for Mindustry servers.
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) 2024 Xpdustry
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.xpdustry.momo.tower;
27+
28+
import mindustry.ai.types.GroundAI;
29+
import mindustry.entities.Units;
30+
import mindustry.gen.Teamc;
31+
import mindustry.world.blocks.storage.CoreBlock;
32+
import org.jspecify.annotations.Nullable;
33+
34+
final class GroundTowerAI extends GroundAI {
35+
36+
@Override
37+
public @Nullable Teamc target(
38+
final float x, final float y, final float range, final boolean air, final boolean ground) {
39+
return Units.closestTarget(
40+
this.unit.team(), x, y, range, $ -> false, build -> build.block() instanceof CoreBlock && ground);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of MOMO. A plugin providing more gamemodes for Mindustry servers.
3+
*
4+
* MIT License
5+
*
6+
* Copyright (c) 2024 Xpdustry
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
package com.xpdustry.momo.tower;
27+
28+
import java.util.List;
29+
import java.util.Map;
30+
import mindustry.type.UnitType;
31+
32+
public record TowerConfig(Map<String, List<TowerDrop>> drops, Map<UnitType, UnitData> units) {
33+
public record UnitData(List<TowerDrop> drops) {}
34+
}

0 commit comments

Comments
 (0)