加载lua脚本方法

This commit is contained in:
flswld
2023-03-01 10:50:59 +08:00
parent 0395dc0bc2
commit a7b3f41839
8 changed files with 239 additions and 73 deletions

View File

@@ -79,6 +79,7 @@ func NewGameManager(dao *dao.Dao, messageQueue *mq.MessageQueue, gsId uint32, gs
TICK_MANAGER = NewTickManager()
COMMAND_MANAGER = NewCommandManager()
GCG_MANAGER = NewGCGManager()
RegLuaLibFunc()
// 创建本服的Ai世界
uid := AiBaseUid + gsId
name := AiName

95
gs/game/lua_func.go Normal file
View File

@@ -0,0 +1,95 @@
package game
import (
"hk4e/gdconf"
"hk4e/pkg/logger"
lua "github.com/yuin/gopher-lua"
)
type LuaCtx struct {
uid uint32
ownerUid uint32
sourceEntityId uint32
targetEntityId uint32
}
type LuaEvt struct {
param1 int32
param2 int32
param3 int32
param4 int32
paramStr1 string
evtType int32
uid uint32
sourceName string
sourceEntityId uint32
targetEntityId uint32
}
func CallLuaFunc(luaState *lua.LState, luaFuncName string, luaCtx *LuaCtx, luaEvt *LuaEvt) bool {
ctx := luaState.NewTable()
luaState.SetField(ctx, "uid", lua.LNumber(luaCtx.uid))
luaState.SetField(ctx, "owner_uid", lua.LNumber(luaCtx.ownerUid))
luaState.SetField(ctx, "source_entity_id", lua.LNumber(luaCtx.sourceEntityId))
luaState.SetField(ctx, "target_entity_id", lua.LNumber(luaCtx.targetEntityId))
evt := luaState.NewTable()
luaState.SetField(evt, "param1", lua.LNumber(luaEvt.param1))
luaState.SetField(evt, "param2", lua.LNumber(luaEvt.param2))
luaState.SetField(evt, "param3", lua.LNumber(luaEvt.param3))
luaState.SetField(evt, "param4", lua.LNumber(luaEvt.param4))
luaState.SetField(evt, "param_str1", lua.LString(luaEvt.paramStr1))
luaState.SetField(evt, "type", lua.LNumber(luaEvt.evtType))
luaState.SetField(evt, "uid", lua.LNumber(luaEvt.uid))
luaState.SetField(evt, "source_name", lua.LString(luaEvt.sourceName))
luaState.SetField(evt, "source_eid", lua.LNumber(luaEvt.sourceEntityId))
luaState.SetField(evt, "target_eid", lua.LNumber(luaEvt.targetEntityId))
err := luaState.CallByParam(lua.P{
Fn: luaState.GetGlobal(luaFuncName),
NRet: 1,
Protect: true,
}, ctx, evt)
if err != nil {
logger.Error("call lua error: %v", err)
return false
}
luaRet := luaState.Get(-1)
luaState.Pop(1)
ret, ok := luaRet.(lua.LBool)
if !ok {
return false
}
return bool(ret)
}
func RegLuaLibFunc() {
gdconf.RegScriptLib("GetEntityType", GetEntityType)
gdconf.RegScriptLib("GetQuestState", GetQuestState)
}
func GetEntityType(luaState *lua.LState) int {
entityId := luaState.ToInt(1)
luaState.Push(lua.LNumber(entityId >> 24))
return 1
}
func GetQuestState(luaState *lua.LState) int {
ctx, ok := luaState.Get(1).(*lua.LTable)
if !ok {
luaState.Push(lua.LNumber(0))
return 1
}
uid, ok := luaState.GetField(ctx, "uid").(lua.LNumber)
if !ok {
luaState.Push(lua.LNumber(0))
return 1
}
player := USER_MANAGER.GetOnlineUser(uint32(uid))
entityId := luaState.ToInt(2)
_ = entityId
questId := luaState.ToInt(3)
dbQuest := player.GetDbQuest()
quest := dbQuest.GetQuestById(uint32(questId))
luaState.Push(lua.LNumber(quest.State))
return 1
}

View File

@@ -370,24 +370,24 @@ func (g *GameManager) CreateConfigEntity(scene *Scene, objectId int64, entityCon
case *gdconf.Monster:
monster := entityConfig.(*gdconf.Monster)
return scene.CreateEntityMonster(&model.Vector{
X: monster.Pos.X,
Y: monster.Pos.Y,
Z: monster.Pos.Z,
X: float64(monster.Pos.X),
Y: float64(monster.Pos.Y),
Z: float64(monster.Pos.Z),
}, &model.Vector{
X: monster.Rot.X,
Y: monster.Rot.Y,
Z: monster.Rot.Z,
X: float64(monster.Rot.X),
Y: float64(monster.Rot.Y),
Z: float64(monster.Rot.Z),
}, uint32(monster.MonsterId), uint8(monster.Level), g.GetTempFightPropMap(), uint32(monster.ConfigId), objectId)
case *gdconf.Npc:
npc := entityConfig.(*gdconf.Npc)
return scene.CreateEntityNpc(&model.Vector{
X: npc.Pos.X,
Y: npc.Pos.Y,
Z: npc.Pos.Z,
X: float64(npc.Pos.X),
Y: float64(npc.Pos.Y),
Z: float64(npc.Pos.Z),
}, &model.Vector{
X: npc.Rot.X,
Y: npc.Rot.Y,
Z: npc.Rot.Z,
X: float64(npc.Rot.X),
Y: float64(npc.Rot.Y),
Z: float64(npc.Rot.Z),
}, uint32(npc.NpcId), 0, 0, 0, uint32(npc.ConfigId), objectId)
case *gdconf.Gadget:
gadget := entityConfig.(*gdconf.Gadget)
@@ -398,23 +398,23 @@ func (g *GameManager) CreateConfigEntity(scene *Scene, objectId int64, entityCon
return 0
}
return scene.CreateEntityGadgetGather(&model.Vector{
X: gadget.Pos.X,
Y: gadget.Pos.Y,
Z: gadget.Pos.Z,
X: float64(gadget.Pos.X),
Y: float64(gadget.Pos.Y),
Z: float64(gadget.Pos.Z),
}, &model.Vector{
X: gadget.Rot.X,
Y: gadget.Rot.Y,
Z: gadget.Rot.Z,
X: float64(gadget.Rot.X),
Y: float64(gadget.Rot.Y),
Z: float64(gadget.Rot.Z),
}, uint32(gatherDataConfig.GadgetId), uint32(gatherDataConfig.GatherId), uint32(gadget.ConfigId), objectId)
} else {
return scene.CreateEntityGadgetNormal(&model.Vector{
X: gadget.Pos.X,
Y: gadget.Pos.Y,
Z: gadget.Pos.Z,
X: float64(gadget.Pos.X),
Y: float64(gadget.Pos.Y),
Z: float64(gadget.Pos.Z),
}, &model.Vector{
X: gadget.Rot.X,
Y: gadget.Rot.Y,
Z: gadget.Rot.Z,
X: float64(gadget.Rot.X),
Y: float64(gadget.Rot.Y),
Z: float64(gadget.Rot.Z),
}, uint32(gadget.GadgetId), uint32(gadget.ConfigId), objectId)
}
default:

View File

@@ -32,7 +32,7 @@ func NewWorldManager(snowflake *alg.SnowflakeWorker) (r *WorldManager) {
r.worldMap = make(map[uint32]*World)
r.snowflake = snowflake
r.sceneBlockAoiMap = make(map[uint32]*alg.AoiManager)
for _, sceneConfig := range gdconf.GetSceneDetailMap() {
for _, sceneLuaConfig := range gdconf.GetSceneLuaConfigMap() {
minX := int16(0)
maxX := int16(0)
minZ := int16(0)
@@ -41,7 +41,7 @@ func NewWorldManager(snowflake *alg.SnowflakeWorker) (r *WorldManager) {
blockYLen := int16(0)
blockZLen := int16(0)
ok := true
for _, blockConfig := range sceneConfig.BlockMap {
for _, blockConfig := range sceneLuaConfig.BlockMap {
if int16(blockConfig.BlockRange.Min.X) < minX {
minX = int16(blockConfig.BlockRange.Min.X)
}
@@ -112,7 +112,7 @@ func NewWorldManager(snowflake *alg.SnowflakeWorker) (r *WorldManager) {
aoiManager := alg.NewAoiManager()
aoiManager.SetAoiRange(minX, maxX, -1.0, 1.0, minZ, maxZ)
aoiManager.Init3DRectAoiManager(numX, 1, numZ)
for _, blockConfig := range sceneConfig.BlockMap {
for _, blockConfig := range sceneLuaConfig.BlockMap {
for _, groupConfig := range blockConfig.GroupMap {
for _, monsterConfig := range groupConfig.MonsterList {
aoiManager.AddObjectToGridByPos(r.snowflake.GenId(), monsterConfig,
@@ -134,7 +134,7 @@ func NewWorldManager(snowflake *alg.SnowflakeWorker) (r *WorldManager) {
}
}
}
r.sceneBlockAoiMap[uint32(sceneConfig.Id)] = aoiManager
r.sceneBlockAoiMap[uint32(sceneLuaConfig.Id)] = aoiManager
}
r.multiplayerWorldNum = 0
return r

View File

@@ -1,14 +1,7 @@
package model
import "math"
type Vector struct {
X float64
Y float64
Z float64
}
// Distance 两坐标之间的距离
func (v *Vector) Distance(vector *Vector) float64 {
return math.Sqrt(math.Pow(v.X-vector.X, 2) + math.Pow(v.Y-vector.Y, 2) + math.Pow(v.Z-vector.Z, 2))
}