Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. -- Originally created by Causeless
  2. -- Free to be used, under the conditions that:
  3. -- 1. If this file has been modified, any significant changes must be stated
  4. -- 2. This file header is included in all derivatives
  5.  
  6. -- custom_events.lua
  7. -- Adds new events
  8. _callbacks = {
  9. ["CharacterMoved"] = {},
  10. ["SimTick"] = {}
  11. }
  12.  
  13. function addCallback(event, func)
  14. table.insert(_callbacks[event], func)
  15. end
  16.  
  17. currentFaction = nil
  18.  
  19. local function MPFactionTurnStart(context)
  20. currentFaction = context:faction()
  21. end
  22.  
  23. local function MPFactionTurnEnd(context)
  24. currentFaction = nil
  25. end
  26.  
  27. _charInfo = {}
  28. local function MPcharacterCreated(context)
  29. local charCQI = context:character():cqi()
  30. if not _charInfo[charCQI] then _charInfo[charCQI] = {["posX"] = posX, ["posY"] = posY, ["regionName"] = regionName} end
  31. end
  32.  
  33. local function callCharacterMoveCallbacks(charCQI, tab)
  34. for key, func in ipairs(_callbacks["CharacterMoved"]) do
  35. func(charCQI, tab["regionName"], tab["posX"], tab["posY"])
  36. end
  37. end
  38.  
  39. local function MPcheckForCharacterMovement()
  40. --PopLog(function()
  41. -- We assume characters can only move if it's their faction's turn
  42. -- This is technically inaccurate - a faction can retreat their army during another faction's turn, when attacked
  43. -- But this is good enough for what we need it for, and a lot faster than checking every faction's characters
  44. if not currentFaction then return end
  45. local charList = currentFaction:character_list()
  46.  
  47. for j = 0, charList:num_items() - 1 do
  48. local character = charList:item_at(j)
  49. local charCQI = character:cqi()
  50.  
  51. local posX = character:display_position_x()
  52. local posY = character:display_position_y()
  53.  
  54. local regionName = nil
  55. if character:has_region() then
  56. regionName = character:region():name()
  57. end
  58.  
  59. if not _charInfo[charCQI] then _charInfo[charCQI] = {["posX"] = posX, ["posY"] = posY, ["regionName"] = regionName} end
  60. local tab = _charInfo[charCQI]
  61.  
  62. if posX ~= tab["posX"] or posY ~= tab["posY"] then
  63. Log("character moved!")
  64.  
  65. callCharacterMoveCallbacks(charCQI, tab)
  66.  
  67. tab["posX"] = posX
  68. tab["posY"] = posY
  69. tab["regionName"] = regionName
  70. end
  71. end
  72. --end --)
  73. end
  74.  
  75. local function MPtickTrigger(context)
  76. if context.string == "tick_tock" then
  77. -- This is for the OnSimTick
  78. -- First we must set up a time trigger so this is called again ASAP
  79. scripting.game_interface:add_time_trigger("tick_tock", 0.0)
  80. -- Time to wait is 0.0 - but the game must defer it to the next tick
  81. -- According to my experimentation, the campaign runs at a logical tickrate of 10fps, so this function is called 10 times a second
  82.  
  83. for key, func in ipairs(_callbacks["SimTick"]) do
  84. func()
  85. end
  86. end
  87. end
  88.  
  89. -- Callbacks
  90. scripting.AddEventCallBack("FactionTurnStart", MPFactionTurnStart)
  91. scripting.AddEventCallBack("FactionTurnEnd", MPFactionTurnEnd)
  92.  
  93. scripting.AddEventCallBack("CharacterCreated", MPcharacterCreated)
  94. scripting.AddEventCallBack("TimeTrigger", MPtickTrigger)
  95. scripting.AddEventCallBack("WorldCreated", function()
  96. scripting.game_interface:add_time_trigger("tick_tock", 0.0)
  97. addCallback("SimTick", MPcheckForCharacterMovement)
  98. end)
  99.  
  100.  
  101. function MPOnCharTurnStart(charCQI)
  102. Log("MP TEST: MPOnCharTurnStart()")
  103. local charList = currentFaction:character_list()
  104.  
  105. for j = 0, charList:num_items() - 1 do
  106. local character = charList:item_at(j)
  107. local searchCQI = character:cqi()
  108. if searchCQI == charCQI then return character
  109. end
  110. end
  111. end
  112.  
  113.  
  114. local function MPCharacterMoved(charCQI, regionName)
  115. Log("MP TEST: MPCharacterMoved()")
  116. local character = MPOnCharTurnStart(charCQI)
  117. if not character:has_military_force() then return end
  118.  
  119. local army = character:military_force()
  120. if army:is_navy() then return end -- We don't support navies yet
  121.  
  122. local armyCQI = character:cqi()
  123. --army:command_queue_index()
  124. Log("MP TEST: "..armyCQI)
  125. --updateArmy(armyCQI, regionName)
  126. end
  127.  
  128. events.addCallback("CharacterMoved", MPCharacterMoved)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement