-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.lua
More file actions
186 lines (176 loc) · 5.48 KB
/
build.lua
File metadata and controls
186 lines (176 loc) · 5.48 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
local building = {{{0},{0},{0},{0},{0}},{{0},{0},{0},{0},{0}},{{0},{0},{0},{0},{0}},{{0},{0},{0},{0},{0}},{{0},{0},{0},{0},{0}}} --a 5x5x5 array
local unit = {}
user = { } --may be moved to user.lua, which will mainly hold tweets (user-created functions) and stats
user.__index = user
user.energy = 9000 --TEMPORARY!
resetter = 1
require("middleclass")
Build = class("Build")
function Build:placeBlock(x,y,z,block) --obvious arguments
if resetter == 1 then
Build:empty(0)
resetter = 0
end
if Build:authenticate(block) == 0 then return "nice try, suckah" end --checks if someone is trying to cheat (by making a block without paying for energy with: block = { } block.blastRes = 9000 and so on.
if x ~= nil then --bonus: use nil for x then it won't do anything except return 1 for "this block COULD be placed" and 0 for "no energy, suckah!"
if user.energy >= block.energy then
building[x][y][z] = block
user.energy = user.energy - block.energy
return 1
else return 0 end
else if user.energy >= block.energy then return 1 else return 0 end
end
end
function Build:rmBlock(x,y,z,block) --obvious arguments. Removes block and returns 50% energy. Woot!
if resetter == 1 then
Build:empty(0)
resetter = 0
end
if building[x][y][z] == block then
building[x][y][z] = 0
local en = block.energy/2
user.energy = user.energy + en
return 1
else return 0 end
end
function Build:suspen(x,y,z,block) --obvious args. builds a suspension block, which is 1/4 of the size, takes 1/4 of the energy but has 1/4 of the stats...
if resetter == 1 then
Build:empty(0)
resetter = 0
end
if Build:authenticate(block) == 0 then return "nice try, suckah" end
quart = block
if quart.density ~= nil then quart.density = quart.density/4 end --add new properties as they come
if quart.density ~= nil then quart.blastRes = quart.blastRes/4 end
if quart.float ~= nil then quart.float = quart.float/4 end
if quart.conductivity ~= nil then quart.conductivity = quart.conductivity/4 end
quart.cpu = 0
if quart.explosive ~= nil then quart.explosive = quart.explosive/4 end
quart.sticky = 1
if quart.energy ~= nil then quart.energy = quart.energy/4 end
if quart.radioactive ~= nil or 0 then quart.radioactive = 1 else quart.radioactive = 0 end
if user.energy >= quart.energy then
building[x][y][z] = quart
user.energy = user.energy - quart.energy
return 1
else
return 0
end
end
function Build:make() --release the building into the field! yaaaayyy!
if resetter == 1 then
Build:empty(0)
resetter = 0
end
unit = Build:new()
for x=1,5,1 do
unit[x] = building[x] --cp building into unit w/o removing instance status
end
for z=1,5,1 do --for every value of Z test if there are any blocks, if not, block above drops down (GRAVITY!)
for x=1,5,1 do --every value of X (I am so glad that I went for 5x5x5, not 10x10x10!)
for y=1,5,1 do --every value of Y
if unit[x][y][z] == 0 then --if there's no block in that place
if Build:testFloat(x,y,z,unit) == 0 then --if not sticky or floating then
local copy = unit[x][y][z+1] --cut the block above
unit[x][y][z+1] = 0 --down one level
unit[x][y][z] = copy --and paste
end
end
end
end
end
resetter = 1
return unit
end
function Build:read(x,y,z) --forcing it to read, but making it unable to write, building[x][y][z]!
return building[x][y][z]
end
function Build:readAll()
return building
end
function Build:empty(restore) --when called w/ restore == 1, restores 50% energy. if called with restore == 0 (when used by build.make()), it doesn't restore
for x=1,5,1 do
for y=1,5,1 do
for z=1,5,1 do
if instanceOf(Chem,building[x][y][z]) then
if restore == 1 then local en = building[x][y][z].energy/2 end
building[x][y][z] = 0
if restore == 1 then
user.energy = user.energy + en
end
end
end
end
end
end
function Build:testFloat(x,y,z,unit)
local density = 0
for xT=1,5,1 do
for yT=1,5,1 do
if instanceOf(Chem,unit[xT][yT][z]) then
if unit[xT][yT][z].density ~= nil then
density = density + unit[xT][yT][z].density
end
end
end
end
local float = 0
for xT=1,x,1 do --check all blocks below stated
for yT=1,y,1 do
if instanceOf(Chem,unit[xT][yT][z]) then
if unit[xT][yT][z].density ~= nil then
float = float + unit[xT][yT][z].density
end
end
end
end
local sticky = 0
for zT=1,5,1 do
if x < 5 then
if instanceOf(Chem,unit[x+1][y][z]) then
if unit[x+1][y][z].sticky ~= nil then sticky = 1 end
end
end
if x > 1 then
if instanceOf(Chem,unit[x-1][y][z]) then
if unit[x-1][y][z].sticky ~= nil then sticky = 1 end
end
end
if y < 5 then
if instanceOf(Chem,unit[x][y+1][z]) then
if unit[x][y+1][z].sticky ~= nil then sticky = 1 end
end
end
if y > 1 then
if instanceOf(Chem,unit[x][y-1][z]) then
if unit[x][y-1][z].sticky ~= nil then sticky = 1 end
end
end
if instanceOf(Chem,unit[x][y][z]) then
if unit[x][y][z].sticky ~= nil then
if x < 5 then
if instanceOf(Chem,unit[x+1][y][z]) then sticky = 1 end
end
if x > 1 then
if instanceOf(Chem,unit[x-1][y][z]) then sticky = 1 end
end
if y < 5 then
if instanceOf(Chem,unit[x][y+1][z]) then sticky = 1 end
end
if y > 1 then
if instanceOf(Chem,unit[x][y-1][z]) then sticky = 1 end
end
end
end
end
if sticky == 1 then
return 1
elseif density < float then
return 1
else return 0 end
end
function Build:authenticate(instance)
if instanceOf(Chem,instance) then
return 1
else return 0 end
end