server/admin_map.go (5663B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/json" 6 "maps" 7 "math" 8 "net/http" 9 "slices" 10 "strconv" 11 ) 12 13 type mapMarker struct { 14 Name string `json:"name"` 15 Point [2]float64 `json:"point"` 16 Destination string `json:"destination,omitempty"` 17 Target *[2]float64 `json:"target,omitempty"` 18 } 19 20 func (server *server) mapEntities(w http.ResponseWriter, r *http.Request) { 21 // The client supplies a viewport, never an unbounded NPC enumeration. 22 var bounds [4]float64 23 for index, name := range []string{"left", "bottom", "right", "top"} { 24 value, err := strconv.ParseFloat(r.URL.Query().Get(name), 64) 25 if err != nil || math.IsNaN(value) || math.IsInf(value, 0) { 26 http.Error(w, "Invalid viewport", 400) 27 return 28 } 29 bounds[index] = value 30 } 31 if bounds[2] <= bounds[0] || bounds[3] <= bounds[1] || bounds[2]-bounds[0] > 100000 || bounds[3]-bounds[1] > 100000 { 32 http.Error(w, "Invalid viewport", 400) 33 return 34 } 35 view := r.URL.Query().Get("view") 36 playfield := uint64(0) 37 if view != "world" { 38 var err error 39 playfield, err = strconv.ParseUint(view, 10, 32) 40 if err != nil || playfield == 0 { 41 http.Error(w, "Invalid playfield", 400) 42 return 43 } 44 } 45 point := func(zone uint32, position [3]float32) *[2]float64 { 46 result := [2]float64{float64(position[0]), float64(position[2])} 47 if view == "world" { 48 origin, known := gamedata.MapCoordinates.Playfields[zone] 49 if !known { 50 return nil 51 } 52 result[0] += float64(origin[0]) 53 result[1] += float64(origin[1]) 54 } else if zone != uint32(playfield) { 55 return nil 56 } 57 return &result 58 } 59 visible := func(p *[2]float64) bool { 60 return p != nil && p[0] >= bounds[0] && p[0] <= bounds[2] && p[1] >= bounds[1] && p[1] <= bounds[3] 61 } 62 result := struct { 63 NPCs []mapMarker `json:"npcs"` 64 Warps []mapMarker `json:"warps"` 65 Limited bool `json:"limited"` 66 }{NPCs: []mapMarker{}, Warps: []mapMarker{}} 67 if r.URL.Query().Get("npcs") == "1" && bounds[2]-bounds[0] <= 2000 && bounds[3]-bounds[1] <= 2000 { 68 query := &zoneMapNPCs{point: point, visible: visible, markers: make(map[uint32]mapMarker)} 69 if server.world == nil { 70 query.execute(server) 71 } else { 72 snapshot, err := server.world.snapshot() 73 if err != nil { 74 http.Error(w, err.Error(), http.StatusInternalServerError) 75 return 76 } 77 for zone, owner := range snapshot.zones { 78 if view != "world" && zone != uint32(playfield) { 79 continue 80 } 81 if err := owner.observe(query); err != nil { 82 http.Error(w, err.Error(), http.StatusInternalServerError) 83 return 84 } 85 } 86 } 87 visibleIDs := slices.Sorted(maps.Keys(query.markers)) 88 if len(visibleIDs) > 5000 { 89 result.Limited = true 90 visibleIDs = visibleIDs[:5000] 91 } 92 for _, id := range visibleIDs { 93 result.NPCs = append(result.NPCs, query.markers[id]) 94 } 95 } 96 97 if r.URL.Query().Get("warps") == "1" && bounds[2]-bounds[0] <= 6000 && bounds[3]-bounds[1] <= 6000 { 98 for _, zone := range slices.Sorted(maps.Keys(server.zoneLinks["target"])) { 99 links := server.zoneLinks["target"][zone] 100 add := func(name string, position [3]float32, destination uint32, arrival *[3]float32) { 101 p := point(zone, position) 102 if !visible(p) { 103 return 104 } 105 if len(result.Warps) == 5000 { 106 result.Limited = true 107 return 108 } 109 marker := mapMarker{Name: name, Point: *p, Destination: gamedata.Playfields[destination].Name + " (" + strconv.FormatUint(uint64(destination), 10) + ")"} 110 if name == "Return door" { 111 marker.Destination = "Entry point" 112 } else if destination == 0 { 113 marker.Destination = "Dynamic destination" 114 } else if arrival != nil { 115 marker.Target = point(destination, *arrival) 116 } 117 result.Warps = append(result.Warps, marker) 118 } 119 for _, door := range links.Doors { 120 var arrival *[3]float32 121 if door.Arrival != nil { 122 arrival = &door.Arrival.Position 123 } else { 124 for _, target := range server.zoneLinks["target"][door.Destination].Doors { 125 if target.ID == door.DestinationDoor { 126 p := target.Position 127 arrival = &p 128 break 129 } 130 } 131 } 132 name := "Door" 133 if door.Return { 134 name = "Return door" 135 } 136 add(name, door.Position, door.Destination, arrival) 137 } 138 for _, wall := range links.Walls { 139 position := [3]float32{(wall.Start[0] + wall.End[0]) / 2, (wall.Start[1] + wall.End[1]) / 2, (wall.Start[2] + wall.End[2]) / 2} 140 arrival := [3]float32{(wall.ArrivalStart[0] + wall.ArrivalEnd[0]) / 2, (wall.ArrivalStart[1] + wall.ArrivalEnd[1]) / 2, (wall.ArrivalStart[2] + wall.ArrivalEnd[2]) / 2} 141 add("Zone border", position, wall.Destination, &arrival) 142 } 143 } 144 } 145 w.Header().Set("Content-Type", "application/json") 146 json.NewEncoder(w).Encode(result) 147 } 148 149 // Coordinates and markers are detached values. Warp definitions are immutable 150 // and stay on the HTTP side; live NPC observations run on their zone owner. 151 type zoneMapNPCs struct { 152 point func(uint32, [3]float32) *[2]float64 153 visible func(*[2]float64) bool 154 markers map[uint32]mapMarker 155 } 156 157 func (query *zoneMapNPCs) execute(state *server) error { 158 if state.populationDormant() { 159 return state.visitDormantNPCs(func(id uint32, name string, position [3]float32, alive bool) { 160 point := query.point(state.playfield, position) 161 if alive && query.visible(point) { 162 query.markers[id] = mapMarker{Name: name, Point: *point} 163 } 164 }) 165 } 166 for id, character := range state.characters { 167 if !character.npc || character.health == 0 { 168 continue 169 } 170 point := query.point(character.playfield, character.position) 171 if query.visible(point) { 172 query.markers[id] = mapMarker{Name: character.name, Point: *point} 173 } 174 } 175 return nil 176 }