aorecomp

Anarchy Online classic in the browser

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


server/scene_playfield.go (2570B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"math"
      7 )
      8 
      9 type scenePlayfield struct {
     10 	cellsX, cellsZ       int
     11 	cellWidth, cellDepth float32
     12 	sizeX, sizeZ         float32
     13 }
     14 
     15 func (database *gameDatabase) outdoorPlayfields() (map[uint32]scenePlayfield, error) {
     16 	playfields := make(map[uint32]scenePlayfield)
     17 	for identity := range database.positions {
     18 		if identity[0] != 1000001 {
     19 			continue
     20 		}
     21 		data, _, err := database.read(identity[0], identity[1])
     22 		if err != nil {
     23 			return nil, err
     24 		}
     25 		if len(data) < 52 || binary.LittleEndian.Uint32(data[:4]) != 8 || binary.LittleEndian.Uint32(data[4:8]) != identity[1] {
     26 			return nil, fmt.Errorf("invalid scene playfield %d", identity[1])
     27 		}
     28 		tilemap := binary.LittleEndian.Uint32(data[40:44])
     29 		zoneSize := binary.LittleEndian.Uint32(data[44:48])
     30 		zoneCount := binary.LittleEndian.Uint32(data[48:52])
     31 		ground, _, err := database.read(1000009, tilemap)
     32 		if err != nil {
     33 			return nil, err
     34 		}
     35 		if len(ground) < 4 || string(ground[:4]) != "CHGA" {
     36 			continue
     37 		}
     38 		if len(ground) < 20 || zoneSize == 0 {
     39 			return nil, fmt.Errorf("invalid scene tilemap %d", tilemap)
     40 		}
     41 		width := uint32(binary.LittleEndian.Uint16(ground[12:14]))
     42 		depth := uint32(binary.LittleEndian.Uint16(ground[14:16]))
     43 		spacing := math.Float32frombits(binary.LittleEndian.Uint32(ground[16:20]))
     44 		cellsX, cellsZ := width/zoneSize, depth/zoneSize
     45 		if cellsX == 0 || cellsZ == 0 || cellsX*cellsZ != zoneCount || !(spacing >= 1 && spacing < math.MaxInt32) {
     46 			return nil, fmt.Errorf("invalid outdoor scene dimensions %d", identity[1])
     47 		}
     48 		// client/game/llr.dll/lifted-0017.c, sub_38029E90/38029EC0: truncate tile size.
     49 		playfields[identity[1]] = scenePlayfield{
     50 			cellsX: int(cellsX), cellsZ: int(cellsZ),
     51 			cellWidth: float32(width*uint32(spacing)) / float32(cellsX),
     52 			cellDepth: float32(depth*uint32(spacing)) / float32(cellsZ),
     53 			sizeX:     float32(width * uint32(spacing)), sizeZ: float32(depth * uint32(spacing)),
     54 		}
     55 	}
     56 	return playfields, nil
     57 }
     58 
     59 func (playfield scenePlayfield) cellID(position [3]float32) uint32 {
     60 	// Source: client/game/ggr.dll/lifted-0000.c, sub_30003930/30003990.
     61 	// CellSurface uses truncation and falls back to cell zero outside the grid.
     62 	id, _ := playfield.cellIndex(position)
     63 	return id
     64 }
     65 
     66 func (playfield scenePlayfield) cellIndex(position [3]float32) (uint32, bool) {
     67 	x, z := int(position[0]/playfield.cellWidth), int(position[2]/playfield.cellDepth)
     68 	if x < 0 || x >= playfield.cellsX || z < 0 || z >= playfield.cellsZ {
     69 		return 0, false
     70 	}
     71 	return uint32(z*playfield.cellsX + x), true
     72 }