removing and re-inserting table elements in my game

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

removing and re-inserting table elements in my game

Post by Mr. Strange »

a few tables with (semi) redundant information.

Alright, this is a stright-up lua question.

I need to take the following steps in my game:

1 - select four random empty cells from a grid of horizontal magnitude hSize and vertical magnitude vSize.
2 - pass those four cells to another function, which might take a long time to process them
3 - select one of the four elements, and mark it filled. (We might also "fill" other elements at this time)
4 - repeat.

My current (working) implementation uses three tables -
* "grid", which stores the data for each cell of the grid
* "sorter", which is a numerically-indexed list of size hSize*vSize, and stores a list of paired grid coordinates in each element
* "currentSelections", which is a numerically-indexed list of size four, and stores the four coordinate pairs for the currently selected cells.

My script looks like this:

Code: Select all

grid = {}
sorter = {}
curList = {}

for i=1,4 do -- initialize currentList
	curList[i] = {}
	curList[i].x = 0
	curList[i].y = 0
end

for x = 1,hSize do -- initialize other tables
	grid[x] = {}
	for y = 1, vSize do
		grid[x][y] = {}
		--data for each cell goes in here, but is not relevant now
		sortNumber = x + hSize*(y-1)
		sorter[sortNumber] = {}
		sorter[sortNumber].xC = x
		sorter[sortNumber].yC = y
	end
end

function PickFour()
	local i
	for i = 1, 4 do
		index = math.random(1,table.maxn(sorter))
		selectedX = sorter[index].xC
		selectedY = sorter[index].yC
		curList[i].x = selectedX
		curList[i].y = selectedY
		table.remove(sorter, index)
	end
end

function ReturnThree(pick) -- pick is a value from 1 to 4
	for i = 1,4 do
		if(i ~= pick) then
			place = table.maxn(sorter) + 1
			sorter[place] = {}
			sorter[place].xC = curList[i].x
			sorter[place].yC = curList[i].y
		end
	end
end
Does anything especially inefficient jump out? I guess my major concern is whether calling table.remove is a good idea or not, since I then need to jump through so many hoops to re-initialize three of those four elements. On the other hand, calling table.remove does help me to reduce the value of table.maxn, which I use to prevent selecting the same cell twice.

It's 3:30am, and I'm not satisfied with this bit of script. I should probably get to sleep though. Maybe someone will have an observation for me in the morning.

--Mr. Strange
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: removing and re-inserting table elements in my game

Post by rude »

I'm having a little bit of trouble understanding exactly what you're trying to do. Is the order of the items in the lists relevant? If not, then you can avoid using table.remove and table.maxn altogether.
Mr. Strange
Party member
Posts: 101
Joined: Mon Aug 11, 2008 5:19 am

Re: removing and re-inserting table elements in my game

Post by Mr. Strange »

The order is not relevant - it's an area-control game. The routine acts like this:

1 - begin with an empty board.
2 - player one is shown four randomly selected tiles.
3 - player one selects one of the random tiles, and occupies the space. Player one might delay this decision for quite a while, during which other time-based effects are continuing.
4 - the other three tiles are unrevealed.

5 - next player's turn - show them four randomly selected tiles.

The point is that the total pool of legal tiles for random selection decreases over time. So I unspool the initial list of tiles into my sort table, and then remove and re-add elements to it as they become valid and invalid. Since the sort table is there to pull random locations from, the order of the locations within the table does not matter.

The question is a bit academic now, since I've expanded the script to the point where changing this method would probably be a poor idea.

--Mr. Strange
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests