server/respawn_test.go (8331B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "sync" 7 "testing" 8 "time" 9 ) 10 11 func newRespawnWorld(t *testing.T) (*server, *combatTestConn) { 12 t.Helper() 13 data := testNPCData() 14 data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{1, 1} 15 data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species" 16 data.Playfields[0].Districts[0].Groups[0].PlacementStatus = "authored_point" 17 useNPCData(t, data) 18 world := newTestServer(t) 19 player := world.newCharacter() 20 player.position = [3]float32{52, 14, 56} 21 conn := &combatTestConn{} 22 world.clients[player.id] = &worldClient{id: player.id, conn: conn, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 23 world.characters[1000000].health = 1 24 if err := world.saveUser(player); err != nil { 25 t.Fatal(err) 26 } 27 if err := world.flushWorld(); err != nil { 28 t.Fatal(err) 29 } 30 return world, conn 31 } 32 33 func TestNPCRespawnCreatesOneFreshLifeAtHomeForBothObservers(t *testing.T) { 34 world, first := newRespawnWorld(t) 35 observer := world.newCharacter() 36 second := &combatTestConn{} 37 world.clients[observer.id] = &worldClient{id: observer.id, conn: second, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 38 npc := world.characters[1000000] 39 npc.position[0]++ 40 npc.rotation = [4]float32{0, 1, 0, 0} 41 npc.motion.moving = true 42 npc.motion.destination = [3]float32{55, 14, 56} 43 npc.rechargeUntil = time.Now().Add(time.Hour) 44 45 world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]) 46 killed := world.characters[1].nextAttack 47 world.advanceCombat(killed) 48 if err := world.handleLootUse(world.clients[1], lootUseRequest(1)); err != nil { 49 t.Fatal(err) 50 } 51 first.packets, second.packets = nil, nil 52 world.advanceNPCRespawns(killed.Add(100*time.Second - time.Nanosecond)) 53 if world.characters[1000001] != nil || len(first.packets) != 0 || len(world.npcRespawns) != 1 { 54 t.Fatal("replacement appeared before the profile's respawn interval") 55 } 56 world.advanceNPCRespawns(killed.Add(100 * time.Second)) 57 world.advanceNPCRespawns(killed.Add(time.Hour)) 58 59 replacement := world.characters[1000001] 60 if replacement == nil || len(world.characters) != 4 || len(world.npcRespawns) != 0 || world.nextNPC != 1000002 { 61 t.Fatal("respawn did not create exactly one replacement") 62 } 63 if replacement.health != 20 || replacement.corpse != nil || replacement.fighting != 0 || 64 !replacement.nextAttack.IsZero() || !replacement.rechargeUntil.IsZero() || 65 replacement.position != [3]float32{52, 14, 56} || replacement.rotation != [4]float32{0, 0, 0, 1} || 66 replacement.motion.home != [3]float32{52, 14, 56} || replacement.motion.moving || !replacement.motion.nextMove.IsZero() || 67 replacement.combatProfile != npc.combatProfile || replacement.npcDefinitionID != "test/npc" { 68 t.Fatalf("replacement retained dead-life state: %+v", replacement) 69 } 70 if len(world.mobileNPCs[950]) != 1 || world.mobileNPCs[950][0] != replacement || 71 npc.health != 0 || npc.corpse == nil || npc.fighting != 0 || len(world.fighting) != 0 || world.clients[1].lootCorpse != npc.id { 72 t.Fatal("respawn changed the corpse, active loot window or roaming population") 73 } 74 if len(first.packets) != 2 || len(second.packets) != 2 || 75 binary.BigEndian.Uint32(first.packets[0][16:20]) != 0x271b3a6b || 76 binary.BigEndian.Uint32(first.packets[0][24:28]) != 1000001 || 77 binary.BigEndian.Uint32(first.packets[1][16:20]) != 0x1d3c0f1c || 78 !bytes.Equal(first.packets[0][16:], second.packets[0][16:]) || !bytes.Equal(first.packets[1][16:], second.packets[1][16:]) { 79 t.Fatal("observers did not receive the same new actor and combat profile") 80 } 81 } 82 83 func TestNPCRespawnWithoutObserversThenReentryAndRestart(t *testing.T) { 84 world, conn := newRespawnWorld(t) 85 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 86 killed := world.characters[1].nextAttack 87 world.advanceCombat(killed) 88 conn.packets = nil 89 world.clients[1].ready = false 90 91 world.advanceNPCRespawns(killed.Add(100 * time.Second)) 92 if len(conn.packets) != 0 || world.characters[1000001] == nil { 93 t.Fatal("offline population did not respawn independently of observers") 94 } 95 world.clients[1].ready = true 96 world.clients[1].npcs = make(map[uint32]bool) 97 world.advanceNPCs(killed.Add(101 * time.Second)) 98 restarted := newServerForTest(t) 99 100 spawns, corpses, profiles := 0, 0, 0 101 for _, packet := range conn.packets { 102 switch binary.BigEndian.Uint32(packet[16:20]) { 103 case 0x271b3a6b: 104 spawns++ 105 case 0x4f474e05: 106 corpses++ 107 case 0x1d3c0f1c: 108 profiles++ 109 } 110 } 111 if spawns != 1 || corpses != 1 || profiles != 1 { 112 t.Fatalf("reentry lost the living replacement or old corpse: %d/%d/%d", spawns, corpses, profiles) 113 } 114 if len(restarted.characters) != 3 || restarted.characters[1000001].health != 20 || 115 restarted.characters[1000000].corpse == nil || len(restarted.npcRespawns) != 0 || len(restarted.mobileNPCs[950]) != 1 || 116 restarted.mobileNPCs[950][0].id != 1000001 { 117 t.Fatal("restart lost the living replacement, old remains or consumed respawn deadline") 118 } 119 } 120 121 func TestNPCRespawnConcurrentTicksAndStaleCombatKeepLifeSeparate(t *testing.T) { 122 world, _ := newRespawnWorld(t) 123 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 124 killed := world.characters[1].nextAttack 125 world.advanceCombat(killed) 126 zone, stop := startTestZone(t, world) 127 var workers sync.WaitGroup 128 129 for range 8 { 130 workers.Go(func() { 131 if err := zone.call(zoneTestCommand(func(state *server) error { state.advanceNPCRespawns(killed.Add(100 * time.Second)); return nil })); err != nil { 132 t.Error(err) 133 } 134 }) 135 } 136 workers.Wait() 137 if err := stop(); err != nil { 138 t.Fatal(err) 139 } 140 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 141 world.advanceCombat(killed.Add(time.Hour)) 142 143 if len(world.characters) != 3 || world.characters[1000001].health != 20 || 144 world.characters[1].fighting != 0 || len(world.fighting) != 0 || len(world.npcRespawns) != 0 || world.nextNPC != 1000002 { 145 t.Fatal("concurrent timers or a stale request duplicated or attacked the new life") 146 } 147 } 148 149 func TestNPCWithoutRespawnProfileStaysDead(t *testing.T) { 150 world, _ := newRespawnWorld(t) 151 world.characters[1000000].combatProfile = nil 152 153 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 154 killed := world.characters[1].nextAttack 155 world.advanceCombat(killed) 156 world.advanceNPCRespawns(killed.Add(time.Hour)) 157 158 if len(world.characters) != 2 || world.characters[1000000].health != 0 || len(world.npcRespawns) != 0 { 159 t.Fatal("an unlisted creature received an invented respawn policy") 160 } 161 } 162 163 func TestNPCRespawnKeepsOldLootSeparateAcrossTwoKills(t *testing.T) { 164 world, _ := newRespawnWorld(t) 165 if err := world.saveUser(world.characters[1]); err != nil { 166 t.Fatal(err) 167 } 168 world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]) 169 killed := world.characters[1].nextAttack 170 world.advanceCombat(killed) 171 world.characters[1000000].corpse.items = []ownedItem{{ID: 17, Type: 51005, Template: 42640, Quality: 1, Count: 1}} 172 world.handleLootUse(world.clients[1], lootUseRequest(1)) 173 174 world.advanceNPCRespawns(killed.Add(100 * time.Second)) 175 if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil { 176 t.Fatal(err) 177 } 178 world.handleInventory(world.clients[1], lootTakeRequest()) 179 world.characters[1000001].health = 1 180 world.characters[1].rechargeUntil = killed.Add(101 * time.Second) 181 world.handleCombat(world.clients[1], attackPacket(1, 1000001)[20:]) 182 secondKill := world.characters[1].nextAttack 183 world.advanceCombat(secondKill) 184 world.advanceNPCRespawns(secondKill.Add(100 * time.Second)) 185 world.advanceNPCRespawns(secondKill.Add(time.Hour)) 186 187 if len(world.characters) != 4 || world.characters[1000002].health != 20 || world.characters[1000002].corpse != nil || 188 len(world.npcRespawns) != 0 || len(world.mobileNPCs[950]) != 1 || world.mobileNPCs[950][0].id != 1000002 { 189 t.Fatal("a second death did not produce exactly one fresh living replacement") 190 } 191 if world.characters[1000000].corpse == world.characters[1000001].corpse || 192 len(world.characters[1000000].corpse.items) != 0 || len(world.characters[1000001].corpse.items) != 0 || 193 len(world.characters[1].inventory.Items) != 1 || world.characters[1].inventory.Items[0].ID != 17 || 194 world.clients[1].lootCorpse != 1000000 { 195 t.Fatal("respawn or repeated pickup duplicated old loot or rebound the open container") 196 } 197 }