Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onLoad(str_SaveState)
- --Make all objects non-interactable
- for Index, Object in pairs(getAllObjects()) do interactable(Object, false) end
- --Initialize battlefield
- BattlefieldSize = 8
- end
- function onPlayerTurnStart(str_PlayerColorStart, str_PlayerColorPrevious)
- --[[Generate a table of all models, and a table of all models that can be
- moved by str_PlayerColorStart, and make them interactable. Make all other
- models non-interactable]]
- Models = {}
- ModelsMoveable = {}
- for Index, Object in pairs(getAllObjects()) do
- if Object.getVar("Controller") then table.insert(Models, Object) end
- if Object.getVar("Controller") == str_PlayerColorStart and Object.getVar("MoveMaximum") then
- table.insert(ModelsMoveable, Object)
- Object.setVar("PositionBackupX", Object.getPosition().x)
- Object.setVar("PositionBackupY", Object.getPosition().y)
- Object.setVar("PositionBackupZ", Object.getPosition().z)
- Object.setVar("RotationBackupX", Object.getPosition().x)
- Object.setVar("RotationBackupY", Object.getRotation().y)
- Object.setVar("RotationBackupZ", Object.getRotation().z)
- Object.setVar("HasMoved", false)
- interactable(Object, true)
- else interactable(Object, false)
- end
- end
- --Start of Movement Phase
- PickedUnitID = 0
- broadcastToAll("Movement Phase")
- end
- function onObjectPickUp(str_PlayerColor, obj_PickedUpObject)
- --Get the position and rotation of obj_PickedUpObject
- PositionOriginal = obj_PickedUpObject.getPosition()
- RotationOriginal = obj_PickedUpObject.getRotation()
- for Index, Model1 in pairs(Models) do
- for Index, Model2 in pairs(modelsInUnit(obj_PickedUpObject.getVar("UnitID"))) do
- 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
- end
- end
- --[[If obj_PickedUpObject is from a unit that was already picked, exit
- function]]
- if obj_PickedUpObject.getVar("UnitID") == PickedUnitID then return end
- --Make all models in the previous unit non-interactable
- for Index, Model in pairs(ModelsMoveable) do
- if Model.getVar("UnitID") == PickedUnitID then
- Model.setVar("HasMoved", true)
- interactable(Model, false)
- end
- end
- --Declare unit as picked
- PickedUnitID = obj_PickedUpObject.getVar("UnitID")
- PickedUnitName = obj_PickedUpObject.getVar("UnitName")
- PickedUnitModelsMoved = 0
- 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
- end
- function onObjectDrop(str_PlayerColor, obj_DroppedObject)
- --[[Illegal movement cases:
- Case 1: obj_DroppedObject was moved less than its Minimum Move
- characteristic
- Case 2: obj_DroppedObject was moved more than its Maximum Move
- characteristic
- Case 3: obj_DroppedObject was moved over the edge of the battlefield
- Case 4: obj_DroppedObject was moved through another model
- Case 5: obj_DroppedObject belongs to a movement that is falling back, and
- ended movement within 1" of an enemy model
- Case 5: obj_DroppedObject was moved within 1" of an enemy model]]
- 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
- 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
- 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
- end
- for Index, Model in pairs(Models) do
- 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
- elseif Model.getVar("Controller") ~= obj_DroppedObject.getVar("Controller") then
- if PickedUnitMovementStatus == "FallBack" then
- 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
- 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
- end
- end
- end
- --Declare obj_DroppedObject as moved
- PickedUnitModelsMoved = PickedUnitModelsMoved + 1
- obj_DroppedObject.setVar("HasMoved", true)
- interactable(obj_DroppedObject, false)
- 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 .. "\".")
- --[[If obj_DroppedObject is the last model in a unit to move, set movement
- status of all models]]
- if PickedUnitModelsMoved == obj_DroppedObject.getVar("NumberOfModelsInUnit") then
- for Index, Model in pairs(modelsInUnit()) do obj_DroppedObject.setVar("MovementStatus", PickedUnitMovementStatus) end
- PickedUnitMovementStatus = nil
- end
- --[[Post-movement unit coherence cases:
- Case 1: obj_DroppedObject is the only model in its unit
- Case 2: obj_DroppedObject belongs to a coherent unit
- Case 3: obj_DroppedObject is not the last model to be moved in a unit
- that is not coherent
- Case 4: obj_DroppedObject is the last model to be moved in a unit that is
- not coherent]]
- if obj_DroppedObject.getVar("NumberOfModelsInUnit") == 1 then return
- elseif unitCoherence(PickedUnitID) then
- for Index, Model in pairs(ModelsMoveable) do
- if Model.getVar("HasMoved") == false then interactable(Model, true) end
- end
- elseif PickedUnitModelsMoved < obj_DroppedObject.getVar("NumberOfModelsInUnit") then
- for Index, Model in pairs(ModelsMoveable) do
- if Model.getVar("UnitID") ~= PickedUnitID then interactable(Model, false) end
- end
- else
- for Index, Model in pairs(modelsInUnit(PickedUnitID)) do
- Model.setPosition({Model.getVar("PositionBackupX"), Model.getVar("PositionBackupY"), Model.getVar("PositionBackupZ")})
- Model.setRotation({Model.getVar("RotationBackupX"), Model.getVar("RotationBackupY"), Model.getVar("RotationBackupZ")})
- Model.setVar("HasMoved", false)
- interactable(Model, true)
- end
- PickedUnitModelsMoved = 0
- 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")
- print(Player[str_PlayerColor].steam_name .. " must redo the movement of " .. PickedUnitName .. " due to lack of unit coherence.")
- end
- end
- --Sets obj_Object as interactable or non-interactable
- function interactable(obj_Object, bool_Interactable)
- if bool_Interactable then
- obj_Object.highlightOn("Green")
- obj_Object.interactable = true
- else
- obj_Object.highlightOff()
- obj_Object.interactable = false
- end
- end
- --Returns a table of models in int_UnitID
- function modelsInUnit(int_UnitID)
- ModelsInUnit = {}
- for Index, Model in pairs(Models) do
- if Model.getVar("UnitID") == int_UnitID then table.insert(ModelsInUnit, Model) end
- end
- return ModelsInUnit
- end
- --Moved obj_Model to its original position and rotation
- function illegalMove(obj_Model, str_BroadcastMessage, str_PlayerColor)
- obj_Model.setPosition(PositionOriginal)
- obj_Model.setRotation(RotationOriginal)
- broadcastToColor(str_BroadcastMessage, str_PlayerColor, "Red") return
- end
- --Returns true if int_UnitID is coherent, otherwise returns false
- function unitCoherence(int_UnitID)
- ModelsInUnit = modelsInUnit(int_UnitID)
- Model1 = ModelsInUnit[1]
- table.remove(ModelsInUnit, 1)
- repeat
- for Index, Model2 in pairs(ModelsInUnit) do
- PositionModel1 = Model1.getPosition()
- PositionModel2 = Model2.getPosition()
- 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
- Model1 = Model2
- table.remove(ModelsInUnit, Index)
- ModelCoherence = true break
- else ModelCoherence = false
- end
- end
- if not ModelCoherence then return false end
- until not ModelsInUnit[1]
- return true
- end
- --[[Returns true if a model is moved within float_Radius of another model,
- otherwise returns false]]
- function threatRadius(float_Position1X1, float_Position1Z1, float_Position1X2, float_Position1Z2, float_Position2X, float_Position2Z, float_Radius)
- 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
- 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
- elseif math.sqrt((float_Position2X - float_Position1X1) ^ 2 + (float_Position2Z - float_Position1Z1) ^ 2) <= float_Radius then return true
- elseif math.sqrt((float_Position2X - float_Position1X2) ^ 2 + (float_Position2Z - float_Position1Z2) ^ 2) <= float_Radius then return true
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement