server/combat_visibility_test.go (6378B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "testing" 6 "time" 7 ) 8 9 func addCombatWall(world *server) { 10 // One front-facing triangle separates two otherwise reachable outdoor actors. 11 world.geometry = &sceneGeometry{ 12 playfields: map[uint32]scenePlayfield{950: {sizeX: 100, sizeZ: 100, cellsX: 1, cellsZ: 1}}, 13 terrain: map[uint32]*terrain{950: {width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, 14 spacing: 50, scale: 1, heights: make([]byte, 9)}}, 15 surfaces: sceneResourcesForTest(map[uint32]*sceneSurface{950 << 16: {primary: &surfaceTree{ 16 vertices: [][3]float32{{15, 0, 0}, {15, 0, 100}, {15, 10, 0}}, 17 groups: []surfaceGroup{{bounds: [6]float32{15, 0, 0, 15, 10, 100}, triangles: [][3]uint32{{0, 1, 2}}}}, 18 nodes: []surfaceNode{{groups: []uint32{0}}}, 19 }}}), 20 } 21 world.characters[1].position = [3]float32{14, 0, 10} 22 world.characters[1000000].position = [3]float32{16, 0, 10} 23 } 24 25 func TestCombatVisibilityRejectsBothWallDirectionsAndAllowsClearRetry(t *testing.T) { 26 for _, kind := range []string{"unarmed", "ranged"} { 27 for _, reverse := range []bool{false, true} { 28 t.Run(kind+map[bool]string{false: "/front", true: "/back"}[reverse], func(t *testing.T) { 29 world, _ := newCombatWorld(t) 30 if kind == "ranged" { 31 world, _ = newRangedWorld(t) 32 } 33 addCombatWall(world) 34 player, target := world.characters[1], world.characters[1000000] 35 if reverse { 36 player.position, target.position = target.position, player.position 37 } 38 health := target.health 39 request := attackPacket(1, target.id)[20:] 40 41 if err := world.handleCombat(world.clients[1], request); err != nil { 42 t.Fatal(err) 43 } 44 world.advanceCombat(time.Now().Add(time.Minute)) 45 46 if player.fighting != 0 || !player.nextAttack.IsZero() || !player.rechargeUntil.IsZero() || target.health != health || 47 len(target.damageContributors) != 0 || len(world.fighting) != 0 || target.corpse != nil || player.experience != 0 { 48 t.Fatal("wall admission changed combat or rewards") 49 } 50 if kind == "ranged" && player.inventory.Items[0].Ammo != 3 { 51 t.Fatal("blocked shot consumed ammunition") 52 } 53 target.position = player.position 54 target.position[2] += 2 55 if err := world.handleCombat(world.clients[1], request); err != nil { 56 t.Fatal(err) 57 } 58 world.advanceCombat(player.nextAttack) 59 if target.health >= health || player.fighting != target.id || kind == "ranged" && player.inventory.Items[0].Ammo != 2 { 60 t.Fatal("clear retry failed to land exactly one swing", target.health) 61 } 62 }) 63 } 64 } 65 } 66 67 func TestCombatVisibilityRechecksBeforeShotAndRetainsNPCThreat(t *testing.T) { 68 world, _ := newRangedWorld(t) 69 addCombatWall(world) 70 player, target := world.characters[1], world.characters[1000000] 71 target.position[0] = 12 72 if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil { 73 t.Fatal(err) 74 } 75 deadline := player.nextAttack 76 target.combatProfile = gamedata.NPCCombatProfiles[17655][1] 77 target.threat = map[uint32]int64{1: 10} 78 target.fighting, target.nextAttack = 1, deadline 79 world.fighting[target.id] = target 80 npcDeadline := target.nextAttack 81 target.position[0] = 16 82 rolls := 0 83 world.combatRoll = func(limit uint32) uint32 { rolls++; return landedCombatRoll(limit) } 84 85 world.advanceCombat(deadline.Add(time.Minute)) 86 world.advanceCombat(deadline.Add(2 * time.Minute)) 87 88 if player.inventory.Items[0].Ammo != 3 || target.health != 1000 || player.health != 100 || rolls != 0 || 89 player.fighting != 0 || !player.nextAttack.IsZero() || !player.rechargeUntil.IsZero() || 90 target.fighting != 1 || target.nextAttack != npcDeadline || target.threat[1] != 10 { 91 t.Fatal("obstructed shot or bite changed damage, costs, clocks or threat", player.health, target.nextAttack, npcDeadline) 92 } 93 target.position[0] = 12 94 world.advanceCombat(deadline.Add(3 * time.Minute)) 95 if player.health >= 100 || target.health != 1000 || rolls == 0 || target.nextAttack.Before(deadline.Add(3*time.Minute)) { 96 t.Fatal("creature did not resume a single bite after visibility returned") 97 } 98 } 99 100 func TestCombatVisibilitySpecialsRejectWithoutLocksOrClockChanges(t *testing.T) { 101 for _, skill := range []uint32{147, 150} { 102 t.Run(map[uint32]string{147: "Fast", 150: "Fling"}[skill], func(t *testing.T) { 103 var world *server 104 var request []byte 105 if skill == 147 { 106 world, _ = newFastAttackWorld(t) 107 request = fastAttackRequest(1, 1000000)[20:] 108 } else { 109 world, _ = newRangedWorld(t) 110 request = flingShotRequest(1, 1000000)[20:] 111 } 112 addCombatWall(world) 113 player, client, target := world.characters[1], world.clients[1], world.characters[1000000] 114 player.nextAttack, player.rechargeUntil = time.Now().Add(time.Second), time.Now().Add(time.Hour) 115 attack, recharge := player.nextAttack, player.rechargeUntil 116 client.nanoCast = &nanoCast{target: target.id, readyAt: time.Now().Add(time.Minute)} 117 cast := client.nanoCast 118 119 if err := world.handleWeaponSpecial(client, request); err != nil { 120 t.Fatal(err) 121 } 122 123 if target.health != 1000 || !player.skillLocks[skill].IsZero() || player.nextAttack != attack || 124 player.rechargeUntil != recharge || client.nanoCast != cast || len(target.damageContributors) != 0 { 125 t.Fatal("obstructed special changed damage, lock or another action clock") 126 } 127 target.position[0] = 12 128 if err := world.handleWeaponSpecial(client, request); err != nil { 129 t.Fatal(err) 130 } 131 if target.health >= 1000 || player.skillLocks[skill].IsZero() || player.nextAttack != attack || player.rechargeUntil != recharge || 132 client.nanoCast != cast || skill == 150 && player.inventory.Items[0].Ammo != 3 { 133 t.Fatal("clear special retry lost damage, independent clocks or pre-15.5 ammo behavior") 134 } 135 }) 136 } 137 } 138 139 func TestCombatVisibilityUsesNativeEyeHeightAboveLowScenery(t *testing.T) { 140 world, _ := newCombatWorld(t) 141 addCombatWall(world) 142 wall := world.geometry.surface(950 << 16).primary 143 wall.vertices[2][1], wall.groups[0].bounds[4] = 1, 1 144 player, target := world.characters[1], world.characters[1000000] 145 target.monsterData = 17655 146 147 if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil { 148 t.Fatal(err) 149 } 150 world.advanceCombat(player.nextAttack) 151 152 if player.fighting != target.id || target.health >= 100 { 153 t.Fatal("low scenery blocked native eye-height rays for a small creature") 154 } 155 }