forked from huchenlei/auto-crossbreeding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposUtil.lua
106 lines (93 loc) · 3.24 KB
/
posUtil.lua
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
local config = require("config")
local function posToSlot(size, pos)
local lastColNum
if pos[1] % 2 == 1 then
lastColNum = pos[2] + 1
else
lastColNum = size - pos[2]
end
return (pos[1] - 1) * size + lastColNum
end
local function slotToPos(size, slot)
local x = (slot - 1) // size + 1
local y
local lastColNum = (slot - 1) % size
if x % 2 == 1 then
y = lastColNum
else
y = size - lastColNum - 1
end
return {x, y}
end
local function globalToFarm(globalPos)
return posToSlot(config.farmSize, globalPos)
end
local function farmToGlobal(farmSlot)
return slotToPos(config.farmSize, farmSlot)
end
local function globalToStorage(globalPos)
return posToSlot(config.storageFarmSize, {-globalPos[1], globalPos[2]})
end
local function storageToGlobal(storageSlot)
local globalPos = slotToPos(config.storageFarmSize, storageSlot)
globalPos[1] = -globalPos[1];
return globalPos
end
local function multifarmPosInFarm(pos)
local absX = math.abs(pos[1])
local absY = math.abs(pos[2])
return (absX + absY) <= config.multifarmSize and (absX > 2 or absY > 2) and absX < config.multifarmSize-1 and absY < config.multifarmSize-1
end
local function globalPosToMultifarmPos(pos)
return {pos[1]-config.multifarmCentorOffset[1], pos[2]-config.multifarmCentorOffset[2]}
end
local function multifarmPosToGlobalPos(pos)
return {pos[1]+config.multifarmCentorOffset[1], pos[2]+config.multifarmCentorOffset[2]}
end
local function multifarmPosIsRelayFarmland(pos)
for i = 1, #config.multifarmRelayFarmlandPoses do
local rPos = config.multifarmRelayFarmlandPoses[i]
if rPos[1] == pos[1] and rPos[2] == pos[2] then
return true
end
end
return false
end
local function nextRelayFarmland(pos)
if pos == nil then
return config.multifarmRelayFarmlandPoses[1]
end
for i = 1, #config.multifarmRelayFarmlandPoses do
local rPos = config.multifarmRelayFarmlandPoses[i]
if rPos[1] == pos[1] and rPos[2] == pos[2] and i < #config.multifarmRelayFarmlandPoses then
return config.multifarmRelayFarmlandPoses[i+1]
end
end
end
local function findOptimalDislocator(pos)
-- return: {dislocatorGlobalPos, relayFarmlandGlobalPos}
local minDistance = 100
local minPosI
for i = 1, #config.multifarmDislocatorPoses do
local rPos = config.multifarmDislocatorPoses[i]
local distance = math.max(math.abs(pos[1] - rPos[1]), math.abs(pos[2] - rPos[2]))
if distance < minDistance then
minDistance = distance
minPosI = i
end
end
return {multifarmPosToGlobalPos(config.multifarmDislocatorPoses[minPosI]),
multifarmPosToGlobalPos(config.multifarmRelayFarmlandPoses[minPosI])}
end
return {
globalToFarm = globalToFarm,
farmToGlobal = farmToGlobal,
globalToStorage = globalToStorage,
storageToGlobal = storageToGlobal,
multifarmPosInFarm = multifarmPosInFarm,
multifarmPosIsRelayFarmland = multifarmPosIsRelayFarmland,
globalPosToMultifarmPos = globalPosToMultifarmPos,
multifarmPosToGlobalPos = multifarmPosToGlobalPos,
findOptimalDislocator = findOptimalDislocator,
nextRelayFarmland = nextRelayFarmland
}