添加插件管理器功能、pubg游戏移植为插件

This commit is contained in:
unkown committed 2023-09-30 15:09:19 +08:00
1 parent df541a1b79
commit a87296d8af
12 files changed
+716 -301

No files matched your search

+2
View File
@@ -35,6 +35,7 @@ var WORLD_MANAGER *WorldManager = nil
var TICK_MANAGER *TickManager = nil
var COMMAND_MANAGER *CommandManager = nil
var GCG_MANAGER *GCGManager = nil
var PLUGIN_MANAGER *PluginManager = nil
var MESSAGE_QUEUE *mq.MessageQueue
var ONLINE_PLAYER_NUM int32 = 0 // 当前在线玩家数
@@ -69,6 +70,7 @@ func NewGameCore(db *dao.Dao, messageQueue *mq.MessageQueue, gsId uint32, gsAppi
TICK_MANAGER = NewTickManager()
COMMAND_MANAGER = NewCommandManager()
GCG_MANAGER = NewGCGManager()
PLUGIN_MANAGER = NewPluginManager()
RegLuaScriptLibFunc()
// 创建本服的Ai世界
uid := AiBaseUid + gsId
+1 -1
View File
@@ -510,7 +510,7 @@ func GiveCommand(c *CommandContent) {
case "weapon":
// 给予玩家所有武器
COMMAND_MANAGER.gmCmd.GMAddAllWeapon(c.AssignPlayer.PlayerId, count, 1, 0, 1)
c.SendSuccMessage(c.Executor, "已给予所有武器,指定UID:%v,数量:%v。", c.AssignPlayer.PlayerId, itemId, count)
c.SendSuccMessage(c.Executor, "已给予所有武器,指定UID:%v,数量:%v。", c.AssignPlayer.PlayerId, count)
case "reliquary":
// 给予玩家所有圣遗物
COMMAND_MANAGER.gmCmd.GMAddAllReliquary(c.AssignPlayer.PlayerId, count)
+4 -4
View File
@@ -556,13 +556,13 @@ func (g *GMCmd) SendMsgToPlayer(cmdName string, userId uint32, msgJson string) {
}
func (g *GMCmd) StartPubg(v bool) {
world := WORLD_MANAGER.GetAiWorld()
world.StartPubg()
pubg := PLUGIN_MANAGER.GetIPlugin("pubg").(*PluginPubg)
pubg.StartPubg()
}
func (g *GMCmd) StopPubg(v bool) {
world := WORLD_MANAGER.GetAiWorld()
world.StopPubg()
pubg := PLUGIN_MANAGER.GetIPlugin("pubg").(*PluginPubg)
pubg.StopPubg()
}
func (g *GMCmd) SetPhysicsEngineParam(pathTracing bool, acc float32, drag float32, pao float32, is float32, ayo float32) {
+310
View File
@@ -0,0 +1,310 @@
package game
import (
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/protocol/proto"
"sort"
)
// 游戏服务器插件管理器
// initPlugin 初始化插件
func (p *PluginManager) initPlugin() {
iPluginList := []IPlugin{
NewPluginPubg(),
}
p.RegAllPlugin(iPluginList...)
}
// PluginEventId 事件编号
type PluginEventId uint16
const (
PluginEventIdNone = PluginEventId(iota)
PluginEventIdPlayerKillAvatar // 角色被杀死
PluginEventIdMarkMap // 地图标点
PluginEventIdAvatarDieAnimationEnd // 角色死亡动画结束
)
// PluginEventKillAvatar 角色被杀死
type PluginEventKillAvatar struct {
*PluginEvent
Player *model.Player // 玩家
AvatarId uint32 // 角色id
DieType proto.PlayerDieType // 死亡类型
}
// PluginEventMarkMap 地图标点
type PluginEventMarkMap struct {
*PluginEvent
Player *model.Player // 玩家
MarkMapReq *proto.MarkMapReq // 地图标点请求
}
// PluginEventAvatarDieAnimationEnd 角色死亡动画结束
type PluginEventAvatarDieAnimationEnd struct {
*PluginEvent
Player *model.Player // 玩家
AvatarDieAnimationEndReq *proto.AvatarDieAnimationEndReq // 角色死亡动画结束请求
}
type PluginEventFunc func(event IPluginEvent)
// IPluginEvent 插件事件接口
type IPluginEvent interface {
Cancel()
IsCancel() bool
}
// PluginEvent 插件事件
type PluginEvent struct {
isCancel bool // 事件是否已取消
}
func NewPluginEvent() *PluginEvent {
return &PluginEvent{}
}
// Cancel 取消事件
// 仍会继续传递事件 但不会在触发事件的地方继续执行
func (p *PluginEvent) Cancel() {
p.isCancel = true
}
// IsCancel 事件是否已取消
func (p *PluginEvent) IsCancel() bool {
return p.isCancel
}
// IPlugin 插件接口
type IPlugin interface {
GetPlugin() *Plugin
Enable()
Disable()
}
// PluginEventPriority 插件事件优先级
type PluginEventPriority uint8
const (
PluginEventPriorityLowest = PluginEventPriority(iota)
PluginEventPriorityLow
PluginEventPriorityNormal
PluginEventPriorityHigh
PluginEventPriorityHighest
)
// PluginEventInfo 插件事件信息
type PluginEventInfo struct {
EventId PluginEventId // 事件id
Priority PluginEventPriority // 优先级
EventFunc PluginEventFunc // 事件执行函数
}
// PluginGlobalTick 全局tick
type PluginGlobalTick uint8
const (
PluginGlobalTick50MilliSecond = PluginGlobalTick(iota)
PluginGlobalTick100MilliSecond
PluginGlobalTick200MilliSecond
PluginGlobalTickSecond
PluginGlobalTick5Second
PluginGlobalTick10Second
PluginGlobalTickMinute
PluginGlobalTickHour
)
// PluginUserTimerFunc 用户timer处理函数
type PluginUserTimerFunc func(player *model.Player, data []any)
// Plugin 插件结构
type Plugin struct {
PluginName string // 插件名
isEnable bool // 是否启用
eventMap map[PluginEventId][]*PluginEventInfo // 事件集合
globalTickMap map[PluginGlobalTick][]func() // 全局tick集合
userTimerMap map[uint64]PluginUserTimerFunc // 用户timer集合
}
func NewPlugin(pluginName string) *Plugin {
return &Plugin{
PluginName: pluginName,
isEnable: true,
eventMap: make(map[PluginEventId][]*PluginEventInfo),
globalTickMap: make(map[PluginGlobalTick][]func()),
userTimerMap: make(map[uint64]PluginUserTimerFunc),
}
}
// GetPlugin 获取插件
func (p *Plugin) GetPlugin() *Plugin {
return p
}
// Enable 启用插件
func (p *Plugin) Enable() {
}
// Disable 禁用插件
func (p *Plugin) Disable() {
}
// ListenEvent 监听事件
func (p *Plugin) ListenEvent(eventId PluginEventId, priority PluginEventPriority, eventFuncList ...PluginEventFunc) {
for _, eventFunc := range eventFuncList {
_, exist := p.eventMap[eventId]
if !exist {
p.eventMap[eventId] = make([]*PluginEventInfo, 0)
}
pluginEventInfo := &PluginEventInfo{
EventId: eventId,
Priority: priority,
EventFunc: eventFunc,
}
p.eventMap[eventId] = append(p.eventMap[eventId], pluginEventInfo)
}
}
// AddGlobalTick 添加全局tick
func (p *Plugin) AddGlobalTick(tick PluginGlobalTick, tickFuncList ...func()) {
for _, tickFunc := range tickFuncList {
_, exist := p.globalTickMap[tick]
if !exist {
p.globalTickMap[tick] = make([]func(), 0)
}
p.globalTickMap[tick] = append(p.globalTickMap[tick], tickFunc)
}
}
// CreateUserTimer 创建用户timer
func (p *Plugin) CreateUserTimer(userId uint32, delay uint32, timerFunc PluginUserTimerFunc, data ...any) {
PLUGIN_MANAGER.userTimerCounter++
userTimerId := PLUGIN_MANAGER.userTimerCounter
p.userTimerMap[userTimerId] = timerFunc
// 用户timer编号插入到数据前
data = append([]any{userTimerId}, data...)
TICK_MANAGER.CreateUserTimer(userId, UserTimerActionPlugin, delay, data...)
}
type PluginManager struct {
pluginMap map[string]IPlugin // 插件集合
userTimerCounter uint64 // 用户timer计数器
}
func NewPluginManager() *PluginManager {
r := new(PluginManager)
r.pluginMap = make(map[string]IPlugin)
r.initPlugin()
return r
}
// RegAllPlugin 注册全部插件
func (p *PluginManager) RegAllPlugin(iPluginList ...IPlugin) {
for _, plugin := range iPluginList {
p.RegPlugin(plugin)
}
}
// RegPlugin 注册插件
func (p *PluginManager) RegPlugin(iPlugin IPlugin) {
plugin := iPlugin.GetPlugin()
// 校验插件名是否已被注册
_, exist := p.pluginMap[plugin.PluginName]
if exist {
logger.Error("plugin has been register, name: %v", plugin.PluginName)
return
}
p.pluginMap[plugin.PluginName] = iPlugin
}
// GetIPlugin 获取抽象插件
func (p *PluginManager) GetIPlugin(pluginName string) IPlugin {
iPlugin, exist := p.pluginMap[pluginName]
if !exist {
logger.Error("plugin not exist, name: %v", pluginName)
return nil
}
return iPlugin
}
// TriggerEvent 触发事件
func (p *PluginManager) TriggerEvent(eventId PluginEventId, event IPluginEvent) bool {
// 获取每个插件监听的事件并根据优先级排序
eventInfoList := make([]*PluginEventInfo, 0)
for _, iPlugin := range p.pluginMap {
plugin := iPlugin.GetPlugin()
// 插件未启用则跳过
if !plugin.isEnable {
continue
}
// 获取插件事件列表
infoList, exist := plugin.eventMap[eventId]
if !exist {
continue
}
for _, info := range infoList {
eventInfoList = append(eventInfoList, info)
}
}
// 根据优先级排序
sort.Slice(eventInfoList, func(i, j int) bool {
return eventInfoList[i].Priority > eventInfoList[j].Priority
})
// 执行每个处理函数
for _, info := range eventInfoList {
info.EventFunc(event)
}
// 判断事件是否被取消
return event.IsCancel()
}
// HandleGlobalTick 处理全局tick
func (p *PluginManager) HandleGlobalTick(tick PluginGlobalTick) {
for _, iPlugin := range p.pluginMap {
plugin := iPlugin.GetPlugin()
// 插件未启用则跳过
if !plugin.isEnable {
continue
}
// 获取插件tick处理函数列表
tickFuncList, exist := plugin.globalTickMap[tick]
if !exist {
continue
}
for _, tickFunc := range tickFuncList {
tickFunc()
}
}
}
// HandleUserTimer 处理用户timer
func (p *PluginManager) HandleUserTimer(player *model.Player, data []any) {
// 如果创建的用户timer没有id数据则报错
if len(data) < 1 {
logger.Error("data len less 1, len: %v", len(data))
return
}
userTimerId := data[0].(uint64)
data = data[1:]
// 通知插件
for _, iPlugin := range p.pluginMap {
plugin := iPlugin.GetPlugin()
// 插件未启用则跳过
if !plugin.isEnable {
continue
}
// 获取插件用户timer处理函数列表
timerFunc, exist := plugin.userTimerMap[userTimerId]
if !exist {
continue
}
timerFunc(player, data)
// 只需要执行一次
break
}
}
+329
View File
@@ -0,0 +1,329 @@
package game
import (
"fmt"
"hk4e/common/constant"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/pkg/random"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
"math"
)
const (
PUBG_PHASE_WAIT = iota
PUBG_PHASE_START
PUBG_PHASE_II
PUBG_PHASE_END
)
const (
PUBG_PHASE_INV_TIME = 180.0
PUBG_FIRST_AREA_REDUCE_TIME = 600.0
)
type PluginPubg struct {
*Plugin
world *World // 世界对象
blueAreaCenterPos *model.Vector // 蓝区中心点
blueAreaRadius float64 // 蓝区半径
safeAreaCenterPos *model.Vector // 安全区中心点
safeAreaRadius float64 // 安全区半径
phase int // 阶段
areaReduceRadiusSpeed float64 // 缩圈半径速度
areaReduceXSpeed float64 // 缩圈X速度
areaReduceZSpeed float64 // 缩圈Z速度
areaPointList []*proto.MapMarkPoint // 客户端区域地图坐标列表
}
func NewPluginPubg() *PluginPubg {
p := &PluginPubg{
Plugin: NewPlugin("pubg"),
world: nil,
blueAreaCenterPos: &model.Vector{X: 0.0, Y: 0.0, Z: 0.0},
blueAreaRadius: 0.0,
safeAreaCenterPos: &model.Vector{X: 0.0, Y: 0.0, Z: 0.0},
safeAreaRadius: 0.0,
phase: PUBG_PHASE_START,
areaReduceRadiusSpeed: 0.0,
areaReduceXSpeed: 0.0,
areaReduceZSpeed: 0.0,
areaPointList: make([]*proto.MapMarkPoint, 0),
}
// 注册事件
p.ListenEvent(PluginEventIdPlayerKillAvatar, PluginEventPriorityNormal, p.EventKillAvatar)
p.ListenEvent(PluginEventIdMarkMap, PluginEventPriorityNormal, p.EventMarkMap)
p.ListenEvent(PluginEventIdAvatarDieAnimationEnd, PluginEventPriorityNormal, p.EventAvatarDieAnimationEnd)
// 添加全局定时器
p.AddGlobalTick(PluginGlobalTickSecond, p.GlobalTickPubg)
p.AddGlobalTick(PluginGlobalTickHour, p.GlobalTickHourStart)
return p
}
/************************************************** 事件监听 **************************************************/
// EventKillAvatar 角色被杀死事件
func (p *PluginPubg) EventKillAvatar(iEvent IPluginEvent) {
event := iEvent.(*PluginEventKillAvatar)
player := event.Player
// 确保游戏开启
if !p.IsStartPubg() || p.world.id != player.WorldId {
return
}
p.CreateUserTimer(player.PlayerId, 10, p.UserTimerPubgDieExit)
}
// EventMarkMap 地图标点事件
func (p *PluginPubg) EventMarkMap(iEvent IPluginEvent) {
event := iEvent.(*PluginEventMarkMap)
player := event.Player
// 确保游戏开启
if !p.IsStartPubg() || p.world.id != player.WorldId {
return
}
GAME.SendMsg(cmd.MarkMapRsp, player.PlayerId, player.ClientSeq, &proto.MarkMapRsp{MarkList: p.GetAreaPointList()})
event.Cancel()
}
// EventAvatarDieAnimationEnd 角色死亡动画结束事件
func (p *PluginPubg) EventAvatarDieAnimationEnd(iEvent IPluginEvent) {
event := iEvent.(*PluginEventAvatarDieAnimationEnd)
player := event.Player
// 确保游戏开启
if !p.IsStartPubg() || p.world.id != player.WorldId {
return
}
alivePlayerNum := len(p.GetAlivePlayerList())
info := fmt.Sprintf("『%v』死亡了,剩余%v位存活玩家。", player.NickName, alivePlayerNum)
GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
player.PubgRank += uint32(100 - alivePlayerNum)
GAME.SendMsg(cmd.AvatarDieAnimationEndRsp, player.PlayerId, player.ClientSeq, &proto.AvatarDieAnimationEndRsp{SkillId: event.AvatarDieAnimationEndReq.SkillId, DieGuid: event.AvatarDieAnimationEndReq.DieGuid})
event.Cancel()
}
/************************************************** 全局定时器 **************************************************/
// GlobalTickPubg pubg游戏定时器
func (p *PluginPubg) GlobalTickPubg() {
world := p.world
if world == nil {
return
}
// 确保游戏开启
if !p.IsStartPubg() {
return
}
p.UpdateArea()
scene := world.GetSceneById(world.GetOwner().SceneId)
for _, scenePlayer := range scene.GetAllPlayer() {
if !p.IsInBlueArea(scenePlayer.Pos) {
GAME.handleEvtBeingHit(scenePlayer, scene, &proto.EvtBeingHitInfo{
AttackResult: &proto.AttackResult{
AttackerId: 0,
DefenseId: world.GetPlayerWorldAvatarEntityId(scenePlayer, world.GetPlayerActiveAvatarId(scenePlayer)),
Damage: 10,
},
})
}
}
alivePlayerList := p.GetAlivePlayerList()
if len(alivePlayerList) <= 1 {
if len(alivePlayerList) == 1 {
info := fmt.Sprintf("『%v』大吉大利,今晚吃鸡。", alivePlayerList[0].NickName)
GAME.PlayerChatReq(world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
}
p.StopPubg()
}
}
// GlobalTickHourStart 每小时开启pubg游戏
func (p *PluginPubg) GlobalTickHourStart() {
p.StartPubg()
}
/************************************************** 用户定时器 **************************************************/
// UserTimerPubgEnd pubg游戏结束后执行定时器
func (p *PluginPubg) UserTimerPubgEnd(player *model.Player, data []any) {
world := WORLD_MANAGER.GetWorldById(player.WorldId)
if world == nil {
logger.Error("get world is nil, worldId: %v, uid: %v", player.WorldId, player.PlayerId)
return
}
for _, worldPlayer := range world.GetAllPlayer() {
if worldPlayer.PlayerId == world.GetOwner().PlayerId {
continue
}
GAME.ReLoginPlayer(worldPlayer.PlayerId, true)
}
}
// UserTimerPubgUpdateArea 更新游戏区域
func (p *PluginPubg) UserTimerPubgUpdateArea(player *model.Player, data []any) {
world := WORLD_MANAGER.GetWorldById(player.WorldId)
if world == nil {
logger.Error("get world is nil, worldId: %v, uid: %v", player.WorldId, player.PlayerId)
return
}
for _, worldPlayer := range world.GetAllPlayer() {
if worldPlayer.PlayerId == world.GetOwner().PlayerId {
continue
}
GAME.ReLoginPlayer(worldPlayer.PlayerId, true)
}
}
// UserTimerPubgDieExit pubg死亡离开
func (p *PluginPubg) UserTimerPubgDieExit(player *model.Player, data []any) {
GAME.ReLoginPlayer(player.PlayerId, true)
}
/************************************************** 插件功能 **************************************************/
// StartPubg 开始pubg游戏
func (p *PluginPubg) StartPubg() {
if p.IsStartPubg() {
return
}
world := WORLD_MANAGER.GetAiWorld()
p.world = world
p.phase = PUBG_PHASE_START
p.RefreshArea()
world.chatMsgList = make([]*proto.ChatInfo, 0)
}
// StopPubg 结束pubg游戏
func (p *PluginPubg) StopPubg() {
if !p.IsStartPubg() {
return
}
p.phase = PUBG_PHASE_WAIT
p.CreateUserTimer(p.world.GetOwner().PlayerId, 60, p.UserTimerPubgEnd)
}
// IsStartPubg pubg游戏是否开启
func (p *PluginPubg) IsStartPubg() bool {
return p.world != nil && p.phase != PUBG_PHASE_WAIT && p.phase != PUBG_PHASE_END
}
// GetAreaPointList 获取游戏区域标点列表
func (p *PluginPubg) GetAreaPointList() []*proto.MapMarkPoint {
return p.areaPointList
}
// UpdateArea 更新游戏区域
func (p *PluginPubg) UpdateArea() {
if p.areaReduceRadiusSpeed > 0.0 && p.blueAreaRadius > p.safeAreaRadius {
p.blueAreaRadius -= p.areaReduceRadiusSpeed
p.blueAreaCenterPos.X += p.areaReduceXSpeed
p.blueAreaCenterPos.Z += p.areaReduceZSpeed
p.SyncMapMarkArea()
}
}
// IsInBlueArea 是否在蓝圈内
func (p *PluginPubg) IsInBlueArea(pos *model.Vector) bool {
distance2D := math.Sqrt(
(p.blueAreaCenterPos.X-pos.X)*(p.blueAreaCenterPos.X-pos.X) +
(p.blueAreaCenterPos.Z-pos.Z)*(p.blueAreaCenterPos.Z-pos.Z),
)
return distance2D < p.blueAreaRadius
}
// RefreshArea 刷新游戏区域
func (p *PluginPubg) RefreshArea() {
info := ""
if p.phase == PUBG_PHASE_START {
info = fmt.Sprintf("安全区已生成,当前%v位存活玩家。", len(p.GetAlivePlayerList()))
p.blueAreaCenterPos = &model.Vector{X: 500.0, Y: 0.0, Z: -500.0}
p.blueAreaRadius = 2000.0
p.safeAreaCenterPos = &model.Vector{X: 0.0, Y: 0.0, Z: 0.0}
p.safeAreaRadius = 0.0
p.CreateUserTimer(p.world.GetOwner().PlayerId, PUBG_PHASE_INV_TIME, p.UserTimerPubgUpdateArea)
} else if p.phase == PUBG_PHASE_END {
info = "安全区已消失。"
p.blueAreaRadius = 0.0
p.safeAreaRadius = 0.0
} else {
switch p.phase % 3 {
case 1:
info = fmt.Sprintf("新的安全区已出现,进度%.1f%%。", float64(p.phase)/PUBG_PHASE_END*100.0)
p.safeAreaCenterPos = &model.Vector{
X: p.blueAreaCenterPos.X + random.GetRandomFloat64(-(p.blueAreaRadius*0.7/2.0), p.blueAreaRadius*0.7/2.0),
Y: 0.0,
Z: p.blueAreaCenterPos.Z + random.GetRandomFloat64(-(p.blueAreaRadius*0.7/2.0), p.blueAreaRadius*0.7/2.0),
}
p.safeAreaRadius = p.blueAreaRadius / 2.0
p.areaReduceRadiusSpeed = 0.0
p.CreateUserTimer(p.world.GetOwner().PlayerId, PUBG_PHASE_INV_TIME, p.UserTimerPubgUpdateArea)
case 2:
info = fmt.Sprintf("安全区正在缩小,进度%.1f%%。", float64(p.phase)/PUBG_PHASE_END*100.0)
invTime := 0.0
if p.phase == PUBG_PHASE_II {
invTime = PUBG_FIRST_AREA_REDUCE_TIME
} else {
invTime = PUBG_PHASE_INV_TIME
}
p.areaReduceRadiusSpeed = (p.blueAreaRadius - p.safeAreaRadius) / invTime
p.areaReduceXSpeed = (p.safeAreaCenterPos.X - p.blueAreaCenterPos.X) / invTime
p.areaReduceZSpeed = (p.safeAreaCenterPos.Z - p.blueAreaCenterPos.Z) / invTime
p.CreateUserTimer(p.world.GetOwner().PlayerId, uint32(invTime), p.UserTimerPubgUpdateArea)
case 0:
info = fmt.Sprintf("安全区缩小完毕,进度%.1f%%。", float64(p.phase)/PUBG_PHASE_END*100.0)
p.CreateUserTimer(p.world.GetOwner().PlayerId, PUBG_PHASE_INV_TIME, p.UserTimerPubgUpdateArea)
}
}
p.SyncMapMarkArea()
GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
}
// SyncMapMarkArea 同步地图标点区域
func (p *PluginPubg) SyncMapMarkArea() {
p.areaPointList = make([]*proto.MapMarkPoint, 0)
if p.blueAreaRadius > 0.0 {
for angleStep := 0; angleStep < 360; angleStep += 5 {
x := p.blueAreaRadius*math.Cos(float64(angleStep)/360.0*2*math.Pi) + p.blueAreaCenterPos.X
z := p.blueAreaRadius*math.Sin(float64(angleStep)/360.0*2*math.Pi) + p.blueAreaCenterPos.Z
p.areaPointList = append(p.areaPointList, &proto.MapMarkPoint{
SceneId: 3,
Name: "",
Pos: &proto.Vector{X: float32(x), Y: 0, Z: float32(z)},
PointType: proto.MapMarkPointType_SPECIAL,
})
}
}
if p.safeAreaRadius > 0.0 {
for angleStep := 0; angleStep < 360; angleStep += 5 {
x := p.safeAreaRadius*math.Cos(float64(angleStep)/360.0*2*math.Pi) + p.safeAreaCenterPos.X
z := p.safeAreaRadius*math.Sin(float64(angleStep)/360.0*2*math.Pi) + p.safeAreaCenterPos.Z
p.areaPointList = append(p.areaPointList, &proto.MapMarkPoint{
SceneId: 3,
Name: "",
Pos: &proto.Vector{X: float32(x), Y: 0, Z: float32(z)},
PointType: proto.MapMarkPointType_COLLECTION,
})
}
}
for _, player := range p.world.GetAllPlayer() {
GAME.SendMsg(cmd.AllMarkPointNotify, player.PlayerId, player.ClientSeq, &proto.AllMarkPointNotify{MarkList: p.areaPointList})
}
}
// GetAlivePlayerList 获取存活玩家列表
func (p *PluginPubg) GetAlivePlayerList() []*model.Player {
scene := p.world.GetSceneById(p.world.GetOwner().SceneId)
alivePlayerList := make([]*model.Player, 0)
for _, scenePlayer := range scene.GetAllPlayer() {
if scenePlayer.PlayerId == p.world.GetOwner().PlayerId {
continue
}
avatarEntityId := p.world.GetPlayerWorldAvatarEntityId(scenePlayer, p.world.GetPlayerActiveAvatarId(scenePlayer))
entity := scene.GetEntity(avatarEntityId)
if entity.GetFightProp()[constant.FIGHT_PROP_CUR_HP] <= 0.0 {
continue
}
alivePlayerList = append(alivePlayerList, scenePlayer)
}
return alivePlayerList
}
-173
View File
@@ -1,173 +0,0 @@
package game
import (
"fmt"
"math"
"hk4e/common/constant"
"hk4e/gs/model"
"hk4e/pkg/random"
"hk4e/protocol/cmd"
"hk4e/protocol/proto"
)
const (
PUBG_PHASE_START = 0
PUBG_PHASE_II = 2
PUBG_PHASE_END = 16
)
const (
PUBG_PHASE_INV_TIME = 180.0
PUBG_FIRST_AREA_REDUCE_TIME = 600.0
)
type Pubg struct {
world *World // 世界对象
blueAreaCenterPos *model.Vector // 蓝区中心点
blueAreaRadius float64 // 蓝区半径
safeAreaCenterPos *model.Vector // 安全区中心点
safeAreaRadius float64 // 安全区半径
phase int // 阶段
areaReduceRadiusSpeed float64 // 缩圈半径速度
areaReduceXSpeed float64 // 缩圈X速度
areaReduceZSpeed float64 // 缩圈Z速度
areaPointList []*proto.MapMarkPoint // 客户端区域地图坐标列表
}
func (w *World) StartPubg() {
w.pubg = &Pubg{
world: w,
blueAreaCenterPos: &model.Vector{X: 0.0, Y: 0.0, Z: 0.0},
blueAreaRadius: 0.0,
safeAreaCenterPos: &model.Vector{X: 0.0, Y: 0.0, Z: 0.0},
safeAreaRadius: 0.0,
phase: PUBG_PHASE_START,
areaReduceRadiusSpeed: 0.0,
areaReduceXSpeed: 0.0,
areaReduceZSpeed: 0.0,
areaPointList: make([]*proto.MapMarkPoint, 0),
}
w.pubg.RefreshArea()
w.chatMsgList = make([]*proto.ChatInfo, 0)
}
func (w *World) StopPubg() {
w.pubg = nil
TICK_MANAGER.CreateUserTimer(w.GetOwner().PlayerId, UserTimerActionPubgEnd, 60)
}
func (p *Pubg) GetAreaPointList() []*proto.MapMarkPoint {
return p.areaPointList
}
func (p *Pubg) UpdateArea() {
if p.areaReduceRadiusSpeed > 0.0 && p.blueAreaRadius > p.safeAreaRadius {
p.blueAreaRadius -= p.areaReduceRadiusSpeed
p.blueAreaCenterPos.X += p.areaReduceXSpeed
p.blueAreaCenterPos.Z += p.areaReduceZSpeed
p.SyncMapMarkArea()
}
}
func (p *Pubg) IsInBlueArea(pos *model.Vector) bool {
distance2D := math.Sqrt(
(p.blueAreaCenterPos.X-pos.X)*(p.blueAreaCenterPos.X-pos.X) +
(p.blueAreaCenterPos.Z-pos.Z)*(p.blueAreaCenterPos.Z-pos.Z),
)
return distance2D < p.blueAreaRadius
}
func (p *Pubg) RefreshArea() {
info := ""
if p.phase == PUBG_PHASE_START {
info = fmt.Sprintf("安全区已生成,当前%v位存活玩家。", len(p.GetAlivePlayerList()))
p.blueAreaCenterPos = &model.Vector{X: 500.0, Y: 0.0, Z: -500.0}
p.blueAreaRadius = 2000.0
p.safeAreaCenterPos = &model.Vector{X: 0.0, Y: 0.0, Z: 0.0}
p.safeAreaRadius = 0.0
TICK_MANAGER.CreateUserTimer(p.world.GetOwner().PlayerId, UserTimerActionPubgUpdateArea, PUBG_PHASE_INV_TIME)
} else if p.phase == PUBG_PHASE_END {
info = "安全区已消失。"
p.blueAreaRadius = 0.0
p.safeAreaRadius = 0.0
} else {
switch p.phase % 3 {
case 1:
info = fmt.Sprintf("新的安全区已出现,进度%.1f%%。", float64(p.phase)/PUBG_PHASE_END*100.0)
p.safeAreaCenterPos = &model.Vector{
X: p.blueAreaCenterPos.X + random.GetRandomFloat64(-(p.blueAreaRadius*0.7/2.0), p.blueAreaRadius*0.7/2.0),
Y: 0.0,
Z: p.blueAreaCenterPos.Z + random.GetRandomFloat64(-(p.blueAreaRadius*0.7/2.0), p.blueAreaRadius*0.7/2.0),
}
p.safeAreaRadius = p.blueAreaRadius / 2.0
p.areaReduceRadiusSpeed = 0.0
TICK_MANAGER.CreateUserTimer(p.world.GetOwner().PlayerId, UserTimerActionPubgUpdateArea, PUBG_PHASE_INV_TIME)
case 2:
info = fmt.Sprintf("安全区正在缩小,进度%.1f%%。", float64(p.phase)/PUBG_PHASE_END*100.0)
invTime := 0.0
if p.phase == PUBG_PHASE_II {
invTime = PUBG_FIRST_AREA_REDUCE_TIME
} else {
invTime = PUBG_PHASE_INV_TIME
}
p.areaReduceRadiusSpeed = (p.blueAreaRadius - p.safeAreaRadius) / invTime
p.areaReduceXSpeed = (p.safeAreaCenterPos.X - p.blueAreaCenterPos.X) / invTime
p.areaReduceZSpeed = (p.safeAreaCenterPos.Z - p.blueAreaCenterPos.Z) / invTime
TICK_MANAGER.CreateUserTimer(p.world.GetOwner().PlayerId, UserTimerActionPubgUpdateArea, uint32(invTime))
case 0:
info = fmt.Sprintf("安全区缩小完毕,进度%.1f%%。", float64(p.phase)/PUBG_PHASE_END*100.0)
TICK_MANAGER.CreateUserTimer(p.world.GetOwner().PlayerId, UserTimerActionPubgUpdateArea, PUBG_PHASE_INV_TIME)
}
}
p.SyncMapMarkArea()
GAME.PlayerChatReq(p.world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
}
func (p *Pubg) SyncMapMarkArea() {
p.areaPointList = make([]*proto.MapMarkPoint, 0)
if p.blueAreaRadius > 0.0 {
for angleStep := 0; angleStep < 360; angleStep += 5 {
x := p.blueAreaRadius*math.Cos(float64(angleStep)/360.0*2*math.Pi) + p.blueAreaCenterPos.X
z := p.blueAreaRadius*math.Sin(float64(angleStep)/360.0*2*math.Pi) + p.blueAreaCenterPos.Z
p.areaPointList = append(p.areaPointList, &proto.MapMarkPoint{
SceneId: 3,
Name: "",
Pos: &proto.Vector{X: float32(x), Y: 0, Z: float32(z)},
PointType: proto.MapMarkPointType_SPECIAL,
})
}
}
if p.safeAreaRadius > 0.0 {
for angleStep := 0; angleStep < 360; angleStep += 5 {
x := p.safeAreaRadius*math.Cos(float64(angleStep)/360.0*2*math.Pi) + p.safeAreaCenterPos.X
z := p.safeAreaRadius*math.Sin(float64(angleStep)/360.0*2*math.Pi) + p.safeAreaCenterPos.Z
p.areaPointList = append(p.areaPointList, &proto.MapMarkPoint{
SceneId: 3,
Name: "",
Pos: &proto.Vector{X: float32(x), Y: 0, Z: float32(z)},
PointType: proto.MapMarkPointType_COLLECTION,
})
}
}
for _, player := range p.world.GetAllPlayer() {
GAME.SendMsg(cmd.AllMarkPointNotify, player.PlayerId, player.ClientSeq, &proto.AllMarkPointNotify{MarkList: p.areaPointList})
}
}
func (p *Pubg) GetAlivePlayerList() []*model.Player {
scene := p.world.GetSceneById(p.world.GetOwner().SceneId)
alivePlayerList := make([]*model.Player, 0)
for _, scenePlayer := range scene.GetAllPlayer() {
if scenePlayer.PlayerId == p.world.GetOwner().PlayerId {
continue
}
avatarEntityId := p.world.GetPlayerWorldAvatarEntityId(scenePlayer, p.world.GetPlayerActiveAvatarId(scenePlayer))
entity := scene.GetEntity(avatarEntityId)
if entity.GetFightProp()[constant.FIGHT_PROP_CUR_HP] <= 0.0 {
continue
}
alivePlayerList = append(alivePlayerList, scenePlayer)
}
return alivePlayerList
}
+39 -89
View File
@@ -1,7 +1,6 @@
package game
import (
"fmt"
"time"
"hk4e/common/constant"
@@ -116,9 +115,7 @@ const (
UserTimerActionTest = iota
UserTimerActionLuaCreateMonster
UserTimerActionLuaGroupTimerEvent
UserTimerActionPubgUpdateArea
UserTimerActionPubgDieExit
UserTimerActionPubgEnd
UserTimerActionPlugin
)
func (t *TickManager) userTimerHandle(userId uint32, action int, data []any) {
@@ -150,35 +147,9 @@ func (t *TickManager) userTimerHandle(userId uint32, action int, data []any) {
return
}
GAME.TimerEventTriggerCheck(player, group, source)
case UserTimerActionPubgUpdateArea:
logger.Debug("UserTimerActionPubgUpdateArea")
world := WORLD_MANAGER.GetWorldById(player.WorldId)
if world == nil {
logger.Error("get world is nil, worldId: %v, uid: %v", player.WorldId, userId)
return
}
pubg := world.GetPubg()
if pubg == nil {
return
}
pubg.phase++
pubg.RefreshArea()
case UserTimerActionPubgDieExit:
logger.Debug("UserTimerActionPubgDieExit")
GAME.ReLoginPlayer(player.PlayerId, true)
case UserTimerActionPubgEnd:
logger.Debug("UserTimerActionPubgEnd")
world := WORLD_MANAGER.GetWorldById(player.WorldId)
if world == nil {
logger.Error("get world is nil, worldId: %v, uid: %v", player.WorldId, userId)
return
}
for _, worldPlayer := range world.GetAllPlayer() {
if worldPlayer.PlayerId == world.GetOwner().PlayerId {
continue
}
GAME.ReLoginPlayer(worldPlayer.PlayerId, true)
}
case UserTimerActionPlugin:
logger.Debug("UserTimerActionPlugin, data: %v", data)
PLUGIN_MANAGER.HandleUserTimer(player, data)
}
}
@@ -190,27 +161,35 @@ func (t *TickManager) OnGameServerTick() {
now := tm.UnixMilli()
if t.globalTickCount%(50/ServerTickTime) == 0 {
t.onTick50MilliSecond(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTick50MilliSecond)
}
if t.globalTickCount%(100/ServerTickTime) == 0 {
t.onTick100MilliSecond(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTick100MilliSecond)
}
if t.globalTickCount%(200/ServerTickTime) == 0 {
t.onTick200MilliSecond(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTick200MilliSecond)
}
if t.globalTickCount%(1000/ServerTickTime) == 0 {
t.onTickSecond(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTickSecond)
}
if t.globalTickCount%(5000/ServerTickTime) == 0 {
t.onTick5Second(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTick5Second)
}
if t.globalTickCount%(10000/ServerTickTime) == 0 {
t.onTick10Second(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTick10Second)
}
if t.globalTickCount%(60000/ServerTickTime) == 0 {
t.onTickMinute(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTickMinute)
}
if t.globalTickCount%(60000*60/ServerTickTime) == 0 {
t.onTickHour(now)
PLUGIN_MANAGER.HandleGlobalTick(PluginGlobalTickHour)
}
for userId, userTick := range t.userTickMap {
if len(userTick.globalTick.C) == 0 {
@@ -256,11 +235,6 @@ func (t *TickManager) onDayChange(now int64) {
func (t *TickManager) onHourChange(now int64) {
logger.Info("on hour change, time: %v", now)
world := WORLD_MANAGER.GetAiWorld()
if world.GetPubg() != nil {
return
}
world.StartPubg()
}
func (t *TickManager) onTickHour(now int64) {
@@ -290,9 +264,7 @@ func (t *TickManager) onTick5Second(now int64) {
}
aiWorld := WORLD_MANAGER.GetAiWorld()
if WORLD_MANAGER.IsAiWorld(aiWorld) {
if aiWorld.GetPubg() != nil {
return
}
// todo pubg允许超过100人
if len(aiWorld.GetAllPlayer()) >= 100 {
return
}
@@ -313,33 +285,6 @@ func (t *TickManager) onTickSecond(now int64) {
for _, game := range GCG_MANAGER.gameMap {
game.onTick()
}
// PUBG游戏
world := WORLD_MANAGER.GetAiWorld()
pubg := world.GetPubg()
if pubg == nil {
return
}
pubg.UpdateArea()
scene := world.GetSceneById(world.GetOwner().SceneId)
for _, scenePlayer := range scene.GetAllPlayer() {
if !pubg.IsInBlueArea(scenePlayer.Pos) {
GAME.handleEvtBeingHit(scenePlayer, scene, &proto.EvtBeingHitInfo{
AttackResult: &proto.AttackResult{
AttackerId: 0,
DefenseId: world.GetPlayerWorldAvatarEntityId(scenePlayer, world.GetPlayerActiveAvatarId(scenePlayer)),
Damage: 10,
},
})
}
}
alivePlayerList := pubg.GetAlivePlayerList()
if len(alivePlayerList) <= 1 {
if len(alivePlayerList) == 1 {
info := fmt.Sprintf("『%v』大吉大利,今晚吃鸡。", alivePlayerList[0].NickName)
GAME.PlayerChatReq(world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
}
world.StopPubg()
}
}
func (t *TickManager) onTick200MilliSecond(now int64) {
@@ -377,28 +322,33 @@ func (t *TickManager) onTick100MilliSecond(now int64) {
Damage: 100,
},
})
if attackResultTemplate != nil {
evtBeingHitInfo := &proto.EvtBeingHitInfo{
PeerId: 0,
AttackResult: attackResultTemplate,
FrameNum: 0,
}
evtBeingHitInfo.AttackResult.AttackerId = rigidBody.avatarEntityId
evtBeingHitInfo.AttackResult.DefenseId = rigidBody.hitAvatarEntityId
evtBeingHitInfo.AttackResult.Damage = 100
evtBeingHitInfo.AttackResult.HitCollision.HitPoint = &proto.Vector{X: float32(defPlayer.Pos.X), Y: float32(defPlayer.Pos.Y), Z: float32(defPlayer.Pos.Z)}
combatData, err := pb.Marshal(evtBeingHitInfo)
if err != nil {
return
}
GAME.SendToSceneA(scene, cmd.CombatInvocationsNotify, 0, &proto.CombatInvocationsNotify{
InvokeList: []*proto.CombatInvokeEntry{{
CombatData: combatData,
ForwardType: proto.ForwardType_FORWARD_TO_ALL,
ArgumentType: proto.CombatTypeArgument_COMBAT_EVT_BEING_HIT,
}},
})
if attackResultTemplate == nil {
return
}
evtBeingHitInfo := &proto.EvtBeingHitInfo{
PeerId: 0,
AttackResult: attackResultTemplate,
FrameNum: 0,
}
evtBeingHitInfo.AttackResult.AttackerId = rigidBody.avatarEntityId
evtBeingHitInfo.AttackResult.DefenseId = rigidBody.hitAvatarEntityId
evtBeingHitInfo.AttackResult.Damage = 100
if evtBeingHitInfo.AttackResult.HitCollision == nil {
return
}
evtBeingHitInfo.AttackResult.HitCollision.HitPoint = &proto.Vector{X: float32(defPlayer.Pos.X), Y: float32(defPlayer.Pos.Y), Z: float32(defPlayer.Pos.Z)}
combatData, err := pb.Marshal(evtBeingHitInfo)
if err != nil {
return
}
GAME.SendToSceneA(scene, cmd.CombatInvocationsNotify, 0, &proto.CombatInvocationsNotify{
InvokeList: []*proto.CombatInvokeEntry{{
CombatData: combatData,
ForwardType: proto.ForwardType_FORWARD_TO_ALL,
ArgumentType: proto.CombatTypeArgument_COMBAT_EVT_BEING_HIT,
}},
})
}
}
-5
View File
@@ -221,17 +221,12 @@ type World struct {
peerList []*model.Player // 玩家编号列表
aiWorldAoi *alg.AoiManager // ai世界的aoi管理器
bulletPhysicsEngine *PhysicsEngine // 蓄力箭子弹物理引擎
pubg *Pubg
}
func (w *World) GetBulletPhysicsEngine() *PhysicsEngine {
return w.bulletPhysicsEngine
}
func (w *World) GetPubg() *Pubg {
return w.pubg
}
func (w *World) GetId() uint32 {
return w.id
}
+3 -3
View File
@@ -153,9 +153,6 @@ func (g *Game) handleEvtBeingHit(player *model.Player, scene *Scene, hitInfo *pr
}
if WORLD_MANAGER.IsAiWorld(world) {
if world.GetPubg() == nil {
return
}
if defEntity.GetEntityType() == constant.ENTITY_TYPE_AVATAR &&
defEntity.GetAvatarEntity().GetUid() == world.GetOwner().PlayerId {
return
@@ -944,6 +941,9 @@ func (g *Game) handleGadgetEntityAbilityLow(player *model.Player, entityId uint3
}
gadgetEntity := entity.GetGadgetEntity()
gadgetId := gadgetEntity.GetGadgetId()
if gadgetId == 0 {
return
}
gadgetDataConfig := gdconf.GetGadgetDataById(int32(gadgetId))
if gadgetDataConfig == nil {
logger.Error("get gadget data config is nil, gadgetId: %v", gadgetEntity.GetGadgetId())
+14 -8
View File
@@ -600,9 +600,6 @@ func (g *Game) KillPlayerAvatar(player *model.Player, avatarId uint32, dieType p
logger.Error("get db avatar is nil, avatarId: %v", avatarId)
return
}
avatar.LifeState = constant.LIFE_STATE_DEAD
avatar.FightPropMap[constant.FIGHT_PROP_CUR_HP] = 0
world := WORLD_MANAGER.GetWorldById(player.WorldId)
if world == nil {
return
@@ -614,6 +611,19 @@ func (g *Game) KillPlayerAvatar(player *model.Player, avatarId uint32, dieType p
scene := world.GetSceneById(player.SceneId)
entity := scene.GetEntity(worldAvatar.GetAvatarEntityId())
// 触发事件
if PLUGIN_MANAGER.TriggerEvent(PluginEventIdPlayerKillAvatar, &PluginEventKillAvatar{
PluginEvent: NewPluginEvent(),
Player: player,
AvatarId: avatarId,
DieType: dieType,
}) {
return
}
avatar.LifeState = constant.LIFE_STATE_DEAD
avatar.FightPropMap[constant.FIGHT_PROP_CUR_HP] = 0
activeAvatarId := world.GetPlayerActiveAvatarId(player)
if avatarId == activeAvatarId {
g.KillEntity(player, scene, entity.GetId(), dieType)
@@ -626,10 +636,6 @@ func (g *Game) KillPlayerAvatar(player *model.Player, avatarId uint32, dieType p
}
g.SendToWorldA(world, cmd.AvatarLifeStateChangeNotify, 0, ntf)
}
if WORLD_MANAGER.IsAiWorld(world) {
TICK_MANAGER.CreateUserTimer(player.PlayerId, UserTimerActionPubgDieExit, 10)
}
}
// RevivePlayerAvatar 复活玩家活跃角色实体
@@ -1312,7 +1318,7 @@ func (g *Game) SetPlayerWeatherByWeatherAreaId(player *model.Player, weatherArea
for key := range weatherTemplateMap {
weatherTemplateList = append(weatherTemplateList, key)
}
weather = random.GetRandomInt32(0, int32(len(weatherTemplateList)-1))
weather = random.GetRandomInt32(1, int32(len(weatherTemplateList)-1))
weatherTemplateConfig = weatherTemplateMap[weather]
}
// 确保指定的天气模版存在
+7 -12
View File
@@ -1,8 +1,6 @@
package game
import (
"fmt"
"hk4e/common/constant"
"hk4e/gdconf"
"hk4e/gs/model"
@@ -200,16 +198,13 @@ func (g *Game) AvatarDieAnimationEndReq(player *model.Player, payloadMsg pb.Mess
}
scene := world.GetSceneById(player.SceneId)
if WORLD_MANAGER.IsAiWorld(world) {
pubg := world.GetPubg()
if pubg != nil {
alivePlayerNum := len(pubg.GetAlivePlayerList())
info := fmt.Sprintf("『%v』死亡了,剩余%v位存活玩家。", player.NickName, alivePlayerNum)
g.PlayerChatReq(world.GetOwner(), &proto.PlayerChatReq{ChatInfo: &proto.ChatInfo{Content: &proto.ChatInfo_Text{Text: info}}})
player.PubgRank += uint32(100 - alivePlayerNum)
g.SendMsg(cmd.AvatarDieAnimationEndRsp, player.PlayerId, player.ClientSeq, &proto.AvatarDieAnimationEndRsp{SkillId: req.SkillId, DieGuid: req.DieGuid})
return
}
// 触发事件
if PLUGIN_MANAGER.TriggerEvent(PluginEventIdAvatarDieAnimationEnd, &PluginEventAvatarDieAnimationEnd{
PluginEvent: NewPluginEvent(),
Player: player,
AvatarDieAnimationEndReq: req,
}) {
return
}
entity := scene.GetEntity(uint32(req.DieGuid))
+7 -6
View File
@@ -147,12 +147,13 @@ func (g *Game) MarkMapReq(player *model.Player, payloadMsg pb.Message) {
return
}
if WORLD_MANAGER.IsAiWorld(world) {
pubg := world.GetPubg()
if pubg != nil {
GAME.SendMsg(cmd.MarkMapRsp, player.PlayerId, player.ClientSeq, &proto.MarkMapRsp{MarkList: pubg.GetAreaPointList()})
return
}
// 触发事件
if PLUGIN_MANAGER.TriggerEvent(PluginEventIdMarkMap, &PluginEventMarkMap{
PluginEvent: NewPluginEvent(),
Player: player,
MarkMapReq: req,
}) {
return
}
// 地图标点传送