Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/aehmttw/tanks
Browse files Browse the repository at this point in the history
  • Loading branch information
aehmttw committed Feb 4, 2025
2 parents b1dbba8 + 13421ff commit 1afc068
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ To develop the game we suggest using Eclipse or IntelliJ. Eclipse can be install

To access the source code you also need to install Git.<br>

### Gradle Build (Recommended)
It is recommended you use gradle to build Tanks using the following commands:

#### Windows
```powershell
./gradlew.bat clean build
```

#### Unix (OSX, Linux, etc.)
```bash
./gradlew clean build
```

### IDE Build
You will need to add the following libraries to the build path of the project: <br>
PNGDecoder by Matthias Mann in TWL<br>
Netty<br>
Expand Down Expand Up @@ -80,6 +94,7 @@ Pythonmcpi<br>
Cool TM<br>
QazCetelic<br>
Lancelot<br>
Parth Iyer (ghostlypi)<br>

### Supporters:
SapphireDrew<br>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import tanks.rendering.ShaderGroundOutOfBounds;
import tanks.rendering.ShaderTracks;
import tanks.tank.*;
import tanks.tankson.Compatibility;

import java.io.*;
import java.util.*;
Expand Down Expand Up @@ -519,6 +520,7 @@ public static void initScript()
Drawing.initialize();
Panel.initialize();
Game.exitToTitle();
Compatibility.init();

Hotbar.toggle = new Button(Drawing.drawing.interfaceSizeX / 2, Drawing.drawing.interfaceSizeY - 20, 150, 40, "", () -> Game.player.hotbar.persistent = !Game.player.hotbar.persistent);

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/tanks/tankson/Compatibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tanks.tankson;

import tanks.tank.Explosion;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.function.Function;

public class Compatibility {
public static final HashMap<String, Function<Object, Object>> compatibility_table = new HashMap<>();
public static final HashMap<String, String> field_table = new HashMap<>();

public static void init() {
compatibility_table.put("explode_on_destroy", Compatibility::updateExplosion);
}

public static Object convert(Field f, Object v) {
return compatibility_table.get(Serializer.getid(f)).apply(v);
}

public static String convert(String f) {
return field_table.get(f);
}

// Explosions 1.6.b -> 1.6.c
public static Object updateExplosion(Object o) {
if (o instanceof Boolean) {
if ((Boolean) o) {
return new Explosion();
} else {
return null;
}
} else {
throw new RuntimeException("Update Explosions from 1.6.b --> 1.6.c");
}
}
}
42 changes: 37 additions & 5 deletions src/main/java/tanks/tankson/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import tanks.Game;
import tanks.bullet.Bullet;
import tanks.item.Item;
import tanks.item.ItemBullet;
import tanks.item.ItemEmpty;
import tanks.tank.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;

Expand Down Expand Up @@ -187,6 +184,8 @@ public static Object parseObject(Map<String, Object> m)
if (m == null)
return null;
Object o = null;
Set<String> processed = new HashSet<>();
processed.add("obj_type");
switch ((String) m.get("obj_type"))
{
case "tank":
Expand All @@ -199,6 +198,7 @@ public static Object parseObject(Map<String, Object> m)
{
try
{
processed.add("bullet_type");
o = Game.registryBullet.getEntry((String) m.get("bullet_type")).bullet.newInstance();
}
catch (Exception e)
Expand All @@ -214,6 +214,7 @@ public static Object parseObject(Map<String, Object> m)
{
try
{
processed.add("item_type");
o = Game.registryItem.getEntry((String) m.get("item_type")).item.getConstructor().newInstance();
}
catch (Exception e)
Expand All @@ -224,6 +225,7 @@ public static Object parseObject(Map<String, Object> m)
}
case "item_stack":
{
processed.add("item");
Item i = (Item) parseObject((Map) m.get("item"));
o = (i.getStack(null));
break;
Expand All @@ -244,25 +246,35 @@ public static Object parseObject(Map<String, Object> m)
o = new Explosion();
break;
case "spawned_tank":
processed.add("tank");
processed.add("weight");
o = new TankAIControlled.SpawnedTankEntry((ITankField) parseObject((Map) m.get("tank")), (Double) m.get("weight"));
break;
case "tank_ref":
case "tank_ref": {
processed.add("tank");
o = new TankReference((String) m.get("tank"));
break;
}
default:
throw new RuntimeException("Bad object type: " + (String) m.get("obj_type"));
}

for (Field f : o.getClass().getFields())
{
if (f.isAnnotationPresent(Property.class) && m.containsKey(getid(f)))
{
processed.add(getid(f));
try
{
Object o2 = f.get(o);
if (isTanksONable(f))
{
Object o3 = m.get(getid(f));
f.set(o, parseObject((Map) o3));
try {
f.set(o, parseObject((Map<String, Object>) o3));
} catch (ClassCastException e) {
f.set(o, Compatibility.convert(f, o3));
}
}
else if (o2 instanceof ArrayList)
{
Expand Down Expand Up @@ -294,10 +306,30 @@ else if (o2 instanceof Boolean)
}
catch (Exception e)
{
System.out.println(getid(f));
throw new RuntimeException(e);
}
}
}

Set<String> unused = new HashSet<>(m.keySet());
unused.removeAll(processed);
for (String k : unused) {
try {
o.getClass().getField(Compatibility.convert(k)).set(o, m.get(k));
} catch (ClassCastException e) {
try {
Field f = o.getClass().getField(Compatibility.convert(k));
f.set(o, Compatibility.convert(f, m.get(k)));
} catch (NoSuchFieldException | IllegalAccessException f) {
throw new RuntimeException(f);
}
} catch (NoSuchFieldException | NullPointerException | IllegalAccessException e) {
System.out.println("Unconvertable field found!");
}
}


return o;
}

Expand Down

0 comments on commit 1afc068

Please sign in to comment.