-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.lua
More file actions
123 lines (114 loc) · 3.53 KB
/
Copy pathstartup.lua
File metadata and controls
123 lines (114 loc) · 3.53 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
local filename = "sounds" -- string : json file where sounds will be registered
local noteBlock -- table : peripheral, note block
local monitor -- table : peripheral, display monitor
local conf = {} -- table : configuration (x,y,z coords of the computer)
--- UTILS ---
-- returns an input entered by the user
local function getInput(question)
if question then
print(question)
end
term.setTextColor(colors.blue)
local answer = io.read()
term.setTextColor(colors.white)
return answer
end
--- INIT ---
local function loadAPIs(apis)
for _,path in pairs(apis) do
if not fs.exists(path) then
error("API [" .. path .. "] does not exist.")
end
print("\nLoading API : [" .. path .. "]")
os.loadAPI(path)
end
end
local function loadConfig()
if fs.exists("config") then
conf = objectJSON.decodeFromFile("config")
end
if next(conf) == nil then
print("\nPlease enter note block coordinates :")
while type(conf["x"]) ~= "number" do
conf["x"] = tonumber(getInput("X :"))
end
while type(conf["y"]) ~= "number" do
conf["y"] = tonumber(getInput("Y :"))
end
while type(conf["z"]) ~= "number" do
conf["z"] = tonumber(getInput("Z :"))
end
while type(conf["radius"]) ~= "number" or conf["radius"] < 0 do
conf["radius"] = tonumber(getInput("\nMap radius (>= 0) :"))
end
print("\nMap center coordinates :")
while type(conf["x0"]) ~= "number" do
conf["x0"] = tonumber(getInput("X0 :"))
end
while type(conf["y0"]) ~= "number" do
conf["y0"] = tonumber(getInput("Y0 :"))
end
while type(conf["z0"]) ~= "number" do
conf["z0"] = tonumber(getInput("Z0 :"))
end
objectJSON.encodeAndSavePretty("config", conf)
end
end
local function init(apis)
--load APIs
loadAPIs(apis)
-- initialize global variables and peripherals
noteBlock = peripheral.find("note_block")
if noteBlock == nil then
error("NoteBlock peripheral not found.")
end
monitor = peripheral.find("monitor")
-- run init() for each API
for _,path in pairs(apis) do
local api
for v in path:gmatch("([^/]+)") do
api = v
end
if _G[api].init ~= nil then
_G[api].init()
end
end
-- load or create configuration
loadConfig()
end
--- FUNCTIONS ---
-- prints all registered sounds
local function actualizeDisplay(doNotClear)
if not doNotClear then
term.clear()
end
sound.displaySounds(filename, false)
if monitor ~= nil then
local native = term.native()
term.redirect(monitor)
sound.displaySounds(filename, true)
term.redirect(native)
end
end
--- MAIN CALL ---
local function main()
init({"lib/sound", "soundManager", "parser"})
if monitor ~= nil then
actualizeDisplay()
end
local inst = {"ADD", "MODIFY", "DEL", "PLAY", "PLAY_HERE", "PLAY_CUSTOM", "PLAY_CUSTOM_HERE", "PLAY_GLOBALLY", "TEST_SOUND", "RESET_CONFIG"}
local input = ""
while true do
print("\nWaiting for an instruction...")
for _,v in pairs(inst) do
print(" - " .. v)
end
input = getInput()
actualizeDisplay()
parser.parse(filename, noteBlock, input)
if input == "ADD" or input == "MODIFY" or input == "DEL" then
actualizeDisplay(true)
end
end
end
main()