diff --git a/common/constant/lua_type.go b/common/constant/lua_type.go index dde6a1cb..2146d78a 100644 --- a/common/constant/lua_type.go +++ b/common/constant/lua_type.go @@ -130,3 +130,10 @@ const ( REGION_SHAPE_CYLINDER = 3 REGION_SHAPE_POLYGON = 4 ) + +const ( + GROUP_KILL_ALL = 0 + GROUP_KILL_MONSTER = 1 + GROUP_KILL_NPC = 2 + GROUP_KILL_GADGET = 3 +) diff --git a/common/constant/vision_level.go b/common/constant/vision_level.go new file mode 100644 index 00000000..88b63b57 --- /dev/null +++ b/common/constant/vision_level.go @@ -0,0 +1,28 @@ +package constant + +const ( + VISION_LEVEL_NORMAL = 0 + VISION_LEVEL_LITTLE_REMOTE = 1 + VISION_LEVEL_REMOTE = 2 + VISION_LEVEL_SUPER = 3 + VISION_LEVEL_NEARBY = 4 + VISION_LEVEL_SUPER_NEARBY = 5 +) + +type Vision struct { + VisionRange uint32 + GridWidth uint32 +} + +var VISION_LEVEL map[int]*Vision + +func init() { + VISION_LEVEL = map[int]*Vision{ + VISION_LEVEL_NORMAL: {VisionRange: 80, GridWidth: 20}, + VISION_LEVEL_LITTLE_REMOTE: {VisionRange: 160, GridWidth: 40}, + VISION_LEVEL_REMOTE: {VisionRange: 1000, GridWidth: 250}, + VISION_LEVEL_SUPER: {VisionRange: 4000, GridWidth: 1000}, + VISION_LEVEL_NEARBY: {VisionRange: 40, GridWidth: 20}, + VISION_LEVEL_SUPER_NEARBY: {VisionRange: 20, GridWidth: 10}, + } +} diff --git a/docker/3rd/mongo/mongo_cluster/create_cluster.sh b/docker/3rd/mongo/mongo_cluster/create_cluster.sh index d0a12cfa..f3b34574 100755 --- a/docker/3rd/mongo/mongo_cluster/create_cluster.sh +++ b/docker/3rd/mongo/mongo_cluster/create_cluster.sh @@ -35,9 +35,6 @@ sleep 5 echo "init hk4e database table" mongo --host "${IP}" --port 27117 < maxZ { maxZ = int32(blockConfig.BlockRange.Max.Z) } - xLen := uint32(int32(blockConfig.BlockRange.Max.X) - int32(blockConfig.BlockRange.Min.X)) - zLen := uint32(int32(blockConfig.BlockRange.Max.Z) - int32(blockConfig.BlockRange.Min.Z)) - if blockXLen == 0 { - blockXLen = xLen - } else { - if blockXLen != xLen { - logger.Error("scene block x len not same, scene id: %v", sceneLuaConfig.Id) - ok = false - break - } - } - if blockZLen == 0 { - blockZLen = zLen - } else { - if blockZLen != zLen { - logger.Error("scene block z len not same, scene id: %v", sceneLuaConfig.Id) - ok = false - break - } - } } - if !ok { - continue - } - numX := uint32(0) - if blockXLen == 0 { - logger.Debug("scene block x len is zero, scene id: %v", sceneLuaConfig.Id) + numX := uint32(maxX-minX) / 1024 + if numX == 0 { numX = 1 - } else { - numX = uint32(maxX-minX) / blockXLen } - numZ := uint32(0) - if blockZLen == 0 { - logger.Debug("scene block z len is zero, scene id: %v", sceneLuaConfig.Id) + numZ := uint32(maxZ-minZ) / 1024 + if numZ == 0 { numZ = 1 - } else { - numZ = uint32(maxZ-minZ) / blockZLen } - // 将每个block作为aoi格子 并在格子中放入block拥有的所有group aoiManager := alg.NewAoiManager() aoiManager.SetAoiRange(minX, maxX, -1000, 1000, minZ, maxZ) aoiManager.Init3DRectAoiManager(numX, 1, numZ, true) + w.sceneBlockAoiMap[sceneId] = aoiManager + w.sceneEntityAoiMap[sceneId] = make(map[int]*alg.AoiManager) + for visionLevel, vision := range constant.VISION_LEVEL { + numX = uint32(maxX-minX) / vision.GridWidth + if numX == 0 { + numX = 1 + } + numZ = uint32(maxZ-minZ) / vision.GridWidth + if numZ == 0 { + numZ = 1 + } + aoiManager = alg.NewAoiManager() + aoiManager.SetAoiRange(minX, maxX, -1000, 1000, minZ, maxZ) + aoiManager.Init3DRectAoiManager(numX, 1, numZ, true) + w.sceneEntityAoiMap[sceneId][visionLevel] = aoiManager + } for _, block := range sceneLuaConfig.BlockMap { + blockCenter := &gdconf.Vector{X: (block.BlockRange.Min.X + block.BlockRange.Max.X) / 2.0, Y: 0.0, Z: (block.BlockRange.Min.Z + block.BlockRange.Max.Z) / 2.0} + w.sceneBlockAoiMap[sceneId].AddObjectToGridByPos(int64(block.Id), block, blockCenter.X, 0.0, blockCenter.Z) for _, group := range block.GroupMap { - aoiManager.AddObjectToGridByPos(int64(group.Id), group, - group.Pos.X, - 0.0, - group.Pos.Z) + for _, monster := range group.MonsterMap { + objectId := int64(group.Id)<<32 + int64(monster.ConfigId) + w.sceneEntityAoiMap[sceneId][int(monster.VisionLevel)].AddObjectToGridByPos(objectId, monster, monster.Pos.X, 0.0, monster.Pos.Z) + } + for _, npc := range group.NpcMap { + objectId := int64(group.Id)<<32 + int64(npc.ConfigId) + w.sceneEntityAoiMap[sceneId][constant.VISION_LEVEL_NORMAL].AddObjectToGridByPos(objectId, npc, npc.Pos.X, 0.0, npc.Pos.Z) + } + for _, gadget := range group.GadgetMap { + objectId := int64(group.Id)<<32 + int64(gadget.ConfigId) + w.sceneEntityAoiMap[sceneId][int(gadget.VisionLevel)].AddObjectToGridByPos(objectId, gadget, gadget.Pos.X, 0.0, gadget.Pos.Z) + } + for _, region := range group.RegionMap { + objectId := int64(group.Id)<<32 + int64(region.ConfigId) + w.sceneEntityAoiMap[sceneId][constant.VISION_LEVEL_NORMAL].AddObjectToGridByPos(objectId, region, region.Pos.X, 0.0, region.Pos.Z) + } } } - w.sceneBlockAoiMap[uint32(sceneLuaConfig.Id)] = aoiManager } } -func (w *World) IsValidSceneBlockPos(sceneId uint32, x, y, z float32) bool { +func (w *World) IsValidScenePos(sceneId uint32, x, y, z float32) bool { aoiManager, exist := WORLD_MANAGER.sceneBlockAoiMap[sceneId] if !exist { return false diff --git a/gs/game/game_world_scene.go b/gs/game/game_world_scene.go index a740cde4..18f2f330 100644 --- a/gs/game/game_world_scene.go +++ b/gs/game/game_world_scene.go @@ -122,6 +122,7 @@ func (s *Scene) CreateEntityAvatar(player *model.Player, avatarId uint32) uint32 uid: player.PlayerId, avatarId: avatarId, }, + visionLevel: constant.VISION_LEVEL_NORMAL, } return s.CreateEntity(entity) } @@ -142,12 +143,13 @@ func (s *Scene) CreateEntityWeapon(pos, rot *model.Vector) uint32 { constant.FIGHT_PROP_MAX_HP: math.MaxFloat32, constant.FIGHT_PROP_BASE_HP: float32(1), }, - entityType: constant.ENTITY_TYPE_WEAPON, + entityType: constant.ENTITY_TYPE_WEAPON, + visionLevel: constant.VISION_LEVEL_NORMAL, } return s.CreateEntity(entity) } -func (s *Scene) CreateEntityMonster(pos, rot *model.Vector, monsterId uint32, level uint8, configId, groupId uint32) uint32 { +func (s *Scene) CreateEntityMonster(pos, rot *model.Vector, monsterId uint32, level uint8, configId, groupId uint32, visionLevel int) uint32 { fpm := map[uint32]float32{ constant.FIGHT_PROP_BASE_ATTACK: float32(50.0), constant.FIGHT_PROP_CUR_ATTACK: float32(50.0), @@ -193,8 +195,9 @@ func (s *Scene) CreateEntityMonster(pos, rot *model.Vector, monsterId uint32, le monsterEntity: &MonsterEntity{ monsterId: monsterId, }, - configId: configId, - groupId: groupId, + configId: configId, + groupId: groupId, + visionLevel: visionLevel, } return s.CreateEntity(entity) } @@ -222,13 +225,14 @@ func (s *Scene) CreateEntityNpc(pos, rot *model.Vector, npcId, roomId, parentQue parentQuestId: parentQuestId, blockId: blockId, }, - configId: configId, - groupId: groupId, + configId: configId, + groupId: groupId, + visionLevel: constant.VISION_LEVEL_NORMAL, } return s.CreateEntity(entity) } -func (s *Scene) CreateEntityGadgetNormal(pos, rot *model.Vector, gadgetId, gadgetState uint32, gadgetNormalEntity *GadgetNormalEntity, configId, groupId uint32) uint32 { +func (s *Scene) CreateEntityGadgetNormal(pos, rot *model.Vector, gadgetId, gadgetState uint32, gadgetNormalEntity *GadgetNormalEntity, configId, groupId uint32, visionLevel int) uint32 { entityId := s.world.GetNextWorldEntityId(constant.ENTITY_TYPE_GADGET) entity := &Entity{ id: entityId, @@ -251,8 +255,9 @@ func (s *Scene) CreateEntityGadgetNormal(pos, rot *model.Vector, gadgetId, gadge gadgetType: GADGET_TYPE_NORMAL, gadgetNormalEntity: gadgetNormalEntity, }, - configId: configId, - groupId: groupId, + configId: configId, + groupId: groupId, + visionLevel: visionLevel, } return s.CreateEntity(entity) } @@ -284,6 +289,7 @@ func (s *Scene) CreateEntityGadgetClient(pos, rot *model.Vector, entityId, confi propOwnerEntityId: propOwnerEntityId, }, }, + visionLevel: constant.VISION_LEVEL_NORMAL, } if s.CreateEntity(entity) == 0 { return false @@ -327,6 +333,7 @@ func (s *Scene) CreateEntityGadgetVehicle(ownerUid uint32, pos, rot *model.Vecto memberMap: make(map[uint32]*model.Player), }, }, + visionLevel: constant.VISION_LEVEL_NORMAL, } return s.CreateEntity(entity) } @@ -481,6 +488,7 @@ type Entity struct { gadgetEntity *GadgetEntity configId uint32 // LUA配置相关 groupId uint32 + visionLevel int } func (e *Entity) GetId() uint32 { @@ -583,6 +591,10 @@ func (e *Entity) GetGroupId() uint32 { return e.groupId } +func (e *Entity) GetVisionLevel() int { + return e.visionLevel +} + type AvatarEntity struct { uid uint32 avatarId uint32 diff --git a/gs/game/lua_func.go b/gs/game/lua_func.go index 45c187a5..68055992 100644 --- a/gs/game/lua_func.go +++ b/gs/game/lua_func.go @@ -103,17 +103,15 @@ func GetContextGroup(player *model.Player, ctx *lua.LTable, luaState *lua.LState return group } -// GetContextDbSceneGroup 获取上下文中的场景组离线数据对象 -func GetContextDbSceneGroup(player *model.Player, groupId uint32) *model.DbSceneGroup { +// GetContextSceneGroup 获取上下文中的场景组对象 +func GetContextSceneGroup(player *model.Player, groupId uint32) *model.SceneGroup { world := WORLD_MANAGER.GetWorldById(player.WorldId) if world == nil { return nil } owner := world.GetOwner() - dbWorld := owner.GetDbWorld() - dbScene := dbWorld.GetSceneById(player.GetSceneId()) - dbSceneGroup := dbScene.GetSceneGroupById(groupId) - return dbSceneGroup + sceneGroup := owner.GetSceneGroupById(groupId) + return sceneGroup } // RegLuaScriptLibFunc 注册LUA侧ScriptLib调用的Golang方法 @@ -148,6 +146,7 @@ func RegLuaScriptLibFunc() { gdconf.RegScriptLibFunc("RefreshGroup", RefreshGroup) gdconf.RegScriptLibFunc("RemoveExtraGroupSuite", RemoveExtraGroupSuite) gdconf.RegScriptLibFunc("ShowReminder", ShowReminder) + gdconf.RegScriptLibFunc("KillGroupEntity", KillGroupEntity) } type CommonLuaTableParam struct { @@ -157,6 +156,7 @@ type CommonLuaTableParam struct { EntityType int32 `json:"entity_type"` GroupId int32 `json:"group_id"` Suite int32 `json:"suite"` + KillPolicy int32 `json:"kill_policy"` } func GetEntityType(luaState *lua.LState) int { @@ -601,12 +601,12 @@ func GetGroupVariableValue(luaState *lua.LState) int { return 1 } name := luaState.ToString(2) - dbSceneGroup := GetContextDbSceneGroup(player, uint32(groupId)) - if dbSceneGroup == nil { + sceneGroup := GetContextSceneGroup(player, uint32(groupId)) + if sceneGroup == nil { luaState.Push(lua.LNumber(-1)) return 1 } - value := dbSceneGroup.GetVariableByName(name) + value := sceneGroup.GetVariableByName(name) luaState.Push(lua.LNumber(value)) return 1 } @@ -624,12 +624,12 @@ func GetGroupVariableValueByGroup(luaState *lua.LState) int { } name := luaState.ToString(2) groupId := luaState.ToInt(3) - dbSceneGroup := GetContextDbSceneGroup(player, uint32(groupId)) - if dbSceneGroup == nil { + sceneGroup := GetContextSceneGroup(player, uint32(groupId)) + if sceneGroup == nil { luaState.Push(lua.LNumber(-1)) return 1 } - value := dbSceneGroup.GetVariableByName(name) + value := sceneGroup.GetVariableByName(name) luaState.Push(lua.LNumber(value)) return 1 } @@ -652,12 +652,12 @@ func SetGroupVariableValue(luaState *lua.LState) int { } name := luaState.ToString(2) value := luaState.ToInt(3) - dbSceneGroup := GetContextDbSceneGroup(player, uint32(groupId)) - if dbSceneGroup == nil { + sceneGroup := GetContextSceneGroup(player, uint32(groupId)) + if sceneGroup == nil { luaState.Push(lua.LNumber(-1)) return 1 } - dbSceneGroup.SetVariable(name, int32(value)) + sceneGroup.SetVariable(name, int32(value)) luaState.Push(lua.LNumber(0)) return 1 } @@ -676,12 +676,12 @@ func SetGroupVariableValueByGroup(luaState *lua.LState) int { name := luaState.ToString(2) value := luaState.ToInt(3) groupId := luaState.ToInt(4) - dbSceneGroup := GetContextDbSceneGroup(player, uint32(groupId)) - if dbSceneGroup == nil { + sceneGroup := GetContextSceneGroup(player, uint32(groupId)) + if sceneGroup == nil { luaState.Push(lua.LNumber(-1)) return 1 } - dbSceneGroup.SetVariable(name, int32(value)) + sceneGroup.SetVariable(name, int32(value)) luaState.Push(lua.LNumber(0)) return 1 } @@ -704,13 +704,13 @@ func ChangeGroupVariableValue(luaState *lua.LState) int { } name := luaState.ToString(2) change := luaState.ToInt(3) - dbSceneGroup := GetContextDbSceneGroup(player, uint32(groupId)) - if dbSceneGroup == nil { + sceneGroup := GetContextSceneGroup(player, uint32(groupId)) + if sceneGroup == nil { luaState.Push(lua.LNumber(-1)) return 1 } - value := dbSceneGroup.GetVariableByName(name) - dbSceneGroup.SetVariable(name, value+int32(change)) + value := sceneGroup.GetVariableByName(name) + sceneGroup.SetVariable(name, value+int32(change)) luaState.Push(lua.LNumber(0)) return 1 } @@ -729,13 +729,13 @@ func ChangeGroupVariableValueByGroup(luaState *lua.LState) int { name := luaState.ToString(2) change := luaState.ToInt(3) groupId := luaState.ToInt(4) - dbSceneGroup := GetContextDbSceneGroup(player, uint32(groupId)) - if dbSceneGroup == nil { + sceneGroup := GetContextSceneGroup(player, uint32(groupId)) + if sceneGroup == nil { luaState.Push(lua.LNumber(-1)) return 1 } - value := dbSceneGroup.GetVariableByName(name) - dbSceneGroup.SetVariable(name, value+int32(change)) + value := sceneGroup.GetVariableByName(name) + sceneGroup.SetVariable(name, value+int32(change)) luaState.Push(lua.LNumber(0)) return 1 } @@ -915,3 +915,69 @@ func ShowReminder(luaState *lua.LState) int { luaState.Push(lua.LNumber(0)) return 1 } + +func KillGroupEntity(luaState *lua.LState) int { + ctx, ok := luaState.Get(1).(*lua.LTable) + if !ok { + luaState.Push(lua.LNumber(-1)) + return 1 + } + player := GetContextPlayer(ctx, luaState) + if player == nil { + luaState.Push(lua.LNumber(-1)) + return 1 + } + groupId, ok := luaState.GetField(ctx, "groupId").(lua.LNumber) + if !ok { + luaState.Push(lua.LNumber(-1)) + return 1 + } + luaTable, ok := luaState.Get(2).(*lua.LTable) + if !ok { + luaState.Push(lua.LNumber(-1)) + return 1 + } + luaTableParam := new(CommonLuaTableParam) + gdconf.ParseLuaTableToObject[*CommonLuaTableParam](luaTable, luaTableParam) + world := WORLD_MANAGER.GetWorldById(player.WorldId) + if world == nil { + luaState.Push(lua.LNumber(-1)) + return 1 + } + scene := world.GetSceneById(player.GetSceneId()) + group := scene.GetGroupById(uint32(groupId)) + if group == nil { + luaState.Push(lua.LNumber(-1)) + return 1 + } + entityMap := group.GetAllEntity() + switch luaTableParam.KillPolicy { + case constant.GROUP_KILL_ALL: + for _, entity := range entityMap { + GAME.KillEntity(player, scene, entity.GetId(), proto.PlayerDieType_PLAYER_DIE_NONE) + } + case constant.GROUP_KILL_MONSTER: + for _, entity := range entityMap { + if entity.GetEntityType() != constant.ENTITY_TYPE_MONSTER { + continue + } + GAME.KillEntity(player, scene, entity.GetId(), proto.PlayerDieType_PLAYER_DIE_NONE) + } + case constant.GROUP_KILL_NPC: + for _, entity := range entityMap { + if entity.GetEntityType() != constant.ENTITY_TYPE_NPC { + continue + } + GAME.KillEntity(player, scene, entity.GetId(), proto.PlayerDieType_PLAYER_DIE_NONE) + } + case constant.GROUP_KILL_GADGET: + for _, entity := range entityMap { + if entity.GetEntityType() != constant.ENTITY_TYPE_GADGET { + continue + } + GAME.KillEntity(player, scene, entity.GetId(), proto.PlayerDieType_PLAYER_DIE_NONE) + } + } + luaState.Push(lua.LNumber(0)) + return 1 +} diff --git a/gs/game/player_fight_sync.go b/gs/game/player_fight_sync.go index 99833d8b..b3c8bcb5 100644 --- a/gs/game/player_fight_sync.go +++ b/gs/game/player_fight_sync.go @@ -38,7 +38,6 @@ func DoForward[IET model.InvokeEntryType](player *model.Player, invokeHandler *m if invokeHandler.AllLen() == 0 && invokeHandler.AllExceptCurLen() == 0 && invokeHandler.HostLen() == 0 { return } - // TODO aoi漏移除玩家的bug解决了就删掉 if WORLD_MANAGER.IsAiWorld(world) { aiWorldAoi := world.GetAiWorldAoi() pos := GAME.GetPlayerPos(player) @@ -46,7 +45,7 @@ func DoForward[IET model.InvokeEntryType](player *model.Player, invokeHandler *m if gid == math.MaxUint32 { return } - gridList := aiWorldAoi.GetSurrGridListByGid(gid) + gridList := aiWorldAoi.GetSurrGridListByGid(gid, 1) for _, grid := range gridList { objectList := grid.GetObjectList() for uid, wa := range objectList { @@ -59,7 +58,6 @@ func DoForward[IET model.InvokeEntryType](player *model.Player, invokeHandler *m } } } - // TODO aoi漏移除玩家的bug解决了就删掉 if WORLD_MANAGER.IsAiWorld(world) && cmdId != cmd.CombatInvocationsNotify { if invokeHandler.AllLen() > 0 { reflection.SetStructFieldValue(newNtf, forwardField, invokeHandler.EntryListForwardAll) @@ -256,8 +254,24 @@ func (g *Game) handleEntityMove(player *model.Player, world *World, scene *Scene } oldPos := g.GetPlayerPos(player) if !WORLD_MANAGER.IsAiWorld(world) { + if !world.IsValidScenePos(scene.GetId(), float32(pos.X), 0.0, float32(pos.Z)) { + return + } + wait := g.LoadSceneBlockAsync(player, scene, scene, oldPos, pos, "SceneBlockAoiPlayerMove", &SceneBlockLoadInfoCtx{ + World: world, + Scene: scene, + OldPos: oldPos, + NewPos: pos, + AvatarEntityId: entity.GetId(), + }) + if wait { + return + } g.SceneBlockAoiPlayerMove(player, world, scene, oldPos, pos, entity.GetId()) } else { + if !world.IsValidAiWorldPos(scene.GetId(), float32(pos.X), float32(pos.Y), float32(pos.Z)) { + return + } g.AiWorldAoiPlayerMove(player, world, scene, oldPos, pos) } // 场景天气区域变更检测 @@ -343,26 +357,18 @@ func (g *Game) handleEntityMove(player *model.Player, world *World, scene *Scene } func (g *Game) SceneBlockAoiPlayerMove(player *model.Player, world *World, scene *Scene, oldPos *model.Vector, newPos *model.Vector, avatarEntityId uint32) { - if !world.IsValidSceneBlockPos(scene.GetId(), float32(newPos.X), 0.0, float32(newPos.Z)) { - return - } - sceneBlockAoiMap := WORLD_MANAGER.GetSceneBlockAoiMap() - aoiManager, exist := sceneBlockAoiMap[player.GetSceneId()] - if !exist { - logger.Error("get scene block aoi is nil, sceneId: %v, uid: %v", player.GetSceneId(), player.PlayerId) - return - } - oldGid := aoiManager.GetGidByPos(float32(oldPos.X), 0.0, float32(oldPos.Z)) - newGid := aoiManager.GetGidByPos(float32(newPos.X), 0.0, float32(newPos.Z)) - if oldGid != newGid { - // 跨越了block格子 - logger.Debug("player cross scene block grid, oldGid: %v, newGid: %v, uid: %v", oldGid, newGid, player.PlayerId) - } // 加载和卸载的group oldNeighborGroupMap := g.GetNeighborGroup(player.GetSceneId(), oldPos) newNeighborGroupMap := g.GetNeighborGroup(player.GetSceneId(), newPos) + otherPlayerNeighborGroupMap := make(map[uint32]*gdconf.Group) + for _, otherPlayer := range scene.GetAllPlayer() { + otherPlayerPos := g.GetPlayerPos(otherPlayer) + for k, v := range g.GetNeighborGroup(player.GetSceneId(), otherPlayerPos) { + otherPlayerNeighborGroupMap[k] = v + } + } for groupId, groupConfig := range oldNeighborGroupMap { - _, exist = newNeighborGroupMap[groupId] + _, exist := newNeighborGroupMap[groupId] if exist { continue } @@ -372,29 +378,14 @@ func (g *Game) SceneBlockAoiPlayerMove(player *model.Player, world *World, scene g.RemoveSceneGroup(player, scene, groupConfig) } else { // 多人世界group附近没有任何玩家则卸载 - remove := true - for _, otherPlayer := range scene.GetAllPlayer() { - pos := g.GetPlayerPos(otherPlayer) - dx := int32(pos.X) - int32(groupConfig.Pos.X) - if dx < 0 { - dx *= -1 - } - dy := int32(pos.Z) - int32(groupConfig.Pos.Z) - if dy < 0 { - dy *= -1 - } - if dx <= GROUP_LOAD_DISTANCE || dy <= GROUP_LOAD_DISTANCE { - remove = false - break - } - } - if remove { + _, exist = otherPlayerNeighborGroupMap[uint32(groupConfig.Id)] + if !exist { g.RemoveSceneGroup(player, scene, groupConfig) } } } for groupId, groupConfig := range newNeighborGroupMap { - _, exist = oldNeighborGroupMap[groupId] + _, exist := oldNeighborGroupMap[groupId] if exist { continue } @@ -406,7 +397,7 @@ func (g *Game) SceneBlockAoiPlayerMove(player *model.Player, world *World, scene newVisionEntityMap := g.GetVisionEntity(scene, newPos) delEntityIdList := make([]uint32, 0) for entityId := range oldVisionEntityMap { - _, exist = newVisionEntityMap[entityId] + _, exist := newVisionEntityMap[entityId] if exist { continue } @@ -415,7 +406,7 @@ func (g *Game) SceneBlockAoiPlayerMove(player *model.Player, world *World, scene } addEntityIdList := make([]uint32, 0) for entityId := range newVisionEntityMap { - _, exist = oldVisionEntityMap[entityId] + _, exist := oldVisionEntityMap[entityId] if exist { continue } @@ -459,9 +450,6 @@ func (g *Game) SceneBlockAoiPlayerMove(player *model.Player, world *World, scene } func (g *Game) AiWorldAoiPlayerMove(player *model.Player, world *World, scene *Scene, oldPos *model.Vector, newPos *model.Vector) { - if !world.IsValidAiWorldPos(scene.GetId(), float32(newPos.X), float32(newPos.Y), float32(newPos.Z)) { - return - } aiWorldAoi := world.GetAiWorldAoi() oldGid := aiWorldAoi.GetGidByPos(float32(oldPos.X), float32(oldPos.Y), float32(oldPos.Z)) newGid := aiWorldAoi.GetGidByPos(float32(newPos.X), float32(newPos.Y), float32(newPos.Z)) @@ -470,8 +458,8 @@ func (g *Game) AiWorldAoiPlayerMove(player *model.Player, world *World, scene *S logger.Debug("player cross ai world aoi grid, oldGid: %v, oldPos: %+v, newGid: %v, newPos: %+v, uid: %v", oldGid, oldPos, newGid, newPos, player.PlayerId) // 找出本次移动所带来的消失和出现的格子 - oldGridList := aiWorldAoi.GetSurrGridListByGid(oldGid) - newGridList := aiWorldAoi.GetSurrGridListByGid(newGid) + oldGridList := aiWorldAoi.GetSurrGridListByGid(oldGid, 1) + newGridList := aiWorldAoi.GetSurrGridListByGid(newGid, 1) delGridIdList := make([]uint32, 0) for _, oldGrid := range oldGridList { exist := false diff --git a/gs/game/player_login.go b/gs/game/player_login.go index af84840a..f9e94041 100644 --- a/gs/game/player_login.go +++ b/gs/game/player_login.go @@ -217,6 +217,24 @@ func (g *Game) CreatePlayer(userId uint32) *model.Player { player.Pos = &model.Vector{X: 2747, Y: 194, Z: -1719} player.Rot = &model.Vector{X: 0, Y: 307, Z: 0} } + + player.SceneBlockMap = make(map[uint32]*model.SceneBlock) + sceneBlockAoi, exist := WORLD_MANAGER.GetSceneBlockAoiMap()[player.SceneId] + if !exist { + logger.Error("scene not exist in aoi, sceneId: %v", player.SceneId) + return player + } + for _, blockAny := range sceneBlockAoi.GetObjectListByPos(float32(player.Pos.X), 0.0, float32(player.Pos.Z), 1) { + block := blockAny.(*gdconf.Block) + sceneBlock := &model.SceneBlock{ + Uid: player.PlayerId, + BlockId: uint32(block.Id), + SceneGroupMap: make(map[uint32]*model.SceneGroup), + IsNew: true, + } + player.SceneBlockMap[sceneBlock.BlockId] = sceneBlock + } + return player } diff --git a/gs/game/player_scene.go b/gs/game/player_scene.go index 2388ac6f..0599acb3 100644 --- a/gs/game/player_scene.go +++ b/gs/game/player_scene.go @@ -21,9 +21,6 @@ import ( const ( ENTITY_MAX_BATCH_SEND_NUM = 1000 // 单次同步客户端的最大实体数量 - BLOCK_SIZE = 1024 // 区块大小 - GROUP_LOAD_DISTANCE = 250 // 场景组加载距离 取值范围(0,BLOCK_SIZE) - ENTITY_VISION_DISTANCE = 100 // 实体视野距离 取值范围(0,GROUP_LOAD_DISTANCE) ) /************************************************** 接口请求 **************************************************/ @@ -73,29 +70,21 @@ func (g *Game) EnterSceneReadyReq(player *model.Player, payloadMsg pb.Message) { if !WORLD_MANAGER.IsAiWorld(world) { // 卸载旧位置附近的group + otherPlayerNeighborGroupMap := make(map[uint32]*gdconf.Group) + for _, otherPlayer := range oldScene.GetAllPlayer() { + otherPlayerPos := g.GetPlayerPos(otherPlayer) + for k, v := range g.GetNeighborGroup(oldSceneId, otherPlayerPos) { + otherPlayerNeighborGroupMap[k] = v + } + } for _, groupConfig := range g.GetNeighborGroup(oldSceneId, oldPos) { if !world.IsMultiplayerWorld() { // 单人世界直接卸载group g.RemoveSceneGroup(player, oldScene, groupConfig) } else { // 多人世界group附近没有任何玩家则卸载 - remove := true - for _, otherPlayer := range oldScene.GetAllPlayer() { - pos := g.GetPlayerPos(otherPlayer) - dx := int32(pos.X) - int32(groupConfig.Pos.X) - if dx < 0 { - dx *= -1 - } - dy := int32(pos.Z) - int32(groupConfig.Pos.Z) - if dy < 0 { - dy *= -1 - } - if dx <= GROUP_LOAD_DISTANCE || dy <= GROUP_LOAD_DISTANCE { - remove = false - break - } - } - if remove { + _, exist := otherPlayerNeighborGroupMap[uint32(groupConfig.Id)] + if !exist { g.RemoveSceneGroup(player, oldScene, groupConfig) } } @@ -120,15 +109,32 @@ func (g *Game) EnterSceneReadyReq(player *model.Player, payloadMsg pb.Message) { entity.SetRot(newRot) } } + + ntf := &proto.EnterScenePeerNotify{ + DestSceneId: player.GetSceneId(), + PeerId: world.GetPlayerPeerId(player), + HostPeerId: world.GetPlayerPeerId(world.GetOwner()), + EnterSceneToken: req.EnterSceneToken, + } + rsp := &proto.EnterSceneReadyRsp{ + EnterSceneToken: req.EnterSceneToken, + } + wait := g.LoadSceneBlockAsync(player, oldScene, newScene, oldPos, newPos, "EnterSceneReadyReq", &SceneBlockLoadInfoCtx{ + EnterScenePeerNotify: ntf, + EnterSceneReadyRsp: rsp, + }) + if wait { + return + } } - enterScenePeerNotify := &proto.EnterScenePeerNotify{ + ntf := &proto.EnterScenePeerNotify{ DestSceneId: player.GetSceneId(), PeerId: world.GetPlayerPeerId(player), HostPeerId: world.GetPlayerPeerId(world.GetOwner()), EnterSceneToken: req.EnterSceneToken, } - g.SendMsg(cmd.EnterScenePeerNotify, player.PlayerId, player.ClientSeq, enterScenePeerNotify) + g.SendMsg(cmd.EnterScenePeerNotify, player.PlayerId, player.ClientSeq, ntf) rsp := &proto.EnterSceneReadyRsp{ EnterSceneToken: req.EnterSceneToken, @@ -359,33 +365,6 @@ func (g *Game) EnterSceneDoneReq(player *model.Player, payloadMsg pb.Message) { g.AddSceneGroup(player, scene, groupConfig) } } - // TODO 七天神像的group太远不加载的临时解决方案 - if player.GetSceneId() == 3 { - groupSuiteNotify := &proto.GroupSuiteNotify{ - GroupMap: map[uint32]uint32{ - // 不属于任何group的七天神像及其npc_id - // 1209 1276 1280 4904 4905 - // 包含七天神像npc的group及其npc_id - 133004001: 1, // 1201 1202 1203 1204 - 133107001: 1, // 1205 1206 1207 1208 - 133105279: 1, // 1268 - 133008051: 2, // 1272 - 133205002: 1, // 1273 1274 1275 - 133217159: 1, // 1277 - 133210213: 1, // 1278 - 133223052: 1, // 1279 - 133106438: 1, // 1281 - 133302001: 1, // 1282 1283 1285 1286 1288 - 133301642: 2, // 1284 - 133301641: 2, // 1287 - 133303649: 1, // 4900 - 133309184: 1, // 4901 - 133310355: 1, // 4902 - 133307014: 1, // 4903 - }, - } - g.SendMsg(cmd.GroupSuiteNotify, player.PlayerId, player.ClientSeq, groupSuiteNotify) - } } // 同步客户端视野内的场景实体 @@ -403,7 +382,7 @@ func (g *Game) EnterSceneDoneReq(player *model.Player, payloadMsg pb.Message) { if WORLD_MANAGER.IsAiWorld(world) { aiWorldAoi := world.GetAiWorldAoi() - otherWorldAvatarMap := aiWorldAoi.GetObjectListByPos(float32(pos.X), float32(pos.Y), float32(pos.Z)) + otherWorldAvatarMap := aiWorldAoi.GetObjectListByPos(float32(pos.X), float32(pos.Y), float32(pos.Z), 1) entityIdList := make([]uint32, 0) for _, otherWorldAvatarAny := range otherWorldAvatarMap { otherWorldAvatar := otherWorldAvatarAny.(*WorldAvatar) @@ -522,6 +501,185 @@ func (g *Game) EntityForceSyncReq(player *model.Player, payloadMsg pb.Message) { /************************************************** 游戏功能 **************************************************/ +type SceneBlockLoadInfo struct { + Uid uint32 + SceneBlockList []*model.SceneBlock + Origin string + Ctx *SceneBlockLoadInfoCtx +} + +type SceneBlockLoadInfoCtx struct { + World *World + Scene *Scene + OldPos *model.Vector + NewPos *model.Vector + AvatarEntityId uint32 + EnterScenePeerNotify *proto.EnterScenePeerNotify + EnterSceneReadyRsp *proto.EnterSceneReadyRsp +} + +// LoadSceneBlockAsync 异步加载场景区块存档 +func (g *Game) LoadSceneBlockAsync(player *model.Player, oldScene *Scene, newScene *Scene, oldPos *model.Vector, newPos *model.Vector, origin string, ctx *SceneBlockLoadInfoCtx) bool { + oldSceneBlockAoi := WORLD_MANAGER.GetSceneBlockAoiMap()[oldScene.GetId()] + if oldSceneBlockAoi == nil { + logger.Error("scene not exist in aoi, sceneId: %v", oldScene.GetId()) + return false + } + newSceneBlockAoi := WORLD_MANAGER.GetSceneBlockAoiMap()[newScene.GetId()] + if newSceneBlockAoi == nil { + logger.Error("scene not exist in aoi, sceneId: %v", newScene.GetId()) + return false + } + oldGid := oldSceneBlockAoi.GetGidByPos(float32(oldPos.X), 0.0, float32(oldPos.Z)) + newGid := newSceneBlockAoi.GetGidByPos(float32(newPos.X), 0.0, float32(newPos.Z)) + if oldGid == newGid { + return false + } + // 跨越了block格子 + logger.Debug("player cross scene block grid, oldGid: %v, newGid: %v, uid: %v", oldGid, newGid, player.PlayerId) + if player.SceneBlockAsyncLoad { + return true + } + logger.Info("async load player scene block from db, uid: %v", player.PlayerId) + player.SceneBlockAsyncLoad = true + oldGridList := oldSceneBlockAoi.GetSurrGridListByGid(oldGid, 1) + newGridList := newSceneBlockAoi.GetSurrGridListByGid(newGid, 1) + delGridIdList := make([]uint32, 0) + for _, oldGrid := range oldGridList { + exist := false + for _, newGrid := range newGridList { + if oldGrid.GetGid() == newGrid.GetGid() { + exist = true + break + } + } + if exist { + continue + } + delGridIdList = append(delGridIdList, oldGrid.GetGid()) + } + addGridIdList := make([]uint32, 0) + for _, newGrid := range newGridList { + exist := false + for _, oldGrid := range oldGridList { + if newGrid.GetGid() == oldGrid.GetGid() { + exist = true + break + } + } + if exist { + continue + } + addGridIdList = append(addGridIdList, newGrid.GetGid()) + } + loadSceneBlockMap := make(map[uint32]*gdconf.Block) + for _, addGridId := range addGridIdList { + for _, blockAny := range newSceneBlockAoi.GetObjectListByGid(addGridId) { + block := blockAny.(*gdconf.Block) + _, exist := player.SceneBlockMap[uint32(block.Id)] + if exist { + continue + } + loadSceneBlockMap[uint32(block.Id)] = block + } + } + if len(loadSceneBlockMap) == 0 { + return false + } + go func() { + loadSceneBlockList := make([]*model.SceneBlock, 0) + for _, block := range loadSceneBlockMap { + sceneBlock, err := g.db.LoadSceneBlockByUidAndBlockId(player.PlayerId, uint32(block.Id)) + if err != nil { + logger.Error("load scene block from db error: %v, uid: %v", err, player.PlayerId) + continue + } + if sceneBlock == nil { + sceneBlock = &model.SceneBlock{ + Uid: player.PlayerId, + BlockId: uint32(block.Id), + SceneGroupMap: make(map[uint32]*model.SceneGroup), + IsNew: true, + } + } + loadSceneBlockList = append(loadSceneBlockList, sceneBlock) + } + LOCAL_EVENT_MANAGER.GetLocalEventChan() <- &LocalEvent{ + EventId: AsyncLoadSceneBlockFinish, + Msg: &SceneBlockLoadInfo{ + Uid: player.PlayerId, + SceneBlockList: loadSceneBlockList, + Origin: origin, + Ctx: ctx, + }, + } + }() + return true +} + +func (g *Game) OnSceneBlockLoad(sceneBlockLoadInfo *SceneBlockLoadInfo) { + player := USER_MANAGER.GetOnlineUser(sceneBlockLoadInfo.Uid) + if player == nil { + logger.Error("player is nil, uid: %v", sceneBlockLoadInfo.Uid) + return + } + logger.Info("async load player scene block ok, uid: %v", player.PlayerId) + for _, sceneBlock := range sceneBlockLoadInfo.SceneBlockList { + player.SceneBlockMap[sceneBlock.BlockId] = sceneBlock + } + ctx := sceneBlockLoadInfo.Ctx + switch sceneBlockLoadInfo.Origin { + case "SceneBlockAoiPlayerMove": + g.SceneBlockAoiPlayerMove(player, ctx.World, ctx.Scene, ctx.OldPos, ctx.NewPos, ctx.AvatarEntityId) + entity := ctx.Scene.GetEntity(ctx.AvatarEntityId) + entity.SetPos(ctx.NewPos) + case "EnterSceneReadyReq": + g.SendMsg(cmd.EnterScenePeerNotify, player.PlayerId, player.ClientSeq, ctx.EnterScenePeerNotify) + g.SendMsg(cmd.EnterSceneReadyRsp, player.PlayerId, player.ClientSeq, ctx.EnterSceneReadyRsp) + } + player.SceneBlockAsyncLoad = false +} + +func (g *Game) LoadSceneBlockSync(uid uint32, sceneId uint32, pos *model.Vector) map[uint32]*model.SceneBlock { + sceneBlockAoi, exist := WORLD_MANAGER.GetSceneBlockAoiMap()[sceneId] + if !exist { + logger.Error("scene not exist in aoi, sceneId: %v", sceneId) + return nil + } + loadSceneBlockMap := sceneBlockAoi.GetObjectListByPos(float32(pos.X), 0.0, float32(pos.Z), 1) + sceneBlockMap := make(map[uint32]*model.SceneBlock) + for _, blockAny := range loadSceneBlockMap { + block := blockAny.(*gdconf.Block) + sceneBlock, err := g.db.LoadSceneBlockByUidAndBlockId(uid, uint32(block.Id)) + if err != nil { + logger.Error("load scene block from db error: %v, uid: %v", err, uid) + return nil + } + if sceneBlock == nil { + sceneBlock = &model.SceneBlock{ + Uid: uid, + BlockId: uint32(block.Id), + SceneGroupMap: make(map[uint32]*model.SceneGroup), + IsNew: true, + } + } + sceneBlockMap[sceneBlock.BlockId] = sceneBlock + } + logger.Info("sync load player scene block ok, uid: %v", uid) + return sceneBlockMap +} + +func (g *Game) SaveSceneBlockSync(uid uint32, sceneBlockMap map[uint32]*model.SceneBlock) { + for _, sceneBlock := range sceneBlockMap { + err := g.db.SaveSceneBlock(sceneBlock) + if err != nil { + logger.Error("save scene block to db error: %v, uid: %v", err, uid) + continue + } + } + logger.Info("sync save player scene block ok, uid: %v", uid) +} + // AddSceneEntityNotifyToPlayer 添加的场景实体同步给玩家 func (g *Game) AddSceneEntityNotifyToPlayer(player *model.Player, visionType proto.VisionType, entityList []*proto.SceneEntityInfo) { ntf := &proto.SceneEntityAppearNotify{ @@ -740,10 +898,11 @@ func (g *Game) KillEntity(player *model.Player, scene *Scene, entityId uint32, d world := scene.GetWorld() owner := world.GetOwner() - dbWorld := owner.GetDbWorld() - dbScene := dbWorld.GetSceneById(scene.GetId()) - dbSceneGroup := dbScene.GetSceneGroupById(entity.GetGroupId()) - dbSceneGroup.AddKill(entity.GetConfigId()) + sceneGroup := owner.GetSceneGroupById(entity.GetGroupId()) + if sceneGroup == nil { + return + } + sceneGroup.AddKill(entity.GetConfigId()) group.DestroyEntity(entity.GetId()) @@ -792,10 +951,11 @@ func (g *Game) ChangeGadgetState(player *model.Player, entityId uint32, state ui } owner := world.GetOwner() - dbWorld := owner.GetDbWorld() - dbScene := dbWorld.GetSceneById(scene.GetId()) - dbSceneGroup := dbScene.GetSceneGroupById(groupId) - dbSceneGroup.ChangeGadgetState(entity.GetConfigId(), uint8(gadgetEntity.GetGadgetState())) + sceneGroup := owner.GetSceneGroupById(groupId) + if sceneGroup == nil { + return + } + sceneGroup.ChangeGadgetState(entity.GetConfigId(), uint8(gadgetEntity.GetGadgetState())) // 物件状态变更触发器检测 g.GadgetStateChangeTriggerCheck(player, group, entity.GetConfigId(), uint8(gadgetEntity.GetGadgetState())) @@ -804,10 +964,9 @@ func (g *Game) ChangeGadgetState(player *model.Player, entityId uint32, state ui // GetVisionEntity 获取某位置视野内的全部实体 func (g *Game) GetVisionEntity(scene *Scene, pos *model.Vector) map[uint32]*Entity { allEntityMap := scene.GetAllEntity() - ratio := float32(ENTITY_VISION_DISTANCE*ENTITY_VISION_DISTANCE) / float32(GROUP_LOAD_DISTANCE*GROUP_LOAD_DISTANCE) - visionEntity := make(map[uint32]*Entity, int(float32(len(allEntityMap))*ratio)) + visionEntity := make(map[uint32]*Entity) for _, entity := range allEntityMap { - if !g.IsInVision(pos, entity.GetPos()) { + if !g.IsInVision(pos, entity.GetPos(), entity.GetVisionLevel()) { continue } if entity.GetEntityType() == constant.ENTITY_TYPE_AVATAR { @@ -828,7 +987,11 @@ func (g *Game) GetVisionEntity(scene *Scene, pos *model.Vector) map[uint32]*Enti return visionEntity } -func (g *Game) IsInVision(p1 *model.Vector, p2 *model.Vector) bool { +func (g *Game) IsInVision(p1 *model.Vector, p2 *model.Vector, visionLevel int) bool { + vision, exist := constant.VISION_LEVEL[visionLevel] + if !exist { + return false + } dx := int32(p1.X) - int32(p2.X) if dx < 0 { dx *= -1 @@ -837,7 +1000,7 @@ func (g *Game) IsInVision(p1 *model.Vector, p2 *model.Vector) bool { if dy < 0 { dy *= -1 } - if dx > ENTITY_VISION_DISTANCE || dy > ENTITY_VISION_DISTANCE { + if uint32(dx) > vision.VisionRange || uint32(dy) > vision.VisionRange { return false } return true @@ -845,31 +1008,53 @@ func (g *Game) IsInVision(p1 *model.Vector, p2 *model.Vector) bool { // GetNeighborGroup 获取某位置附近的场景组 func (g *Game) GetNeighborGroup(sceneId uint32, pos *model.Vector) map[uint32]*gdconf.Group { - aoiManager, exist := WORLD_MANAGER.GetSceneBlockAoiMap()[sceneId] + sceneEntityAoi, exist := WORLD_MANAGER.GetSceneEntityAoiMap()[sceneId] if !exist { logger.Error("scene not exist in aoi, sceneId: %v", sceneId) return nil } - objectList := aoiManager.GetObjectListByPos(float32(pos.X), 0.0, float32(pos.Z)) - ratio := float32(GROUP_LOAD_DISTANCE*GROUP_LOAD_DISTANCE) / float32(BLOCK_SIZE*BLOCK_SIZE*9) - neighborGroup := make(map[uint32]*gdconf.Group, int(float32(len(objectList))*ratio)) - for _, groupAny := range objectList { - groupConfig := groupAny.(*gdconf.Group) - dx := int32(pos.X) - int32(groupConfig.Pos.X) - if dx < 0 { - dx *= -1 + neighborGroup := make(map[uint32]*gdconf.Group) + for visionLevel, aoiManager := range sceneEntityAoi { + vision := constant.VISION_LEVEL[visionLevel] + objectMap := aoiManager.GetObjectListByPos(float32(pos.X), 0.0, float32(pos.Z), vision.VisionRange/vision.GridWidth+1) + for objectId, obj := range objectMap { + var objPos *gdconf.Vector = nil + switch obj.(type) { + case *gdconf.Monster: + monster := obj.(*gdconf.Monster) + objPos = monster.Pos + case *gdconf.Npc: + npc := obj.(*gdconf.Npc) + objPos = npc.Pos + case *gdconf.Gadget: + gadget := obj.(*gdconf.Gadget) + objPos = gadget.Pos + case *gdconf.Region: + region := obj.(*gdconf.Region) + objPos = region.Pos + } + dx := int32(pos.X) - int32(objPos.X) + if dx < 0 { + dx *= -1 + } + dy := int32(pos.Z) - int32(objPos.Z) + if dy < 0 { + dy *= -1 + } + if uint32(dx) > vision.VisionRange+vision.GridWidth || uint32(dy) > vision.VisionRange+vision.GridWidth { + continue + } + groupId := int32(objectId >> 32) + _, exist = neighborGroup[uint32(groupId)] + if exist { + continue + } + groupConfig := gdconf.GetSceneGroup(groupId) + if groupConfig.DynamicLoad { + continue + } + neighborGroup[uint32(groupConfig.Id)] = groupConfig } - dy := int32(pos.Z) - int32(groupConfig.Pos.Z) - if dy < 0 { - dy *= -1 - } - if dx > GROUP_LOAD_DISTANCE || dy > GROUP_LOAD_DISTANCE { - continue - } - if groupConfig.DynamicLoad { - continue - } - neighborGroup[uint32(groupConfig.Id)] = groupConfig } return neighborGroup } @@ -890,23 +1075,26 @@ func (g *Game) AddSceneGroup(player *model.Player, scene *Scene, groupConfig *gd } // logger.Debug("add scene group, groupId: %v, initSuiteId: %v, uid: %v", groupConfig.Id, initSuiteId, player.PlayerId) g.AddSceneGroupSuiteCore(player, scene, uint32(groupConfig.Id), uint8(initSuiteId)) - // ntf := &proto.GroupSuiteNotify{ - // GroupMap: make(map[uint32]uint32), - // } - // ntf.GroupMap[uint32(groupConfig.Id)] = uint32(initSuiteId) - // g.SendMsg(cmd.GroupSuiteNotify, player.PlayerId, player.ClientSeq, ntf) + if len(groupConfig.NpcMap) > 0 { + ntf := &proto.GroupSuiteNotify{ + GroupMap: make(map[uint32]uint32), + } + ntf.GroupMap[uint32(groupConfig.Id)] = uint32(initSuiteId) + g.SendMsg(cmd.GroupSuiteNotify, player.PlayerId, player.ClientSeq, ntf) + } world := scene.GetWorld() owner := world.GetOwner() - dbWorld := owner.GetDbWorld() - dbScene := dbWorld.GetSceneById(scene.GetId()) - dbSceneGroup := dbScene.GetSceneGroupById(uint32(groupConfig.Id)) + sceneGroup := owner.GetSceneGroupById(uint32(groupConfig.Id)) + if sceneGroup == nil { + return + } for _, variable := range groupConfig.VariableMap { - exist := dbSceneGroup.CheckVariableExist(variable.Name) + exist := sceneGroup.CheckVariableExist(variable.Name) if exist && variable.NoRefresh { continue } - dbSceneGroup.SetVariable(variable.Name, variable.Value) + sceneGroup.SetVariable(variable.Name, variable.Value) } group = scene.GetGroupById(uint32(groupConfig.Id)) @@ -934,11 +1122,12 @@ func (g *Game) RemoveSceneGroup(player *model.Player, scene *Scene, groupConfig for suiteId := range group.GetAllSuite() { scene.RemoveGroupSuite(uint32(groupConfig.Id), suiteId) } - // ntf := &proto.GroupUnloadNotify{ - // GroupList: make([]uint32, 0), - // } - // ntf.GroupList = append(ntf.GroupList, uint32(groupConfig.Id)) - // g.SendMsg(cmd.GroupUnloadNotify, player.PlayerId, player.ClientSeq, ntf) + if len(groupConfig.NpcMap) > 0 { + ntf := &proto.GroupUnloadNotify{ + GroupList: []uint32{uint32(groupConfig.Id)}, + } + g.SendMsg(cmd.GroupUnloadNotify, player.PlayerId, player.ClientSeq, ntf) + } } // AddSceneGroupSuite 场景组中添加场景小组 @@ -960,11 +1149,6 @@ func (g *Game) AddSceneGroupSuite(player *model.Player, groupId uint32, suiteId } scene := world.GetSceneById(player.GetSceneId()) g.AddSceneGroupSuiteCore(player, scene, groupId, suiteId) - // ntf := &proto.GroupSuiteNotify{ - // GroupMap: make(map[uint32]uint32), - // } - // ntf.GroupMap[uint32(groupConfig.Id)] = uint32(suiteId) - // g.SendMsg(cmd.GroupSuiteNotify, player.PlayerId, player.ClientSeq, ntf) group := scene.GetGroupById(groupId) suite := group.GetSuiteById(suiteId) entityIdList := make([]uint32, 0) @@ -997,10 +1181,11 @@ func (g *Game) RemoveSceneGroupSuite(player *model.Player, groupId uint32, suite // RefreshSceneGroupSuite 刷新场景小组 func (g *Game) RefreshSceneGroupSuite(player *model.Player, groupId uint32, suiteId uint8) { - dbWorld := player.GetDbWorld() - dbScene := dbWorld.GetSceneById(player.GetSceneId()) - dbSceneGroup := dbScene.GetSceneGroupById(groupId) - dbSceneGroup.RemoveAllKill() + sceneGroup := player.GetSceneGroupById(groupId) + if sceneGroup == nil { + return + } + sceneGroup.RemoveAllKill() g.RemoveSceneGroupSuite(player, groupId, suiteId) g.AddSceneGroupSuite(player, groupId, suiteId) } @@ -1018,9 +1203,10 @@ func (g *Game) AddSceneGroupSuiteCore(player *model.Player, scene *Scene, groupI } world := scene.GetWorld() owner := world.GetOwner() - dbWorld := owner.GetDbWorld() - dbScene := dbWorld.GetSceneById(scene.GetId()) - dbSceneGroup := dbScene.GetSceneGroupById(groupId) + sceneGroup := owner.GetSceneGroupById(groupId) + if sceneGroup == nil { + return + } entityMap := make(map[uint32]*Entity) for _, monsterConfigId := range suiteConfig.MonsterConfigIdList { monsterConfig, exist := groupConfig.MonsterMap[monsterConfigId] @@ -1028,7 +1214,7 @@ func (g *Game) AddSceneGroupSuiteCore(player *model.Player, scene *Scene, groupI logger.Error("monster config not exist, monsterConfigId: %v", monsterConfigId) continue } - isKill := dbSceneGroup.CheckIsKill(uint32(monsterConfig.ConfigId)) + isKill := sceneGroup.CheckIsKill(uint32(monsterConfig.ConfigId)) if isKill { continue } @@ -1045,7 +1231,7 @@ func (g *Game) AddSceneGroupSuiteCore(player *model.Player, scene *Scene, groupI logger.Error("gadget config not exist, gadgetConfigId: %v", gadgetConfigId) continue } - isKill := dbSceneGroup.CheckIsKill(uint32(gadgetConfig.ConfigId)) + isKill := sceneGroup.CheckIsKill(uint32(gadgetConfig.ConfigId)) if isKill { continue } @@ -1071,16 +1257,17 @@ func (g *Game) AddSceneGroupSuiteCore(player *model.Player, scene *Scene, groupI func (g *Game) CreateConfigEntity(scene *Scene, groupId uint32, entityConfig any) uint32 { world := scene.GetWorld() owner := world.GetOwner() - dbWorld := owner.GetDbWorld() - dbScene := dbWorld.GetSceneById(scene.GetId()) - dbSceneGroup := dbScene.GetSceneGroupById(groupId) + sceneGroup := owner.GetSceneGroupById(groupId) + if sceneGroup == nil { + return 0 + } switch entityConfig.(type) { case *gdconf.Monster: monster := entityConfig.(*gdconf.Monster) return scene.CreateEntityMonster( &model.Vector{X: float64(monster.Pos.X), Y: float64(monster.Pos.Y), Z: float64(monster.Pos.Z)}, &model.Vector{X: float64(monster.Rot.X), Y: float64(monster.Rot.Y), Z: float64(monster.Rot.Z)}, - uint32(monster.MonsterId), uint8(monster.Level), uint32(monster.ConfigId), groupId, + uint32(monster.MonsterId), uint8(monster.Level), uint32(monster.ConfigId), groupId, int(monster.VisionLevel), ) case *gdconf.Npc: npc := entityConfig.(*gdconf.Npc) @@ -1109,12 +1296,13 @@ func (g *Game) CreateConfigEntity(scene *Scene, groupId uint32, entityConfig any }, uint32(gadget.ConfigId), groupId, + int(gadget.VisionLevel), ) } else { state := uint8(gadget.State) - exist := dbSceneGroup.CheckGadgetExist(uint32(gadget.ConfigId)) + exist := sceneGroup.CheckGadgetExist(uint32(gadget.ConfigId)) if exist { - state = dbSceneGroup.GetGadgetState(uint32(gadget.ConfigId)) + state = sceneGroup.GetGadgetState(uint32(gadget.ConfigId)) } return scene.CreateEntityGadgetNormal( &model.Vector{X: float64(gadget.Pos.X), Y: float64(gadget.Pos.Y), Z: float64(gadget.Pos.Z)}, @@ -1124,6 +1312,7 @@ func (g *Game) CreateConfigEntity(scene *Scene, groupId uint32, entityConfig any new(GadgetNormalEntity), uint32(gadget.ConfigId), groupId, + int(gadget.VisionLevel), ) } } @@ -1215,7 +1404,7 @@ func (g *Game) CreateMonster(player *model.Player, pos *model.Vector, monsterId entityId := scene.CreateEntityMonster( pos, rot, monsterId, uint8(random.GetRandomInt32(1, 90)), - 0, 0, + 0, 0, constant.VISION_LEVEL_NORMAL, ) entity := scene.GetEntity(entityId) if entity == nil { @@ -1254,7 +1443,7 @@ func (g *Game) CreateGadget(player *model.Player, pos *model.Vector, gadgetId ui entityId := scene.CreateEntityGadgetNormal( pos, rot, gadgetId, constant.GADGET_STATE_DEFAULT, normalEntity, - 0, 0, + 0, 0, constant.VISION_LEVEL_NORMAL, ) g.AddSceneEntityNotify(player, proto.VisionType_VISION_BORN, []uint32{entityId}, true, false) return entityId @@ -1800,22 +1989,24 @@ func (g *Game) PacketSceneAvatarInfo(scene *Scene, player *model.Player, avatarI WearingFlycloakId: avatar.FlyCloak, CostumeId: avatar.Costume, BornTime: uint32(avatar.BornTime), - TeamResonanceList: make([]uint32, 0), + TeamResonanceList: make([]uint32, 0), // 队伍元素共鸣 } - // for id := range player.TeamConfig.TeamResonances { - // sceneAvatarInfo.TeamResonanceList = append(sceneAvatarInfo.TeamResonanceList, uint32(id)) - // } return sceneAvatarInfo } func (g *Game) PacketSceneMonsterInfo(entity *Entity) *proto.SceneMonsterInfo { + blockId := uint32(0) + if entity.GetGroupId() != 0 { + groupConfig := gdconf.GetSceneGroup(int32(entity.GetGroupId())) + blockId = uint32(groupConfig.BlockId) + } sceneMonsterInfo := &proto.SceneMonsterInfo{ MonsterId: entity.GetMonsterEntity().GetMonsterId(), AuthorityPeerId: 1, BornType: proto.MonsterBornType_MONSTER_BORN_DEFAULT, - // BlockId: 3001, - // TitleId: 3001, - // SpecialNameId: 40, + BlockId: blockId, + TitleId: blockId, + SpecialNameId: 0, } return sceneMonsterInfo } diff --git a/gs/game/player_world.go b/gs/game/player_world.go index 4824c600..26a2c8a5 100644 --- a/gs/game/player_world.go +++ b/gs/game/player_world.go @@ -481,7 +481,6 @@ func (g *Game) GadgetInteractReq(player *model.Player, payloadMsg pb.Message) { func (g *Game) EnterTransPointRegionNotify(player *model.Player, payloadMsg pb.Message) { ntf := payloadMsg.(*proto.EnterTransPointRegionNotify) - _ = ntf world := WORLD_MANAGER.GetWorldById(player.WorldId) if world == nil { @@ -492,13 +491,20 @@ func (g *Game) EnterTransPointRegionNotify(player *model.Player, payloadMsg pb.M return } + dbWorld := player.GetDbWorld() + dbScene := dbWorld.GetSceneById(ntf.SceneId) + unlock := dbScene.CheckPointUnlock(ntf.PointId) + if !unlock { + return + } + dbAvatar := player.GetDbAvatar() for _, worldAvatar := range world.GetPlayerWorldAvatarList(player) { avatar := dbAvatar.GetAvatarById(worldAvatar.GetAvatarId()) if avatar.LifeState == constant.LIFE_STATE_DEAD { g.RevivePlayerAvatar(player, worldAvatar.GetAvatarId()) } - g.AddPlayerAvatarHp(player.PlayerId, worldAvatar.GetAvatarId(), 0.0, true, proto.ChangHpReason_CHANGE_HP_ADD_GM) + g.AddPlayerAvatarHp(player.PlayerId, worldAvatar.GetAvatarId(), 0.0, true, proto.ChangHpReason_CHANGE_HP_ADD_STATUE) } } diff --git a/gs/model/db_world.go b/gs/model/db_world.go index e164b2d3..dd10188b 100644 --- a/gs/model/db_world.go +++ b/gs/model/db_world.go @@ -1,7 +1,6 @@ package model import ( - "hk4e/common/constant" "hk4e/gdconf" ) @@ -15,13 +14,6 @@ type DbScene struct { UnlockPointMap map[uint32]bool UnHidePointMap map[uint32]bool UnlockAreaMap map[uint32]bool - SceneGroupMap map[uint32]*DbSceneGroup -} - -type DbSceneGroup struct { - VariableMap map[string]int32 - KillConfigMap map[uint32]bool - GadgetStateMap map[uint32]uint8 } type MapMark struct { @@ -68,9 +60,6 @@ func (w *DbWorld) GetSceneById(sceneId uint32) *DbScene { if scene.UnlockAreaMap == nil { scene.UnlockAreaMap = make(map[uint32]bool) } - if scene.SceneGroupMap == nil { - scene.SceneGroupMap = make(map[uint32]*DbSceneGroup) - } return scene } @@ -133,59 +122,3 @@ func (s *DbScene) CheckAreaUnlock(areaId uint32) bool { _, exist := s.UnlockAreaMap[areaId] return exist } - -func (s *DbScene) GetSceneGroupById(groupId uint32) *DbSceneGroup { - dbSceneGroup, exist := s.SceneGroupMap[groupId] - if !exist { - dbSceneGroup = &DbSceneGroup{ - VariableMap: make(map[string]int32), - KillConfigMap: make(map[uint32]bool), - GadgetStateMap: make(map[uint32]uint8), - } - s.SceneGroupMap[groupId] = dbSceneGroup - } - return dbSceneGroup -} - -func (g *DbSceneGroup) GetVariableByName(name string) int32 { - return g.VariableMap[name] -} - -func (g *DbSceneGroup) SetVariable(name string, value int32) { - g.VariableMap[name] = value -} - -func (g *DbSceneGroup) CheckVariableExist(name string) bool { - _, exist := g.VariableMap[name] - return exist -} - -func (g *DbSceneGroup) AddKill(configId uint32) { - g.KillConfigMap[configId] = true -} - -func (g *DbSceneGroup) CheckIsKill(configId uint32) bool { - _, exist := g.KillConfigMap[configId] - return exist -} - -func (g *DbSceneGroup) RemoveAllKill() { - g.KillConfigMap = make(map[uint32]bool) -} - -func (g *DbSceneGroup) GetGadgetState(configId uint32) uint8 { - state, exist := g.GadgetStateMap[configId] - if !exist { - return constant.GADGET_STATE_DEFAULT - } - return state -} - -func (g *DbSceneGroup) ChangeGadgetState(configId uint32, state uint8) { - g.GadgetStateMap[configId] = state -} - -func (g *DbSceneGroup) CheckGadgetExist(configId uint32) bool { - _, exist := g.GadgetStateMap[configId] - return exist -} diff --git a/gs/model/player.go b/gs/model/player.go index 9721903b..af9afa8c 100644 --- a/gs/model/player.go +++ b/gs/model/player.go @@ -88,10 +88,12 @@ type Player struct { MpSceneId uint32 `bson:"-" msgpack:"-"` // 多人世界场景 MpPos *Vector `bson:"-" msgpack:"-"` // 多人世界坐标 MpRot *Vector `bson:"-" msgpack:"-"` // 多人世界朝向 + SceneBlockAsyncLoad bool `bson:"-" msgpack:"-"` // 是否正在异步加载场景区块存档 // 特殊数据 - ChatMsgMap map[uint32][]*ChatMsg `bson:"-" msgpack:"-"` // 聊天信息 只从db读写 不保存到redis - RemoteWorldPlayerNum uint32 `bson:"-"` // 远程展示世界内人数 不保存到db 在线同步到redis - MailMap map[uint32]*Mail `bson:"-" msgpack:"-"` // 邮件信息 只从db读写 不保存到redis + ChatMsgMap map[uint32][]*ChatMsg `bson:"-" msgpack:"-"` // 聊天信息 只从db读写 不保存到redis + RemoteWorldPlayerNum uint32 `bson:"-"` // 远程展示世界内人数 不保存到db 在线同步到redis + MailMap map[uint32]*Mail `bson:"-" msgpack:"-"` // 邮件信息 只从db读写 不保存到redis + SceneBlockMap map[uint32]*SceneBlock `bson:"-" msgpack:"-"` // 场景区块存档 只从db读写 不保存到redis } // 存档场景 diff --git a/gs/model/scene_block.go b/gs/model/scene_block.go new file mode 100644 index 00000000..bd798ba9 --- /dev/null +++ b/gs/model/scene_block.go @@ -0,0 +1,88 @@ +package model + +import ( + "hk4e/common/constant" + "hk4e/gdconf" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type SceneBlock struct { + ID primitive.ObjectID `bson:"_id,omitempty"` + Uid uint32 `bson:"uid"` + BlockId uint32 `bson:"block_id"` + SceneGroupMap map[uint32]*SceneGroup `bson:"scene_group_map"` + IsNew bool `bson:"-"` +} + +type SceneGroup struct { + GroupId uint32 `bson:"group_id"` + VariableMap map[string]int32 `bson:"variable_map"` + KillConfigMap map[uint32]bool `bson:"kill_config_map"` + GadgetStateMap map[uint32]uint8 `bson:"gadget_state_map"` +} + +func (p *Player) GetSceneGroupById(groupId uint32) *SceneGroup { + groupConfig := gdconf.GetSceneGroup(int32(groupId)) + if groupConfig == nil { + return nil + } + sceneBlock := p.SceneBlockMap[uint32(groupConfig.BlockId)] + if sceneBlock == nil { + return nil + } + sceneGroup, exist := sceneBlock.SceneGroupMap[groupId] + if !exist { + sceneGroup = &SceneGroup{ + GroupId: groupId, + VariableMap: make(map[string]int32), + KillConfigMap: make(map[uint32]bool), + GadgetStateMap: make(map[uint32]uint8), + } + sceneBlock.SceneGroupMap[groupId] = sceneGroup + } + return sceneGroup +} + +func (g *SceneGroup) GetVariableByName(name string) int32 { + return g.VariableMap[name] +} + +func (g *SceneGroup) SetVariable(name string, value int32) { + g.VariableMap[name] = value +} + +func (g *SceneGroup) CheckVariableExist(name string) bool { + _, exist := g.VariableMap[name] + return exist +} + +func (g *SceneGroup) AddKill(configId uint32) { + g.KillConfigMap[configId] = true +} + +func (g *SceneGroup) CheckIsKill(configId uint32) bool { + _, exist := g.KillConfigMap[configId] + return exist +} + +func (g *SceneGroup) RemoveAllKill() { + g.KillConfigMap = make(map[uint32]bool) +} + +func (g *SceneGroup) GetGadgetState(configId uint32) uint8 { + state, exist := g.GadgetStateMap[configId] + if !exist { + return constant.GADGET_STATE_DEFAULT + } + return state +} + +func (g *SceneGroup) ChangeGadgetState(configId uint32, state uint8) { + g.GadgetStateMap[configId] = state +} + +func (g *SceneGroup) CheckGadgetExist(configId uint32) bool { + _, exist := g.GadgetStateMap[configId] + return exist +} diff --git a/pkg/alg/aoi.go b/pkg/alg/aoi.go index 157eec24..ac7d98b7 100644 --- a/pkg/alg/aoi.go +++ b/pkg/alg/aoi.go @@ -117,34 +117,37 @@ func (a *AoiManager) IsValidAoiPos(x, y, z float32) bool { } // GetSurrGridListByGid 根据格子的gid得到当前周边的格子信息 -func (a *AoiManager) GetSurrGridListByGid(gid uint32) []*Grid { +func (a *AoiManager) GetSurrGridListByGid(gid uint32, level uint32) []*Grid { gridList := make([]*Grid, 0, 27) - // 判断grid是否存在 grid := a.GetGrid(gid) if grid == nil { return nil } // 添加自己 - if grid != nil { - gridList = append(gridList, grid) - } + gridList = append(gridList, grid) // 根据gid得到当前格子所在的x轴编号 idx := gid % (a.numX * a.numZ) % a.numX - // 判断当前格子左边是否还有格子 + // 添加当前格子左边的格子 if idx > 0 { - grid = a.GetGrid(gid - 1) - if grid != nil { + for i := uint32(1); i <= level; i++ { + grid = a.GetGrid(gid - 1*i) + if grid == nil { + break + } gridList = append(gridList, grid) } } - // 判断当前格子右边是否还有格子 + // 添加当前格子右边的格子 if idx < a.numX-1 { - grid = a.GetGrid(gid + 1) - if grid != nil { + for i := uint32(1); i <= level; i++ { + grid = a.GetGrid(gid + 1*i) + if grid == nil { + break + } gridList = append(gridList, grid) } } - // 将x轴当前的格子都取出进行遍历 再分别得到每个格子的平面上下是否有格子 + // 将x轴当前的格子都取出进行遍历 再分别添加每个格子的平面上下的格子 // 得到当前x轴的格子id集合 gidListX := make([]uint32, 0) for _, v := range gridList { @@ -154,22 +157,28 @@ func (a *AoiManager) GetSurrGridListByGid(gid uint32) []*Grid { for _, v := range gidListX { // 计算该格子的idz idz := v % (a.numX * a.numZ) / a.numX - // 判断当前格子平面上方是否还有格子 + // 添加当前格子平面上方的格子 if idz > 0 { - grid = a.GetGrid(v - a.numX) - if grid != nil { + for i := uint32(1); i <= level; i++ { + grid = a.GetGrid(v - a.numX*i) + if grid == nil { + break + } gridList = append(gridList, grid) } } - // 判断当前格子平面下方是否还有格子 + // 添加当前格子平面下方的格子 if idz < a.numZ-1 { - grid = a.GetGrid(v + a.numX) - if grid != nil { + for i := uint32(1); i <= level; i++ { + grid = a.GetGrid(v + a.numX*i) + if grid == nil { + break + } gridList = append(gridList, grid) } } } - // 将xoz平面当前的格子都取出进行遍历 再分别得到每个格子的空间上下是否有格子 + // 将xoz平面当前的格子都取出进行遍历 再分别添加每个格子的空间上下的格子 // 得到当前xoz平面的格子id集合 gidListXOZ := make([]uint32, 0) for _, v := range gridList { @@ -179,17 +188,23 @@ func (a *AoiManager) GetSurrGridListByGid(gid uint32) []*Grid { for _, v := range gidListXOZ { // 计算该格子的idy idy := v / (a.numX * a.numZ) - // 判断当前格子空间上方是否还有格子 + // 添加当前格子空间上方的格子 if idy > 0 { - grid = a.GetGrid(v - a.numX*a.numZ) - if grid != nil { + for i := uint32(1); i <= level; i++ { + grid = a.GetGrid(v - a.numX*a.numZ*i) + if grid == nil { + break + } gridList = append(gridList, grid) } } - // 判断当前格子空间下方是否还有格子 + // 添加当前格子空间下方的格子 if idy < a.numY-1 { - grid = a.GetGrid(v + a.numX*a.numZ) - if grid != nil { + for i := uint32(1); i <= level; i++ { + grid = a.GetGrid(v + a.numX*a.numZ*i) + if grid == nil { + break + } gridList = append(gridList, grid) } } @@ -198,14 +213,14 @@ func (a *AoiManager) GetSurrGridListByGid(gid uint32) []*Grid { } // GetObjectListByPos 通过坐标得到周边格子内的全部object -func (a *AoiManager) GetObjectListByPos(x, y, z float32) map[int64]any { +func (a *AoiManager) GetObjectListByPos(x, y, z float32, level uint32) map[int64]any { // 根据坐标得到当前坐标属于哪个格子id gid := a.GetGidByPos(x, y, z) if gid == math.MaxUint32 { return nil } // 根据格子id得到周边格子的信息 - gridList := a.GetSurrGridListByGid(gid) + gridList := a.GetSurrGridListByGid(gid, level) objectListLen := 0 for _, v := range gridList { tmp := v.GetObjectList() diff --git a/pkg/alg/aoi_test.go b/pkg/alg/aoi_test.go index bf2022a8..d80c9fc2 100644 --- a/pkg/alg/aoi_test.go +++ b/pkg/alg/aoi_test.go @@ -15,7 +15,7 @@ func TestAoiManagerGetSurrGridListByGid(t *testing.T) { aoiManager.Init3DRectAoiManager(3, 3, 3, true) for k := range aoiManager.gridMap { // 得到当前格子周边的九宫格 - gridList := aoiManager.GetSurrGridListByGid(k) + gridList := aoiManager.GetSurrGridListByGid(k, 1) // 得到九宫格所有的id log.Printf("gid: %d gridList len: %d", k, len(gridList)) gidList := make([]uint32, 0, len(gridList))