aorecomp

Anarchy Online classic in the browser

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


server/world_partition.go (5144B)


      1 package main
      2 
      3 import "fmt"
      4 
      5 // Startup has no input producers or simulation workers. Build distinct mutable
      6 // maps first; the caller relinquishes the root maps only after owners start.
      7 func (source *server) partitionWorld() (map[uint32]*server, error) {
      8 	states := make(map[uint32]*server)
      9 	zone := func(playfield uint32) *server {
     10 		if states[playfield] == nil {
     11 			state := source.emptyZone()
     12 			state.populationLoaded = source.populationLoaded
     13 			state.savedNPCs = make(map[uint32]savedNPC)
     14 			state.activeNPCs = make(map[uint32]bool)
     15 			state.simulatedNPCs = make(map[uint32]*character)
     16 			state.lastNPCUpdate = source.lastNPCUpdate
     17 			states[playfield] = state
     18 		}
     19 		return states[playfield]
     20 	}
     21 	for playfield := range source.populations {
     22 		zone(playfield)
     23 	}
     24 	for _, playfield := range source.npcOwners {
     25 		zone(playfield)
     26 	}
     27 	for id, actor := range source.characters {
     28 		zone(actor.playfield).characters[id] = actor
     29 	}
     30 	for id, client := range source.clients {
     31 		actor := source.characters[id]
     32 		if actor == nil {
     33 			return nil, fmt.Errorf("client %d has no character owner", id)
     34 		}
     35 		zone(actor.playfield).clients[id] = client
     36 	}
     37 	for id, actor := range source.fighting {
     38 		zone(actor.playfield).fighting[id] = actor
     39 	}
     40 	for playfield, actors := range source.mobileNPCs {
     41 		zone(playfield).mobileNPCs[playfield] = actors
     42 	}
     43 	for id, saved := range source.savedNPCs {
     44 		zone(saved.State.Character.Position.Playfield).savedNPCs[id] = saved
     45 	}
     46 	for id := range source.activeNPCs {
     47 		if actor := source.characters[id]; actor != nil {
     48 			zone(actor.playfield).activeNPCs[id] = true
     49 		} else if saved, exists := source.savedNPCs[id]; exists {
     50 			zone(saved.State.Character.Position.Playfield).activeNPCs[id] = true
     51 		} else {
     52 			return nil, fmt.Errorf("active NPC %d has no live or saved owner", id)
     53 		}
     54 	}
     55 	for id, actor := range source.simulatedNPCs {
     56 		if source.characters[id] == actor {
     57 			zone(actor.playfield).simulatedNPCs[id] = actor
     58 		}
     59 	}
     60 	// Keep expiry and respawn ordering within each destination zone.
     61 	for _, respawn := range source.npcRespawns {
     62 		state := zone(respawn.npc.playfield)
     63 		state.npcRespawns = append(state.npcRespawns, respawn)
     64 	}
     65 	for _, pet := range source.petExpiry {
     66 		state := zone(pet.playfield)
     67 		state.petExpiry = append(state.petExpiry, pet)
     68 	}
     69 	for _, corpse := range source.corpseExpiry {
     70 		state := zone(corpse.playfield)
     71 		state.corpseExpiry = append(state.corpseExpiry, corpse)
     72 	}
     73 	missions := make(map[uint32]uint32)
     74 	for _, mission := range source.missionExpiry {
     75 		missions[mission.id] = mission.world
     76 		zone(mission.world)
     77 	}
     78 	for id := range source.depletedResources {
     79 		resource, exists := source.worldResources[id]
     80 		if !exists {
     81 			return nil, fmt.Errorf("depleted resource %d has no playfield", id)
     82 		}
     83 		zone(resource.playfield).depletedResources[id] = true
     84 	}
     85 
     86 	// Bags and children share the root item's owner. Unassigned roots and their
     87 	// children remain SQLite-only and never enter a simulation cache.
     88 	for id, placement := range source.itemPlacements {
     89 		root := placement
     90 		if placement.Backpack != 0 {
     91 			if _, exists := source.items[placement.Backpack]; !exists {
     92 				return nil, fmt.Errorf("item %d has no parent bag %d", id, placement.Backpack)
     93 			}
     94 			var assigned bool
     95 			root, assigned = source.itemPlacements[placement.Backpack]
     96 			if !assigned {
     97 				continue
     98 			}
     99 			if root.Backpack != 0 {
    100 				return nil, fmt.Errorf("item %d has a nested bag", id)
    101 			}
    102 		}
    103 		owner := root.Player
    104 		if owner == 0 {
    105 			owner = root.Bank
    106 		}
    107 		if owner == 0 {
    108 			owner = root.Corpse
    109 		}
    110 		var playfield uint32
    111 		switch {
    112 		case owner != 0:
    113 			actor := source.characters[owner]
    114 			if actor != nil {
    115 				playfield = actor.playfield
    116 			} else if playfield = source.npcOwners[owner]; playfield == 0 {
    117 				return nil, fmt.Errorf("item %d has no character owner %d", id, owner)
    118 			}
    119 		case root.Mission != 0:
    120 			var exists bool
    121 			playfield, exists = missions[root.Mission]
    122 			if !exists {
    123 				return nil, fmt.Errorf("item %d has no mission owner %d", id, root.Mission)
    124 			}
    125 		case root.Resource != 0:
    126 			resource, exists := source.worldResources[root.Resource]
    127 			if !exists {
    128 				return nil, fmt.Errorf("item %d has no resource owner %d", id, root.Resource)
    129 			}
    130 			playfield = resource.playfield
    131 		default:
    132 			return nil, fmt.Errorf("item %d has an empty placement", id)
    133 		}
    134 		record, exists := source.items[id]
    135 		if !exists {
    136 			return nil, fmt.Errorf("placed item %d has no record", id)
    137 		}
    138 		state := zone(playfield)
    139 		state.items[id], state.itemPlacements[id] = record, placement
    140 		if item, exists := source.worldItems[id]; exists {
    141 			state.worldItems[id] = item
    142 		}
    143 	}
    144 	return states, nil
    145 }
    146 
    147 func (source *server) relinquishWorld() {
    148 	source.characters, source.clients, source.fighting = nil, nil, nil
    149 	source.npcOwners = nil
    150 	source.items, source.itemPlacements, source.worldItems = nil, nil, nil
    151 	source.savedNPCs, source.activeNPCs, source.depletedResources = nil, nil, nil
    152 	source.mobileNPCs, source.simulatedNPCs, source.npcCells, source.npcLocations = nil, nil, nil, nil
    153 	source.npcRespawns, source.petExpiry, source.corpseExpiry, source.missionExpiry = nil, nil, nil, nil
    154 }