server/corpse_placement_test.go (5296B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "math" 6 "testing" 7 ) 8 9 func TestTrainingCorpsePlacementRetainsOriginalRouteAndColdPose(t *testing.T) { 10 for _, row := range []struct { 11 name string 12 monster uint32 13 zone uint32 14 position [3]float32 15 }{ 16 {"Android", 17649, 954, [3]float32{98.22058, 4.3194237, 107.11992}}, 17 {"Clawfinger", 38387, 954, [3]float32{90.388504, 5.5916004, 144.4074}}, 18 {"Functionary", 26147, 950, [3]float32{65.821846, 16.346714, 175.79474}}, 19 {"Mercenary", 26088, 952, [3]float32{155.22652, 11.569292, 184.89328}}, 20 } { 21 t.Run(row.name, func(t *testing.T) { 22 // Reproduce the body buried at its original training placement. 23 world, _ := newKillExperienceWorld(t, "unarmed") 24 ground, err := readTerrain() 25 if err != nil { 26 t.Fatal(err) 27 } 28 world.terrain = ground 29 target := world.characters[1000000] 30 target.monsterData, target.playfield = row.monster, row.zone 31 target.position, target.rotation = row.position, [4]float32{0, 0, 0, 1} 32 world.characters[1].playfield, world.characters[1].position = row.zone, target.position 33 world.characters[1].position[2]++ 34 if err := world.flushWorld(); err != nil { 35 t.Fatal(err) 36 } 37 38 attackForExperience(t, world, "unarmed") 39 cold := newTestInventoryServer(t) 40 41 if target.health != 0 || target.corpse == nil || target.position[1] <= row.position[1]+.5 || 42 target.position[0] != row.position[0] || target.position[2] != row.position[2] || target.rotation != [4]float32{0, 0, 0, 1} || 43 cold.characters[target.id].position != target.position || cold.characters[target.id].rotation != target.rotation { 44 t.Fatal("death did not retain its raised pose and original horizontal placement", target.position) 45 } 46 }) 47 } 48 } 49 50 func TestCorpseSlopePlacementCommitsWithDeath(t *testing.T) { 51 for _, scenario := range []struct { 52 name string 53 flat bool 54 failure string 55 }{ 56 {name: "slope"}, 57 {name: "flat", flat: true}, 58 {name: "rejected write", failure: "CREATE TRIGGER reject_pose BEFORE UPDATE ON npcs WHEN NEW.has_corpse=1 BEGIN SELECT RAISE(FAIL,'corpse'); END"}, 59 {name: "ignored write", failure: "CREATE TRIGGER reject_pose BEFORE UPDATE ON npcs WHEN NEW.has_corpse=1 BEGIN SELECT RAISE(IGNORE); END"}, 60 {name: "commit failure", failure: "CREATE TABLE pose_commit_failure (id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_pose AFTER UPDATE ON npcs WHEN NEW.has_corpse=1 BEGIN INSERT INTO pose_commit_failure VALUES(99999); END"}, 61 } { 62 t.Run(scenario.name, func(t *testing.T) { 63 // Set up the recorded body, facing and terrain under an ordinary attack. 64 world, output := newKillExperienceWorld(t, "unarmed") 65 ground, err := readTerrain() 66 if err != nil { 67 t.Fatal(err) 68 } 69 world.terrain = ground 70 target := world.characters[1000000] 71 target.monsterData = 30369 72 target.position = [3]float32{186, 13.912122, 174.375} 73 target.rotation = [4]float32{0, -.930363, 0, .366640} 74 if scenario.flat { 75 world.terrain[target.playfield] = &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, 76 spacing: 200, scale: 1, heights: []byte{14, 14, 14, 14, 14, 14, 14, 14, 14}} 77 target.position[1] = 14.2 78 } 79 world.characters[1].position = target.position 80 world.characters[1].position[0] -= 2 81 position, rotation := target.position, target.rotation 82 if err := world.flushWorld(); err != nil { 83 t.Fatal(err) 84 } 85 if scenario.failure != "" { 86 if _, err := world.db.Exec(scenario.failure); err != nil { 87 t.Fatal(err) 88 } 89 } 90 91 // Kill through the public attack path; failed transactions must not move it. 92 attackForExperience(t, world, "unarmed") 93 if scenario.failure != "" { 94 cold := newTestInventoryServer(t) 95 if target.health != 1 || target.corpse != nil || target.position != position || cold.characters[target.id].position != position { 96 t.Fatal("failed death leaked its corrected corpse position") 97 } 98 for _, packet := range output.packets { 99 if opcode := binary.BigEndian.Uint32(packet[16:20]); opcode == 0x36510078 || opcode == 0x4f474e05 { 100 t.Fatal("failed death published actor removal or corpse creation") 101 } 102 } 103 if _, err := world.db.Exec("DROP TRIGGER reject_pose"); err != nil { 104 t.Fatal(err) 105 } 106 attackForExperience(t, world, "unarmed") 107 } 108 109 // The committed pose is replayable, with no extra lift on already clear ground. 110 expectedY := float32(15.081813) 111 if scenario.flat { 112 expectedY = 14.2 113 } 114 cold := newTestInventoryServer(t) 115 if target.health != 0 || target.corpse == nil || math.Abs(float64(target.position[1]-expectedY)) > .0001 || 116 target.position[0] != 186 || target.position[2] != 174.375 || target.rotation != rotation || 117 cold.characters[target.id].position != target.position || cold.characters[target.id].rotation != rotation { 118 t.Fatal("death did not retain the expected terrain clearance and original facing", target.position, target.rotation) 119 } 120 removed := false 121 for _, packet := range output.packets { 122 switch binary.BigEndian.Uint32(packet[16:20]) { 123 case 0x36510078: 124 removed = true 125 case 0x4f474e05: 126 if removed == scenario.flat { 127 t.Fatal("corrected corpse did not replace the old actor before activation") 128 } 129 } 130 } 131 }) 132 } 133 }