aorecomp

Anarchy Online classic in the browser

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


server/mission_entrance.go (7143B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math"
      8 	"time"
      9 )
     10 
     11 func (database *gameDatabase) missionEntrance(playfield, identity uint32) (zoneArrival, error) {
     12 	data, _, err := database.read(1000026, playfield)
     13 	if err != nil {
     14 		return zoneArrival{}, err
     15 	}
     16 	if len(data) < 4 {
     17 		return zoneArrival{}, fmt.Errorf("truncated statel list")
     18 	}
     19 	count, offset := binary.LittleEndian.Uint32(data), 4
     20 	for range count {
     21 		if len(data)-offset < 4 {
     22 			return zoneArrival{}, fmt.Errorf("truncated statel length")
     23 		}
     24 		length := uint64(binary.LittleEndian.Uint32(data[offset:]))
     25 		offset += 4
     26 		if length < 64 || length > uint64(len(data)-offset) {
     27 			return zoneArrival{}, fmt.Errorf("invalid statel length")
     28 		}
     29 		record := data[offset : offset+int(length)]
     30 		offset += int(length)
     31 		if uint64(binary.LittleEndian.Uint32(record[60:64]))+64 != length {
     32 			return zoneArrival{}, fmt.Errorf("invalid statel descriptor framing")
     33 		}
     34 		if binary.LittleEndian.Uint32(record) != 56006 || binary.LittleEndian.Uint32(record[4:8]) != identity {
     35 			continue
     36 		}
     37 		arrival := zoneArrival{}
     38 		for axis := range arrival.Position {
     39 			arrival.Position[axis] = math.Float32frombits(binary.LittleEndian.Uint32(record[24+axis*4:]))
     40 		}
     41 		for axis := range arrival.Rotation {
     42 			arrival.Rotation[axis] = math.Float32frombits(binary.LittleEndian.Uint32(record[48-axis*4:]))
     43 		}
     44 		return arrival, nil
     45 	}
     46 	return zoneArrival{}, fmt.Errorf("missing mission entrance %d/%d", playfield, identity)
     47 }
     48 
     49 func (server *server) missionWorld(identity uint32) *acceptedMission {
     50 	if server.world != nil {
     51 		missions := server.world.queryMissions(missionLookup{world: identity})
     52 		if len(missions) == 0 {
     53 			return nil
     54 		}
     55 		return &missions[0]
     56 	}
     57 	for index := range server.missionExpiry {
     58 		if server.missionExpiry[index].world == identity {
     59 			return &server.missionExpiry[index]
     60 		}
     61 	}
     62 	return nil
     63 }
     64 
     65 func (server *server) prepareMissionWorld(mission *acceptedMission) (*missionScene, error) {
     66 	if scene := server.geometry.mission(mission.world); scene != nil {
     67 		return scene, nil
     68 	}
     69 	layout, err := decodeMissionLayout(mission.layout)
     70 	if err != nil {
     71 		return nil, err
     72 	}
     73 	if err := server.geometry.addMission(mission.world, layout); err != nil {
     74 		return nil, err
     75 	}
     76 	return server.geometry.mission(mission.world), nil
     77 }
     78 
     79 func (server *server) validateUser(data userData) error {
     80 	if err := data.validateFields(); err != nil {
     81 		return err
     82 	}
     83 	if _, public := gamedata.Playfields[data.Playfield]; public {
     84 		return data.validate()
     85 	}
     86 	mission := server.missionWorld(data.Playfield)
     87 	if mission == nil {
     88 		return fmt.Errorf("playfield has no active mission assignment")
     89 	}
     90 	_, err := server.prepareMissionWorld(mission)
     91 	return err
     92 }
     93 
     94 func (server *server) playerPlayfieldPacket(player *character) ([]byte, error) {
     95 	mission := server.missionWorld(player.playfield)
     96 	if mission == nil {
     97 		return playfieldPacket(player), nil
     98 	}
     99 	layout, err := decodeMissionLayout(mission.layout)
    100 	if err != nil {
    101 		return nil, err
    102 	}
    103 	return missionPlayfieldPacket(player, layout)
    104 }
    105 
    106 func (server *server) missionReturn(player *character, mission *acceptedMission) (userData, error) {
    107 	entrance, err := server.geometry.database.missionEntrance(mission.offer.definition.Playfield, mission.offer.definition.Entrance)
    108 	if err != nil {
    109 		return userData{}, err
    110 	}
    111 	destination := player.userData()
    112 	destination.Playfield = mission.offer.definition.Playfield
    113 	destination.Position, destination.Rotation = entrance.Position, entrance.Rotation
    114 	forward, _ := bodyAxes(entrance.Rotation)
    115 	for _, axis := range []int{0, 2} {
    116 		destination.Position[axis] += 2.5 * forward[axis]
    117 	}
    118 	// The existing return link still describes the player's city/backyard journey.
    119 	return destination, nil
    120 }
    121 
    122 func (server *server) missionDestination(player *character, client *worldClient) (*userData, error) {
    123 	if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 ||
    124 		client.trade != nil || client.shop != nil || client.bank != nil || client.equipment != nil || !player.movement.active() && player.followTarget == 0 {
    125 		return nil, nil
    126 	}
    127 	near := func(position [3]float32) bool {
    128 		x, z := player.position[0]-position[0], player.position[2]-position[2]
    129 		return x*x+z*z < 4 && math.Abs(float64(player.position[1]-position[1])) < 8
    130 	}
    131 	if mission := server.missionWorld(player.playfield); mission != nil {
    132 		scene, err := server.prepareMissionWorld(mission)
    133 		if err != nil {
    134 			return nil, err
    135 		}
    136 		if !near(scene.exitPosition) {
    137 			return nil, nil
    138 		}
    139 		destination, err := server.missionReturn(player, mission)
    140 		return &destination, err
    141 	}
    142 
    143 	// Only query credentials when a live assignment's exterior is nearby.
    144 	var candidates []*acceptedMission
    145 	now := time.Now()
    146 	missions := server.missionExpiry
    147 	if server.world != nil {
    148 		missions = server.world.queryMissions(missionLookup{exterior: player.playfield})
    149 	}
    150 	for index := range missions {
    151 		mission := &missions[index]
    152 		if !mission.completed && mission.offer.definition.Playfield == player.playfield && now.Before(mission.expiresAt) && near(mission.offer.definition.Position) {
    153 			candidates = append(candidates, mission)
    154 		}
    155 	}
    156 	if len(candidates) == 0 {
    157 		return nil, nil
    158 	}
    159 	rows, err := server.db.Query(`SELECT DISTINCT k.mission_id FROM mission_keys k
    160 		JOIN item_locations l ON l.item_id=k.item_id JOIN items i ON i.id=k.item_id
    161 		WHERE l.player_id=? AND l.slot BETWEEN 64 AND 93 AND l.reclaim_at IS NULL
    162 		AND i.type=51053 AND i.template=28577`, player.id)
    163 	if err != nil {
    164 		return nil, err
    165 	}
    166 	defer rows.Close()
    167 	var selected *acceptedMission
    168 	for rows.Next() {
    169 		var identity uint32
    170 		if err := rows.Scan(&identity); err != nil {
    171 			return nil, err
    172 		}
    173 		for _, candidate := range candidates {
    174 			if candidate.id != identity {
    175 				continue
    176 			}
    177 			// An ambiguous set of keys must not select another player's world arbitrarily.
    178 			if selected != nil && selected.id != identity {
    179 				return nil, nil
    180 			}
    181 			selected = candidate
    182 		}
    183 	}
    184 	if err := rows.Err(); err != nil {
    185 		return nil, err
    186 	}
    187 	if selected == nil {
    188 		return nil, nil
    189 	}
    190 	entrance, err := server.geometry.database.missionEntrance(player.playfield, selected.offer.definition.Entrance)
    191 	if err != nil {
    192 		return nil, err
    193 	}
    194 	if !near(entrance.Position) {
    195 		return nil, nil
    196 	}
    197 	scene, err := server.prepareMissionWorld(selected)
    198 	if err != nil {
    199 		return nil, err
    200 	}
    201 	destination := player.userData()
    202 	destination.Playfield, destination.Position = selected.world, scene.exitPosition
    203 	angle := float64((scene.exitDirection+2)&3) * math.Pi / 4
    204 	destination.Rotation = [4]float32{0, float32(math.Sin(angle)), 0, float32(math.Cos(angle))}
    205 	forward, _ := bodyAxes(destination.Rotation)
    206 	for _, axis := range []int{0, 2} {
    207 		destination.Position[axis] += 2.5 * forward[axis]
    208 	}
    209 	origin := destination.Position
    210 	origin[1]++
    211 	var normal [3]float32
    212 	scene.floor(origin, &destination.Position, &normal)
    213 	destination.Position[1] += .01
    214 	if scene.roomAt(destination.Position) != scene.entrance {
    215 		return nil, fmt.Errorf("mission entrance does not lead into its exterior room")
    216 	}
    217 	return &destination, nil
    218 }