整理代码、调用LUA修改场景天气

This commit is contained in:
unkown committed 2023-09-29 08:09:25 +08:00
1 parent a08d28a895
commit 282c16f7fe
7 files changed
+198 -104

No files matched your search

+37
View File
@@ -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
}
+130 -91
View File
@@ -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
}
+2 -1
View File
@@ -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)
+13
View File
@@ -0,0 +1,13 @@
package model
type WeatherInfo struct {
WeatherAreaId uint32 // 天气区域id
ClimateType uint32 // 气候类型
}
func NewWeatherInfo() *WeatherInfo {
return &WeatherInfo{
WeatherAreaId: 0,
ClimateType: 0,
}
}