-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformspecs.lua
More file actions
200 lines (189 loc) · 5.88 KB
/
formspecs.lua
File metadata and controls
200 lines (189 loc) · 5.88 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
local api = robot.internal_api
local S = api.translator
api.add_formspec('program', function (code, errmsg, ignore_errors)
return ([[
size[12,10]
style_type[label,textarea;font=mono]
label[0.1,8.3;%s]
textarea[0.2,0.2;12.2,9.5;code;;%s]
button[4.75,9;3,1;program;%s]
checkbox[1,9;ignore_errors;%s;%s]
button[9,9;3,1;reset_memory;%s]
]]):format(
core.formspec_escape(errmsg),
core.formspec_escape(code),
core.formspec_escape(S("Save Program")),
core.formspec_escape(S("Ignore errors")),
ignore_errors and 'true' or 'false',
core.formspec_escape(S("Reset memory"))
)
end)
api.add_formspec('inventory', function (nodeinfo)
local meta = nodeinfo.meta()
local extras_enabled_list = string.split(meta:get_string('extras'),',')
local extras_enabled = {}
for _,def in ipairs(extras_enabled_list) do
extras_enabled[def] = true
end
local inv = nodeinfo.inv()
local fuel_size = inv:get_size('fuel')
local abilities_size = inv:get_size('abilities')
local info = nodeinfo.info()
local is_connective = api.is_connective(nodeinfo)
local has_connection = is_connective and api.is_connected(nodeinfo)
local tier_def = api.tier(info.tier)
local exec_button = ''
if info.status == 'stopped' then
exec_button = core.formspec_escape(S("Run"))
elseif info.status == 'error' then
exec_button = core.formspec_escape(S("ERROR"))
elseif info.status == 'running' then
exec_button = core.formspec_escape(S("Stop"))
elseif info.status == 'broken' then
exec_button = core.formspec_escape(S("Broken"))
end
local size = tier_def.form_size
local fuel_pos = {x=0,y=3}
fuel_pos.y = 3 - math.floor((fuel_size-1)/2)
local default_abilities = {}
for _,ability in ipairs(api.part(info.part).default_abilities or {}) do
if api.ability_enabled(ability) then
table.insert(default_abilities, ([[
item_image[%i,3;1,1;%s]
]]):format(3+#default_abilities, api.ability(ability).item))
end
end
local extra_abilities = {}
for i,ability in ipairs(tier_def.extra_abilities or {}) do
if api.ability_enabled(ability) then
local ability_obj = api.ability(ability)
table.insert(extra_abilities, ([[
item_image_button[%i,2;1,1;%s;ability_switch_%s;]
tooltip[ability_switch_%s;%s]
]]):format(
2+#extra_abilities,
ability_obj.item,
ability,ability,
core.formspec_escape(ability_obj.description)
)..(extras_enabled[ability] and ("item_image[%i,2;1,1;%s]"):format(2+#extra_abilities, "moretrees:coconut_1") or ""))
end
end
return ("size[%f,8]"):format(size)..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
([[
list[context;main;%f,0;%i,2;]
list[context;abilities;%f,3;%i,1;]
list[context;fuel;%i,%i;2,%i;]
item_image[%i,%i;1,1;%s]
item_image_button[2,3;1,1;%s;ability_reference;]
tooltip[ability_reference;%s]
list[current_player;main;%f,4.3;8,4;]
listring[context;main]
listring[current_player;main]
%s
%s
%s
]]):format(
fuel_size > 4 and 2 or 0,
fuel_size > 4 and size-2 or size,
3+#default_abilities,
abilities_size,
fuel_pos.x,fuel_pos.y,math.ceil(fuel_size/2),
fuel_pos.x,fuel_pos.y,
api.config.fuel_item,
api.config.ability_item,
core.formspec_escape(S("Ability Reference")),
(size-8)/2,
is_connective
and ("button[%f,2;6,1;connection;%s]")
:format(
2+#extra_abilities,
has_connection and "Connected" or "Disconnected"
)
or ([[
button[%f,2;4,1;program_edit;%s]
button[%f,2;2,1;status;%s]
]]):format(
4+#extra_abilities,
core.formspec_escape(S("Edit Program")),
2+#extra_abilities,
exec_button
),
table.concat(default_abilities, ""),
table.concat(extra_abilities, "")
)
end)
local command_wrap_limit = 70
api.add_formspec('ability', function (nodeinfo)
local info = nodeinfo.info()
local entries = {}
for _,ability_name in ipairs(api.abilities()) do
local ability = api.ability(ability_name)
if api.ability_enabled(ability.ability)
and not ability.interface_enabled
and (not ability.done_by or ability.done_by[info.part]) then
local command = S("No command")
if ability.action then
command = S("Command")..": "
if ability.command_example then
command = command .. ability.command_example
else
command = command .. "robot."..ability.ability.."()"
end
end
local command_wraps = #command > command_wrap_limit
local wrap_position = command_wraps
and (string.find(command, "%W", command_wrap_limit, false))
or 1000 -- any number bigger than the max length
table.insert(entries, ([[
item_image[0,%i;1,1;%s]
label[1,%i;%s]
label[1,%f;%s]
]]..(command_wraps and "label[%f,%f;%s]" or "")):format(
#entries, ability.item,
#entries, core.formspec_escape(ability.description),
#entries+0.4, core.formspec_escape(command_wraps
and string.sub(command, 1, wrap_position)
or command
),
7.5 - ((#command - wrap_position) / 12), #entries+0.65,
core.formspec_escape(string.sub(command, wrap_position+1))
))
end
end
return ("size[8,%i]"):format(#entries)..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
table.concat(entries, "")
end)
api.add_formspec('error', function (error)
return ([[
size[12,2]
label[0.1,0.3;%s]
button[4.75,1;3,1;dismiss_error;%s]
]]):format(
core.formspec_escape(error),
core.formspec_escape(S("Dismiss error"))
)
end)
api.add_formspec('broken', function ()
local item_description = api.config.repair_item
local item_def = core.registered_nodes[api.config.repair_item] or core.registered_items[api.config.repair_item]
if item_def then
item_description = item_def.description
end
return ([[
size[8,2]
item_image[0,0;2,2;%s]
label[2,0.5;%s]
label[2,1;%s]
]]):format(
api.config.repair_item,
core.formspec_escape(S("The robot is broken")),
core.formspec_escape(S("Use a").." "..item_description.." "..S("to repair it"))
)
end)
core.register_on_player_receive_fields(api.global_on_receive_fields)