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
addhook("objectkill","treewallkill")
function treewallkill(obj,id)
	if (object(obj,"type")==1) then
		local x,y=toTiles(object(obj,"x"),object(obj,"y"))
		freeimage(imageInRange(x,y))
		resources[id][1]=resources[id][1]+1
	end
	if (object(obj,"type")==3) then
		local tx,ty=toTiles(object(obj,"x"),object(obj,"y"))
		if (blockInRange(tx,ty)~=nil) then
			resources[id][block[blockInRange(tx,ty)][2]]=resources[id][block[blockInRange(tx,ty)][2]]+1
			if (bimg[blockInRange(tx,ty)]~=nil) then
				freeimage(bimg[blockInRange(tx,ty)])
				bimgpos[bimg[blockInRange(tx,ty)]][1]=0
				bimgpos[bimg[blockInRange(tx,ty)]][2]=0
				bimg[blockInRange(tx,ty)]=0
			end
		end
	end
end
function blockInRange(x,y)
	for blox=1,1024 do
		local bx=block[blox][3]
		local by=block[blox][4]
		if (bx==x and by==y) then
			return blox
		else
			return nil
		end
	end
end
function imageInRange(x,y)
	for img=1,256 do
		if (x==imgpos[img][1] and y==imgpos[img][2]) then
			return img
		else
			return nil
		end
		if (x==bimgpos[img][1] and y==bimgpos[img][2]) then
			return img
		else
			return nil
		end
	end
end
function buildBlock(id,bbblock,mode,x,y)
	if (resources[id][bbblock]>0) then
		resources[id][bbblock]=resources[id][bbblock]-1
		if (mode==1) then
			if (objectInRange(x,y)==nil) then
				parse("spawnobject 3 "..x.." "..y.." 1 1 0 "..id)
				bimg[curblock]=image(blocks[bbblock].img,x*32+16,y*32+16,1)
				block[curblock][2]=bbblock
				block[curblock][3]=x
				block[curblock][4]=y
				block[curblock][5]=1 -- type - wall
				bimgpos[bimg[curblock]][1]=x
				bimgpos[bimg[curblock]][2]=y
				curblock=curblock+1
			end
		end
		if (mode==2) then
			if (imageInRange(x,y)==nil) then
				bimg[curblock]=image(blocks[bbblock].img,x*32+16,y*32+16,0)
				block[curblock][2]=bbblock
				block[curblock][3]=x
				block[curblock][4]=y
				block[curblock][5]=2 -- type - floor
				bimgpos[bimg[curblock]][1]=x
				bimgpos[bimg[curblock]][2]=y
				curblock=curblock+1
			end
		end
	end
end