server/pet_follow.go (6259B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "log" 7 "math" 8 "time" 9 ) 10 11 func (server *server) handlePetCommand(client *worldClient, body []byte) error { 12 if len(body) < 36 { 13 return fmt.Errorf("invalid pet command length") 14 } 15 // Original unfiltered commands: follow=1, behind=2, wait=4, attack=7, terminate=10. 16 command := binary.BigEndian.Uint32(body[16:20]) 17 if len(body) != 36 || !client.target || binary.BigEndian.Uint32(body[4:8]) != 50000 || 18 binary.BigEndian.Uint32(body[8:12]) != client.id || command != 1 && command != 2 && command != 4 && command != 7 && command != 10 || 19 binary.BigEndian.Uint32(body[20:24]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 1009 || 20 binary.BigEndian.Uint32(body[28:32]) != 0 || binary.BigEndian.Uint32(body[32:36]) != 0 { 21 return nil 22 } 23 owner := server.characters[client.id] 24 if server.clients[client.id] != client || !client.ready || client.loading || owner.health == 0 { 25 return nil 26 } 27 now := time.Now() 28 updates := make(map[*worldClient][][]byte) 29 target := server.characters[client.selectedTarget[1]] 30 if command == 7 && (owner.driving() || client.selectedTarget[0] != 50000 || target == nil || !target.attackableNPC() || 31 !client.npcs[target.id] || target.health == 0 || target.playfield != owner.playfield) { 32 return nil 33 } 34 for id := range owner.pets { 35 pet := server.characters[id] 36 if pet == nil { 37 if command == 10 { 38 server.world.send(id, petDismissal{owner: owner.id, pet: id}) 39 } 40 continue 41 } 42 if command == 10 { 43 if err := server.retirePet(pet, updates); err != nil { 44 log.Printf("Pet dismissal: %v", err) 45 } 46 continue 47 } 48 if pet.health == 0 || pet.playfield != owner.playfield || pet.pet.expired(now) { 49 continue 50 } 51 if command == 7 && pet.fighting == target.id { 52 continue 53 } 54 if pet.motion != nil && pet.motion.navigation != nil { 55 close(pet.motion.navigation.cancel) 56 pet.motion.navigation = nil 57 } 58 if command == 7 { 59 pet.followTarget = 0 60 pet.pet.paused = false 61 server.queueCombat(updates, pet, owner, petFollowPacket(pet, 0)) 62 pet.geometry = server.geometry 63 pet.motion = &npcMotion{home: pet.position} 64 pet.movement.prepare(pet, now) 65 pet.movement.updatedAt = now 66 pet.threat = nil 67 server.stopAttack(pet, updates) 68 server.startAttack(pet, target, now, updates) 69 continue 70 } 71 // Follow/behind/wait must not reselect the stopped encounter next tick. 72 pet.threat = nil 73 server.stopAttack(pet, updates) 74 if command == 4 { 75 pet.followTarget = 0 76 pet.pet.paused = false 77 pet.motion = nil 78 pet.movement.action(pet, 19) 79 server.queueCombat(updates, pet, owner, petFollowPacket(pet, 0)) 80 continue 81 } 82 pet.geometry = server.geometry 83 pet.followTarget = owner.id 84 pet.pet.paused = false 85 pet.motion = &npcMotion{home: pet.position} 86 pet.movement.prepare(pet, now) 87 pet.movement.updatedAt = now 88 pet.movement.action(pet, 23) 89 server.queueCombat(updates, pet, owner, petFollowPacket(pet, owner.id)) 90 } 91 server.sendCombatUpdates(updates) 92 return nil 93 } 94 95 func petFollowPacket(pet *character, target uint32) []byte { 96 // KKR FollowTargetIIR 1007e690 installs an NPC owner target without waypoints. 97 body := make([]byte, 29) 98 body[12] = 19 99 if target != 0 { 100 binary.BigEndian.PutUint32(body, 50000) 101 binary.BigEndian.PutUint32(body[4:], target) 102 body[12] = 23 103 } 104 for axis, value := range pet.position { 105 binary.BigEndian.PutUint32(body[13+axis*4:], math.Float32bits(value)) 106 } 107 return n3Packet(0x260f3671, 50000, pet.id, body) 108 } 109 110 func (server *server) advancePetFollowing(now time.Time, updates map[*worldClient][][]byte) { 111 for _, pet := range server.petExpiry { 112 if !server.npcActive(pet, now) { 113 continue 114 } 115 if pet.motion != nil && (pet.fighting != 0 || pet.motion.returning) { 116 seconds := min(.25, max(0, now.Sub(pet.movement.updatedAt).Seconds())) 117 pet.movement.updatedAt = now 118 if !server.combatParticipantAvailable(pet, now) { 119 server.cancelCombat(pet.id, updates) 120 server.stopNPCMotion(pet, updates) 121 pet.motion.returning = false 122 continue 123 } 124 server.advanceNPCPursuit(pet, now, seconds, updates) 125 continue 126 } 127 if pet.followTarget == 0 { 128 continue 129 } 130 seconds := min(.25, max(0, now.Sub(pet.movement.updatedAt).Seconds())) 131 pet.movement.updatedAt = now 132 owner, client := server.characters[pet.pet.owner], server.clients[pet.pet.owner] 133 available := pet.health != 0 && !pet.pet.expired(now) && owner != nil && owner.health != 0 && 134 owner.playfield == pet.playfield && client != nil && client.ready && !client.loading 135 if !available { 136 if !pet.pet.paused { 137 pet.pet.paused = true 138 pet.motion.route, pet.motion.moving = nil, false 139 pet.movement.velocity, pet.movement.speed = [3]float32{}, 0 140 server.queueCombat(updates, pet, pet, petFollowPacket(pet, 0)) 141 } 142 continue 143 } 144 if pet.pet.paused { 145 pet.pet.paused = false 146 server.queueCombat(updates, pet, owner, petFollowPacket(pet, owner.id)) 147 } 148 if pet.pursuitArrived(owner.position) { 149 if pet.motion.navigation != nil { 150 close(pet.motion.navigation.cancel) 151 pet.motion.navigation = nil 152 } 153 pet.motion.route, pet.motion.moving = nil, false 154 pet.movement.velocity, pet.movement.speed = [3]float32{}, 0 155 continue 156 } 157 158 // Share NPC route planning and native arrival forces, not player follow's 4m offset. 159 if pet.geometry.physical(pet.playfield) { 160 if pet.motion.navigation != nil || 161 len(pet.motion.route) == 0 && !now.Before(pet.motion.routeRetry) { 162 if !server.requestNPCRoute(pet, owner.position, now) { 163 continue 164 } 165 pet.motion.destination = owner.position 166 pet.motion.routeRetry = now.Add(time.Second) 167 } 168 if len(pet.motion.route) == 0 { 169 continue 170 } 171 } 172 previous := pet.position 173 _, blocked := pet.advanceNPCMotion(owner.position, seconds) 174 server.indexNPC(pet) 175 pet.motion.moving = !blocked 176 if blocked { 177 pet.motion.route = nil 178 pet.motion.routeRetry = now.Add(time.Second) 179 } else if pet.geometry.physical(pet.playfield) && 180 (len(pet.motion.route) == 0 || surfaceDistanceSquared(pet.motion.route[len(pet.motion.route)-1], owner.position) >= 1) { 181 server.requestNPCRoute(pet, owner.position, now) 182 } 183 if pet.position != previous { 184 server.queueCombat(updates, pet, pet, npcMovementPacket(pet, 0x94)) 185 } 186 } 187 }