miduo_server/luafight/BattleMain.lua

214 lines
8.6 KiB
Lua
Raw Normal View History

2019-03-21 15:32:29 +08:00
package.path = package.path ..';luafight/?.lua';
2019-06-24 11:21:25 +08:00
-- linux
-- package.path = package.path ..';../luafight/?.lua';
2019-03-12 14:05:45 +08:00
require("Modules.Battle.Logic.Misc.BattleDefine")
require("Modules.Battle.Logic.Misc.BattleUtil")
require("Modules.Battle.Logic.Misc.BattleQueue")
require("Modules.Battle.Logic.Misc.BattleDictionary")
2019-03-21 14:33:56 +08:00
require("Modules.Battle.Logic.Misc.BattleList")
require("Modules.Battle.Logic.Misc.BattleObjectPool")
2019-03-12 14:05:45 +08:00
require("Modules.Battle.Logic.Base.BattleEvent")
require("Modules.Battle.Logic.Base.Random")
2019-03-21 14:33:56 +08:00
2019-03-12 14:05:45 +08:00
require("Modules.Battle.Logic.Base.RoleData")
require("Modules.Battle.Logic.Base.Buff")
require("Modules.Battle.Logic.Base.Skill")
require("Modules.Battle.Logic.Base.Passivity")
2019-04-16 14:32:11 +08:00
require("Modules.Battle.Logic.BattleLogic")
require("Modules.Battle.Logic.RoleLogic")
2019-03-21 15:01:27 +08:00
local BattleMain = {}
local BattleLogic = BattleLogic
local Random = Random
2019-03-21 14:33:56 +08:00
2019-08-20 15:38:33 +08:00
local _BattleErrorCache = {}
local _BattleErrorIndex = 0
local _seed
2019-08-31 13:45:45 +08:00
local _type
2019-03-21 15:01:27 +08:00
local _fightData
local _optionData
2019-06-24 11:21:25 +08:00
local function pairsByKeys(t)
local a = {}
for n in pairs(t) do
if n then
a[#a+1] = n
end
end
table.sort(a, function( op1, op2 )
local type1, type2 = type(op1), type(op2)
local num1, num2 = tonumber(op1), tonumber(op2)
if ( num1 ~= nil) and (num2 ~= nil) then
return num1 < num2
elseif type1 ~= type2 then
return type1 < type2
elseif type1 == "string" then
return op1 < op2
elseif type1 == "boolean" then
return op1
-- 以上处理: number, string, boolean
else -- 处理剩下的: function, table, thread, userdata
return tostring(op1) < tostring(op2) -- tostring后比较字符串
end
end)
local i = 0
return function()
i = i + 1
return a[i], t[a[i]]
end
end
2019-03-22 15:00:06 +08:00
local function PrintTable(tb)
2019-03-21 15:01:27 +08:00
local indent_str = "{"
2019-05-15 12:05:54 +08:00
local count = 0
for k,v in pairs(tb) do
count = count + 1
end
2019-03-22 15:00:06 +08:00
for k=1, #tb do
local v = tb[k]
2019-03-21 15:01:27 +08:00
if type(v) == "table" then
indent_str = indent_str .. PrintTable(v)
2019-03-22 15:00:06 +08:00
else
2019-05-15 12:05:54 +08:00
indent_str = indent_str .. tostring(v)
end
if k < count then
indent_str = indent_str..","
2019-03-22 15:00:06 +08:00
end
end
2019-05-15 12:05:54 +08:00
local index = 0
2019-06-24 11:21:25 +08:00
for k,v in pairsByKeys(tb) do
2019-05-15 12:05:54 +08:00
index = index + 1
2019-03-22 15:00:06 +08:00
if type(k) ~= "number" then
if type(v) == "table" then
indent_str = string.format("%s%s=%s", indent_str, tostring(k), PrintTable(v))
else
2019-05-15 12:05:54 +08:00
indent_str = string.format("%s%s=%s", indent_str, tostring(k), tostring(v))
end
if index < count then
indent_str = indent_str .. ","
2019-03-22 15:00:06 +08:00
end
2019-03-21 15:01:27 +08:00
end
end
2019-05-15 12:05:54 +08:00
indent_str = indent_str .. "}"
2019-03-21 15:01:27 +08:00
return indent_str
end
2019-03-12 14:05:45 +08:00
2019-03-22 15:00:06 +08:00
local function generateRecordFile()
local time = string.format("%d-%d-%d-%d-%d-%d",
os.date("%Y"),
os.date("%m"),
os.date("%d"),
os.date("%H"),
os.date("%M"),
os.date("%S"))
local file = io.open("luafight/BattleRecord/"..time..".txt", "a")
local record = BattleLogic.GetRecord()
for i=1, #record do
file:write(record[i].."\n")
end
io.close(file)
end
2019-08-20 15:38:33 +08:00
local function addBattleData()
local str = ""
str = str.."seed:\n"..tostring(_seed).."\n"
2019-08-31 13:45:45 +08:00
str = str.."type:\n"..tostring(_type).."\n"
2019-08-20 15:38:33 +08:00
str = str.."fightData:\n"..PrintTable(_fightData).."\n"
str = str.."optionData:\n"..PrintTable(_optionData).."\n"
_BattleErrorIndex = _BattleErrorIndex + 1
if _BattleErrorIndex > 5 then
_BattleErrorIndex = 1
end
_BattleErrorCache[_BattleErrorIndex] = str
end
2019-05-09 17:50:38 +08:00
function BattleMain.Execute(args, fightData, optionData)
2019-08-31 13:45:45 +08:00
print("trfghjdfgucvhbjnfdcgvj")
2019-08-20 15:38:33 +08:00
_seed = args.seed
2019-08-31 13:45:45 +08:00
_type = args.type
2019-03-21 15:01:27 +08:00
_fightData = fightData
2019-06-03 13:43:32 +08:00
_optionData = optionData or {}
2019-03-12 14:05:45 +08:00
2019-03-21 15:01:27 +08:00
--该开关用于输出战斗过程中的日志,用于验证前后端是否出现战斗不同步
2019-03-26 11:45:28 +08:00
BattleLogic.IsOpenBattleRecord = false
2019-03-21 14:33:56 +08:00
2019-06-03 13:43:32 +08:00
local isError = false
local errorCache
2019-03-21 15:01:27 +08:00
if xpcall(function ()
2019-05-09 17:50:38 +08:00
Random.SetSeed(args.seed)
BattleLogic.Init(args.maxTime, fightData, optionData)
2019-08-31 13:45:45 +08:00
BattleLogic.Type = _type
2019-03-21 15:01:27 +08:00
BattleLogic.StartOrder()
2019-03-21 14:33:56 +08:00
2019-03-21 15:01:27 +08:00
while not BattleLogic.IsEnd do
BattleLogic.Update()
end
2019-06-03 13:43:32 +08:00
end, function (err)
isError = true
errorCache = "error:\n"..debug.traceback(err).."\n"
end) then
2019-03-21 15:01:27 +08:00
local resultList = {}
if BattleLogic.Result == 1 then --胜利记录我方剩余血量
local arr = BattleLogic.Query(function (r) return r.camp == 0 end, true)
for i=1, #arr do
resultList[i] = arr[i]:GetRoleData(RoleDataName.Hp)
2019-03-21 14:33:56 +08:00
end
2019-03-21 15:01:27 +08:00
elseif BattleLogic.Result == 0 then --失败记录敌方剩余血量
2019-06-03 20:00:53 +08:00
local arr = BattleLogic.Query(function (r) return r.camp == 1 end, true)
2019-03-21 15:01:27 +08:00
for i=1, #arr do
resultList[i] = arr[i]:GetRoleData(RoleDataName.Hp)
2019-03-21 14:33:56 +08:00
end
end
2019-03-21 15:01:27 +08:00
resultList.result = BattleLogic.Result
2019-03-12 14:05:45 +08:00
2019-03-21 15:01:27 +08:00
if BattleLogic.IsOpenBattleRecord then
2019-03-22 15:00:06 +08:00
generateRecordFile()
2019-03-12 14:05:45 +08:00
end
2019-03-21 15:01:27 +08:00
-- print -----------------------------------------
2019-03-22 18:36:24 +08:00
--for i=1, #resultList do
-- print("hp:"..resultList[i])
--end
--print("最终运行帧数:"..BattleLogic.CurFrame())
--print("result:"..BattleLogic.Result)
2019-03-22 15:00:06 +08:00
2019-08-20 15:38:33 +08:00
addBattleData()
2019-06-30 19:24:45 +08:00
return resultList
end
if isError then --捕获异常,并输出错误日志
2019-06-30 18:15:18 +08:00
local time = string.format("%d-%d-%d-%d-%d-%d",
os.date("%Y"),
os.date("%m"),
os.date("%d"),
os.date("%H"),
os.date("%M"),
os.date("%S"))
2019-06-30 19:24:45 +08:00
local file = io.open("luafight/LogError/"..time..".txt", "a")
file:write(errorCache)
2019-08-20 15:38:33 +08:00
file:write("seed:\n"..tostring(args.seed).."\n")
2019-08-31 13:45:45 +08:00
file:write("type:\n"..tostring(args.type).."\n")
2019-06-30 18:15:18 +08:00
file:write("fightData:\n"..PrintTable(_fightData).."\n")
file:write("optionData:\n"..PrintTable(_optionData).."\n")
file:write("\n\nlast com.ljsd.battle data:\n")
2019-08-20 15:38:33 +08:00
for i=1, #_BattleErrorCache do
file:write(string.format("\nindex:%d\n", i))
file:write(_BattleErrorCache[i])
end
2019-06-30 18:15:18 +08:00
io.close(file)
2019-06-03 13:43:32 +08:00
end
2019-03-21 15:01:27 +08:00
return { result = -1 }
2019-03-12 14:05:45 +08:00
end
2019-08-20 15:38:33 +08:00
--[[
BattleMain.Execute({seed=1565094199,maxTime=300}
,{enemyData={{{ai={0},camp=1,element=1,passivity={{45,1},{45,2}},professionId=0,property={139,117440,117440,18564,2911,2911,5960,0.0,0.0,0.05,0.15,0.2,1.5,0.0,1.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},quality=3,roleId=23,skill={0,{20001,0.7,{1,0.848,1},{13,2,0.1,2,4},{4,4,0.2,12,4}}},superSkill={1.5,{20002,0.7,{10,1.072,1,0.4},{13,3,0.2,1,4}},{30000,0.7,{4,4,0.2,9,2}}},type=4},teamSkill={}}},playerData={{camp=0,element=4,passivity={{5,0.3,1,0.1,5},{39,5,2800},{12,0.15,0.15,2},{44,0.1,4,0.05,6,2}},professionId=5,property={139,76728,76728,4144,3372,2321,3740,0,0,0,0,0.0847,1.5,0,1,1,0,0,0,0,0,0,0,0,0},quality=1,roleId=10005,skill={0,{20001,0.7,{1,0.65,2}},{30101,0.7,{24,1,0.68}},{30001,0.7,{4,1,0.2,9,2}}},superSkill={2,{20004,0.7,{2,0.86,2}},{10100,0.7,{24,1,0.74}}},type=1},{camp=0,element=3,passivity={{5,0.3,1,0.1,5},{39,5,2800},{12,0.15,0.15,2},{44,0.1,4,0.05,6,2}},professionId=5,property={139,76559,76559,4086,2372,3370,3734,0,0,0,0,0.0808,1.5,0,1,1,0,0,0,0,0,0,0,0,0},quality=1,roleId=10007,skill={0,{20001,0.7,{6,3,3,0.88,1},{4,2,0.15,8,4},{4,3,0.15,8,4}}},superSkill={2,{20001,0.7,{1,1.1,1}},{10721,0.7,{8,0,1,1}}},type=1},{camp=0,element=5,passivity={{6,0.25,1,0.25,1,10},{39,5,2800},{12,0.15,0.15,2},{44,0.1,4,0.05,6,2}},professionId=2,property={139,60127,60127,7669,1672,3242,4060,0,0,0,0,0.1287,1.5,0,1,1,0,0,0,0,0,0,0,0,0},quality=1,roleId=10015,skill={0,{20001,0.7,{10,0.95,2,0.3}}},superSkill={3,{20002,0.7,{33,1.4,2,1,10015,0.15,10},{16,0.04,1,10,2}}},type=1},{camp=0,element=1,passivity={{6,0.25,1,0.25,1,10},{39,5,2800},{12,0.15,0.15,2},{44,0.1,4,0.05,6,2}},professionId=1,property={140,72220,72220,3858,4378,2630,4246,0,0,0,0,0.1274,1.5,0,1,1,0,0,0,0,0,0,0,0,0},quality=1,roleId=10020,skill={0,{20001,0.7,{6,3,3,0.35,1}},{10002,0.7,{24,1,0.9}}},superSkill={5,{20000,0.7,{2,0.65,1}},{10000,0.7,{4,6,0.15,6,1}}},type=1},{camp=0,element=2,passivity={{14,0.4,2,0.3,1,10,1},{39,5,2800},{12,0.15,0.15,2},{44,0.1,4,0.05,6,2}},professionId=3,property={139,58750,58750,5175,2145,3634,3581,0,0,0,0,0.0915,1.5,0,1,1,0,0,0,0,0,0,0,0,0},quality=1,roleId=10008,skill={0,{20001,0.7,{29,4,4,0.45,2,2,0.1}}},superSkill={4,{20000,0.7,{2,0.7,2}},{20000,0.7,{3,0.15,1,8}}},type=1},teamSkill={}}}
,{{1,1,0},{1,6,11},{1,6,21},{1,6,31},{1,6,41},{1,6,51},{1,5,0}})
]]--
2019-03-12 17:01:40 +08:00
2019-03-21 15:01:27 +08:00
return BattleMain