Skip to content

Commit 8a2ba3e

Browse files
committed
1.5, finally stable and working...
1 parent 1df3e54 commit 8a2ba3e

File tree

4 files changed

+85
-80
lines changed

4 files changed

+85
-80
lines changed

plugin.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Xpdustry DominationPlugin",
44
"author": "Phinner",
55
"description": "Capture the zones to win!",
6-
"version": "1.4",
6+
"version": "1.5",
77
"minGameVersion": "105",
88
"hidden": true,
99
"java": true,

src/main/java/fr/xpdustry/domination/DominationMap.java

+31-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import arc.math.geom.*;
55
import arc.struct.*;
66
import arc.util.*;
7+
78
import mindustry.*;
89
import mindustry.content.*;
910
import mindustry.entities.*;
@@ -24,6 +25,10 @@ public class DominationMap implements Iterable<Zone>{
2425

2526
private static final Seq<Effect> effects = Seq.with(Fx.mine, Fx.mineBig, Fx.mineHuge);
2627

28+
public void update(){
29+
zones.forEach(z -> z.update(this));
30+
}
31+
2732
public int getZoneRadius(){
2833
return zoneRadius;
2934
}
@@ -64,6 +69,10 @@ public ArrayList<Zone> getZones(){
6469
return new ArrayList<>(zones);
6570
}
6671

72+
public int getZoneNumber(){
73+
return zones.size();
74+
}
75+
6776
public void addZone(Zone zone){
6877
zones.add(zone);
6978
}
@@ -87,24 +96,6 @@ public Zone getZone(int x, int y){
8796
return null;
8897
}
8998

90-
@Override
91-
public Iterator<Zone> iterator(){
92-
return zones.listIterator();
93-
}
94-
95-
@Override
96-
public void forEach(Consumer<? super Zone> action){
97-
Objects.requireNonNull(action);
98-
for(Zone zone : zones){
99-
action.accept(zone);
100-
}
101-
}
102-
103-
@Override
104-
public Spliterator<Zone> spliterator(){
105-
return zones.spliterator();
106-
}
107-
10899
public float getShowdownDuration(){
109100
return showdownDuration;
110101
}
@@ -146,21 +137,39 @@ public void drawZoneCenters(NetConnection con){
146137
});
147138
}
148139

149-
public void drawZoneTexts(){
140+
public void drawZoneTexts(float lifetime){
150141
zones.forEach(z -> {
151142
String percent = Strings.format("[#@]@%", z.getTeam().color, Strings.fixed(z.getPercent(), 0));
152-
Call.label(percent, 1.0F / 6, z.getX() * Vars.tilesize, z.getY() * Vars.tilesize);
143+
Call.label(percent, lifetime, z.getX() * Vars.tilesize, z.getY() * Vars.tilesize);
153144
});
154145
}
155146

156-
public void drawZoneTexts(NetConnection con){
147+
public void drawZoneTexts(NetConnection con, float lifetime){
157148
zones.forEach(z -> {
158149
String percent = Strings.format("[#@]@%", z.getTeam().color, Strings.fixed(z.getPercent(), 0));
159-
Call.label(con, percent, 1.0F / 6, z.getX() * Vars.tilesize, z.getY() * Vars.tilesize);
150+
Call.label(con, percent, lifetime, z.getX() * Vars.tilesize, z.getY() * Vars.tilesize);
160151
});
161152
}
162153

163154
public float[] createZoneCircle(){
164155
return Geometry.regPoly((int)(Mathf.pi * zoneRadius), zoneRadius);
165156
}
157+
158+
@Override
159+
public Iterator<Zone> iterator(){
160+
return zones.listIterator();
161+
}
162+
163+
@Override
164+
public void forEach(Consumer<? super Zone> action){
165+
Objects.requireNonNull(action);
166+
for(Zone zone : zones){
167+
action.accept(zone);
168+
}
169+
}
170+
171+
@Override
172+
public Spliterator<Zone> spliterator(){
173+
return zones.spliterator();
174+
}
166175
}

src/main/java/fr/xpdustry/domination/DominationPlugin.java

+51-53
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import arc.*;
44
import arc.files.*;
55
import arc.struct.*;
6-
import arc.struct.ObjectFloatMap.*;
6+
import arc.struct.ObjectMap.*;
77
import arc.util.*;
88

99
import mindustry.*;
@@ -19,27 +19,24 @@
1919
import com.google.gson.*;
2020
import com.google.gson.reflect.*;
2121

22-
2322
import java.util.*;
2423

2524

2625
@SuppressWarnings("unused")
2726
public class DominationPlugin extends Plugin{
2827
private static boolean showdown = false;
29-
private static final Interval interval = new Interval(3);
28+
private static final Interval timers = new Interval(3);
3029
private static final ObjectSet<Playerc> editors = new ObjectSet<>();
31-
32-
private static final ObjectFloatMap<Team> leaderboard = new ObjectFloatMap<>();
30+
private static final OrderedMap<Team, Float> leaderboard = new OrderedMap<>();
3331

3432
private static final Gson gson;
3533
private static final TreeMap<String, DominationMap> dominationMaps = new TreeMap<>();
3634
private static final Fi configFile = new Fi(Core.files.external("domination-config.json").absolutePath());
3735

3836
private static final int
39-
LOGIC_TIMER_SLOT = 0,
40-
COUNTDOWN_TIMER_SLOT = 1,
41-
GRAPHICS_TIMER_SLOT = 2;
42-
37+
LOGIC_TIMER = 0,
38+
COUNTDOWN_TIMER = 1,
39+
GRAPHICS_TIMER = 2;
4340

4441
static{
4542
gson = new GsonBuilder()
@@ -61,15 +58,14 @@ public void init(){
6158
// Main
6259
Events.run(Trigger.update, () -> {
6360
if(isActive()){
64-
if(interval.get(LOGIC_TIMER_SLOT, getCurrentMap().getUpdateTicks())){
65-
// Updates the zone internal data
66-
getCurrentMap().forEach(z -> z.update(getCurrentMap()));
61+
if(timers.get(LOGIC_TIMER, getCurrentMap().getUpdateTicks())){
62+
getCurrentMap().update();
6763
// Updates the leaderboard [team -> percent_captured]
6864
leaderboard.clear(Vars.state.teams.active.size + 1);
69-
getCurrentMap().forEach(z -> leaderboard.increment(z.getTeam(), 0, z.getPercent()));
65+
getCurrentMap().forEach(z -> leaderboard.put(z.getTeam(), leaderboard.get(z.getTeam(), 0F) + z.getPercent()));
7066
}
7167

72-
if(interval.get(COUNTDOWN_TIMER_SLOT, (showdown ? getCurrentMap().getShowdownDuration() : getCurrentMap().getGameDuration()) * Time.toMinutes)){
68+
if(timers.get(COUNTDOWN_TIMER, (showdown ? getCurrentMap().getShowdownDuration() : getCurrentMap().getGameDuration()) * Time.toMinutes)){
7369
List<Team> winners = getWinners();
7470
if(winners.size() == 0){
7571
Events.fire(new GameOverEvent(Team.derelict));
@@ -81,37 +77,33 @@ public void init(){
8177
}
8278
}
8379

84-
if(interval.get(GRAPHICS_TIMER_SLOT, Time.toSeconds / 6)){
80+
if(timers.get(GRAPHICS_TIMER, Time.toSeconds / 6)){
8581
if(isActive()){
8682
// HUD text
8783
StringBuilder builder = new StringBuilder(100);
8884

8985
getCurrentMap().drawZoneCircles();
90-
getCurrentMap().drawZoneTexts();
86+
getCurrentMap().drawZoneTexts(1F / 6);
9187

9288
// Time remaining
93-
int time = (int)(((showdown ? Time.toMinutes : getCurrentMap().getGameDuration() * Time.toMinutes) - interval.getTime(1)) / Time.toSeconds);
94-
builder.append(Strings.format("@Time remaining > @@", (showdown ? "[red]" : ""), Strings.formatMillis(time * 1000L), (showdown ? "[]" : "")));
95-
96-
// Unclaimed zones
97-
if(leaderboard.containsKey(Team.derelict)){
98-
builder.append(Strings.format("\n[#@]unclaimed[] > @%", Team.derelict.color, (int)leaderboard.get(Team.derelict, 0F)));
99-
}
89+
builder.append(showdown ? "[red]" : "");
90+
builder.append(Strings.format("Time remaining > @", Strings.formatMillis(getRemainingTime())));
91+
builder.append(showdown ? "[]" : "");
10092

10193
// Leaderboard
102-
Seq<Entry<Team>> orderedLeaderboard = Seq.with(leaderboard.entries()).sort(Comparator.comparing(a -> a.key));
103-
orderedLeaderboard.each(entry -> {
104-
if(entry.key != Team.derelict) builder.append(Strings.format("\n[#@]@[] > @%", entry.key.color, entry.key.name, (int)entry.value));
94+
leaderboard.each((team, percent) -> {
95+
String percentString = Strings.fixed(percent / getCurrentMap().getZoneNumber(), 2);
96+
builder.append(Strings.format("\n[#@]@[] > @%", team.color, team == Team.derelict ? "unclaimed" : team.name, percentString));
10597
});
10698

10799
Call.setHudText(builder.toString());
108100
}
109101

110102
// Rendering for editors
111103
editors.each(p -> {
112-
getCurrentMap().drawZoneCenters(p.con());
113-
if(!Vars.state.rules.pvp && Vars.state.isGame()){
114-
getCurrentMap().drawZoneCircles(p.con());
104+
if(Vars.state.isGame()){
105+
getCurrentMap().drawZoneCenters(p.con());
106+
if(!isActive()) getCurrentMap().drawZoneCircles(p.con());
115107
}
116108
});
117109
}
@@ -172,7 +164,7 @@ public void registerClientCommands(CommandHandler handler){
172164
player.sendMessage("You disabled editor mode, how unfortunate...");
173165
break;
174166

175-
default: player.sendMessage(Strings.format("'@' is not a valid option.", args[0]));
167+
default: player.sendMessage(Strings.format("'@' is not a valid option.", args[0]));
176168
}
177169
});
178170
}
@@ -192,8 +184,28 @@ public static void saveDominationMaps(){
192184
Log.info("Domination maps have been saved.");
193185
}
194186

195-
public static boolean isActive(){
196-
return Vars.state.rules.pvp && !Vars.state.isMenu() && !Vars.state.gameOver;
187+
public static void resetGameCountdown(){
188+
timers.reset(COUNTDOWN_TIMER, 0);
189+
}
190+
191+
public static void triggerShowdown(List<Team> teams){
192+
showdown = true;
193+
Call.sendMessage("[red]SHOWDOWN[].");
194+
resetGameCountdown();
195+
196+
for(TeamData data : Vars.state.teams.getActive()){
197+
if(!teams.contains(data.team)){
198+
data.cores.each(CoreBuild::kill);
199+
Groups.player.each(p -> {
200+
if(p.team() == data.team){
201+
p.team(Team.derelict);
202+
p.unit().kill();
203+
}
204+
});
205+
206+
Call.sendMessage(Strings.format("Team [#@]@[] has been reduced to ashes...", data.team.color, data.team.name));
207+
}
208+
}
197209
}
198210

199211
@Nullable
@@ -209,7 +221,7 @@ public static List<Team> getWinners(){
209221
float maxPercent = 0F;
210222
List<Team> winners = new ArrayList<>();
211223

212-
for(Entry<Team> entry : leaderboard.entries()){
224+
for(Entry<Team, Float> entry : leaderboard.entries()){
213225
if(entry.key == Team.derelict) continue;
214226

215227
if(entry.value > maxPercent){
@@ -224,27 +236,13 @@ public static List<Team> getWinners(){
224236
return winners;
225237
}
226238

227-
public static void resetGameCountdown(){
228-
interval.reset(COUNTDOWN_TIMER_SLOT, 0);
239+
/** Returns the remaining time in milliseconds */
240+
public static long getRemainingTime(){
241+
float remainingTicks = ((showdown ? getCurrentMap().getShowdownDuration() : getCurrentMap().getGameDuration()) * Time.toMinutes) - timers.getTime(COUNTDOWN_TIMER);
242+
return ((long)(remainingTicks / Time.toSeconds)) * 1000L;
229243
}
230244

231-
public static void triggerShowdown(List<Team> teams){
232-
showdown = true;
233-
Call.sendMessage("[red]SHOWDOWN[].");
234-
resetGameCountdown();
235-
236-
for(TeamData data : Vars.state.teams.getActive()){
237-
if(!teams.contains(data.team)){
238-
data.cores.each(CoreBuild::kill);
239-
Groups.player.each(p -> {
240-
if(p.team() == data.team){
241-
p.team(Team.derelict);
242-
p.unit().kill();
243-
}
244-
});
245-
246-
Call.sendMessage(Strings.format("Team [#@]@[] has been reduced to ashes...", data.team.color, data.team.name));
247-
}
248-
}
245+
public static boolean isActive(){
246+
return Vars.state.rules.pvp && !Vars.state.isMenu() && !Vars.state.gameOver;
249247
}
250248
}

src/main/java/fr/xpdustry/domination/Zone.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void update(DominationMap map){
5252
for(Entry<Team> entry : players){
5353
if(entry.value > maxPlayers){
5454
winner = entry.key;
55+
maxPlayers = entry.value;
5556
}else if(entry.value == maxPlayers){
5657
// If 2 teams have the same number of players, don't update so set back to derelict.
5758
winner = Team.derelict;
@@ -106,10 +107,7 @@ public Zone read(JsonReader reader) throws IOException{
106107
coords[i] = coords[i].trim();
107108
}
108109

109-
return new Zone(
110-
Integer.parseInt(coords[0]),
111-
Integer.parseInt(coords[1])
112-
);
110+
return new Zone(Integer.parseInt(coords[0]), Integer.parseInt(coords[1]));
113111
}
114112
}
115113
}

0 commit comments

Comments
 (0)