"redistribute"-type flotilla: update
This commit is contained in:
parent
928e7f7418
commit
fa206c37c5
@ -1570,7 +1570,7 @@ function flotilla:load(ulc_cx, ulc_cy, lvl)
|
|||||||
-- and we allow alternates
|
-- and we allow alternates
|
||||||
-- for this type of ship
|
-- for this type of ship
|
||||||
ship_t+=(uv>>ship_t&0x1)&(f&0x10>>4)
|
ship_t+=(uv>>ship_t&0x1)&(f&0x10>>4)
|
||||||
add(row, self.ship_bases[ship_t]:new{column=cx-ulc_cx})
|
add(row, self.ship_bases[ship_t]:new{col=cx-ulc_cx})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
until mode==1
|
until mode==1
|
||||||
@ -1602,7 +1602,59 @@ function flotilla:statisfy(counts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function flotilla:update()
|
function flotilla:update()
|
||||||
-- TODO: assign new want_x, want_y to everyone who isn't dead
|
-- algorithm: redistribute
|
||||||
|
-- TODO: alternate flotilla movement algorithms
|
||||||
|
|
||||||
|
-- find effective width and height
|
||||||
|
local min_col,max_col,live_rows=999,0,0
|
||||||
|
for row in all(self.rows) do
|
||||||
|
local row_alive=false
|
||||||
|
for ship in all(row) do
|
||||||
|
if not ship.dead then
|
||||||
|
row_alive=true
|
||||||
|
local col=ship.col
|
||||||
|
if (col < min_col) min_col = col
|
||||||
|
if (col > max_col) max_col = col
|
||||||
|
end
|
||||||
|
end -- scanned row
|
||||||
|
if (row_alive) live_rows += 1
|
||||||
|
end -- extent search
|
||||||
|
|
||||||
|
if (live_rows == 0) return true -- done
|
||||||
|
|
||||||
|
-- distribute across box:
|
||||||
|
-- x = [4, 100)
|
||||||
|
-- y = [4, 80)
|
||||||
|
|
||||||
|
local x_interval,x_offset = 0,52
|
||||||
|
if min_col < max_col then
|
||||||
|
x_interval=96/(max_col-min_col)
|
||||||
|
x_offset = 4-min_col*x_interval
|
||||||
|
end
|
||||||
|
|
||||||
|
local y_interval,y_offset=0,40
|
||||||
|
if live_rows > 1 then
|
||||||
|
y_interval=76/(live_rows-1)
|
||||||
|
x_offset=4-y_interval
|
||||||
|
end
|
||||||
|
|
||||||
|
-- now assign target locations
|
||||||
|
local real_row=0
|
||||||
|
for row in all(self.rows) do
|
||||||
|
local row_alive = false
|
||||||
|
for ship in all(row) do
|
||||||
|
if not ship.dead then
|
||||||
|
if not row_alive then
|
||||||
|
real_row += 1
|
||||||
|
row_alive = true
|
||||||
|
end
|
||||||
|
ship.want_x = ship.col * x_interval - x_offset
|
||||||
|
ship.want_y = real_row * y_interval - y_offset
|
||||||
|
end -- ship updated
|
||||||
|
end -- row updated
|
||||||
|
end -- table updated
|
||||||
|
|
||||||
|
return false -- some ships remain
|
||||||
end
|
end
|
||||||
|
|
||||||
-->8
|
-->8
|
||||||
|
Loading…
x
Reference in New Issue
Block a user