aorecomp

Anarchy Online classic in the browser

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


server/mission_world.go (5030B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"math"
      7 )
      8 
      9 func (geometry *sceneGeometry) mission(identity uint32) *missionScene {
     10 	geometry.missionsMutex.RLock()
     11 	defer geometry.missionsMutex.RUnlock()
     12 	return geometry.missions[identity]
     13 }
     14 
     15 func (geometry *sceneGeometry) physical(identity uint32) bool {
     16 	if geometry == nil {
     17 		return false
     18 	}
     19 	if geometry.dungeon(identity) != nil {
     20 		return true
     21 	}
     22 	_, exists := geometry.playfields[identity]
     23 	return exists && geometry.terrain[identity] != nil
     24 }
     25 
     26 func (geometry *sceneGeometry) addMission(identity uint32, layout missionLayout) error {
     27 	_, public := gamedata.Playfields[identity]
     28 	_, outdoor := geometry.playfields[identity]
     29 	if identity == 0 || public || outdoor {
     30 		return fmt.Errorf("mission identity %d conflicts with a public playfield", identity)
     31 	}
     32 	scene, err := geometry.missionScene(layout)
     33 	if err != nil {
     34 		return err
     35 	}
     36 	if err := scene.connectRooms(layout); err != nil {
     37 		return err
     38 	}
     39 
     40 	// Publish only complete scenes; immutable room meshes remain shared across instances.
     41 	geometry.missionsMutex.Lock()
     42 	defer geometry.missionsMutex.Unlock()
     43 	if geometry.missions[identity] != nil {
     44 		return fmt.Errorf("mission identity %d is already registered", identity)
     45 	}
     46 	geometry.missions[identity] = scene
     47 	return nil
     48 }
     49 
     50 func (geometry *sceneGeometry) removeMission(identity uint32) {
     51 	geometry.missionsMutex.Lock()
     52 	delete(geometry.missions, identity)
     53 	geometry.missionsMutex.Unlock()
     54 }
     55 
     56 func (scene *missionScene) connectRooms(layout missionLayout) error {
     57 	// client/game/kkr.dll/lifted-0071.c, sub_360F2F30: opposing portals on the same floor.
     58 	type portal struct {
     59 		room      *placedRoom
     60 		position  [3]float32
     61 		direction byte
     62 		floor     int8
     63 		paired    bool
     64 	}
     65 	var portals []portal
     66 	for index := range scene.rooms {
     67 		room := &scene.rooms[index]
     68 		for _, door := range room.template.doors {
     69 			// CreateDungeonRoom turns internal template links into self-links.
     70 			if door[0] != -1 {
     71 				continue
     72 			}
     73 			position, direction := scene.portal(room, uint16(door[1]))
     74 			portals = append(portals, portal{room: room, position: position, direction: direction, floor: layout.rooms[index].floor})
     75 		}
     76 	}
     77 	for index := range portals {
     78 		first := &portals[index]
     79 		if first.paired {
     80 			continue
     81 		}
     82 		match := -1
     83 		for other := index + 1; other < len(portals); other++ {
     84 			second := &portals[other]
     85 			if second.paired || first.room == second.room || first.floor != second.floor || (first.direction+2)&3 != second.direction {
     86 				continue
     87 			}
     88 			if math.Abs(float64(first.position[0]-second.position[0])) < .1 &&
     89 				math.Abs(float64(first.position[1]-second.position[1])) < .3 &&
     90 				math.Abs(float64(first.position[2]-second.position[2])) < .1 {
     91 				if match != -1 {
     92 					return fmt.Errorf("ambiguous dungeon portal")
     93 				}
     94 				match = other
     95 			}
     96 		}
     97 		if match == -1 {
     98 			if scene.entrance != nil {
     99 				return fmt.Errorf("dungeon must have exactly one exterior portal")
    100 			}
    101 			scene.entrance = first.room
    102 			scene.exitPosition, scene.exitDirection = first.position, first.direction
    103 			continue
    104 		}
    105 		first.paired, portals[match].paired = true, true
    106 		scene.links = append(scene.links, roomLink{[2]*placedRoom{first.room, portals[match].room}, first.position, first.direction})
    107 	}
    108 	if scene.entrance == nil {
    109 		return fmt.Errorf("dungeon has no exterior portal")
    110 	}
    111 
    112 	// Every published room must be reachable from the unique exterior entrance.
    113 	visited := map[*placedRoom]bool{scene.entrance: true}
    114 	queue := []*placedRoom{scene.entrance}
    115 	for len(queue) != 0 {
    116 		room := queue[0]
    117 		queue = queue[1:]
    118 		for _, link := range scene.links {
    119 			for side := range 2 {
    120 				if link.rooms[side] == room && !visited[link.rooms[1-side]] {
    121 					visited[link.rooms[1-side]] = true
    122 					queue = append(queue, link.rooms[1-side])
    123 				}
    124 			}
    125 		}
    126 	}
    127 	if len(visited) != len(scene.rooms) {
    128 		return fmt.Errorf("dungeon contains unreachable rooms")
    129 	}
    130 	return nil
    131 }
    132 
    133 func (scene *missionScene) portal(room *placedRoom, code uint16) ([3]float32, byte) {
    134 	// client/game/llr.dll/lifted-0020.c, sub_38033650: GetDoorPosRot, before facing inward.
    135 	width := int(room.template.bounds[2] - room.template.bounds[0])
    136 	depth := int(room.template.bounds[3] - room.template.bounds[1])
    137 	local := [3]float32{float32(int(code>>2)%width)*scene.ground.spacing + 1, 0, float32(int(code>>2)/width)*scene.ground.spacing + 1}
    138 	switch code & 3 {
    139 	case 0:
    140 		local[2] += .999
    141 	case 1:
    142 		local[0] += .999
    143 	case 2:
    144 		local[2] -= .999
    145 	case 3:
    146 		local[0] -= .999
    147 	}
    148 	atlas := local
    149 	atlas[0] += float32(room.template.bounds[0]) * scene.ground.spacing
    150 	atlas[2] += float32(room.template.bounds[1]) * scene.ground.spacing
    151 	height, _ := scene.ground.floor(atlas)
    152 	local[1] = height - room.template.minHeight
    153 	local[0] -= float32(((width-1)&^1)+1)*.5*scene.ground.spacing + 1
    154 	local[2] -= float32(((depth-1)&^1)+1)*.5*scene.ground.spacing + 1
    155 	local = roomRotate(local, room.rotation)
    156 	for axis := range local {
    157 		local[axis] += room.position[axis]
    158 	}
    159 	return local, (byte(code) + room.rotation) & 3
    160 }