| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- package data
- import (
- maths "f1-game/internal/extend/math"
- "f1-game/internal/pb"
- "f1-game/internal/types"
- "math"
- "os"
- cfile "github.com/cherry-game/cherry/extend/file"
- clog "github.com/cherry-game/cherry/logger"
- cprofile "github.com/cherry-game/cherry/profile"
- "google.golang.org/protobuf/proto"
- )
- var (
- _mapData = &pb.MapData{}
- _mapDataExt = &pb.MapDataEXT{}
- _mapInnerScene = &pb.MapData{}
- )
- func MapData() *pb.MapData {
- return _mapData
- }
- func InnerScene() []*pb.TileData {
- return _mapInnerScene.Tiles
- }
- func MapInfo() *pb.MapInfo {
- return _mapData.MapInfo
- }
- func MapWidth() int32 {
- return MapInfo().Width
- }
- func MapHeight() int32 {
- return MapInfo().Height
- }
- func AreaData() []*pb.AreaData {
- return _mapDataExt.AreaData
- }
- func BornData() map[int32]*pb.BornData {
- return _mapDataExt.BornData
- }
- func ReserveBornData() map[int32]*pb.BornData {
- return _mapDataExt.ReserveBornData
- }
- func ResData() map[int32]*pb.ResData {
- return _mapDataExt.ResData
- }
- func PointToTileID(x, y int32) (int32, bool) {
- return MapData().PointToTileID(x, y)
- }
- func TileIDToPoint(tileID int32) (int32, int32) {
- return MapData().TileIDToPoint(tileID)
- }
- func IsPointOutOfBound(x, y int32) bool {
- return MapData().IsPointOutOfBound(x, y)
- }
- func GetBornTileID(tileID int32) (int32, bool) {
- tileExtend := _mapDataExt.Tiles[tileID]
- if tileExtend == nil || !tileExtend.IsBornPoint {
- return 0, false
- }
- tile := _mapData.Tiles[tileID]
- if tile == nil {
- return 0, false
- }
- if tile.CastleCenter >= 0 {
- return tile.CastleCenter, true
- }
- return tile.CoordIndex, true
- }
- func GetReserveBornTileID(tileID int32) (int32, bool) {
- tileExtend := _mapDataExt.Tiles[tileID]
- if tileExtend == nil || !tileExtend.IsReserveBornPoint {
- return 0, false
- }
- tile := _mapData.Tiles[tileID]
- if tile == nil {
- return 0, false
- }
- if tile.CastleCenter >= 0 {
- return tile.CastleCenter, true
- }
- return tile.CoordIndex, true
- }
- // IsInnerSceneFoundation 判断是否内城地基
- func IsInnerSceneFoundation(tileID int32) bool {
- innerScene := InnerScene()
- if tileID >= int32(len(innerScene)) {
- return true
- }
- return innerScene[tileID].Terrain == pb.TerrainType_tt_foundation
- }
- func (p *mapIDConfig) LoadData(nodeID string) {
- nodeRow, ok := Node.GetByNodeID(nodeID)
- if !ok {
- clog.Fatalf("node id not found. map id = %v", nodeID)
- return
- }
- mapIDRow, ok := MapID.GetByMapID(nodeRow.MapID)
- if !ok {
- clog.Fatalf("map id not found. mapID = %v", nodeRow.MapID)
- }
- var err error
- // !!! 这个必须放在第一个
- err = p.loadMapData(mapIDRow.MapGroup, mapIDRow.MapDataFilepath)
- if err != nil {
- clog.Fatalf("map data not found. mapID = %v", nodeRow.MapID)
- return
- }
- err = p.loadMapDataExt(mapIDRow.MapGroup, mapIDRow.MapDataExtFilepath)
- if err != nil {
- clog.Fatalf("map ext data not found. mapID = %v", nodeRow.MapID)
- return
- }
- err = p.loadMapInnerScene(mapIDRow.MapGroup, mapIDRow.InnerSceneFilepath)
- if err != nil {
- clog.Fatalf("inner scene data not found. mapID = %v", nodeRow.MapID)
- return
- }
- }
- func (p *mapIDConfig) loadMapData(dir, filePath string) error {
- if err := p.loadProtoData(dir, filePath, _mapData); err != nil {
- return err
- }
- // 检测数据是否有效,无效则 Panic
- // 检查系统城上的点有没有被设置成固定障碍点的
- for _, castle := range _mapData.Castles {
- tile := _mapData.Tiles[castle.Center]
- if tile.IsBlock() {
- x, y := _mapData.TileIDToPoint(castle.Center)
- clog.Fatalf("castle center is block. tileID = %d, point = (%d, %d) ,configID = %d", castle.Center, x, y, castle.ConfigId)
- }
- for _, tileID := range castle.Childs {
- tile := _mapData.Tiles[tileID]
- if tile.IsBlock() {
- x, y := _mapData.TileIDToPoint(tileID)
- clog.Fatalf("castle child is block. tileID = %d, point = (%d, %d) ,configID = %d", tileID, x, y, castle.ConfigId)
- }
- }
- for tag, value := range castle.Tag_TilePoints {
- if tag == int32(pb.MapRuleTag_mrt_wall) {
- continue
- }
- for _, tileID := range value.Points {
- tile := _mapData.Tiles[tileID]
- if tile.IsBlock() {
- x, y := _mapData.TileIDToPoint(tileID)
- clog.Fatalf("castle tag tile is block. tileID = %d, point = (%d, %d) ,configID = %d, tag = %s", tileID, x, y, castle.ConfigId, pb.MapRuleTag_name[tag])
- }
- }
- }
- }
- return nil
- }
- func (p *mapIDConfig) loadMapDataExt(dir, filePath string) error {
- if err := p.loadProtoData(dir, filePath, _mapDataExt); err != nil {
- return err
- }
- // 检查资源点有没有导常
- for _, resData := range _mapDataExt.ResData {
- if _mapData.IsTileBlock(resData.Center) {
- clog.Fatalf("res data is block: %+v", resData)
- }
- }
- return nil
- }
- func (p *mapIDConfig) loadMapInnerScene(dir, filePath string) error {
- return p.loadProtoData(dir, filePath, _mapInnerScene)
- }
- func (p *mapIDConfig) loadProtoData(dir, filePath string, m proto.Message) error {
- fullPath, err := cfile.JoinPath(cprofile.Path(), "../data_map/", dir, filePath)
- if err != nil {
- return err
- }
- bytes, err := os.ReadFile(fullPath)
- if err != nil {
- return err
- }
- if err = proto.Unmarshal(bytes, m); err != nil {
- return err
- }
- return nil
- }
- // GetOffsetPoints 获取偏移坐标点,不包含中心点
- func GetOffsetPoints(x, y int32, odd, even types.Points) types.Points {
- var points types.Points
- var offsets types.Points
- if x%2 == 0 {
- offsets = even
- } else {
- offsets = odd
- }
- for _, pt := range offsets {
- if pt.X == 0 && pt.Y == 0 {
- continue
- }
- points.Add(x+pt.X, y+pt.Y)
- }
- return points
- }
- func GetPointDistanceRange(x, y int32, points types.Points) (min int32, max int32) {
- min = math.MaxInt32
- max = math.MinInt32
- for _, pt := range points {
- distance := maths.GetPointDistance(x, y, pt.X, pt.Y)
- if distance < min {
- min = distance
- }
- if distance > max {
- max = distance
- }
- }
- // 没有点,距离为0
- if min == math.MaxInt32 {
- min = 0
- }
- if max == math.MinInt32 {
- max = 0
- }
- return
- }
|