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
building_time = {
	[1] = 1, -- Barricade
	[2] = 1, -- Barbed Wire
	[3] = 1, -- Wall I
	[4] = 1, -- Wall II
	[5] = 2, -- Wall III
	[6] = 2, -- Gate Field
	[7] = 2, -- Dispenser
	[8] = 3, -- Turret
	[9] = 3, -- Supply
	[13] = 2, -- Teleporter Entrance
	[14] = 2, -- Teleporter Exit
}
building_price = {
	[1] = 300, -- Barricade
	[2] = 500, -- Barbed Wire
	[3] = 1000, -- Wall I
	[4] = 2000, -- Wall II
	[5] = 3000, -- Wall III
	[6] = 1500, -- Gate Field
	[7] = 5000, -- Dispenser
	[8] = 5000, -- Turret
	[9] = 5000, -- Supply
	[13] = 3000, -- Teleporter Entrance
	[14] = 3000, -- Teleporter Exit
}
t_buildings = {}
addhook("buildattempt", "simulatebuilding")
function simulatebuilding(id, type, x, y, mode)
	if (math.ceil(math.sqrt((player(id, "tilex") - x)^2 + (player(id, "tiley") - y)^2)) == 1) then
		if (player(id, "money") > building_price[type]) then
			t_buildings[#t_buildings+1] = {type = type, id = id, x = x, y = y, mode = mode, timer = {"parse", "spawnobject "..type.." "..x.." "..y.." 0 "..mode.." "..player(id,"team").." "..id}}
			parse("spawnobject 10 "..x.." "..y.." 0 "..mode.." "..player(id,"team").." "..id) -- Spawn a construction site on the spot
			timer(building_time[type]*1000, "parse", "killobject "..#object(0,"table")) -- Destroy the construction site after the building time
			timer(building_time[type]*1000, t_buildings[#t_buildings].timer[1], t_buildings[#t_buildings].timer[2]) -- Turn that construction site into a complete building after the building time
			parse("setmoney "..id.." "..player(id, "money") - building_price[type])
		else
			msg2(id,"\169255000000You have insufficient funds@C")
		end
	else
		msg2(id,"\169255000000you cannot build here@C")
	end
	return 1
end
addhook("objectkill", "destroycsites")
function destroycsites(oid, id)
	if (id > 0) then
		for k, v in pairs(t_buildings) do
			if (v.x == object(oid,"tilex") and v.y == object(oid,"tiley")) then
				freetimer(unpack(v.timer)) -- Free the completion timer in case it's still being built
				table.remove(t_buildings, k) -- Remove this entry from the table and rearrange the table neatly again
			end
		end
	end
end