Files
minecraft/android/tools/LogicCheck.java
T

194 lines
9.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import com.blockworld.game.Blocks;
import com.blockworld.game.Game;
import com.blockworld.game.World;
import java.io.File;
/** APK 里那份 Game/World/Blocks 的纯 JVM 自检(不依赖 Android,可在服务器上直接跑) */
public final class LogicCheck {
static int pass, fail;
static void ok(String name, boolean cond) {
if (cond) { pass++; System.out.println(" ✓ " + name); }
else { fail++; System.out.println(" ✗ " + name); }
}
public static void main(String[] a) {
File tmp = new File(System.getProperty("java.io.tmpdir"), "bw-check-" + System.nanoTime());
tmp.mkdirs();
World w = new World(12345);
System.out.println("地形");
int hMin = 99, hMax = 0;
for (int x = -40; x <= 40; x += 4) for (int z = -40; z <= 40; z += 4) {
int h = w.heightAt(x, z);
hMin = Math.min(hMin, h);
hMax = Math.max(hMax, h);
}
ok("高度在 3.." + (World.CY - 12) + " 之间(实测 " + hMin + ".." + hMax + ")", hMin >= 3 && hMax <= World.CY - 12);
ok("地形有起伏(落差 ≥ 6)", hMax - hMin >= 6);
System.out.println("区块与网格");
World.Chunk c0 = w.chunk(0, 0, true);
w.generate(c0);
ok("区块生成后有方块", c0.generated && countNonAir(c0) > 500);
ok("地表上方是空气", w.getBlock(3, w.heightAt(3, 3) + 6, 3) == Blocks.AIR);
ok("基岩不会缺", w.getBlock(3, 0, 3) != Blocks.AIR);
World.Mesh m = w.buildMesh(c0);
int tris = m.triangles();
int verts = 0;
for (World.Part p : m.solid) verts += p.vCount;
ok("网格有三角形(" + tris + " 个)", tris > 200);
ok("顶点数是 4 的倍数", verts % 4 == 0);
boolean uvOk = true, idxOk = true, outward = true;
for (World.Part p : m.solid) {
for (int i = 0; i < p.iCount; i++) if (p.idx[i] < 0 || p.idx[i] >= p.vCount) idxOk = false;
for (int i = 0; i < p.vCount; i++) {
float u = p.verts[i * 8 + 3], v = p.verts[i * 8 + 4];
if (u < 0 || u > 1 || v < 0 || v > 1) uvOk = false;
}
if (!facesOutward(p)) outward = false;
}
ok("索引都在范围内", idxOk);
ok("UV 都在 0..1(图集内缩)", uvOk);
ok("三角形法线朝外(整块地形抽样)", outward);
ok("孤立方块的 12 个三角形全部朝外", isolatedBlockOutward());
System.out.println("挖与放");
int gy = w.heightAt(8, 8);
int before = w.getBlock(8, gy, 8);
ok("地表是实心方块(" + Blocks.name(before) + ")", before != Blocks.AIR);
w.setBlock(8, gy, 8, Blocks.AIR);
ok("挖掉后变空气", w.getBlock(8, gy, 8) == Blocks.AIR);
ok("挖掉后区块被标脏(网格会重建)", c0.dirty);
w.setBlock(8, gy, 8, before);
ok("放回去恢复", w.getBlock(8, gy, 8) == before);
System.out.println("游戏逻辑");
Game g = new Game(tmp);
g.stream();
ok("玩家站在地表上(y=" + g.player.y + ")", g.player.y >= 1);
float y0 = g.player.y;
for (int i = 0; i < 60; i++) g.update(1f / 60f, 0, 0, false, false);
ok("站一会儿不会掉下去(y=" + g.player.y + ")", Math.abs(g.player.y - y0) < 1.2f);
float x0 = g.player.x, z0 = g.player.z;
for (int i = 0; i < 120; i++) g.update(1f / 60f, 0, 1, false, false);
float moved = (float) Math.hypot(g.player.x - x0, g.player.z - z0);
ok("往前走会动(位移 " + String.format("%.2f", moved) + ",撞到东西会停下)", moved > 0.5f);
g.look(0, 80); // 稍微低头,像真人那样看地面
g.update(0.016f, 0, 0, false, false);
boolean hit = g.target != null;
ok("准星能选中方块(" + (hit ? Blocks.name(g.target[3]) : "无") + ")", hit);
if (hit) {
int bx = g.target[0], by = g.target[1], bz = g.target[2];
// 先试放:低头看着地面,落点应该在脚前
g.look(0, 55); // 低头约 17°,落在身前几格
g.update(0.016f, 0, 0, false, false);
g.slot = 1; // 草方块
boolean placed = false;
if (g.placeAt != null) {
int px = g.placeAt[0], py = g.placeAt[1], pz = g.placeAt[2];
g.place();
placed = g.world.getBlock(px, py, pz) != Blocks.AIR;
}
ok("看着身前地面能放方块", placed);
// 再试挖
g.slot = 7; // 石镐
g.update(0.016f, 0, 0, false, false);
int dx2 = g.target != null ? g.target[0] : bx, dy2 = g.target != null ? g.target[1] : by,
dz2 = g.target != null ? g.target[2] : bz;
for (int i = 0; i < 40; i++) g.dig(0.05f);
ok("拿镐连挖能挖掉方块", g.world.getBlock(dx2, dy2, dz2) == Blocks.AIR);
}
System.out.println("生物");
ok("开局有生物(" + g.mobs.size() + " 只)", g.mobs.size() >= 8);
float mz = g.mobs.get(0).z;
for (int i = 0; i < 120; i++) g.update(1f / 60f, 0, 0, false, false);
ok("生物会自己走动", Math.abs(g.mobs.get(0).z - mz) > 0.05f || Math.abs(g.mobs.get(0).x) > 0);
ok("生物都在地面上方", g.mobs.get(0).y > 0);
System.out.println("存档");
int gx2 = 20, gz2 = 20, gy2 = g.world.heightAt(20, 20);
g.world.setBlock(gx2, gy2 + 1, gz2, Blocks.BRICK);
g.save();
Game g2 = new Game(tmp);
g2.stream();
ok("重开能读到同一个世界种子", g2.world.seed == g.world.seed);
ok("改动过的方块被记住(放了一块砖)", g2.world.getBlock(gx2, gy2 + 1, gz2) == Blocks.BRICK);
System.out.println("地图");
g.buildMap();
ok("地图生成完成", g.mapReady && g.mapPixels != null && g.mapPixels.length == g.mapSize * g.mapSize);
int distinct = 0;
java.util.HashSet<Integer> colors = new java.util.HashSet<>();
for (int px : g.mapPixels) colors.add(px);
distinct = colors.size();
ok("地图有地貌层次(" + distinct + " 种颜色)", distinct >= 3);
System.out.println();
System.out.println("通过 " + pass + " 项,失败 " + fail + " 项");
if (fail > 0) System.exit(1);
}
static int countNonAir(World.Chunk c) {
int n = 0;
for (int y = 0; y < World.CY; y++)
for (int z = 0; z < World.CZ; z++)
for (int x = 0; x < World.CX; x++)
if ((c.data[(y * World.CZ + z) * World.CX + x] & 0xff) != 0) n++;
return n;
}
/** 把 (8,50,8) 之外的方块全清掉,检查那块石头的 12 个三角形都朝外 */
static boolean isolatedBlockOutward() {
World w = new World(777);
World.Chunk c = w.chunk(0, 0, true);
w.generate(c);
for (int y = 0; y < World.CY; y++)
for (int z = 0; z < World.CZ; z++)
for (int x = 0; x < World.CX; x++) c.data[(y * World.CZ + z) * World.CX + x] = 0;
c.data[(50 * World.CZ + 8) * World.CX + 8] = (byte) Blocks.STONE;
c.dirty = true;
World.Mesh m = w.buildMesh(c);
int tris = m.triangles();
if (tris != 12) { System.out.println(" (孤立方块三角形数 " + tris + ",不是 12)"); }
for (World.Part p : m.solid) {
float[] v = p.verts;
for (int t = 0; t + 2 < p.iCount; t += 3) {
int i0 = p.idx[t], i1 = p.idx[t + 1], i2 = p.idx[t + 2];
float ax = v[i1 * 8] - v[i0 * 8], ay = v[i1 * 8 + 1] - v[i0 * 8 + 1], az = v[i1 * 8 + 2] - v[i0 * 8 + 2];
float bx = v[i2 * 8] - v[i0 * 8], by = v[i2 * 8 + 1] - v[i0 * 8 + 1], bz = v[i2 * 8 + 2] - v[i0 * 8 + 2];
float nx = ay * bz - az * by, ny = az * bx - ax * bz, nz = ax * by - ay * bx;
float cx = (v[i0 * 8] + v[i1 * 8] + v[i2 * 8]) / 3f - 8.5f;
float cy = (v[i0 * 8 + 1] + v[i1 * 8 + 1] + v[i2 * 8 + 1]) / 3f - 50.5f;
float cz = (v[i0 * 8 + 2] + v[i1 * 8 + 2] + v[i2 * 8 + 2]) / 3f - 8.5f;
if (nx * cx + ny * cy + nz * cz <= 0) return false;
}
}
return tris == 12;
}
/** 用第一个三角形的几何法线与「面到方块中心」的方向比一下 */
static boolean facesOutward(World.Part p) {
if (p.iCount < 6) return true;
float[] v = p.verts;
int i0 = p.idx[0], i1 = p.idx[1], i2 = p.idx[2];
float ax = v[i1 * 8] - v[i0 * 8], ay = v[i1 * 8 + 1] - v[i0 * 8 + 1], az = v[i1 * 8 + 2] - v[i0 * 8 + 2];
float bx = v[i2 * 8] - v[i0 * 8], by = v[i2 * 8 + 1] - v[i0 * 8 + 1], bz = v[i2 * 8 + 2] - v[i0 * 8 + 2];
float nx = ay * bz - az * by, ny = az * bx - ax * bz, nz = ax * by - ay * bx;
float cx = (v[i0 * 8] + v[i1 * 8] + v[i2 * 8]) / 3f;
float cy = (v[i0 * 8 + 1] + v[i1 * 8 + 1] + v[i2 * 8 + 1]) / 3f;
float cz = (v[i0 * 8 + 2] + v[i1 * 8 + 2] + v[i2 * 8 + 2]) / 3f;
// 三角形就在方块表面上:把中心往法线反方向推 0.5 就是方块中心
float len = (float) Math.sqrt(nx * nx + ny * ny + nz * nz);
if (len < 1e-6) return true;
// 判断法线是否指向「远离方块内部」:比较平面到方块中心的距离
float bx2 = cx - nx / len * 0.5f, by2 = cy - ny / len * 0.5f, bz2 = cz - nz / len * 0.5f;
int gx = (int) Math.floor(bx2), gy = (int) Math.floor(by2), gz = (int) Math.floor(bz2);
return gx == (int) Math.floor(cx - nx / len * 0.49f)
|| Math.abs(nx / len) + Math.abs(ny / len) + Math.abs(nz / len) > 0.99f;
}
}