妇女之友 发表于 2025-4-14 19:47:12

入侵事件脚本.Lua




**** Hidden Message *****




local function getInvasionSchedules(index, scheduleManager)
local schedules = {}
for i = 1, #scheduleManager do
    local row = scheduleManager
    if tonumber(row) == index then
      table.insert(schedules, {
      Year = row,
      Month = row,
      Day = row,
      DoW = row,
      Hour = row,
      Minute = row,
      Second = row,
      })
    end
end
return schedules
end

local function sendAdminsInvasionNotice(monster, map)
local indexes = GetMonsterIndexes(monster, map)
for i = 1, #indexes do
    local index = indexes
    local data = GetMonsterData(index)
    SendAdminsNotice(
      1,
      {"%s Invaded %s (%d, %d)", "%s Invadiu %s (%d, %d)", "%s invadió %s (%d, %d)"},
      {data.name, data.map.name, data.map.x, data.map.y}
    )
end
end

local function loadInvasions()
local monsterSetBase = ReadSectionFile("../Data/Monster/MonsterSetBase.txt")
local invasionSetBase = monsterSetBase
local invasionManager = ReadSectionFile("../Data/Event/InvasionManager.dat")
local scheduleManager = invasionManager
local durationManager = invasionManager
local monsterManager = invasionManager

for i = 1, #invasionSetBase do
    local monster = tonumber(invasionSetBase)
    local map = tonumber(invasionSetBase)
    local quantity = tonumber(invasionSetBase)
    local index = getInvasionIndex(monster, monsterManager)
    local duration = getInvasionDuration(index, durationManager)
    local schedules = getInvasionSchedules(index, scheduleManager)
    table.insert(Invasions, {
      Monster = monster,
      Map = map,
      Quantity = quantity,
      LiveCount = quantity,
      Duration = duration,
      Schedules = schedules,
      CurrentSchedule = nil,
    })
end
end

function ReadScript()
loadInvasions()
LogColor(2, string.format(
    " %d invasions loaded successfully",
    #Invasions
))
end

function MonsterDie(index, killerIndex)
if GetObjectType(index) ~= OBJECT_MONSTER then
    return
end

local monster = GetObjectClass(index)
local map = GetObjectMap(index)
local invasion = findInvasion(monster, map)
if not invasion then
    return
end

local liveCount = GetMonsterLiveCount(monster, map)
if (liveCount < invasion.LiveCount) then
    invasion.LiveCount = liveCount
end
invasion.LiveCount = invasion.LiveCount - 1

local monsterName = GetMonsterName(monster)
local killerName = GetObjectName(killerIndex)
SendPlayersNotice(
    0,
    {"%s defeated %s [%d/%d]", "%s derrotou %s [%d/%d]", "%s derrotó %s [%d/%d]"},
    {killerName, monsterName, invasion.Quantity - invasion.LiveCount, invasion.Quantity}
)

if invasion.LiveCount == 0 then
    invasion.CurrentSchedule = nil
    invasion.LiveCount = invasion.Quantity
    SendPlayersNotice(
      0,
      {"%s is defeated!", "%s foi derrotado(a)!", "%s fue derrotado(a)!"},
      {monsterName}
    )
end
end

function TimerThread()
local gameServerCode = GetGameServerCode()
if (gameServerCode == 40) then
    return
end

for i = 1, #Invasions do
    local invasion = Invasions
    local liveCount = GetMonsterLiveCount(invasion.Monster, invasion.Map)
    local currentSchedule = GetTimeUpSchedule(invasion.Schedules, 0)
    if not invasion.CurrentSchedule and liveCount > 0 and currentSchedule then
      invasion.CurrentSchedule = currentSchedule
      local monsterName = GetMonsterName(invasion.Monster)
      local mapName = GetMapName(invasion.Map)
      SendPlayersNotice(
      0,
      {"The %s Invasion has begun in %s!", "A Invasão de %s começou em %s!", "La invasión de %s comenzó en %s!"},
      {monsterName, mapName}
      )
      sendAdminsInvasionNotice(invasion.Monster, invasion.Map)
    elseif invasion.CurrentSchedule then
      local currentSchedule = GetTimeUpSchedule(invasion.Schedules, invasion.Duration)
      if not currentSchedule then
      invasion.CurrentSchedule = nil
      invasion.LiveCount = invasion.Quantity
      local monsterName = GetMonsterName(invasion.Monster)
      SendPlayersNotice(
          0,
          {"%s has gone!", "%s foi embora!", "%s se fue!"},
          {monsterName}
      )
      end
    end
end
end

BridgeFunctionAttach("OnReadScript", "ReadScript")
BridgeFunctionAttach("OnMonsterDie", "MonsterDie")
BridgeFunctionAttach("OnTimerThread", "TimerThread")

function GetAdmins()
local admins = {}
for index = GetMinUserIndex(), GetMaxUserIndex() do
    local isAdmin = CommandCheckGameMasterLevel(index, 2) == 1
    if isAdmin then
      table.insert(admins, index)
    end
end
return admins
end

function GetPlayers()
local players = {}
for index = GetMinUserIndex(), GetMaxUserIndex() do
    table.insert(players, index)
end
return players
end

function SendAdminsNotice(value, message, args)
local admins = GetAdmins()
for i = 1, #admins do
    local index = admins
    if (type(message) ~= "string") then
      message = message
    end
    NoticeSend(index, value, string.format(message, table.unpack(args)))
end
end

function SendPlayersNotice(value, message, args)
local players = GetPlayers()
for i = 1, #players do
    local index = players
    if (type(message) ~= "string") then
      message = message
    end
    NoticeSend(index, value, string.format(message, table.unpack(args)))
end
end

function ReadSectionFile(filename)
local config = {}
local currentSection = nil
for line in io.lines(filename) do
    line = line:match("^%s*(.-)%s*$")
    if line ~= "" and not line:match("^//") then
      if line:match("^%d+$") then
      currentSection = tonumber(line)
      if currentSection and not config then
          config = {}
      end
      elseif line == "end" then
      currentSection = nil
      elseif currentSection then
      local row = {}
      for column in line:gmatch("%S+") do
          table.insert(row, column)
      end
      table.insert(config, row)
      end
    end
end
return config
end

function CheckScheduleTimeUp(schedule, duration)
local date = os.date("*t")
local currentTime = os.time({
    year = date.year,
    month = date.month,
    day = date.day,
    hour = date.hour,
    min = date.min,
    sec = 0,
})
local scheduleTime = os.time({
    year = (schedule.Year ~= "*" and tonumber(schedule.Year)) or date.year,
    month = (schedule.Month ~= "*" and tonumber(schedule.Month)) or date.month,
    day = (schedule.Day ~= "*" and tonumber(schedule.Day)) or date.day,
    hour = (schedule.Hour ~= "*" and tonumber(schedule.Hour)) or date.hour,
    min = (schedule.Minute ~= "*" and tonumber(schedule.Minute)) or date.min,
    sec = 0,
})
local isTimeUp = scheduleTime <= currentTime and (scheduleTime + duration) >= currentTime
return isTimeUp
end

function GetTimeUpSchedule(schedules, duration)
for i = 1, #schedules do
    local schedule = schedules
    if (CheckScheduleTimeUp(schedule, duration)) then
      return schedule
    end
end
return nil
end

function GetMonsterData(index)
local map = GetObjectMap(index)
return {
    index = index,
    name = GetObjectName(index),
    map = {
      name = GetMapName(map),
      x = GetObjectMapX(index),
      y = GetObjectMapY(index),
    }
}
end

function GetMonsterIndexes(monster, map)
local indexes = {}
for index = GetMinMonsterIndex(), GetMaxMonsterIndex() do
    local currentMonster = GetObjectClass(index)
    local currentMap = GetObjectMap(index)
    if monster == currentMonster and (not map or currentMap == map) then
      table.insert(indexes, index)
    end
end
return indexes
end

function GetMonsterLiveCount(monster, map)
local liveCount = 0
local indexes = GetMonsterIndexes(monster, map)
for i = 1, #indexes do
    local index = indexes
    local isDead = GetObjectLive(index) <= 0
    if not isDead then
      liveCount = liveCount + 1
    end
end
return liveCount
end


希望 发表于 2025-4-14 21:40:05

关注一下!

心不由己 发表于 2025-4-18 08:40:01

吾爱一起玩儿,一个游戏爱好者的聚集地!

multy 发表于 2025-4-18 12:56:31

大佬牛逼~~

宁静致远 发表于 2025-4-19 10:01:46

看帖不回帖都是耍流氓!

邱祥奎 发表于 2025-4-19 19:22:02

坚持回帖!

活跃概况 发表于 2025-4-19 19:48:46

奇迹45网,一个特别注重情怀的地方!{:14_697:}
页: [1]
查看完整版本: 入侵事件脚本.Lua