-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholofailure.lua
378 lines (330 loc) · 11.7 KB
/
holofailure.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
--imports
local unicode = require('unicode')
local event = require('event')
local term = require('term')
local fs = require('filesystem')
local component = require("component")
local keyboard = require("keyboard")
local computer = require("computer")
local const={}--Constants, NEVER UNDER ANY CIRCUMSTANCES WRITE NEW VALUES TO THINGS DEFINED HERE!!! (if u need to, move them out of that table)
do --resolution constants
const.removeCode=-999
const.resolutionMax={32,48,48}-- x,y,z max size
const.resolutionMaxSize=73728 -- 48*48*32
const.tDefaultPallete={}
const.tDefaultPallete[1]=0x00ffff--C - cos i hate red
const.tDefaultPallete[2]=0x00ff00--G
const.tDefaultPallete[3]=0x0000ff--B
const.tDefaultPallete.__index=const.tDefaultPallete
end
local meta={}--main meta table holding obj definitions (meta tables and meta methods)
do meta.machineArray={} meta.machineArray.typeName="machineArray"
function meta.machineArray:new()
local o={}
setmetatable(o,self)
self.__index=self
self["machines"]={}
return o
end
function meta.machineArray:add(mMachine)
for k,v in ipairs(self.machines) do
if v.pos==mMachine.pos then
return false
end
end
table.insert(self.machines,mMachine)--yeah put the machine there
return true
end
function meta.machineArray:del(mMachine)
for i=#self.machines,1,-1 do
if self.machines[i].pos==mMachine.pos then
--table.remove(self.machines,i)
self.machines[i].state=const.removeCode
return true
end
end
return false
end
function meta.machineArray:update(mFrame)
for i=#self.machines,1,-1 do
if self.machines[i].state==const.removeCode then
mFrame:setPosition(self.machines[i].pos,mFrame.default)
table.remove(self.machines,i)
else
mFrame:setPosition(self.machines[i].pos,self.machines[i].state)
end
end
end
end
do meta.machine={} meta.machine.typeName="machine"
function meta.machine:new(position,state)
local o={--using o like u know object
["pos"]=position,
["state"]=state}
setmetatable(o,self)
self.__index=self
--self["check"]={}
return o
end
function meta.machine:newFromXYZ(x,y,z,state)
return self:new(func.xyzToPosition(x,y,z),state)
end
function meta.machine:set(mState)
self.state=mState
end
end
do meta.frame={} meta.frame.typeName="frame"
function meta.frame:new(mDefault)--default should be 0 - 3
local o={
["default"]=mDefault or 0}
setmetatable(o,self)
self.__index=self
self["voxels"]={}
self["Palette"]={}
local tDefault={}--seting default value metatable
tDefault.__index=function() return mDefault end
setmetatable(self.voxels,tDefault)
setmetatable(self.Palette,const.tDefaultPallete)--no custom default palletes
return o
end
function meta.frame:setPosition(index,mState)--states are normal numbers
if mState~=self.default then
self.voxels[index]=mState
return true
else
if self.voxels[index] then
self.voxels[index]=nil
return true
end
return false
end
end
function meta.frame:set(x,y,z,mState)--states are normal numbers
return self:setPosition(func.xyzToPosition(x,y,z),mState)
end
function meta.frame:setLine(x,y,z,length,axis,state) -- length: 1 = bottom to top 2 = back to front 3 = left to right DIAGONALS NOT A THING YET TODO
local switch={}
switch[1]=function()--x 0 = bottom; 31 = top
if length > 0 then
for i = x, x+length do
self:set(i,y,z,state)
end
else
for i = x+length,x do
self:set(i,y,z,state)
end
end
end
switch[2]=function()--y 0 = back ; 47 = front
if length > 0 then
for i = y, y+length do
self:set(x,i,z,state)
end
else
for i = y+length,y do
self:set(x,i,z,state)
end
end
end
switch[3]=function()--z 0 = left ; 47 = right
if length > 0 then
for i = z, z+length do
self:set(x,y,i,state)
end
else
for i = z+length,z do
self:set(x,y,i,state)
end
end
end
return switch[axis]
end
--function meta.frame:setCuboid(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax,state,tWalls)--tWalls = {top,bottom,left,right,front,back}
-- self:set(x,y,z,state)
--end
function meta.frame:setPalette(tPallete)
self.Palette=func.iDup(tPallete)
return self.Palette
end
function meta.frame:sendPallete(cHolo)
return cHolo.setPaletteColor(1,self.Palette[1]),
cHolo.setPaletteColor(2,self.Palette[2]),
cHolo.setPaletteColor(3,self.Palette[3])
end
function meta.frame:sendVoxels(cHolo)
return cHolo.setRaw(self:toString())
end
function meta.frame:toString()
local str=""
local char=string.char
for i=1,const.resolutionMaxSize do
str=str..char(self.voxels[i])
end
return str
end
function meta.frame:setFromString(str)
self.voxels=func.tClean(self.voxels)--if table has metatable must be constructed like so, it forwards metatable to the object/table
for i=1,#str do
if str:byte(i)~=self.default then ---48 changes string byte to number if it is a single digit
self.voxels[i]=str:byte(i)
end
end
return self.voxels
end
function meta.frame:toCompact()
local char=string.char
local str=char(self.default)..
func.formatColor(self.Palette[1])..
func.formatColor(self.Palette[2])..
func.formatColor(self.Palette[3])
for i=1,const.resolutionMaxSize,4 do
str=str..char(
self.voxels[i]*64+
self.voxels[i+1]*16+
self.voxels[i+2]*4+
self.voxels[i+3])
end
return str
end
function meta.frame:setFromCompact(str)
self.default=str:byte(1)
local tDefault={}--seting default value metatable
tDefault.__index=function() return str:byte(1) end
setmetatable(self.voxels,tDefault)
self.Palette[1]=func.readColor(str,2)
self.Palette[2]=func.readColor(str,5)
self.Palette[3]=func.readColor(str,8)
self.voxels=func.tClean(self.voxels)
local band=bit32.band
local rshift=bit32.rshift
local insert=table.insert
local b
for i=11,#str do
insert(self.voxels,rshift(band(colorNumber,0xC0),6))
insert(self.voxels,rshift(band(colorNumber,0x30),4))
insert(self.voxels,rshift(band(colorNumber,0x0C),2))
insert(self.voxels,band(colorNumber,0x03))
end
return self.voxels
end
end
func={}--functions
do --table test/duplication
function func.isTable(tab)
return type(tab)=="table"
end
function func.iDup(tab)--iterable TABLE DUPLICATOR
local t={}
for k,v in ipairs(tab) do
t[k]=v
end
return t
end
function func.pDup(tab)--non-iterable TABLE DUPLICATOR
local t={}
for k,v in pairs(tab) do
t[k]=v
end
return t
end
function func.oDup(object)--used to duplicate objects
local obj=setmetatable({},getmetatable(object))--reuse metatables
for k,v in pairs(object) do
if func.isTable(v) then
obj[k]=func.oDup(v)--function is dumb will halt the process on looped table
else
obj[k]=v
end
end
return obj--reference to new table
end
function func.tClean(tab)--cleans the table keeping metatable
return setmetatable({},getmetatable(tab))--cheeky scrubness, result shuld be put into the variable
end
end
do --instanceof and obj helper functions
function func.instanceOf(tab,mTab)--checks if the object (tab) is instance of the thing
return getmetatable(tab)==mTab
--func.instanceOf(someMachine,meta.machine) should give true
end
function func.getTypeName(tab)--gets obj type name
return getmetatable(tab).typeName
end
function func.getObjTypeFromName(str)
for k,v in pairs(meta) do
if v.typeName==str then
return v
end
end
end
end
do --translations of coordinates
--x 0 = bottom; 31 = top
--y 0 = back ; 47 = front
--z 0 = left ; 47 = right
--x ,y ,z starts from 0
--positions start from 1
function func.xyzToPosition(x,y,z)
return 1+x+32*y+1536*z -- y is shifted by xMax, z is shifted by (xMax)*(yMax)
end
function func.positionToXYZ(pos)
pos=pos-1
local Z=math.floor(pos/1536)-- full multiples of 1536 OK
local X=bit32.band(pos,0x20) -- 0-47 modulus gets the beggining OK
return X,math.floor((pos-Z*1536)/48),Z --calculate Y as the remaining thing
end
end
do --other util funtions (single functions without groups)
function func.cyclicRandom(actual,minimum,maximum)--a random function for generating integer in range but different from previous one
if minimum>maximum then minimum,maximum=maximum,minimum end
local range=maximum-minimum
local randomNumber=actual+math.random(1,range-1)
if randomNumber>maximum then randomNumber=randomNumber-range end
return randomNumber
end
end
do --color formating
function func.formatColor(colorNumber)--no alpha
local band=bit32.band
local rshift=bit32.rshift
local char=string.char
local r=rshift(band(colorNumber,0xFF0000),16)
local g=rshift(band(colorNumber,0x00FF00),8)
local b=band(colorNumber,0x0000FF)
return char(r)..char(g)..char(b)
end
function func.readColor(str,startPos)
return str:byte(startPos)*65536+
str:byte(startPos+1)*256+
str:byte(startPos+2)
end
end
--VARIABLES
local holo = component.hologram
local colours = {--Layer 1 Norm, Fail, Crit Layer 2 Primary, Secondary, Highlight
{0x090c9b,0x3d52d5,0x3d52d5},
{0x0b5563,0x58a4b0,0x2de1fc},
{0xff8811,0xf4d06f,0xefc700}}
local rPallet = 1
--Objects
local machineArray1=meta.machineArray:new()
local frame1=meta.frame:new(0)
--do once
holo.clear()
holo.setScale(.3)
while true do--main loop
rPallet=func.cyclicRandom(rPallet,1,3)
frame1:setPalette(colours[rPallet])
frame1:sendPallete(holo)
--Define 3D frame
local newmachine1=meta.machine:newFromXYZ(math.random(0,31),math.random(0,47),math.random(0,47),math.random(1,3))--creating new machine
machineArray1:add(newmachine1)--adding to machine array
local l,a,c
frame1:setLine(15,23,23,l,a,0)
l,a,c=math.random(-10,10),math.random(1,3),math.random(1,3)
frame1:setLine(15,23,23,l,a,c)
machineArray1:update(frame1)--updating frame content
frame1:sendVoxels(holo)--
print(tostring(math.floor((computer.totalMemory()-computer.freeMemory()-235738)/1024)).."k RAM Used") --Shows used Ram
os.sleep(5)
end