Advertisement
cribolouf

Untitled

Apr 9th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.60 KB | None | 0 0
  1. function onLoad(str_SaveState)
  2.     --Make all objects non-interactable
  3.     for Index, Object in pairs(getAllObjects()) do interactable(Object, false) end
  4.  
  5.     --Initialize battlefield
  6.     BattlefieldSize = 8
  7. end
  8.  
  9. function onPlayerTurnStart(str_PlayerColorStart, str_PlayerColorPrevious)
  10.     --[[Generate a table of all models, and a table of all models that can be
  11.     moved by str_PlayerColorStart, and make them interactable. Make all other
  12.     models non-interactable]]
  13.     Models = {}
  14.     ModelsMoveable = {}
  15.     for Index, Object in pairs(getAllObjects()) do
  16.         if Object.getVar("Controller") then table.insert(Models, Object) end
  17.         if Object.getVar("Controller") == str_PlayerColorStart and Object.getVar("MoveMaximum") then
  18.             table.insert(ModelsMoveable, Object)
  19.               Object.setVar("PositionBackupX", Object.getPosition().x)
  20.               Object.setVar("PositionBackupY", Object.getPosition().y)
  21.               Object.setVar("PositionBackupZ", Object.getPosition().z)
  22.               Object.setVar("RotationBackupX", Object.getPosition().x)
  23.               Object.setVar("RotationBackupY", Object.getRotation().y)
  24.               Object.setVar("RotationBackupZ", Object.getRotation().z)
  25.             Object.setVar("HasMoved", false)
  26.             interactable(Object, true)
  27.         else interactable(Object, false)
  28.         end
  29.     end
  30.  
  31.     --Start of Movement Phase
  32.     PickedUnitID = 0
  33.     broadcastToAll("Movement Phase")
  34. end
  35.  
  36. function onObjectPickUp(str_PlayerColor, obj_PickedUpObject)
  37.     --Get the position and rotation of obj_PickedUpObject
  38.     PositionOriginal = obj_PickedUpObject.getPosition()
  39.     RotationOriginal = obj_PickedUpObject.getRotation()
  40.  
  41.     for Index, Model1 in pairs(Models) do
  42.         for Index, Model2 in pairs(modelsInUnit(obj_PickedUpObject.getVar("UnitID"))) do
  43.             if Model1.getVar("Controller") ~= Model2.getVar("Controller") and math.sqrt((Model1.getPosition().x - Model2.getPosition().x) ^ 2 + (Model1.getPosition().z - Model2.getPosition().z) ^ 2) - Model1.getVar("BaseSize") / 50.8 - Model2.getVar("BaseSize") / 50.8 <= 1 then PickedUnitMovementStatus = "FallBack" end
  44.         end
  45.     end
  46.  
  47.     --[[If obj_PickedUpObject is from a unit that was already picked, exit
  48.     function]]
  49.     if obj_PickedUpObject.getVar("UnitID") == PickedUnitID then return end
  50.  
  51.     --Make all models in the previous unit non-interactable
  52.     for Index, Model in pairs(ModelsMoveable) do
  53.         if Model.getVar("UnitID") == PickedUnitID then
  54.             Model.setVar("HasMoved", true)
  55.             interactable(Model, false)
  56.         end
  57.     end
  58.  
  59.     --Declare unit as picked
  60.     PickedUnitID = obj_PickedUpObject.getVar("UnitID")
  61.     PickedUnitName = obj_PickedUpObject.getVar("UnitName")
  62.     PickedUnitModelsMoved = 0
  63.     if PickedUnitMovementStatus == "FallBack" then print (Player[str_PlayerColor].steam_name .. " has chosen to Fall Back with " .. obj_PickedUpObject.getVar("UnitName") .. ".") else print(Player[str_PlayerColor].steam_name .. " has picked " .. obj_PickedUpObject.getVar("UnitName") .. " to move.") end
  64. end
  65.  
  66. function onObjectDrop(str_PlayerColor, obj_DroppedObject)
  67.     --[[Illegal movement cases:
  68.  
  69.     Case 1: obj_DroppedObject was moved less than its Minimum Move
  70.     characteristic
  71.  
  72.     Case 2: obj_DroppedObject was moved more than its Maximum Move
  73.     characteristic
  74.  
  75.     Case 3: obj_DroppedObject was moved over the edge of the battlefield
  76.  
  77.     Case 4: obj_DroppedObject was moved through another model
  78.  
  79.     Case 5: obj_DroppedObject belongs to a movement that is falling back, and
  80.     ended movement within 1" of an enemy model
  81.  
  82.     Case 5: obj_DroppedObject was moved within 1" of an enemy model]]
  83.     if obj_DroppedObject.getVar("MoveMinimum") and math.sqrt((obj_DroppedObject.getPosition().x - PositionOriginal.x) ^ 2 + (obj_DroppedObject.getPosition().z - PositionOriginal.z) ^ 2) < obj_DroppedObject.getVar("MoveMinimum") then illegalMove(obj_DroppedObject, "A model may not be moved a distance, in inches, less than the Minimum Move characteristic on its datasheet.", str_PlayerColor) return
  84.     elseif math.sqrt((obj_DroppedObject.getPosition().x - PositionOriginal.x) ^ 2 + (obj_DroppedObject.getPosition().z - PositionOriginal.z) ^ 2) > obj_DroppedObject.getVar("MoveMaximum") then illegalMove(obj_DroppedObject, "A model may not be moved a distance, in inches, greater than the Maximum Move characteristic on its datasheet.", str_PlayerColor) return
  85.     elseif math.abs(obj_DroppedObject.getPosition().x) + obj_DroppedObject.getVar("BaseSize") / 50.8 > BattlefieldSize * 6 or math.abs(obj_DroppedObject.getPosition().z) + obj_DroppedObject.getVar("BaseSize") / 50.8 > 24 then illegalMove(obj_DroppedObject, "A model may not be moved over the edge of the battlefield.", str_PlayerColor) return
  86.     end
  87.     for Index, Model in pairs(Models) do
  88.         if Model ~= obj_DroppedObject and threatRadius(PositionOriginal.x, PositionOriginal.z, obj_DroppedObject.getPosition().x, obj_DroppedObject.getPosition().z, Model.getPosition().x, Model.get_Position().z, obj_DroppedObject.getVar("BaseSize") / 50.8 + Model.getVar("BaseSize") / 50.8) then illegalMove(obj_DroppedObject, "A model may not be moved through other models.", str_PlayerColor) return
  89.         elseif Model.getVar("Controller") ~= obj_DroppedObject.getVar("Controller") then
  90.             if PickedUnitMovementStatus == "FallBack" then
  91.                 if math.sqrt((Model.getPosition().x - obj_DroppedObject.getPosition().x) ^ 2 + (Model.getPosition().z - obj_DroppedObject.getPosition().z) ^ 2) - obj_DroppedObject.getVar("BaseSize") / 50.8 - Model.getVar("BaseSize") / 50.8 <= 1 then illegalMove(obj_DroppedObject, "A model that is falling back may not end its movement within 1\" of an enemy model.", str_PlayerColor) return end
  92.             elseif threatRadius(PositionOriginal.x, PositionOriginal.z, obj_DroppedObject.getPosition().x, obj_DroppedObject.getPosition().z, Model.getPosition().x, Model.get_Position().z, 1 + obj_DroppedObject.getVar("BaseSize") / 50.8 + Model.getVar("BaseSize") / 50.8) then illegalMove(obj_DroppedObject, "A model may not be moved within 1\" of an enemy model.", str_PlayerColor) return
  93.             end
  94.         end
  95.     end
  96.  
  97.     --Declare obj_DroppedObject as moved
  98.     PickedUnitModelsMoved = PickedUnitModelsMoved + 1
  99.     obj_DroppedObject.setVar("HasMoved", true)
  100.     interactable(obj_DroppedObject, false)
  101.     print(Player[str_PlayerColor].steam_name .. " moved " .. obj_DroppedObject.getVar("ModelName") .. " " .. math.floor(math.sqrt((obj_DroppedObject.getPosition().x - PositionOriginal.x) ^ 2 + (obj_DroppedObject.getPosition().z - PositionOriginal.z) ^ 2) * 10 ^ 1 + 0.5) / 10 ^ 1 .. "\".")
  102.  
  103.     --[[If obj_DroppedObject is the last model in a unit to move, set movement
  104.     status of all models]]
  105.     if PickedUnitModelsMoved == obj_DroppedObject.getVar("NumberOfModelsInUnit") then
  106.         for Index, Model in pairs(modelsInUnit()) do obj_DroppedObject.setVar("MovementStatus", PickedUnitMovementStatus) end
  107.         PickedUnitMovementStatus = nil
  108.     end
  109.  
  110.     --[[Post-movement unit coherence cases:
  111.  
  112.     Case 1: obj_DroppedObject is the only model in its unit
  113.  
  114.     Case 2: obj_DroppedObject belongs to a coherent unit
  115.  
  116.     Case 3: obj_DroppedObject is not the last model to be moved in a unit
  117.     that is not coherent
  118.  
  119.     Case 4: obj_DroppedObject is the last model to be moved in a unit that is
  120.     not coherent]]
  121.     if obj_DroppedObject.getVar("NumberOfModelsInUnit") == 1 then return
  122.     elseif unitCoherence(PickedUnitID) then
  123.         for Index, Model in pairs(ModelsMoveable) do
  124.             if Model.getVar("HasMoved") == false then interactable(Model, true) end
  125.         end
  126.     elseif PickedUnitModelsMoved < obj_DroppedObject.getVar("NumberOfModelsInUnit") then
  127.         for Index, Model in pairs(ModelsMoveable) do
  128.             if Model.getVar("UnitID") ~= PickedUnitID then interactable(Model, false) end
  129.         end
  130.     else
  131.         for Index, Model in pairs(modelsInUnit(PickedUnitID)) do
  132.             Model.setPosition({Model.getVar("PositionBackupX"), Model.getVar("PositionBackupY"), Model.getVar("PositionBackupZ")})
  133.             Model.setRotation({Model.getVar("RotationBackupX"), Model.getVar("RotationBackupY"), Model.getVar("RotationBackupZ")})
  134.             Model.setVar("HasMoved", false)
  135.             interactable(Model, true)
  136.         end
  137.         PickedUnitModelsMoved = 0
  138.         broadcastToColor("A unit must finish any sort of move as a group, with every model within 2\" horizontally, and 6\" vertically, of at least one other model from their unit.", str_PlayerColor, "Red")
  139.         print(Player[str_PlayerColor].steam_name .. " must redo the movement of " .. PickedUnitName .. " due to lack of unit coherence.")
  140.     end
  141. end
  142.  
  143. --Sets obj_Object as interactable or non-interactable
  144. function interactable(obj_Object, bool_Interactable)
  145.     if bool_Interactable then
  146.         obj_Object.highlightOn("Green")
  147.         obj_Object.interactable = true
  148.     else
  149.         obj_Object.highlightOff()
  150.         obj_Object.interactable = false
  151.     end
  152. end
  153.  
  154. --Returns a table of models in int_UnitID
  155. function modelsInUnit(int_UnitID)
  156.     ModelsInUnit = {}
  157.     for Index, Model in pairs(Models) do
  158.         if Model.getVar("UnitID") == int_UnitID then table.insert(ModelsInUnit, Model) end
  159.     end
  160.     return ModelsInUnit
  161. end
  162.  
  163. --Moved obj_Model to its original position and rotation
  164. function illegalMove(obj_Model, str_BroadcastMessage, str_PlayerColor)
  165.     obj_Model.setPosition(PositionOriginal)
  166.     obj_Model.setRotation(RotationOriginal)
  167.     broadcastToColor(str_BroadcastMessage, str_PlayerColor, "Red") return
  168. end
  169.  
  170. --Returns true if int_UnitID is coherent, otherwise returns false
  171. function unitCoherence(int_UnitID)
  172.     ModelsInUnit = modelsInUnit(int_UnitID)
  173.     Model1 = ModelsInUnit[1]
  174.     table.remove(ModelsInUnit, 1)
  175.     repeat
  176.         for Index, Model2 in pairs(ModelsInUnit) do
  177.             PositionModel1 = Model1.getPosition()
  178.             PositionModel2 = Model2.getPosition()
  179.             if math.sqrt((PositionModel1.x - PositionModel2.x) ^ 2 + (PositionModel1.z - PositionModel2.z) ^ 2) - Model1.getVar("BaseSize") / 50.8 - Model2.getVar("BaseSize") / 50.8 <= 2 and PositionModel1.y - PositionModel2.y <= 6 then
  180.                 Model1 = Model2
  181.                 table.remove(ModelsInUnit, Index)
  182.                 ModelCoherence = true break
  183.             else ModelCoherence = false
  184.             end
  185.         end
  186.         if not ModelCoherence then return false end
  187.     until not ModelsInUnit[1]
  188.     return true
  189. end
  190.  
  191. --[[Returns true if a model is moved within float_Radius of another model,
  192. otherwise returns false]]
  193. function threatRadius(float_Position1X1, float_Position1Z1, float_Position1X2, float_Position1Z2, float_Position2X, float_Position2Z, float_Radius)
  194.     if ((float_Position2Z - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position2X - float_Position1X1) * (float_Position1X2 - float_Position1X1)) / ((float_Position1Z2 - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position1X2 - float_Position1X1) * (float_Position1X2 - float_Position1X1)) >= 0 and ((float_Position2Z - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position2X - float_Position1X1) * (float_Position1X2 - float_Position1X1)) / ((float_Position1Z2 - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position1X2 - float_Position1X1) * (float_Position1X2 - float_Position1X1)) <= 1 then
  195.         if math.sqrt(((((float_Position2Z - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position2X - float_Position1X1) * (float_Position1X2 - float_Position1X1)) / ((float_Position1Z2 - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position1X2 - float_Position1X1) * (float_Position1X2 - float_Position1X1))) * (float_Position1X2 - float_Position1X1) + float_Position1X1 - float_Position2X) ^ 2 + ((((float_Position2Z - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position2X - float_Position1X1) * (float_Position1X2 - float_Position1X1)) / ((float_Position1Z2 - float_Position1Z1) * (float_Position1Z2 - float_Position1Z1) + (float_Position1X2 - float_Position1X1) * (float_Position1X2 - float_Position1X1))) * (float_Position1Z2 - float_Position1Z1) + float_Position1Z1 - float_Position2Z) ^ 2) <= float_Radius then return true else return false end
  196.     elseif math.sqrt((float_Position2X - float_Position1X1) ^ 2 + (float_Position2Z - float_Position1Z1) ^ 2) <= float_Radius then return true
  197.     elseif math.sqrt((float_Position2X - float_Position1X2) ^ 2 + (float_Position2Z - float_Position1Z2) ^ 2) <= float_Radius then return true
  198.     end
  199.     return false
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement