aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/world_actions.go (9056B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math"
      8 	"time"
      9 )
     10 
     11 func (server *server) handlePosture(client *worldClient, body []byte) error {
     12 	character := server.characters[client.id]
     13 	if server.clients[client.id] != client || !client.ready || client.loading || character.health == 0 {
     14 		return nil
     15 	}
     16 	character.anchorMovement(time.Now())
     17 	action := uint32(28)
     18 	if binary.BigEndian.Uint32(body[16:20]) == 87 {
     19 		action = 33
     20 	}
     21 	character.movement.action(character, action)
     22 	playfield := character.playfield
     23 
     24 	// Publish posture before another resource update uses its rest modifier.
     25 	response := n3Packet(0x5e477770, 50000, client.id, body[16:])
     26 	server.broadcast(playfield, client.id, response)
     27 	return client.send(response)
     28 }
     29 
     30 func (server *server) handleCharacterReady(client *worldClient) error {
     31 	if server.clients[client.id] != client || !client.ready {
     32 		return nil
     33 	}
     34 	// The observer's new actor must finish loading before stats and OnWear replay.
     35 	// Repeated ready messages acknowledge the actor without applying effects twice.
     36 	var replay [][]byte
     37 	var team [][]byte
     38 	var death []byte
     39 	var motion [][]byte
     40 	resources := make(map[*worldClient][][]byte)
     41 	if client.loading {
     42 		player := server.characters[client.id]
     43 		player.body = playerCollisionBody{}
     44 		player.movement.prepare(player, time.Now())
     45 		player.alignSurface([3]float32{}, true)
     46 		motion = server.playerMovementReplay(client, time.Now(), nil)
     47 		motion = append(motion, server.playerFollowReplay(client, nil)...)
     48 		replay = playerCombatStatPackets(server.characters[client.id])
     49 		replay = append(replay, equippedItemPackets(server.characters[client.id])...)
     50 		replay = append(replay, activeNanoPackets(server.characters[client.id], time.Now())...)
     51 		if player.flying() {
     52 			// OnWear lifts newly loaded actors. The native owner correction bit
     53 			// restores the authoritative position without another helper transition.
     54 			correction := npcMovementPacket(player, 0x94)
     55 			replay = append(replay, correction)
     56 			motion = append(motion, correction)
     57 		}
     58 		// Remote pet-owner state must follow CharacterReady and the actor's effect replay.
     59 		pets := server.petOwnerPackets(player)
     60 		replay = append(replay, pets...)
     61 		motion = append(motion, pets...)
     62 		team = server.teamPackets(client.id)
     63 		server.queueTeamResources(resources, server.characters[client.id])
     64 		// Resume the native return request after a disconnect during death.
     65 		if server.characters[client.id].health == 0 {
     66 			death = deathPacket(server.characters[client.id])
     67 		}
     68 	}
     69 	client.loading = false
     70 	playfield := server.characters[client.id].playfield
     71 
     72 	response := n3Packet(0x570c2039, 50000, client.id, nil)
     73 	server.broadcast(playfield, client.id, response)
     74 	for _, packet := range replay {
     75 		server.broadcast(playfield, client.id, packet)
     76 	}
     77 	server.sendCombatUpdates(resources)
     78 	for _, packet := range team {
     79 		if err := client.send(packet); err != nil {
     80 			return err
     81 		}
     82 	}
     83 	if err := client.send(response); err != nil {
     84 		return err
     85 	}
     86 	for _, packet := range motion {
     87 		if err := client.send(packet); err != nil {
     88 			return err
     89 		}
     90 	}
     91 	if death != nil {
     92 		server.broadcast(playfield, client.id, death)
     93 		return client.send(death)
     94 	}
     95 	return nil
     96 }
     97 
     98 func (server *server) handleMovement(client *worldClient, body []byte) error {
     99 	if len(body) < 16 {
    100 		return fmt.Errorf("short character movement message")
    101 	}
    102 	key := uint32(0x54111123)
    103 	if binary.BigEndian.Uint32(body) == 0x25327751 || binary.BigEndian.Uint32(body) == 0x2d2b2169 {
    104 		if !client.target {
    105 			return nil
    106 		}
    107 		key = binary.BigEndian.Uint32(body)
    108 		length := 31
    109 		if key == 0x25327751 {
    110 			length = 63
    111 		}
    112 		if len(body) != length {
    113 			return fmt.Errorf("invalid pose movement length")
    114 		}
    115 		if binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id ||
    116 			binary.BigEndian.Uint32(body[12:16]) != 1 || body[16] != 20 || binary.BigEndian.Uint64(body[23:31]) == 0 {
    117 			return nil
    118 		}
    119 		// ZoneMove contains only the common pose; MouseMove adds eight opaque floats.
    120 		for offset := 31; offset < len(body); offset += 4 {
    121 			value := float64(math.Float32frombits(binary.BigEndian.Uint32(body[offset:])))
    122 			if math.IsNaN(value) || math.IsInf(value, 0) {
    123 				return nil
    124 			}
    125 		}
    126 		extra := body[31:]
    127 		body = append(body[:16:16], decodeMovementBase1491(body[16:31])...)
    128 		body = append(body, extra...)
    129 	} else if client.target {
    130 		if len(body) < 33 {
    131 			return fmt.Errorf("short 14.9.1 movement message")
    132 		}
    133 		body = append(body[:16:16], decodeMovement1491(body[16:33])...)
    134 	}
    135 	if len(body) < 48 || key == 0x54111123 && len(body) < 52 {
    136 		return fmt.Errorf("short character movement message")
    137 	}
    138 	// Only supported movement inputs may change authoritative state or be relayed.
    139 	// Native receivers also execute internal equipment events such as 27/35.
    140 	action := binary.BigEndian.Uint32(body[16:20])
    141 	switch action {
    142 	case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 22, 23, 26, 32:
    143 	default:
    144 		return nil
    145 	}
    146 	character := server.characters[client.id]
    147 	// Portal attempts can report the old position while a new zone loads.
    148 	if server.clients[client.id] != client || !client.ready || client.loading || character.health == 0 {
    149 		return nil
    150 	}
    151 	// Entry needs a prior163 approval; exit is meaningful only while sneaking.
    152 	if (action == 26 || action == 32) && character.movement.components[0] != 6 {
    153 		return nil
    154 	}
    155 	previousPosition := character.position
    156 	now := time.Now()
    157 	character.anchorMovement(now)
    158 	var position [3]float32
    159 	var rotation [4]float32
    160 	for i := range position {
    161 		position[i] = math.Float32frombits(binary.BigEndian.Uint32(body[20+i*4:]))
    162 	}
    163 	for i := range rotation {
    164 		rotation[i] = math.Float32frombits(binary.BigEndian.Uint32(body[32+i*4:]))
    165 	}
    166 	character.placeMovement(position, rotation)
    167 	if action == 20 {
    168 		// VehicleForwardUpdate composes yaw after the common placement; MouseMove uses zero.
    169 		angle := float32(0)
    170 		if key == 0x54111123 {
    171 			angle = math.Float32frombits(binary.BigEndian.Uint32(body[48:52]))
    172 		}
    173 		half := float64(angle) * .5
    174 		if angle < 0 || float64(angle) >= 2*math.Pi {
    175 			turns := float64(angle) * float64(float32(1/(2*math.Pi)))
    176 			half = (float64(float32(turns)) - math.Floor(turns)) * float64(float32(math.Pi))
    177 		}
    178 		sine, cosine := math.Sincos(half)
    179 		sine = float64(float32(sine))
    180 		x, y, z, w := float64(rotation[0]), float64(rotation[1]), float64(rotation[2]), float64(rotation[3])
    181 		rotation = [4]float32{float32(cosine*x + sine*z), float32(cosine*y + sine*w), float32(cosine*z - sine*x), float32(cosine*w - sine*y)}
    182 		character.placeMovement(position, rotation)
    183 	}
    184 	playfield := character.playfield
    185 	if action != 20 {
    186 		character.followTarget = 0
    187 	}
    188 	character.movement.action(character, action)
    189 	if client.trade != nil && !server.playerTradeAvailable(client.trade.clients[0], client.trade.clients[1]) {
    190 		server.cancelPlayerTrade(client)
    191 	}
    192 	// Native movement may close the shop locally without sending TradeAbort.
    193 	if client.shop != nil {
    194 		machine, exists := server.shopMachine(client, client.shop.machine)
    195 		if !exists || !server.canUseShop(client, machine) {
    196 			if err := server.abortShop(client); err != nil {
    197 				return err
    198 			}
    199 		}
    200 	}
    201 	// Locomotion interrupts an admitted cast; rotation-only updates do not.
    202 	updates := make(map[*worldClient][][]byte)
    203 	if client.bank != nil && !server.bankAvailable(client) {
    204 		updates[client] = append(updates[client], characterActionPacket(client.id, [5]uint32{106}))
    205 	}
    206 	if character.position != previousPosition || action == 1 || action == 3 || action == 5 || action == 7 || action == 13 {
    207 		server.abortNano(client, now, updates)
    208 	}
    209 	traceLog.Printf("Character %d action=%d position=%v", character.id, binary.BigEndian.Uint32(body[16:20]), character.position)
    210 	destination, err := server.zoneDestination(character, client)
    211 	if err != nil {
    212 		return err
    213 	}
    214 	if destination != nil {
    215 		server.sendCombatUpdates(updates)
    216 		traceLog.Printf("Character %d zoning %d -> %d", client.id, playfield, destination.Playfield)
    217 		return server.updateUser(*destination)
    218 	}
    219 	if server.geometry != nil && server.geometry.dungeon(playfield) != nil {
    220 		// Relay the admitted pose, and correct the owner when its compact position changed.
    221 		reported := encodeMovementBase1491(body[16:])
    222 		for axis, value := range character.position {
    223 			binary.BigEndian.PutUint32(body[20+axis*4:], math.Float32bits(value))
    224 		}
    225 		admitted := encodeMovementBase1491(body[16:])
    226 		if !bytes.Equal(reported[1:7], admitted[1:7]) {
    227 			updates[client] = append(updates[client], npcMovementPacket(character, 0x94))
    228 		}
    229 	}
    230 	if err := server.saveUser(character); err != nil {
    231 		return err
    232 	}
    233 	if key == 0x2d2b2169 {
    234 		// Native scene sections retire world items on departure. Restore the
    235 		// current authored scene when its controlled actor changes sections.
    236 		scene, err := server.worldScenePackets(character)
    237 		if err != nil {
    238 			return err
    239 		}
    240 		updates[client] = append(updates[client], scene...)
    241 	}
    242 	server.sendCombatUpdates(updates)
    243 	server.broadcast(playfield, client.id, n3Packet(key, 50000, client.id, body[16:]))
    244 	return nil
    245 }