@@ -12,111 +12,116 @@ UPDATE_DIG = 1
1212UPDATE_SIGN = 2
1313
1414function NewUpdateQueue ()
15- queue = {first = nil , last = nil , current = nil }
15+ queue = {first = 0 , last =- 1 , current = nil }
1616
17- -- queue.newUpdate creates an update operation and
18- -- inserts it in the queue or returns an error
19- -- in case of UPDATE_SIGN, line 1 to 4 should
20- -- be present in meta parameter.
21- -- the delay is optional and will make sure that
22- -- the update is not triggered before given amount
23- -- of ticks (0 by default)
24- function queue :newUpdate (updateType , x , y , z , blockID , meta , delay )
25- if updateType ~= UPDATE_SET and updateType ~= UPDATE_DIG and updateType ~= UPDATE_SIGN
26- then
27- return NewError (1 ," NewUpdate: wrong update type" )
28- end
29-
30- if delay == nil
31- then
32- delay = 0
33- end
34-
35- update = {op = updateType ,next = nil ,x = x ,y = y ,z = z ,blockID = blockID ,meta = meta , delay = delay }
17+ -- queue.newUpdate creates an update operation and
18+ -- inserts it in the queue or returns an error
19+ -- in case of UPDATE_SIGN, line 1 to 4 should
20+ -- be present in meta parameter.
21+ -- the delay is optional and will make sure that
22+ -- the update is not triggered before given amount
23+ -- of ticks (0 by default)
24+ function queue :newUpdate (updateType , x , y , z , blockID , meta , delay )
25+ if updateType ~= UPDATE_SET and updateType ~= UPDATE_DIG and updateType ~= UPDATE_SIGN
26+ then
27+ return NewError (1 ," NewUpdate: wrong update type" )
28+ end
3629
37- -- update.exec executes update operation
38- -- and returns an error if it fails
39- function update :exec ()
40- if self .op == UPDATE_SET
41- then
42- cRoot :Get ():GetDefaultWorld ():SetBlock (self .x ,self .y ,self .z ,self .blockID ,self .meta )
43- elseif self .op == UPDATE_DIG
44- then
45- cRoot :Get ():GetDefaultWorld ():DigBlock (self .x ,self .y ,self .z )
46- elseif self .op == UPDATE_SIGN
47- then
48- cRoot :Get ():GetDefaultWorld ():SetSignLines (self .x ,self .y ,self .z ,self .meta .line1 ,self .meta .line2 ,self .meta .line3 ,self .meta .line4 )
49- else
50- return NewError (1 ," update:exec: unknown update type: " .. tostring (self .op ))
51- end
52- end
30+ if delay == nil
31+ then
32+ delay = 0
33+ end
5334
54- if self .first == nil
55- then
56- self .first = update
57- self .last = update
58- else
59- self .last .next = update
60- self .last = update
61- end
62- end -- ends queue.newUpdate
35+ update = {op = updateType ,x = x ,y = y ,z = z ,blockID = blockID ,meta = meta ,delay = delay }
6336
64- -- update triggers updates starting from
65- -- the first one. It stops when the limit
66- -- is reached, of if there are no more
67- -- operations in the queue. It returns
68- -- the amount of updates executed.
69- -- When an update has a delay > 0, the delay
70- -- is decremented, and the number of updates
71- -- executed is not incremented.
72- function queue :update (limit )
73- n = 0
74- self .current = self .first
37+ -- update.exec executes update operation
38+ -- and returns an error if it fails
39+ function update :exec ()
40+ if self .op == UPDATE_SET
41+ then
42+ cRoot :Get ():GetDefaultWorld ():SetBlock (self .x ,self .y ,self .z ,self .blockID ,self .meta )
43+ elseif self .op == UPDATE_DIG
44+ then
45+ cRoot :Get ():GetDefaultWorld ():DigBlock (self .x ,self .y ,self .z )
46+ elseif self .op == UPDATE_SIGN
47+ then
48+ cRoot :Get ():GetDefaultWorld ():SetSignLines (self .x ,self .y ,self .z ,self .meta .line1 ,self .meta .line2 ,self .meta .line3 ,self .meta .line4 )
49+ else
50+ return NewError (1 ," update:exec: unknown update type: " .. tostring (self .op ))
51+ end
52+ end
7553
76- while n < limit and self .current ~= nil
77- do
78- if self .current .delay == 0
79- then
80- err = self .current :exec ()
81- if err ~= nil
82- then
83- LOG (" queue:update error: " .. err .message )
84- break
85- end
86-
87- if self .current == self .first
88- then
89- self .first = self .current .next
90- end
91- n = n + 1
92- else
93- self .current .delay = self .current .delay - 1
94- end
95- self .current = self .current .next
96- end
54+ self :push (update )
55+ end -- ends queue.newUpdate
9756
98- return n
57+ -- update triggers updates starting from
58+ -- the first one. It stops when the limit
59+ -- is reached, of if there are no more
60+ -- operations in the queue. It returns
61+ -- the amount of updates executed.
62+ -- When an update has a delay > 0, the delay
63+ -- is decremented, and the number of updates
64+ -- executed is not incremented.
65+ function queue :update (limit )
66+ n = 0
67+ if self .current == nil
68+ then
69+ self .current = self :pop ()
70+ end
71+ while n < limit and self .current ~= nil
72+ do
73+ if self .current .delay == 0
74+ then
75+ err = self .current :exec ()
76+ if err ~= nil
77+ then
78+ LOG (" queue:update error: " .. err .message )
79+ break
80+ end
81+ n = n + 1
82+ self .current = self :pop ()
83+ else
84+ self .current .delay = self .current .delay - 1
85+ end
9986 end
87+ return n
88+ end
10089
101- return queue
90+ function queue :push (value )
91+ local last = self .last + 1
92+ self .last = last
93+ self [last ] = value
94+ end
95+
96+ function queue :pop ()
97+ local first = self .first
98+ if first > self .last then return nil end
99+ local value = self [first ]
100+ self [first ] = nil -- to allow garbage collection
101+ self .first = first + 1
102+ return value
103+ end
104+
105+ return queue
102106end
103107
104- -- setBlock adds an update in given queue to
108+ -- setBlock adds an update in given queue to
105109-- set a block at x,y,z coordinates
106110function setBlock (queue ,x ,y ,z ,blockID ,meta )
107- queue :newUpdate (UPDATE_SET , x , y , z , blockID , meta )
111+ queue :newUpdate (UPDATE_SET , x , y , z , blockID , meta )
108112end
109113
110- -- setBlock adds an update in given queue to
114+ -- setBlock adds an update in given queue to
111115-- remove a block at x,y,z coordinates
112116function digBlock (queue ,x ,y ,z )
113- queue :newUpdate (UPDATE_DIG , x , y , z )
117+ queue :newUpdate (UPDATE_DIG , x , y , z )
114118end
115119
116- -- setBlock adds an update in given queue to
120+ -- setBlock adds an update in given queue to
117121-- update a sign at x,y,z coordinates with
118122-- 4 lines of text
119123function updateSign (queue ,x ,y ,z ,line1 ,line2 ,line3 ,line4 ,delay )
120- meta = {line1 = line1 ,line2 = line2 ,line3 = line3 ,line4 = line4 }
121- queue :newUpdate (UPDATE_SIGN , x , y , z , nil , meta , delay )
122- end
124+ meta = {line1 = line1 ,line2 = line2 ,line3 = line3 ,line4 = line4 }
125+ queue :newUpdate (UPDATE_SIGN , x , y , z , nil , meta , delay )
126+ end
127+
0 commit comments