-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.lua
More file actions
108 lines (94 loc) · 2.43 KB
/
Copy pathUtility.lua
File metadata and controls
108 lines (94 loc) · 2.43 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
-- ("lame boring text"):title() - "Lame Boring Text"
function string:title()
return self:gsub("(%a)([%w_']*)", function(a,b) return a:upper() .. b:lower() end)
end
function string:startswith(check)
check = tostring(check)
return #self >= #check and self:sub(1, #check) == check
end
function string:endswith(check)
check = tostring(check)
return #self >= #check and self:sub(-#check) == check
end
-- ("Hello world"):insert(6, " there") - "Hello there world"
function string:insert(index, insertion)
if #self < index then
return self .. insertion
end
local start = self:sub(1, index - 1)
local ending = self:sub(index)
return start .. insertion .. ending
end
-- ("book"):replace("b", "r") - "rook"
function string:replace(character, replacement)
return self:gsub("%" .. character .. "+", replacement)
end
function table:contains(value)
for _, v in pairs(self) do
if v == value then
return true
end
end
return false
end
function table:clear()
for k in pairs(self) do
self[k] = nil
end
end
function table:copy()
local copy = {}
for key, value in pairs(self) do
if type(value) == "table" then
copy[key] = table.copy(value)
else
copy[key] = value
end
end
return setmetatable(copy, getmetatable(self))
end
function table:set()
local new = {}
for _, value in ipairs(self) do
new[value] = true
end
return new
end
function math.round(num, numDecimalPlaces)
local mult = 10 ^ (numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
local _lineswitch
function moveToLine(x1, y1, x2, y2)
assert(x1 == x2 or y1 == y2, "error: moveToLine: coordinates must be a direct line from each other.")
if math.random() < .5 then -- 50% chance of making the line a bit shorter
if x1 == x2 then -- Vertical
if getPlayerY() ~= y1 then -- Don't modify the current cell, to prevent player from moving back and forth on the same spot
y1 = y1 + 1
end
if getPlayerY() ~= y2 then
y2 = y2 - 1
end
else -- Horizontal
if getPlayerX() ~= x1 then
x1 = x1 + 1
end
if getPlayerX() ~= x2 then
x2 = x2 - 1
end
end
end
if _lineswitch then
if getPlayerX() == x1 and getPlayerY() == y1 then
_lineswitch = not _lineswitch
else
moveToCell(x1, y1)
end
else
if getPlayerX() == x2 and getPlayerY() == y2 then
_lineswitch = not _lineswitch
else
moveToCell(x2, y2)
end
end
end