This repository has been archived on 2023-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
family-lineage/server/plugin/gamemap/gamemap.go

66 lines
1.6 KiB
Go

package gamemap
import (
"encoding/json"
"fmt"
)
// WorldMap is the data structure used game world
type WorldMap struct {
data [64][64]int
max_x int
max_y int
}
// GetTile method is used to grab a tile value with error checking
func (m WorldMap) GetTile(x int, y int) (int, error) {
if x > m.max_x || y > m.max_y {
return -1, fmt.Errorf("Map out of bounds error: %d, %d", x, y)
}
return m.data[x][y], nil
}
// GetJSONRegion method returns a JSON object containing the tile values of everything
// within a given range
func (m WorldMap) GetJSONRegion(startX, endX, startY, endY int) ([]byte, error) {
regionMap := map[int]map[int]int{}
for x := startX; x < endX; x++ {
regionMap[x] = map[int]int{}
for y := startY; y < endY; y++ {
if result, err := m.GetTile(x, y); err != nil {
return nil, err
} else {
regionMap[x][y] = result
}
}
}
jsonString, err := json.Marshal(regionMap)
return jsonString, err
}
// GetJSONRegionAround returns a JSON object of tile data from a center point
func (m WorldMap) GetJSONRegionAround(centerX, centerY, regionRadius int) ([]byte, error) {
jsonString, err := m.GetJSONRegion(centerX-regionRadius, centerX+regionRadius, centerY-regionRadius, centerY+regionRadius)
return jsonString, err
}
// IntializeMap is a method that helps easily
// generate WorldMap objects
func IntializeMap() *WorldMap {
worldMap := new(WorldMap)
worldMap.max_x = 64
worldMap.max_y = 64
worldMap.data = [64][64]int{}
for x := 0; x < worldMap.max_x; x++ {
for y := 0; y < worldMap.max_y; y++ {
worldMap.data[x][y] = 0
}
}
// Add dot at top left for testing
worldMap.data[0][0] = 1
return worldMap
}