Skip to content

Commit

Permalink
场景实体分级视野距离&&场景区块存档异步加载
Browse files Browse the repository at this point in the history
  • Loading branch information
flswld committed Dec 4, 2023
1 parent 6e9e245 commit ac3c694
Show file tree
Hide file tree
Showing 22 changed files with 935 additions and 388 deletions.
7 changes: 7 additions & 0 deletions common/constant/lua_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
28 changes: 28 additions & 0 deletions common/constant/vision_level.go
Original file line number Diff line number Diff line change
@@ -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},
}
}
5 changes: 2 additions & 3 deletions docker/3rd/mongo/mongo_cluster/create_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ sleep 5

echo "init hk4e database table"
mongo --host "${IP}" --port 27117 <<EOF
sh.enableSharding("node_hk4e")
sh.shardCollection("node_hk4e.region", {"region_id": "hashed"})
sh.enableBalancing("node_hk4e.region")
sh.enableSharding("dispatch_hk4e")
sh.shardCollection("dispatch_hk4e.account", {"account_id": "hashed"})
sh.enableBalancing("dispatch_hk4e.account")
Expand All @@ -51,6 +48,8 @@ sh.shardCollection("gs_hk4e.player", {"player_id": "hashed"})
sh.enableBalancing("gs_hk4e.player")
sh.shardCollection("gs_hk4e.chat_msg", {"uid": "hashed"})
sh.enableBalancing("gs_hk4e.chat_msg")
sh.shardCollection("gs_hk4e.scene_block", {"uid": "hashed"})
sh.enableBalancing("gs_hk4e.scene_block")
sh.startBalancer()
db.adminCommand("flushRouterConfig")
EOF
17 changes: 13 additions & 4 deletions gdconf/game_data_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,19 @@ func initLuaState(luaState *lua.LState) {

visionLevelType := luaState.NewTable()
luaState.SetGlobal("VisionLevelType", visionLevelType)
luaState.SetField(visionLevelType, "VISION_LEVEL_NEARBY", lua.LNumber(1))
luaState.SetField(visionLevelType, "VISION_LEVEL_NORMAL", lua.LNumber(2))
luaState.SetField(visionLevelType, "VISION_LEVEL_REMOTE", lua.LNumber(3))
luaState.SetField(visionLevelType, "VISION_LEVEL_LITTLE_REMOTE", lua.LNumber(4))
luaState.SetField(visionLevelType, "VISION_LEVEL_NORMAL", lua.LNumber(constant.VISION_LEVEL_NORMAL))
luaState.SetField(visionLevelType, "VISION_LEVEL_LITTLE_REMOTE", lua.LNumber(constant.VISION_LEVEL_LITTLE_REMOTE))
luaState.SetField(visionLevelType, "VISION_LEVEL_REMOTE", lua.LNumber(constant.VISION_LEVEL_REMOTE))
luaState.SetField(visionLevelType, "VISION_LEVEL_SUPER", lua.LNumber(constant.VISION_LEVEL_SUPER))
luaState.SetField(visionLevelType, "VISION_LEVEL_NEARBY", lua.LNumber(constant.VISION_LEVEL_NEARBY))
luaState.SetField(visionLevelType, "VISION_LEVEL_SUPER_NEARBY", lua.LNumber(constant.VISION_LEVEL_SUPER_NEARBY))

groupKillPolicy := luaState.NewTable()
luaState.SetGlobal("GroupKillPolicy", groupKillPolicy)
luaState.SetField(groupKillPolicy, "GROUP_KILL_ALL", lua.LNumber(constant.GROUP_KILL_ALL))
luaState.SetField(groupKillPolicy, "GROUP_KILL_MONSTER", lua.LNumber(constant.GROUP_KILL_MONSTER))
luaState.SetField(groupKillPolicy, "GROUP_KILL_NPC", lua.LNumber(constant.GROUP_KILL_NPC))
luaState.SetField(groupKillPolicy, "GROUP_KILL_GADGET", lua.LNumber(constant.GROUP_KILL_GADGET))
}

func newLuaState(luaStr string) *lua.LState {
Expand Down
21 changes: 12 additions & 9 deletions gdconf/scene_lua_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Group struct {
SuiteMap map[int32]*Suite `json:"-"` // 小组配置
LuaStr string `json:"-"` // LUA原始字符串缓存
LuaState *lua.LState `json:"-"` // LUA虚拟机实例
BlockId int32 `json:"-"`
}

type GroupInitConfig struct {
Expand All @@ -84,15 +85,16 @@ type Replaceable struct {
}

type Monster struct {
ConfigId int32 `json:"config_id"`
MonsterId int32 `json:"monster_id"`
Pos *Vector `json:"pos"`
Rot *Vector `json:"rot"`
Level int32 `json:"level"`
AreaId int32 `json:"area_id"`
DropTag string `json:"drop_tag"` // 关联MonsterDropData表
DropId int32 `json:"drop_id"`
IsOneOff bool `json:"isOneoff"`
ConfigId int32 `json:"config_id"`
MonsterId int32 `json:"monster_id"`
Pos *Vector `json:"pos"`
Rot *Vector `json:"rot"`
Level int32 `json:"level"`
AreaId int32 `json:"area_id"`
DropTag string `json:"drop_tag"` // 关联MonsterDropData表
DropId int32 `json:"drop_id"`
IsOneOff bool `json:"isOneoff"`
VisionLevel int32 `json:"vision_level"`
}

type Npc struct {
Expand Down Expand Up @@ -301,6 +303,7 @@ func (g *GameDataConfig) loadGroup(group *Group, block *Block, sceneId int32, bl
group.SuiteMap[int32(len(suiteLuaTableList)-index)] = suite
}
luaState.Close()
group.BlockId = blockId
block.groupMapLoadLock.Lock()
block.GroupMap[group.Id] = group
block.groupMapLoadLock.Unlock()
Expand Down
147 changes: 147 additions & 0 deletions gs/dao/player_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func (d *Dao) InsertChatMsg(chatMsg *model.ChatMsg) error {
return nil
}

func (d *Dao) InsertSceneBlock(sceneBlock *model.SceneBlock) error {
db := d.db.Collection("scene_block")
_, err := db.InsertOne(context.TODO(), sceneBlock)
if err != nil {
return err
}
return nil
}

func (d *Dao) InsertPlayerList(playerList []*model.Player) error {
if len(playerList) == 0 {
return nil
Expand Down Expand Up @@ -68,6 +77,23 @@ func (d *Dao) InsertChatMsgList(chatMsgList []*model.ChatMsg) error {
return nil
}

func (d *Dao) InsertSceneBlockList(sceneBlockList []*model.SceneBlock) error {
if len(sceneBlockList) == 0 {
return nil
}
db := d.db.Collection("scene_block")
modelOperateList := make([]mongo.WriteModel, 0)
for _, sceneBlock := range sceneBlockList {
modelOperate := mongo.NewInsertOneModel().SetDocument(sceneBlock)
modelOperateList = append(modelOperateList, modelOperate)
}
_, err := db.BulkWrite(context.TODO(), modelOperateList)
if err != nil {
return err
}
return nil
}

func (d *Dao) DeletePlayer(playerId uint32) error {
db := d.db.Collection("player")
_, err := db.DeleteOne(context.TODO(), bson.D{{"player_id", playerId}})
Expand All @@ -86,6 +112,15 @@ func (d *Dao) DeleteChatMsg(id primitive.ObjectID) error {
return nil
}

func (d *Dao) DeleteSceneBlock(id primitive.ObjectID) error {
db := d.db.Collection("scene_block")
_, err := db.DeleteOne(context.TODO(), bson.D{{"_id", id}})
if err != nil {
return err
}
return nil
}

func (d *Dao) DeletePlayerList(playerIdList []uint32) error {
if len(playerIdList) == 0 {
return nil
Expand Down Expand Up @@ -120,6 +155,23 @@ func (d *Dao) DeleteChatMsgList(idList []primitive.ObjectID) error {
return nil
}

func (d *Dao) DeleteSceneBlockList(idList []primitive.ObjectID) error {
if len(idList) == 0 {
return nil
}
db := d.db.Collection("scene_block")
modelOperateList := make([]mongo.WriteModel, 0)
for _, id := range idList {
modelOperate := mongo.NewDeleteOneModel().SetFilter(bson.D{{"_id", id}})
modelOperateList = append(modelOperateList, modelOperate)
}
_, err := db.BulkWrite(context.TODO(), modelOperateList)
if err != nil {
return err
}
return nil
}

func (d *Dao) UpdatePlayer(player *model.Player) error {
db := d.db.Collection("player")
_, err := db.UpdateMany(
Expand All @@ -146,6 +198,19 @@ func (d *Dao) UpdateChatMsg(chatMsg *model.ChatMsg) error {
return nil
}

func (d *Dao) UpdateSceneBlock(sceneBlock *model.SceneBlock) error {
db := d.db.Collection("scene_block")
_, err := db.UpdateMany(
context.TODO(),
bson.D{{"_id", sceneBlock.ID}},
bson.D{{"$set", sceneBlock}},
)
if err != nil {
return err
}
return nil
}

func (d *Dao) UpdatePlayerList(playerList []*model.Player) error {
if len(playerList) == 0 {
return nil
Expand Down Expand Up @@ -180,6 +245,23 @@ func (d *Dao) UpdateChatMsgList(chatMsgList []*model.ChatMsg) error {
return nil
}

func (d *Dao) UpdateSceneBlockList(sceneBlockList []*model.SceneBlock) error {
if len(sceneBlockList) == 0 {
return nil
}
db := d.db.Collection("scene_block")
modelOperateList := make([]mongo.WriteModel, 0)
for _, sceneBlock := range sceneBlockList {
modelOperate := mongo.NewUpdateManyModel().SetFilter(bson.D{{"_id", sceneBlock.ID}}).SetUpdate(bson.D{{"$set", sceneBlock}})
modelOperateList = append(modelOperateList, modelOperate)
}
_, err := db.BulkWrite(context.TODO(), modelOperateList)
if err != nil {
return err
}
return nil
}

func (d *Dao) QueryPlayerById(playerId uint32) (*model.Player, error) {
db := d.db.Collection("player")
result := db.FindOne(
Expand Down Expand Up @@ -216,6 +298,24 @@ func (d *Dao) QueryChatMsgById(id primitive.ObjectID) (*model.ChatMsg, error) {
return chatMsg, nil
}

func (d *Dao) QuerySceneBlockById(id primitive.ObjectID) (*model.SceneBlock, error) {
db := d.db.Collection("scene_block")
result := db.FindOne(
context.TODO(),
bson.D{{"_id", id}},
)
sceneBlock := new(model.SceneBlock)
err := result.Decode(sceneBlock)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
} else {
return nil, err
}
}
return sceneBlock, nil
}

func (d *Dao) QueryPlayerList() ([]*model.Player, error) {
db := d.db.Collection("player")
find, err := db.Find(
Expand Down Expand Up @@ -258,6 +358,27 @@ func (d *Dao) QueryChatMsgList() ([]*model.ChatMsg, error) {
return result, nil
}

func (d *Dao) QuerySceneBlockList() ([]*model.SceneBlock, error) {
db := d.db.Collection("scene_block")
find, err := db.Find(
context.TODO(),
bson.D{},
)
if err != nil {
return nil, err
}
result := make([]*model.SceneBlock, 0)
for find.Next(context.TODO()) {
item := new(model.SceneBlock)
err = find.Decode(item)
if err != nil {
return nil, err
}
result = append(result, item)
}
return result, nil
}

func (d *Dao) QueryChatMsgListByUid(uid uint32) ([]*model.ChatMsg, error) {
db := d.db.Collection("chat_msg")
result := make([]*model.ChatMsg, 0)
Expand Down Expand Up @@ -319,3 +440,29 @@ func (d *Dao) DeleteAllUpdateChatMsgByUid(uid uint32) error {
}
return nil
}

func (d *Dao) LoadSceneBlockByUidAndBlockId(uid uint32, blockId uint32) (*model.SceneBlock, error) {
db := d.db.Collection("scene_block")
result := db.FindOne(
context.TODO(),
bson.D{{"$and", []bson.D{{{"uid", uid}}, {{"block_id", blockId}}}}},
)
sceneBlock := new(model.SceneBlock)
err := result.Decode(sceneBlock)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
} else {
return nil, err
}
}
return sceneBlock, nil
}

func (d *Dao) SaveSceneBlock(sceneBlock *model.SceneBlock) error {
if sceneBlock.IsNew {
return d.InsertSceneBlock(sceneBlock)
} else {
return d.UpdateSceneBlock(sceneBlock)
}
}
4 changes: 2 additions & 2 deletions gs/game/audio_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func UpdateFrame(fileData []byte, basePos *model.Vector, rgb bool) {
Z: leftTopPos.Z,
}, new(model.Vector),
uint32(FRAME_COLOR[w][h]), uint32(constant.GADGET_STATE_DEFAULT),
new(GadgetNormalEntity), 0, 0)
new(GadgetNormalEntity), 0, 0, constant.VISION_LEVEL_SUPER)
SCREEN_ENTITY_ID_LIST = append(SCREEN_ENTITY_ID_LIST, entityId)
} else {
if !FRAME[w][h] {
Expand All @@ -329,7 +329,7 @@ func UpdateFrame(fileData []byte, basePos *model.Vector, rgb bool) {
Z: leftTopPos.Z,
}, new(model.Vector),
uint32(GADGET_ID), uint32(constant.GADGET_STATE_DEFAULT),
new(GadgetNormalEntity), 0, 0)
new(GadgetNormalEntity), 0, 0, constant.VISION_LEVEL_SUPER)
SCREEN_ENTITY_ID_LIST = append(SCREEN_ENTITY_ID_LIST, entityId)
}
}
Expand Down
9 changes: 5 additions & 4 deletions gs/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"time"

"hk4e/common/constant"
"hk4e/common/mq"
"hk4e/common/rpc"
"hk4e/gate/kcp"
Expand Down Expand Up @@ -477,7 +478,7 @@ func (g *Game) SendToSceneA(scene *Scene, cmdId uint16, seq uint32, msg pb.Messa
if WORLD_MANAGER.IsAiWorld(world) && SELF != nil {
aiWorldAoi := world.GetAiWorldAoi()
pos := g.GetPlayerPos(SELF)
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)
for uid := range otherWorldAvatarMap {
if aecUid == uint32(uid) {
continue
Expand All @@ -492,7 +493,7 @@ func (g *Game) SendToSceneA(scene *Scene, cmdId uint16, seq uint32, msg pb.Messa
if SELF != nil {
p1 := g.GetPlayerPos(SELF)
p2 := g.GetPlayerPos(v)
if !g.IsInVision(p1, p2) {
if !g.IsInVision(p1, p2, constant.VISION_LEVEL_NORMAL) {
continue
}
}
Expand All @@ -507,7 +508,7 @@ func (g *Game) SendToSceneACV(scene *Scene, cmdId uint16, seq uint32, msg pb.Mes
if WORLD_MANAGER.IsAiWorld(world) && SELF != nil {
aiWorldAoi := world.GetAiWorldAoi()
pos := g.GetPlayerPos(SELF)
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)
for uid := range otherWorldAvatarMap {
player := USER_MANAGER.GetOnlineUser(uint32(uid))
if player == nil {
Expand All @@ -533,7 +534,7 @@ func (g *Game) SendToSceneACV(scene *Scene, cmdId uint16, seq uint32, msg pb.Mes
if SELF != nil {
p1 := g.GetPlayerPos(SELF)
p2 := g.GetPlayerPos(v)
if !g.IsInVision(p1, p2) {
if !g.IsInVision(p1, p2, constant.VISION_LEVEL_NORMAL) {
continue
}
}
Expand Down
Loading

0 comments on commit ac3c694

Please sign in to comment.