Forum

> > CS2D > Scripts > Check if Building was completed
Forums overviewCS2D overview Scripts overviewLog in to reply

English Check if Building was completed

10 replies
To the start Previous 1 Next To the start

old Check if Building was completed

AtomKuh
User Off Offline

Quote
Is there a way to proof whether the Construction Site which someone built, had turned into a real building or not?

I want to send a message in case of a successful construction.

For instance:
1
2
3
4
5
6
7
addhook("build","b2d")
function b2d(id,type,x,y)
     if type < 20 then
		  msg("Test message") -- ONLY IF THE CONSTRUCTION SITE TURNED INTO A BUILDING
          return 0
     end
end

old Re: Check if Building was completed

AtomKuh
User Off Offline

Quote
Well, the construction site could be destroyed until it's finished. Is there a way to inspect the X and Y coords while the building is under construction and check after the constant time whether it is finished or not?

EDIT: I havent read Starkkz post yet

Could someone tell me how I can find out the construction time of each building?
edited 1×, last 27.02.17 11:07:52 pm

old Re: Check if Building was completed

VADemon
User Off Offline

Quote
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
addhook("build", "buildmsg")

buildmsg_timer = {
	-- {id, type, x, y, mode, objectid, counter=0}
}

function buildmsg(id, type, x, y, mode, objectid)
	msg("Player: ".. id ..", type: ".. type ..", mode: ".. mode ..", objid: ".. objectid)
	if type < 20 then
		msg("Starting ticker for building")
		table.insert(buildmsg_timer, {id, type, x, y, mode, objectid, 0})
	end
end

addhook("always", "always_buildmsg")
function always_buildmsg()
	for i = #buildmsg_timer, 1, -1  do 	-- reverse order is required for table.remove
		--msg("Checking ID: ".. i .. " out of ".. #buildmsg_timer)
		if objectat(buildmsg_timer[i][3], buildmsg_timer[i][4], 10) ~= 0 then
			buildmsg_timer[i][7] = buildmsg_timer[i][7] + 1
		else
			msg("Object built at ".. buildmsg_timer[i][3].."|"..buildmsg_timer[i][4] .." in ".. buildmsg_timer[i][7] .."ticks")
			table.remove(buildmsg_timer, i)
		end
	end
end

Turns out building times differ:
Wall 3 = 200/201 ticks
Supply: 250 ticks etc.?

old Re: Check if Building was completed

Apache uwu
User Off Offline

Quote
Try this:

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
addhook("build", "_build")
addhook("objectkill", "_objectkill")

timers = {}
obj_times = {1000, 0, 2000, 3000, 4000, 3000, 5000, 5000, 5000, 0, 0, 0, 3000, 3000}

function wait(delay, func)
	local k = tostring(func):gsub("[^a-zA-Z0-9_]*", "_")
	timers[k] = function()
        func()
        timers[k] = nil
    end
	timer(delay + 100, "timers." .. k)
end

function _build(id, _type, x, y, mode, obj_id)
    wait(obj_times[_type], function()
        if object(obj_id, "type") == _type then
            msg(player(id, "name").." successfully built a "..object(obj_id, "typename").." at tile ["..x..","..y.."]")
        end
    end)

end

function _objectkill(id, pl)
    if object(id, "type") == 10 then
        msg(player(id, "name").."'s construction site was destroyed by "..(pl ~= 0 and player(pl, "name") or "a ghost"))
    end
end

You'll get a message when a building is finished, or when a construction site is destroyed (see lines 19 and 27 respectively).

old Re: Check if Building was completed

AtomKuh
User Off Offline

Quote
Your script worked for the first buildings, but after some tries killing the construction site I got this error message:

1
2
3
LUA ERROR: sys/lua/server.lua:27: attempt to concatenate a boolean value
 -> sys/lua/server.lua:27: in function <sys/lua/server.lua:25>
 -> in Lua hook 'objectkill', params: 3, 1

old Re: Check if Building was completed

Apache uwu
User Off Offline

Quote
I'm not getting the same error.

Are you running any other scripts that replaces player object?

In any case, you can replace line 27 with a simple msg(pl.." destroyed "..id).

old Fixed

VADemon
User Off Offline

Quote
Shows building+destruction time
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
addhook("build", "buildmsg")

buildmsg_timer = {
     -- {id, type, x, y, mode, objectid, counter=0}
}

function buildmsg(id, type, x, y, mode, objectid)
     msg("Player: ".. id ..", type: ".. type ..", mode: ".. mode ..", objid: ".. objectid)
     if type < 20 then
          msg("Starting ticker for building")
          table.insert(buildmsg_timer, {id, type, x, y, mode, objectid, 0})
     end
end

addhook("always", "always_buildmsg")
function always_buildmsg()
     for i = #buildmsg_timer, 1, -1  do      -- reverse order is required for table.remove
	 
          msg("Checking ID: ".. i .. " out of ".. #buildmsg_timer.." = ".. tostring(object(buildmsg_timer[i][6], "typename")))
		  
		  local btype = object(buildmsg_timer[i][6], "type")
		  
          if btype and btype==10 then
               buildmsg_timer[i][7] = buildmsg_timer[i][7] + 1
			   
          elseif btype then
               msg("Object built at ".. buildmsg_timer[i][3].."|"..buildmsg_timer[i][4] .." in ".. buildmsg_timer[i][7] .."ticks")
               table.remove(buildmsg_timer, i)
          else
			-- use buildmsg_timer[i][2] to find out building type
               msg("Object destroyed at ".. buildmsg_timer[i][3].."|"..buildmsg_timer[i][4] .." in ".. buildmsg_timer[i][7] .."ticks")
               table.remove(buildmsg_timer, i)
          end
     end
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview