diff --git a/gdconf/game_data_config.go b/gdconf/game_data_config.go index 8d00f4ef..f1d3f9a7 100644 --- a/gdconf/game_data_config.go +++ b/gdconf/game_data_config.go @@ -74,7 +74,7 @@ type GameDataConfig struct { OpenStateDataMap map[int32]*OpenStateData // 开放状态 WeatherDataMap map[int32]*WeatherData // 天气 WeatherTemplateMap map[string]map[int32]*WeatherTemplate // 天气模版 - SceneWeatherAreaMap map[int32][]*SceneWeatherArea // 天气区域 + SceneWeatherAreaMap map[int32]map[int32]*SceneWeatherArea // 天气区域 } func InitGameDataConfig() { diff --git a/gdconf/scene_weather_area.go b/gdconf/scene_weather_area.go index 7e137186..22433aa1 100644 --- a/gdconf/scene_weather_area.go +++ b/gdconf/scene_weather_area.go @@ -20,7 +20,7 @@ type AreaPoint struct { } func (g *GameDataConfig) loadSceneWeatherArea() { - g.SceneWeatherAreaMap = make(map[int32][]*SceneWeatherArea) + g.SceneWeatherAreaMap = make(map[int32]map[int32]*SceneWeatherArea) sceneLuaPrefix := g.luaPrefix + "scene/" count := 0 for _, sceneData := range g.SceneDataMap { @@ -43,19 +43,23 @@ func (g *GameDataConfig) loadSceneWeatherArea() { for _, area := range sceneWeatherAreaList { _, exist := g.SceneWeatherAreaMap[sceneId] if !exist { - g.SceneWeatherAreaMap[sceneId] = make([]*SceneWeatherArea, 0) + g.SceneWeatherAreaMap[sceneId] = make(map[int32]*SceneWeatherArea, len(area.Points)) } - g.SceneWeatherAreaMap[sceneId] = append(g.SceneWeatherAreaMap[sceneId], area) + g.SceneWeatherAreaMap[sceneId][area.AreaId] = area count++ } } logger.Info("SceneWeatherArea count: %v", count) } -func GetSceneWeatherAreaListBySceneId(sceneId int32) []*SceneWeatherArea { - return CONF.SceneWeatherAreaMap[sceneId] +func GetSceneWeatherAreaMapBySceneIdAndWeatherAreaId(sceneId int32, WeatherAreaId int32) *SceneWeatherArea { + value, exist := CONF.SceneWeatherAreaMap[sceneId] + if !exist { + return nil + } + return value[WeatherAreaId] } -func GetSceneWeatherAreaMap() map[int32][]*SceneWeatherArea { +func GetSceneWeatherAreaMap() map[int32]map[int32]*SceneWeatherArea { return CONF.SceneWeatherAreaMap } diff --git a/gs/game/lua_func.go b/gs/game/lua_func.go index 4cc40c8f..9c5e2b70 100644 --- a/gs/game/lua_func.go +++ b/gs/game/lua_func.go @@ -154,6 +154,8 @@ func RegLuaScriptLibFunc() { gdconf.RegScriptLibFunc("ChangeGroupVariableValueByGroup", ChangeGroupVariableValueByGroup) gdconf.RegScriptLibFunc("GetRegionEntityCount", GetRegionEntityCount) gdconf.RegScriptLibFunc("CreateGroupTimerEvent", CreateGroupTimerEvent) + gdconf.RegScriptLibFunc("EnterWeatherArea", EnterWeatherArea) + gdconf.RegScriptLibFunc("SetWeatherAreaState", SetWeatherAreaState) } func GetEntityType(luaState *lua.LState) int { @@ -749,3 +751,38 @@ func CreateGroupTimerEvent(luaState *lua.LState) int { luaState.Push(lua.LNumber(0)) return 1 } + +func EnterWeatherArea(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 + } + weatherAreaId := luaState.ToInt(2) + GAME.SetPlayerWeatherByWeatherAreaId(player, uint32(weatherAreaId)) + luaState.Push(lua.LNumber(0)) + return 1 +} + +func SetWeatherAreaState(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 + } + weatherAreaId := luaState.ToInt(2) + climateType := luaState.ToInt(3) + GAME.SetPlayerWeather(player, uint32(weatherAreaId), uint32(climateType)) + luaState.Push(lua.LNumber(0)) + return 1 +} diff --git a/gs/game/player_scene.go b/gs/game/player_scene.go index 48b8cd7f..cbdfddde 100644 --- a/gs/game/player_scene.go +++ b/gs/game/player_scene.go @@ -245,8 +245,8 @@ func (g *Game) SceneInitFinishReq(player *model.Player, payloadMsg pb.Message) { } g.SendMsg(cmd.PlayerEnterSceneInfoNotify, player.PlayerId, player.ClientSeq, playerEnterSceneInfoNotify) - weatherAreaId := g.GetPlayerInWeatherAreaId(player) - g.SendMsg(cmd.SceneAreaWeatherNotify, player.PlayerId, player.ClientSeq, g.PacketSceneAreaWeatherNotify(weatherAreaId)) + weatherAreaId := g.GetPlayerInWeatherAreaId(player, player.Pos) + g.SetPlayerWeatherByWeatherAreaId(player, weatherAreaId) } g.UpdateWorldScenePlayerInfo(player, world) @@ -358,8 +358,7 @@ func (g *Game) EnterSceneDoneReq(player *model.Player, payloadMsg pb.Message) { g.AddSceneEntityNotify(player, visionType, entityIdList, false, false) } - weatherAreaId := g.GetPlayerInWeatherAreaId(player) - g.SendMsg(cmd.SceneAreaWeatherNotify, player.PlayerId, player.ClientSeq, g.PacketSceneAreaWeatherNotify(weatherAreaId)) + g.SetPlayerWeather(player, player.WeatherInfo.WeatherAreaId, player.WeatherInfo.ClimateType) enterSceneDoneRsp := &proto.EnterSceneDoneRsp{ EnterSceneToken: req.EnterSceneToken, @@ -1211,30 +1210,44 @@ func (g *Game) CreateDropGadget(player *model.Player, pos *model.Vector, gadgetI }) } -// GetPlayerInWeatherAreaId 获取玩家所在的天气区域id -func (g *Game) GetPlayerInWeatherAreaId(player *model.Player) int32 { +// GetPosIsInWeatherArea 获取坐标是否在指定的天气区域 +func (g *Game) GetPosIsInWeatherArea(posX, posZ float64, sceneId, weatherAreaId uint32) bool { // 获取场景天气区域配置表 - sceneWeatherAreaList := gdconf.GetSceneWeatherAreaListBySceneId(int32(player.SceneId)) - if sceneWeatherAreaList == nil { - logger.Error("scene weather area config not exist, sceneId: %v", player.SceneId) + weatherAreaConfig := gdconf.GetSceneWeatherAreaMapBySceneIdAndWeatherAreaId(int32(sceneId), int32(weatherAreaId)) + if weatherAreaConfig == nil { + logger.Error("scene weather area config not exist, sceneId: %v, weatherAreaId: %v", sceneId, weatherAreaId) + return false + } + // 寻找玩家所在范围内的天气区域 + polygon := weatherAreaConfig.Points + isInside := false + for i, j := 0, len(polygon)-1; i < len(polygon); i++ { + if (polygon[i].Y > float32(posZ)) != (polygon[j].Y > float32(posZ)) && + float32(posX) < (polygon[j].X-polygon[i].X)*(float32(posZ)-polygon[i].Y)/(polygon[j].Y-polygon[i].Y)+polygon[i].X { + isInside = !isInside + } + j = i + } + if isInside { + return true + } + return false +} + +// GetPlayerInWeatherAreaId 获取玩家所在的天气区域id +func (g *Game) GetPlayerInWeatherAreaId(player *model.Player, newPos *model.Vector) uint32 { + // 获取场景天气区域配置表 + sceneWeatherAreaMap := gdconf.GetSceneWeatherAreaMap()[int32(player.SceneId)] + if sceneWeatherAreaMap == nil { + logger.Error("scene weather area map not exist, sceneId: %v", player.SceneId) return 0 } // 寻找玩家所在范围内的天气区域 - var weatherAreaId int32 + var weatherAreaId uint32 var priority int32 - posX := float32(player.Pos.X) - posZ := float32(player.Pos.Z) // 玩家所在的天气区域 - for _, area := range sceneWeatherAreaList { - polygon := area.Points - isInside := false - for i, j := 0, len(polygon)-1; i < len(polygon); i++ { - if (polygon[i].Y > posZ) != (polygon[j].Y > posZ) && - posX < (polygon[j].X-polygon[i].X)*(posZ-polygon[i].Y)/(polygon[j].Y-polygon[i].Y)+polygon[i].X { - isInside = !isInside - } - j = i - } + for _, area := range sceneWeatherAreaMap { + isInside := g.GetPosIsInWeatherArea(newPos.X, newPos.Z, player.SceneId, uint32(area.AreaId)) // 获取天气数据配置表 weatherConfig := gdconf.GetWeatherDataById(area.AreaId) if weatherConfig == nil { @@ -1247,7 +1260,7 @@ func (g *Game) GetPlayerInWeatherAreaId(player *model.Player) int32 { continue } if isInside && weatherConfig.Priority > priority { - weatherAreaId = area.AreaId + weatherAreaId = uint32(area.AreaId) priority = weatherConfig.Priority } } @@ -1260,13 +1273,101 @@ func (g *Game) SceneWeatherAreaCheck(player *model.Player, oldPos *model.Vector, if oldPos.X == newPos.X && oldPos.Z == newPos.Z { return } - // 获取当前所在的天气区域 - weatherAreaId := g.GetPlayerInWeatherAreaId(player) - // 判断天气区域是否变更 - if player.WeatherAreaId != weatherAreaId { - player.WeatherAreaId = weatherAreaId - g.SendMsg(cmd.SceneAreaWeatherNotify, player.PlayerId, player.ClientSeq, g.PacketSceneAreaWeatherNotify(weatherAreaId)) + // 如果玩家还在历史区域内就不获取当前所在区域 + if g.GetPosIsInWeatherArea(newPos.X, newPos.Z, player.SceneId, player.WeatherInfo.WeatherAreaId) { + logger.Error("nonono") + return } + // 获取当前所在的天气区域 + weatherAreaId := g.GetPlayerInWeatherAreaId(player, newPos) + // 判断天气区域是否变更 + if player.WeatherInfo.WeatherAreaId == weatherAreaId { + return + } + // 设置玩家天气 + g.SetPlayerWeatherByWeatherAreaId(player, weatherAreaId) +} + +// SetPlayerWeatherByWeatherAreaId 通过天气区域设置玩家天气 +func (g *Game) SetPlayerWeatherByWeatherAreaId(player *model.Player, weatherAreaId uint32) { + // 获取天气数据配置表 + weatherConfig := gdconf.GetWeatherDataById(int32(weatherAreaId)) + if weatherConfig == nil { + logger.Error("weather data config not exist, weatherId: %v", weatherAreaId) + return + } + // 如果指定了则使用指定的天气 + var weatherTemplateConfig *gdconf.WeatherTemplate + var weather int32 + if weatherConfig.UseDefaultWeather == 1 && weatherConfig.DefaultWeather != 0 { + weather = weatherConfig.DefaultWeather + weatherTemplateConfig = gdconf.GetWeatherTemplateByTemplateNameAndWeather(weatherConfig.TemplateName, weather) + } else { + // 随机取个天气类型 + weatherTemplateMap := gdconf.GetWeatherTemplateMap()[weatherConfig.TemplateName] + if weatherTemplateMap == nil { + logger.Error("weather template map not exist, templateName: %v", weatherConfig.TemplateName) + return + } + weatherTemplateList := make([]int32, 0, len(weatherTemplateMap)) + for key := range weatherTemplateMap { + weatherTemplateList = append(weatherTemplateList, key) + } + weather = random.GetRandomInt32(0, int32(len(weatherTemplateList)-1)) + weatherTemplateConfig = weatherTemplateMap[weather] + } + // 确保指定的天气模版存在 + if weatherTemplateConfig == nil { + logger.Error("weather template config not exist, templateName: %v, weather: %v", weatherConfig.TemplateName, weather) + return + } + // 随机气象 轮盘赌选择法RWS + var climateType uint32 + climateWeightMap := map[uint32]int32{ + constant.CLIMATE_TYPE_SUNNY: weatherTemplateConfig.Sunny, + constant.CLIMATE_TYPE_CLOUDY: weatherTemplateConfig.Cloudy, + constant.CLIMATE_TYPE_RAIN: weatherTemplateConfig.Rain, + constant.CLIMATE_TYPE_THUNDERSTORM: weatherTemplateConfig.ThunderStorm, + constant.CLIMATE_TYPE_SNOW: weatherTemplateConfig.Snow, + constant.CLIMATE_TYPE_MIST: weatherTemplateConfig.Mist, + constant.CLIMATE_TYPE_DESERT: weatherTemplateConfig.Desert, + } + randNum := random.GetRandomInt32(0, 100-1) + sumWeight := int32(0) + for climate, weight := range climateWeightMap { + sumWeight += weight + if sumWeight > randNum { + climateType = climate + break + } + } + // 设置玩家天气 + g.SetPlayerWeather(player, weatherAreaId, climateType) +} + +// SetPlayerWeather 设置玩家天气 +func (g *Game) SetPlayerWeather(player *model.Player, weatherAreaId uint32, climateType uint32) { + logger.Debug("weather climateType: %v, weatherAreaId: %v", climateType, weatherAreaId) + + // 获取天气数据配置表 + weatherConfig := gdconf.GetWeatherDataById(int32(weatherAreaId)) + if weatherConfig == nil { + logger.Error("weather data config not exist, weatherId: %v", weatherAreaId) + return + } + + // 记录数据 + player.WeatherInfo.WeatherAreaId = weatherAreaId + player.WeatherInfo.ClimateType = climateType + + sceneAreaWeatherNotify := &proto.SceneAreaWeatherNotify{ + WeatherAreaId: uint32(weatherConfig.WeatherAreaId), + WeatherGadgetId: uint32(weatherConfig.GadgetID), + ClimateType: climateType, + TransDuration: 0, + WeatherValueMap: nil, + } + g.SendMsg(cmd.SceneAreaWeatherNotify, player.PlayerId, player.ClientSeq, sceneAreaWeatherNotify) } /************************************************** 打包封装 **************************************************/ @@ -1811,65 +1912,3 @@ func (g *Game) PacketDelTeamEntityNotify(scene *Scene, player *model.Player) *pr } return delTeamEntityNotify } - -// PacketSceneAreaWeatherNotify 场景区域天气通知 -func (g *Game) PacketSceneAreaWeatherNotify(weatherAreaId int32) *proto.SceneAreaWeatherNotify { - // 获取天气数据配置表 - weatherConfig := gdconf.GetWeatherDataById(weatherAreaId) - if weatherConfig == nil { - logger.Error("weather data config not exist, weatherId: %v", weatherAreaId) - return new(proto.SceneAreaWeatherNotify) - } - - // 如果指定了则使用指定的天气 - var weatherTemplateConfig *gdconf.WeatherTemplate - if weatherConfig.UseDefaultWeather == 1 && weatherConfig.DefaultWeather != 0 { - weatherTemplateConfig = gdconf.GetWeatherTemplateByTemplateNameAndWeather(weatherConfig.TemplateName, weatherConfig.DefaultWeather) - } else { - // 随机取个天气类型 - weatherTemplateMap := gdconf.GetWeatherTemplateMap()[weatherConfig.TemplateName] - if weatherTemplateMap == nil { - logger.Error("weather template map not exist, templateName: %v", weatherConfig.TemplateName) - return new(proto.SceneAreaWeatherNotify) - } - weatherTemplateList := make([]int32, 0, len(weatherTemplateMap)) - for key := range weatherTemplateMap { - weatherTemplateList = append(weatherTemplateList, key) - } - weatherTemplateConfig = weatherTemplateMap[random.GetRandomInt32(0, int32(len(weatherTemplateList)))] - } - // 确保指定的天气模版存在 - if weatherTemplateConfig == nil { - logger.Error("weather template config not exist, templateName: %v", weatherConfig.TemplateName) - return new(proto.SceneAreaWeatherNotify) - } - // 随机气象 轮盘赌选择法RWS - var climateType uint32 - climateWeightMap := map[uint32]int32{ - constant.CLIMATE_TYPE_SUNNY: weatherTemplateConfig.Sunny, - constant.CLIMATE_TYPE_CLOUDY: weatherTemplateConfig.Cloudy, - constant.CLIMATE_TYPE_RAIN: weatherTemplateConfig.Rain, - constant.CLIMATE_TYPE_THUNDERSTORM: weatherTemplateConfig.ThunderStorm, - constant.CLIMATE_TYPE_SNOW: weatherTemplateConfig.Snow, - constant.CLIMATE_TYPE_MIST: weatherTemplateConfig.Mist, - constant.CLIMATE_TYPE_DESERT: weatherTemplateConfig.Desert, - } - randNum := random.GetRandomInt32(0, 100-1) - sumWeight := int32(0) - for climate, weight := range climateWeightMap { - sumWeight += weight - if sumWeight > randNum { - climateType = climate - break - } - } - logger.Debug("weather climateType: %v, weatherAreaId: %v", climateType, weatherAreaId) - sceneAreaWeatherNotify := &proto.SceneAreaWeatherNotify{ - WeatherAreaId: uint32(weatherConfig.WeatherAreaId), - WeatherGadgetId: uint32(weatherConfig.GadgetID), - ClimateType: climateType, - TransDuration: 0, - WeatherValueMap: nil, - } - return sceneAreaWeatherNotify -} diff --git a/gs/model/player.go b/gs/model/player.go index 8c171c72..186ccff8 100644 --- a/gs/model/player.go +++ b/gs/model/player.go @@ -84,7 +84,7 @@ type Player struct { NetFreeze bool `bson:"-" msgpack:"-"` // 客户端网络上下行冻结状态 LastSceneBlockAoiMoveTime uint64 `bson:"-" msgpack:"-"` // 上一次移动处理场景区块aoi的时间 CommandAssignUid uint32 `bson:"-" msgpack:"-"` // 命令指定uid - WeatherAreaId int32 `bson:"-" msgpack:"-"` // 天气区域id + WeatherInfo *WeatherInfo `bson:"-" msgpack:"-"` // 天气信息 // 特殊数据 ChatMsgMap map[uint32][]*ChatMsg `bson:"-" msgpack:"-"` // 聊天信息 数据量偏大 只从db读写 不保存到redis } @@ -103,6 +103,7 @@ func (p *Player) InitOnlineData() { p.CombatInvokeHandler = NewInvokeHandler[proto.CombatInvokeEntry]() p.AbilityInvokeHandler = NewInvokeHandler[proto.AbilityInvokeEntry]() p.GCGInfo = NewGCGInfo() // 临时测试用数据 + p.WeatherInfo = NewWeatherInfo() dbAvatar := p.GetDbAvatar() dbAvatar.InitDbAvatar(p) diff --git a/gs/model/weather.go b/gs/model/weather.go new file mode 100644 index 00000000..941ccb64 --- /dev/null +++ b/gs/model/weather.go @@ -0,0 +1,13 @@ +package model + +type WeatherInfo struct { + WeatherAreaId uint32 // 天气区域id + ClimateType uint32 // 气候类型 +} + +func NewWeatherInfo() *WeatherInfo { + return &WeatherInfo{ + WeatherAreaId: 0, + ClimateType: 0, + } +} diff --git a/protocol/cmd/cmd_id_proto_obj_map.go b/protocol/cmd/cmd_id_proto_obj_map.go index 933c93a2..8f347158 100644 --- a/protocol/cmd/cmd_id_proto_obj_map.go +++ b/protocol/cmd/cmd_id_proto_obj_map.go @@ -106,11 +106,11 @@ func (c *CmdProtoMap) registerMessage() { c.regMsg(PlayerGameTimeNotify, func() any { return new(proto.PlayerGameTimeNotify) }) // 玩家游戏内时间通知 c.regMsg(SceneEntityAppearNotify, func() any { return new(proto.SceneEntityAppearNotify) }) // 场景实体出现通知 c.regMsg(SceneEntityDisappearNotify, func() any { return new(proto.SceneEntityDisappearNotify) }) // 场景实体消失通知 - c.regMsg(SceneAreaWeatherNotify, func() any { return new(proto.SceneAreaWeatherNotify) }) // 场景地区天气通知 - c.regMsg(EnterSceneWeatherAreaNotify, func() any { return new(proto.EnterSceneWeatherAreaNotify) }) // 进入场景天气地区通知 - c.regMsg(ExitSceneWeatherAreaNotify, func() any { return new(proto.ExitSceneWeatherAreaNotify) }) // 离开场景天气地区通知 - c.regMsg(SetSceneWeatherAreaReq, func() any { return new(proto.SetSceneWeatherAreaReq) }) // 设置场景天气地区请求 - c.regMsg(SetSceneWeatherAreaRsp, func() any { return new(proto.SetSceneWeatherAreaRsp) }) // 设置场景天气地区响应 + c.regMsg(SceneAreaWeatherNotify, func() any { return new(proto.SceneAreaWeatherNotify) }) // 场景天气区域通知 + c.regMsg(EnterSceneWeatherAreaNotify, func() any { return new(proto.EnterSceneWeatherAreaNotify) }) // 进入场景天气区域通知 + c.regMsg(ExitSceneWeatherAreaNotify, func() any { return new(proto.ExitSceneWeatherAreaNotify) }) // 离开场景天气区域通知 + c.regMsg(SetSceneWeatherAreaReq, func() any { return new(proto.SetSceneWeatherAreaReq) }) // 设置场景天气区域请求 + c.regMsg(SetSceneWeatherAreaRsp, func() any { return new(proto.SetSceneWeatherAreaRsp) }) // 设置场景天气区域响应 c.regMsg(SceneWeatherForecastReq, func() any { return new(proto.SceneWeatherForecastReq) }) // 场景天气预测请求 c.regMsg(SceneWeatherForecastRsp, func() any { return new(proto.SceneWeatherForecastRsp) }) // 场景天气预测响应 c.regMsg(WorldPlayerLocationNotify, func() any { return new(proto.WorldPlayerLocationNotify) }) // 世界玩家位置通知