aorecomp

Anarchy Online classic in the browser

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


server/mission_geometry.go (4231B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"math"
      7 )
      8 
      9 type missionRoom struct {
     10 	template uint16
     11 	floor    int8
     12 	x, z     byte
     13 	rotation byte
     14 }
     15 
     16 type missionLayout struct {
     17 	template                  uint32
     18 	width, depth, floorHeight uint16
     19 	ambient                   [3]byte
     20 	rooms                     []missionRoom
     21 }
     22 
     23 func (layout missionLayout) validate() error {
     24 	// KKR 14.9.1 0x100f1850 / 0x100f5e80: serialized generator and room fields.
     25 	if layout.template == 0 || layout.width == 0 || layout.depth == 0 ||
     26 		layout.floorHeight == 0 || layout.floorHeight > 1000 || len(layout.rooms) == 0 || len(layout.rooms) > 10000 {
     27 		return fmt.Errorf("invalid mission building dimensions or room count")
     28 	}
     29 	for _, room := range layout.rooms {
     30 		if room.template > 10000 || room.floor < -16 || room.floor > 16 || room.x > 32 || room.z > 32 || room.rotation > 3 {
     31 			return fmt.Errorf("invalid mission building room")
     32 		}
     33 	}
     34 	return nil
     35 }
     36 
     37 func missionPlayfieldPacket(player *character, layout missionLayout) ([]byte, error) {
     38 	generator, err := layout.blob()
     39 	if err != nil {
     40 		return nil, err
     41 	}
     42 
     43 	// Model 51105 selects the generated-building factory; running identity is separate.
     44 	body := binary.BigEndian.AppendUint32(nil, 4)
     45 	for _, value := range player.position {
     46 		body = binary.BigEndian.AppendUint32(body, math.Float32bits(value))
     47 	}
     48 	body = append(body, 'a')
     49 	for _, value := range []uint32{51105, player.playfield, player.playfield, 0, 40016, player.playfield} {
     50 		body = binary.BigEndian.AppendUint32(body, value)
     51 	}
     52 
     53 	// HHR DbObject_t::WriteBlob 0x100055e0 writes identity and revision first.
     54 	for _, value := range []uint32{51103, player.playfield, 0} {
     55 		body = binary.BigEndian.AppendUint32(body, value)
     56 	}
     57 	body = append(body, generator...)
     58 	// KKR PlayfieldAnarchyFIIR 0x10145f00 reads two origins; conversion removes the legacy empty name.
     59 	for _, value := range []uint32{0, 0, 0} {
     60 		body = binary.BigEndian.AppendUint32(body, value)
     61 	}
     62 	return n3Packet(0x5f4b1a39, 40016, player.playfield, body), nil
     63 }
     64 
     65 func (layout missionLayout) blob() ([]byte, error) {
     66 	if err := layout.validate(); err != nil {
     67 		return nil, err
     68 	}
     69 	var body []byte
     70 	for _, value := range []uint16{3, layout.width, layout.depth, layout.floorHeight} {
     71 		body = binary.BigEndian.AppendUint16(body, value)
     72 	}
     73 	body = binary.BigEndian.AppendUint32(body, layout.template)
     74 	body = append(body, layout.ambient[:]...)
     75 	body = binary.BigEndian.AppendUint32(body, uint32(len(layout.rooms)))
     76 	for _, room := range layout.rooms {
     77 		body = binary.BigEndian.AppendUint16(body, room.template)
     78 		body = append(body, byte(room.floor), room.x, room.z, room.rotation)
     79 	}
     80 	return body, nil
     81 }
     82 
     83 func decodeMissionLayout(blob []byte) (missionLayout, error) {
     84 	// The database retains the same current generator blob used by the native packet.
     85 	if len(blob) < 19 || binary.BigEndian.Uint16(blob) != 3 {
     86 		return missionLayout{}, fmt.Errorf("invalid mission generator header")
     87 	}
     88 	count := binary.BigEndian.Uint32(blob[15:19])
     89 	if count == 0 || count > 10000 || uint64(len(blob)) != 19+6*uint64(count) {
     90 		return missionLayout{}, fmt.Errorf("invalid mission generator room framing")
     91 	}
     92 	layout := missionLayout{width: binary.BigEndian.Uint16(blob[2:4]), depth: binary.BigEndian.Uint16(blob[4:6]),
     93 		floorHeight: binary.BigEndian.Uint16(blob[6:8]), template: binary.BigEndian.Uint32(blob[8:12]),
     94 		ambient: [3]byte(blob[12:15]), rooms: make([]missionRoom, count)}
     95 	for index := range layout.rooms {
     96 		data := blob[19+6*index : 25+6*index]
     97 		layout.rooms[index] = missionRoom{template: binary.BigEndian.Uint16(data), floor: int8(data[2]), x: data[3], z: data[4], rotation: data[5]}
     98 	}
     99 	if err := layout.validate(); err != nil {
    100 		return missionLayout{}, err
    101 	}
    102 	return layout, nil
    103 }
    104 
    105 func starterMissionLayout(seed uint32) missionLayout {
    106 	layout := missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10, ambient: [3]byte{100, 100, 100},
    107 		rooms: []missionRoom{{template: 0, x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}}
    108 	// Reconstruction selection between reviewed orientations of the connected pair.
    109 	if seed&1 != 0 {
    110 		layout.rooms = []missionRoom{{template: 0, x: 2, z: 1}, {template: 37, x: 1, z: 3, rotation: 2}}
    111 	}
    112 	return layout
    113 }