Skip to content

Commit 7b195f9

Browse files
committed
feat: the files were missing, lol
1 parent 3469cb6 commit 7b195f9

16 files changed

+1727
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (C) 2025 The MegaMek Team. All Rights Reserved.
3+
*
4+
* This file is part of MegaMek.
5+
*
6+
*
7+
* MegaMek is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License (GPL),
9+
* version 3 or (at your option) any later version,
10+
* as published by the Free Software Foundation.
11+
*
12+
* MegaMek is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty
14+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
* See the GNU General Public License for more details.
16+
*
17+
* A copy of the GPL should have been included with this project;
18+
* if not, see <https://www.gnu.org/licenses/>.
19+
*
20+
* NOTICE: The MegaMek organization is a non-profit group of volunteers
21+
* creating free software for the BattleTech community.
22+
*
23+
* MechWarrior, BattleMech, `Mech and AeroTech are registered trademarks
24+
* of The Topps Company, Inc. All Rights Reserved.
25+
*
26+
* Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of
27+
* InMediaRes Productions, LLC.
28+
*/
29+
package megamek.ai.dataset;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
import megamek.common.Board;
35+
import megamek.common.Hex;
36+
37+
/**
38+
* Flexible container for board data using a map-based approach with enum keys.
39+
* @author Luana Coppio
40+
*/
41+
public class BoardData extends EntityDataMap<BoardData.Field> {
42+
43+
/**
44+
* Enum defining all available board data fields.
45+
*/
46+
public enum Field {
47+
BOARD_NAME,
48+
WIDTH,
49+
HEIGHT,
50+
HEX_DATA; // Special field that contains all hex data
51+
}
52+
53+
/**
54+
* Nested class to represent a single row of hex data
55+
*/
56+
public static class HexRow {
57+
private final int rowIndex;
58+
private final List<Hex> hexes;
59+
60+
public HexRow(int rowIndex, List<Hex> hexes) {
61+
this.rowIndex = rowIndex;
62+
this.hexes = new ArrayList<>(hexes);
63+
}
64+
65+
public int getRowIndex() {
66+
return rowIndex;
67+
}
68+
69+
public List<Hex> getHexes() {
70+
return new ArrayList<>(hexes);
71+
}
72+
}
73+
74+
/**
75+
* Creates an empty BoardData.
76+
*/
77+
public BoardData() {
78+
super(Field.class);
79+
}
80+
81+
/**
82+
* Creates a BoardData from a Board.
83+
* @param board The board to extract data from
84+
* @return A populated BoardData
85+
*/
86+
public static BoardData fromBoard(Board board) {
87+
BoardData data = new BoardData();
88+
89+
// Basic board information
90+
data.put(Field.BOARD_NAME, board.getMapName())
91+
.put(Field.WIDTH, board.getWidth())
92+
.put(Field.HEIGHT, board.getHeight());
93+
94+
// Extract hex data by row
95+
List<HexRow> hexRows = new ArrayList<>();
96+
for (int y = 0; y < board.getHeight(); y++) {
97+
List<Hex> rowHexes = new ArrayList<>();
98+
for (int x = 0; x < board.getWidth(); x++) {
99+
rowHexes.add(board.getHex(x, y));
100+
}
101+
hexRows.add(new HexRow(y, rowHexes));
102+
}
103+
104+
data.put(Field.HEX_DATA, hexRows);
105+
106+
return data;
107+
}
108+
109+
/**
110+
* Gets the board's hex rows.
111+
* @return List of HexRow objects containing the board's hexes
112+
*/
113+
@SuppressWarnings("unchecked")
114+
public List<HexRow> getHexRows() {
115+
return (List<HexRow>) get(Field.HEX_DATA);
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (C) 2025 The MegaMek Team. All Rights Reserved.
3+
*
4+
* This file is part of MegaMek.
5+
*
6+
*
7+
* MegaMek is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License (GPL),
9+
* version 3 or (at your option) any later version,
10+
* as published by the Free Software Foundation.
11+
*
12+
* MegaMek is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty
14+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
* See the GNU General Public License for more details.
16+
*
17+
* A copy of the GPL should have been included with this project;
18+
* if not, see <https://www.gnu.org/licenses/>.
19+
*
20+
* NOTICE: The MegaMek organization is a non-profit group of volunteers
21+
* creating free software for the BattleTech community.
22+
*
23+
* MechWarrior, BattleMech, `Mech and AeroTech are registered trademarks
24+
* of The Topps Company, Inc. All Rights Reserved.
25+
*
26+
* Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of
27+
* InMediaRes Productions, LLC.
28+
*/
29+
package megamek.ai.dataset;
30+
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
import java.util.stream.Collectors;
34+
import java.util.stream.IntStream;
35+
36+
import megamek.ai.dataset.BoardData.Field;
37+
import megamek.ai.dataset.BoardData.HexRow;
38+
39+
/**
40+
* <p>Serializer for BoardData to TSV format.</p>
41+
* @author Luana Coppio
42+
*/
43+
public class BoardDataSerializer extends EntityDataSerializer<Field, BoardData> {
44+
45+
public BoardDataSerializer() {
46+
super(Field.class);
47+
}
48+
49+
@Override
50+
public String serialize(BoardData data) {
51+
List<String> lines = new ArrayList<>();
52+
53+
// First line: Main board data
54+
lines.add(getHeaderLine());
55+
56+
String mainData = String.join("\t",
57+
String.valueOf(data.get(Field.BOARD_NAME)),
58+
String.valueOf(data.get(Field.WIDTH)),
59+
String.valueOf(data.get(Field.HEIGHT)));
60+
lines.add(mainData);
61+
62+
// Get the hex data for serialization
63+
List<HexRow> hexRows = data.getHexRows();
64+
if (hexRows != null && !hexRows.isEmpty()) {
65+
// Add the column header row (COL_0, COL_1, etc.)
66+
int width = ((Integer) data.get(Field.WIDTH));
67+
String colHeader = IntStream.range(0, width)
68+
.mapToObj(i -> "COL_" + i)
69+
.collect(Collectors.joining("\t"));
70+
lines.add(colHeader);
71+
72+
// Add each row of hex data
73+
for (HexRow row : hexRows) {
74+
StringBuilder sb = new StringBuilder("ROW_").append(row.getRowIndex()).append("\t");
75+
sb.append(row.getHexes().stream()
76+
.map(hex -> hex == null ? "" : hex.toString())
77+
.collect(Collectors.joining("\t")));
78+
lines.add(sb.toString());
79+
}
80+
}
81+
82+
return String.join("\n", lines);
83+
}
84+
85+
@Override
86+
public String getHeaderLine() {
87+
return String.join("\t", Field.BOARD_NAME.name(), Field.WIDTH.name(), Field.HEIGHT.name());
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (C) 2025 The MegaMek Team. All Rights Reserved.
3+
*
4+
* This file is part of MegaMek.
5+
*
6+
*
7+
* MegaMek is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License (GPL),
9+
* version 3 or (at your option) any later version,
10+
* as published by the Free Software Foundation.
11+
*
12+
* MegaMek is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty
14+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
* See the GNU General Public License for more details.
16+
*
17+
* A copy of the GPL should have been included with this project;
18+
* if not, see <https://www.gnu.org/licenses/>.
19+
*
20+
* NOTICE: The MegaMek organization is a non-profit group of volunteers
21+
* creating free software for the BattleTech community.
22+
*
23+
* MechWarrior, BattleMech, `Mech and AeroTech are registered trademarks
24+
* of The Topps Company, Inc. All Rights Reserved.
25+
*
26+
* Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of
27+
* InMediaRes Productions, LLC.
28+
*/
29+
package megamek.ai.dataset;
30+
31+
import java.util.ArrayList;
32+
import java.util.EnumMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
/**
37+
* Abstract base class for data maps that use enum fields.
38+
* @param <F> The enum type representing field names
39+
* @author Luana Coppio
40+
*/
41+
public abstract class EntityDataMap<F extends Enum<F>> {
42+
43+
// The class of the enum type F, used for creating EnumMap
44+
private final Class<F> fieldEnumClass;
45+
46+
// Use EnumMap for type safety with our enum fields
47+
private final Map<F, Object> data;
48+
49+
// Keep track of insertion order separately
50+
private final List<F> fieldOrder = new ArrayList<>();
51+
52+
/**
53+
* Creates an empty EntityDataMap.
54+
* @param fieldEnumClass The class of the enum type F
55+
*/
56+
protected EntityDataMap(Class<F> fieldEnumClass) {
57+
this.fieldEnumClass = fieldEnumClass;
58+
this.data = new EnumMap<>(fieldEnumClass);
59+
}
60+
61+
/**
62+
* Adds a field to the data map.
63+
* @param field The field enum
64+
* @param value The field value
65+
* @return This EntityDataMap for method chaining
66+
*/
67+
public EntityDataMap<F> put(F field, Object value) {
68+
if (!data.containsKey(field)) {
69+
fieldOrder.add(field);
70+
}
71+
data.put(field, value);
72+
return this;
73+
}
74+
75+
/**
76+
* Gets a field value from the data map.
77+
* @param field The field enum
78+
* @return The field value, or null if not present
79+
*/
80+
public Object get(F field) {
81+
return data.get(field);
82+
}
83+
84+
/**
85+
* Gets a field value with type casting.
86+
* @param <T> The expected type
87+
* @param field The field enum
88+
* @param type The class of the expected type
89+
* @return The field value cast to the expected type, or null if not present
90+
*/
91+
@SuppressWarnings("unchecked")
92+
public <T> T get(F field, Class<T> type) {
93+
Object value = data.get(field);
94+
if (type.isInstance(value)) {
95+
return (T) value;
96+
}
97+
return null;
98+
}
99+
100+
/**
101+
* Gets all fields and values in the map.
102+
* @return The underlying map of data
103+
*/
104+
public Map<F, Object> getAllFields() {
105+
return new EnumMap<>(data);
106+
}
107+
108+
/**
109+
* Gets the ordered list of field enums.
110+
* @return List of fields in insertion order
111+
*/
112+
public List<F> getFieldOrder() {
113+
return new ArrayList<>(fieldOrder);
114+
}
115+
116+
/**
117+
* Gets the class of the field enum.
118+
* @return The enum class
119+
*/
120+
public Class<F> getFieldEnumClass() {
121+
return fieldEnumClass;
122+
}
123+
}

0 commit comments

Comments
 (0)