Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Originally created by Causeless
- -- Free to be used, under the conditions that:
- -- 1. If this file has been modified, any significant changes must be stated
- -- 2. This file header is included in all derivatives
- -- custom_events.lua
- -- Adds new events
- _callbacks = {
- ["CharacterMoved"] = {},
- ["SimTick"] = {}
- }
- function addCallback(event, func)
- table.insert(_callbacks[event], func)
- end
- currentFaction = nil
- local function MPFactionTurnStart(context)
- currentFaction = context:faction()
- end
- local function MPFactionTurnEnd(context)
- currentFaction = nil
- end
- _charInfo = {}
- local function MPcharacterCreated(context)
- local charCQI = context:character():cqi()
- if not _charInfo[charCQI] then _charInfo[charCQI] = {["posX"] = posX, ["posY"] = posY, ["regionName"] = regionName} end
- end
- local function callCharacterMoveCallbacks(charCQI, tab)
- for key, func in ipairs(_callbacks["CharacterMoved"]) do
- func(charCQI, tab["regionName"], tab["posX"], tab["posY"])
- end
- end
- local function MPcheckForCharacterMovement()
- --PopLog(function()
- -- We assume characters can only move if it's their faction's turn
- -- This is technically inaccurate - a faction can retreat their army during another faction's turn, when attacked
- -- But this is good enough for what we need it for, and a lot faster than checking every faction's characters
- if not currentFaction then return end
- local charList = currentFaction:character_list()
- for j = 0, charList:num_items() - 1 do
- local character = charList:item_at(j)
- local charCQI = character:cqi()
- local posX = character:display_position_x()
- local posY = character:display_position_y()
- local regionName = nil
- if character:has_region() then
- regionName = character:region():name()
- end
- if not _charInfo[charCQI] then _charInfo[charCQI] = {["posX"] = posX, ["posY"] = posY, ["regionName"] = regionName} end
- local tab = _charInfo[charCQI]
- if posX ~= tab["posX"] or posY ~= tab["posY"] then
- Log("character moved!")
- callCharacterMoveCallbacks(charCQI, tab)
- tab["posX"] = posX
- tab["posY"] = posY
- tab["regionName"] = regionName
- end
- end
- --end --)
- end
- local function MPtickTrigger(context)
- if context.string == "tick_tock" then
- -- This is for the OnSimTick
- -- First we must set up a time trigger so this is called again ASAP
- scripting.game_interface:add_time_trigger("tick_tock", 0.0)
- -- Time to wait is 0.0 - but the game must defer it to the next tick
- -- According to my experimentation, the campaign runs at a logical tickrate of 10fps, so this function is called 10 times a second
- for key, func in ipairs(_callbacks["SimTick"]) do
- func()
- end
- end
- end
- -- Callbacks
- scripting.AddEventCallBack("FactionTurnStart", MPFactionTurnStart)
- scripting.AddEventCallBack("FactionTurnEnd", MPFactionTurnEnd)
- scripting.AddEventCallBack("CharacterCreated", MPcharacterCreated)
- scripting.AddEventCallBack("TimeTrigger", MPtickTrigger)
- scripting.AddEventCallBack("WorldCreated", function()
- scripting.game_interface:add_time_trigger("tick_tock", 0.0)
- addCallback("SimTick", MPcheckForCharacterMovement)
- end)
- function MPOnCharTurnStart(charCQI)
- Log("MP TEST: MPOnCharTurnStart()")
- local charList = currentFaction:character_list()
- for j = 0, charList:num_items() - 1 do
- local character = charList:item_at(j)
- local searchCQI = character:cqi()
- if searchCQI == charCQI then return character
- end
- end
- end
- local function MPCharacterMoved(charCQI, regionName)
- Log("MP TEST: MPCharacterMoved()")
- local character = MPOnCharTurnStart(charCQI)
- if not character:has_military_force() then return end
- local army = character:military_force()
- if army:is_navy() then return end -- We don't support navies yet
- local armyCQI = character:cqi()
- --army:command_queue_index()
- Log("MP TEST: "..armyCQI)
- --updateArmy(armyCQI, regionName)
- end
- events.addCallback("CharacterMoved", MPCharacterMoved)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement