aorecomp

Anarchy Online classic in the browser

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


server/zoning.go (10803B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"fmt"
      7 	"io/fs"
      8 	"log"
      9 	"math"
     10 	"time"
     11 )
     12 
     13 type doorOrigin struct {
     14 	Playfield uint32
     15 	Door      uint32
     16 }
     17 
     18 type zoneDoor struct {
     19 	ID              uint32                     `yaml:"id"`
     20 	Position        [3]float32                 `yaml:"position"`
     21 	Rotation        [4]float32                 `yaml:"rotation"`
     22 	Destination     uint32                     `yaml:"destination"`
     23 	DestinationDoor uint32                     `yaml:"destination_door"`
     24 	WhomPah         bool                       `yaml:"whompah"`
     25 	Return          bool                       `yaml:"return"`
     26 	Requirements    []gamedata.ItemRequirement `yaml:"requirements"`
     27 	RefusalMessage  string                     `yaml:"refusal_message"`
     28 
     29 	Arrival *zoneArrival `yaml:"arrival"`
     30 }
     31 
     32 type zoneArrival struct {
     33 	Position [3]float32 `yaml:"position"`
     34 	Rotation [4]float32 `yaml:"rotation"`
     35 }
     36 
     37 type zoneWall struct {
     38 	Start            [3]float32 `yaml:"start"`
     39 	End              [3]float32 `yaml:"end"`
     40 	Destination      uint32     `yaml:"destination"`
     41 	DestinationIndex uint32     `yaml:"destination_index"`
     42 	ArrivalStart     [3]float32 `yaml:"arrival_start"`
     43 	ArrivalEnd       [3]float32 `yaml:"arrival_end"`
     44 }
     45 
     46 type zonePortal struct {
     47 	Polygon          [][3]float32 `yaml:"polygon"`
     48 	Destination      uint32       `yaml:"destination"`
     49 	DestinationIndex uint32       `yaml:"destination_index"`
     50 }
     51 
     52 type zoneLinks struct {
     53 	Doors   []zoneDoor   `yaml:"doors"`
     54 	Walls   []zoneWall   `yaml:"walls"`
     55 	Portals []zonePortal `yaml:"portals"`
     56 }
     57 
     58 func readZoneLinks() (map[string]map[uint32]zoneLinks, error) {
     59 	contents, err := fs.ReadFile(resources, "zone_links.yaml")
     60 	if err != nil {
     61 		return nil, err
     62 	}
     63 	var links map[string]map[uint32]zoneLinks
     64 	if err := decodeYAML(bytes.NewReader(contents), &links); err != nil {
     65 		return nil, fmt.Errorf("zone links: %w", err)
     66 	}
     67 	return links, nil
     68 }
     69 
     70 func (server *server) zoneDestination(character *character, client *worldClient) (*userData, error) {
     71 	if destination, err := server.missionDestination(character, client); destination != nil || err != nil {
     72 		return destination, err
     73 	}
     74 	// Grid statel 0xc04c0098 runs LineTeleport on collision with its exit beam.
     75 	// Use a provisional two-metre envelope until the native mesh is recovered.
     76 	if client.target && character.playfield == 152 &&
     77 		character.effectiveSkill(161, character.stats.TradeRepair.ComputerLiteracy, [6]uint32{0, 0, 0, 100, 0, 0}) > 90 {
     78 		x, y, z := character.position[0]-242.31323, character.position[1]-4.199992, character.position[2]-199.41223
     79 		if x*x+y*y+z*z <= 2*2 {
     80 			destination := character.userData()
     81 			destination.Playfield = 800
     82 			destination.Position = [3]float32{620.773, 67.30926, 720.8844}
     83 			destination.Rotation = [4]float32{0, 0.61582947, 0, 0.78787947}
     84 			destination.ReturnDoor = nil
     85 			return &destination, nil
     86 		}
     87 	}
     88 
     89 	version := "original"
     90 	if client.target {
     91 		version = "target"
     92 	}
     93 	links := server.zoneLinks[version]
     94 	zone := links[character.playfield]
     95 	// Door transforms are height anchors; use the horizontal entrance position.
     96 	{
     97 		var selected *zoneDoor
     98 		distance := float32(4)
     99 		for index := range zone.Doors {
    100 			door := &zone.Doors[index]
    101 			if door.Destination == 0 && !door.Return {
    102 				continue
    103 			}
    104 			x, z := door.Position[0]-character.position[0], door.Position[2]-character.position[2]
    105 			if squared := x*x + z*z; squared < distance && math.Abs(float64(door.Position[1]-character.position[1])) < 8 {
    106 				selected, distance = door, squared
    107 			}
    108 		}
    109 		if selected != nil {
    110 			if selected.Arrival != nil {
    111 				// Check the door plane before using the authored arrival line.
    112 				x, y, z, w := selected.Rotation[0], selected.Rotation[1], selected.Rotation[2], selected.Rotation[3]
    113 				dx, dy, dz := character.position[0]-selected.Position[0], character.position[1]-selected.Position[1], character.position[2]-selected.Position[2]
    114 				plane := dx*(2*x*z+2*y*w) + dy*(2*y*z-2*x*w) + dz*(1-2*x*x-2*y*y)
    115 				if math.Abs(float64(plane)) >= float64(float32(0.8)) {
    116 					client.enteredDoor = doorOrigin{}
    117 					return nil, nil
    118 				}
    119 			}
    120 			origin := doorOrigin{character.playfield, selected.ID}
    121 			entered := client.enteredDoor != origin
    122 			client.enteredDoor = origin
    123 			if !requirementsMet(selected.Requirements, character.meetsStatRequirement) {
    124 				if entered && selected.RefusalMessage != "" {
    125 					client.doorMessages = append(client.doorMessages, selected.RefusalMessage)
    126 				}
    127 				return nil, nil
    128 			}
    129 			if selected.Arrival != nil {
    130 				destination := character.userData()
    131 				destination.Playfield, destination.Position, destination.Rotation = selected.Destination, selected.Arrival.Position, selected.Arrival.Rotation
    132 				destination.ReturnDoor = nil
    133 				return &destination, nil
    134 			}
    135 			destinationPlayfield, destinationDoor := selected.Destination, selected.DestinationDoor
    136 			if selected.Return && character.returnDoor != nil {
    137 				destinationPlayfield, destinationDoor = character.returnDoor.Playfield, character.returnDoor.Door
    138 			}
    139 			for _, door := range links[destinationPlayfield].Doors {
    140 				if door.ID != destinationDoor {
    141 					continue
    142 				}
    143 				destination := character.userData()
    144 				destination.Playfield, destination.Position, destination.Rotation = destinationPlayfield, door.Position, door.Rotation
    145 				// Face away from the exit and arrive outside its collision area.
    146 				x, y, z, w := door.Rotation[0], door.Rotation[1], door.Rotation[2], door.Rotation[3]
    147 				destination.Position[0] += 2.5 * (2*x*z + 2*y*w)
    148 				destination.Position[2] += 2.5 * (1 - 2*x*x - 2*y*y)
    149 				destination.ReturnDoor = &origin
    150 				if selected.Return {
    151 					destination.ReturnDoor = nil
    152 				}
    153 				return &destination, nil
    154 			}
    155 		}
    156 		client.enteredDoor = doorOrigin{}
    157 	}
    158 
    159 	// Boundary vertices carry a destination line and preserve position along it.
    160 	for _, wall := range zone.Walls {
    161 		inPortal := false
    162 		for _, portal := range zone.Portals {
    163 			if portal.Destination != wall.Destination || portal.DestinationIndex != wall.DestinationIndex {
    164 				continue
    165 			}
    166 			inside := false
    167 			for index, end := range portal.Polygon {
    168 				start := portal.Polygon[(index+len(portal.Polygon)-1)%len(portal.Polygon)]
    169 				if (start[2] > character.position[2]) != (end[2] > character.position[2]) &&
    170 					character.position[0] < (end[0]-start[0])*(character.position[2]-start[2])/(end[2]-start[2])+start[0] {
    171 					inside = !inside
    172 				}
    173 			}
    174 			inPortal = inPortal || inside
    175 		}
    176 		dx, dz := wall.End[0]-wall.Start[0], wall.End[2]-wall.Start[2]
    177 		length := dx*dx + dz*dz
    178 		if length == 0 {
    179 			continue
    180 		}
    181 		factor := ((character.position[0]-wall.Start[0])*dx + (character.position[2]-wall.Start[2])*dz) / length
    182 		if !inPortal && (factor < 0 || factor > 1) {
    183 			continue
    184 		}
    185 		factor = max(0, min(1, factor))
    186 		x, z := wall.Start[0]+factor*dx-character.position[0], wall.Start[2]+factor*dz-character.position[2]
    187 		if !inPortal && x*x+z*z > 4 {
    188 			continue
    189 		}
    190 		destination := character.userData()
    191 		destination.Playfield = wall.Destination
    192 		for index := range destination.Position {
    193 			destination.Position[index] = wall.ArrivalStart[index] + factor*(wall.ArrivalEnd[index]-wall.ArrivalStart[index])
    194 		}
    195 		dx, dz = wall.ArrivalEnd[0]-wall.ArrivalStart[0], wall.ArrivalEnd[2]-wall.ArrivalStart[2]
    196 		length = float32(math.Hypot(float64(dx), float64(dz)))
    197 		if length == 0 {
    198 			continue
    199 		}
    200 		destination.Position[0] -= 8 * dz / length
    201 		destination.Position[2] += 8 * dx / length
    202 		// Keep airborne wall crossings at their altitude, clear of destination terrain.
    203 		if character.flying() {
    204 			destination.Position[1] = character.position[1]
    205 		}
    206 		if ground := server.terrain[destination.Playfield]; ground != nil {
    207 			if height, inside := ground.elevation(destination.Position[0], destination.Position[2]); inside &&
    208 				(!character.flying() || destination.Position[1] < height+0.5) {
    209 				// Native human movement settles half a metre above the ground surface.
    210 				destination.Position[1] = height + 0.5
    211 			}
    212 		}
    213 		destination.ReturnDoor = nil
    214 		return &destination, nil
    215 	}
    216 	return nil, nil
    217 }
    218 
    219 func (server *server) advanceZoneCrossings(now time.Time) {
    220 	// The native client sends start/stop controls rather than every walking frame.
    221 	var destinations []userData
    222 	hallExits := make(map[*worldClient]uint16)
    223 	updates := make(map[*worldClient][][]byte)
    224 	server.advanceCityDoors(now, updates)
    225 	for id, client := range server.clients {
    226 		if !client.ready || client.loading {
    227 			continue
    228 		}
    229 		if len(client.doorMessages) != 0 {
    230 			chat, err := server.chat()
    231 			if err != nil {
    232 				log.Printf("Door message: %v", err)
    233 			} else {
    234 				chat.post(chatSystemMessages{player: id, messages: client.doorMessages})
    235 				client.doorMessages = nil
    236 			}
    237 		}
    238 		character := *server.characters[id]
    239 		if character.health == 0 {
    240 			continue
    241 		}
    242 		character.position = predictedPosition(&character, now)
    243 		// Include stopped players: a native stop packet can already be inside the exit.
    244 		if character.playfield == 4604 {
    245 			side, inside := arrivalHallExit(character.position)
    246 			origin := doorOrigin{4604, uint32(side) + 1}
    247 			if !inside {
    248 				client.enteredDoor = doorOrigin{}
    249 			} else if client.enteredDoor != origin {
    250 				client.enteredDoor = origin
    251 				hallExits[client] = side
    252 			}
    253 			continue
    254 		}
    255 		if !character.movement.active() && character.followTarget == 0 {
    256 			continue
    257 		}
    258 		destination, err := server.zoneDestination(&character, client)
    259 		if err != nil {
    260 			log.Printf("Automatic mission zoning: %v", err)
    261 			continue
    262 		}
    263 		if destination != nil {
    264 			destinations = append(destinations, *destination)
    265 		}
    266 	}
    267 	server.sendCombatUpdates(updates)
    268 	for client, side := range hallExits {
    269 		message, err := server.leaveArrivalHall(client, 0, side)
    270 		if err != nil {
    271 			log.Printf("Arrival Hall exit: %v", err)
    272 		}
    273 		if message != "" {
    274 			client.doorMessages = append(client.doorMessages, message)
    275 		}
    276 	}
    277 	for _, destination := range destinations {
    278 		if err := server.updateUser(destination); err != nil {
    279 			log.Printf("Automatic zoning: %v", err)
    280 		}
    281 	}
    282 }
    283 
    284 func (character *character) runningMovementSpeed(backward bool) float32 {
    285 	// Native movement reads RunSpeed without ability trickle-down.
    286 	runSpeed := float32(100)
    287 	if !character.npc {
    288 		runSpeed = float32(character.baseStats().Speed.RunSpeed)
    289 	}
    290 	runSpeed += float32(character.inventory.equipmentBonus(156) + character.nanoBonus(156))
    291 	if float32(character.health) < float32(character.maximumHealth())*float32(0.15) {
    292 		runSpeed = (runSpeed+500)*(float32(character.health)/(float32(character.maximumHealth())*float32(0.15))) - 500
    293 	}
    294 	speed := 4 + runSpeed*float32(0.005)
    295 	if backward {
    296 		speed = 3 + runSpeed*float32(0.0035)
    297 	}
    298 	if character.flying() {
    299 		// Mode 7 uses the same base and RunSpeed helper in both directions.
    300 		speed = 12 + runSpeed*float32(0.005)
    301 	}
    302 	return max(float32(0.1), min(15, speed))
    303 }