-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_utils.lua
More file actions
142 lines (120 loc) · 3.74 KB
/
_utils.lua
File metadata and controls
142 lines (120 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
local STORE = require("_variables")
local UTILS = {}
UTILS.onDiceAdded = function(dice)
dice.randomize("someColor")
end
UTILS.addDiceToTile = function(tile, bagGUID)
local position = tile.getPosition()
local bounds = tile.getBounds().size
position.y = 3
position.x = math.random(position.x - bounds.x / 3, position.x + bounds.x / 3)
position.z = math.random(position.z - 2, position.z + 2)
getObjectFromGUID(bagGUID).takeObject({
position = position,
callback_function = UTILS.onDiceAdded
})
end
UTILS.getTileData = function(tile)
local num = tile.getName()
local letter
num = string.sub(num, string.len("Tile ") + 1)
letter = tile.is_face_down and "B" or "A"
return STORE.tilesData[num .. letter]
end
UTILS.normalizeSectorNumber = function(sector, tile)
local offset = UTILS.round(tile.getRotation().y / 90)
sector = sector - offset
if sector < 1 then
sector = 4 + sector
end
if sector > 4 then
sector = math.fmod(sector, 4)
end
return sector
end
UTILS.concatTileNames = function(battles)
local result = ""
for i, v in ipairs(battles) do
result = result .. v.tileName .. ", "
end
return string.sub(result, 1, string.len(result) - 2)
end
UTILS.getSectorArmy = function(sector, tile, isCountBuildings)
local army
army = sortObjectsByFactions(getSectorObjects(tile, sector), isCountBuildings)
return convertArmyToArray(army)
end
function convertArmyToArray(army)
local result = {}
for k, v in pairs(army) do
table.insert(result, {
faction = k,
units = v
})
end
return result
end
function sortObjectsByFactions(objs, isCountBuildings)
local army = {}
local faction, unitData, buildingData
for i, v in ipairs(objs) do
faction = STORE.unitsData[v.getName()] and STORE.unitsData[v.getName()].faction
faction = faction or (isCountBuildings and STORE.buildingsData[v.getName()])
if faction then
army[faction] = army[faction] or {}
table.insert(army[faction], v)
end
end
return army
end
function getSectorObjects(tile, sector)
local position = tile.getPosition()
local signX = {-1, 1, 1, -1}
local signZ = {1, 1, -1, -1}
local data, objs = {}, {}
position.x = position.x + signX[sector] * tile.getBounds().size.x / 4
position.z = position.z + signZ[sector] * tile.getBounds().size.z / 4
data = Physics.cast({
origin = position,
type = 3,
size = {5, 5, 5},
direction = {0, 1, 0},
max_distance = 0
})
for i, v in ipairs(data) do
table.insert(objs, v.hit_object)
end
return objs
end
UTILS.round = function(x)
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
end
function printTable(tab)
for key, value in pairs(tab) do
print("key: " .. key .. " value: ")
print(value)
end
end
function getFactionOfObjectiveToken(id)
for faction, tokens in pairs(orderTokens) do
for type, ids in pairs(tokens) do
for _, tokenId in ipairs(ids) do
if tokenId == id then
return faction
end
end
end
end
end
return UTILS
-- alternative way to get object rotation, but can't attach to an object >:-|
--[[
function onObjectRotate(object, spin, flip, player_color, old_spin, old_flip)
if spin ~= old_spin then
print(player_color .. " spun " .. tostring(object) .. " from " .. old_spin .. " degrees to " .. spin .. " degrees")
end
-- flip 180 means right side up
if flip ~= old_flip then
print(player_color .. " flipped " .. tostring(object) .. " from " .. old_flip .. " degrees to " .. flip .. " degrees")
end
end ]]