Page 1 of 2

Module-making questions

Posted: Mon Mar 11, 2013 2:33 am
by Grey
Stupid questions ahoy!

Here's a bit of code I'm trying to make work:

Code: Select all

newEntity{
   define_as = "TILE1",
   name = "tile1",
   display=' ', back_color={240,120,90},
   add_mos={{image="outlines/outline"..(rng.range(1, 96))..".png"}},
   always_remember = true,
}
I have 96 outline files and I want it to choose one randomly. What I've done above doesn't work. What would be the correct way for it to insert a number from 1 to 96?

Re: Module-making questions

Posted: Mon Mar 11, 2013 4:42 am
by nate
What exactly is happening?

It looks to me like you'll be picking a tile png randomly, but that png you picked randomly is going to be used for every instance of that tile, until you restart the game. That's because your code is run every time that call to NewEntity is called, which is once, at load. So you pick randomly, but you only pick once.

What I might try instead is something like: for i, 96 newEntity{...outline"..i..".png"... }end

That way, you get 96 new entities, each with a unique graphic, rather than a single entity, with one randomly chosen png. I suppose you'd need a different name for each as well, but that's no harder than the filename thingie. When it comes time to draw the tile, or put in on the map or whatever, tell it to pick one kind at random.

Re: Module-making questions

Posted: Mon Mar 11, 2013 8:59 am
by darkgod
As nate said, either you make 96 entities and select them at random later, or you can use a random resolver like that:

Code: Select all

newEntity{
   define_as = "TILE1",
   name = "tile1",
   display=' ', back_color={240,120,90},
   resolvers.generic(function(e)
      e.add_mos={{image="outlines/outline"..(rng.range(1, 96))..".png"}}
   end),
   always_remember = true,
}

Re: Module-making questions

Posted: Mon Mar 11, 2013 6:37 pm
by Grey
Oh, hmm, ahh...

Your code don't work, DarkGod! It just displays empty space.

Re: Module-making questions

Posted: Mon Mar 11, 2013 7:40 pm
by Grey
Actually, it half works, but the background colour isn't showing up :(

Re: Module-making questions

Posted: Wed Mar 13, 2013 8:13 pm
by Grey
Okay, so different question: I'm trying to do a floodfill on a map. Probably badly, especially with my lack of sleep in the middle of the 7DRL week... I've been attempting to use a 2d array to mark co-ords filled or not, but I get an error as soon as I start the floodfill that it's referencing a nil value. Bah...

Here's my code:

Code: Select all

--Look for empty square around edge, floodfill when found or make stairs if not there
function _M:edgeCheck()
	local foundx, foundy = -1, -1
	self.map_array = {}
	
	-- Mark all level unconnected
	for i = 0, self.level.map.w - 1 do
		self.map_array[i] = {}
		for j = 0, self.level.map.h - 1 do
			if self.level.map:checkEntity(i, j, Map.TERRAIN, "level") < 1 then
				self.map_array[i][j] = true
			else self.map_array[i][j] = false
			end
		end
	end
	
	
	for i = 0, self.level.map.w - 1 do
		if self.map_array[i][0] == true then
			foundx = i
			self:floodfill(i,0)
		end
	end
	
	if foundx == -1 then
		for i = 0, self.level.map.w - 1 do
			if self.map_array[i][self.level.map.h-1] == true then
				self:floodfill(i,self.level.map.h-1)
				foundx = i
			end
		end
	end
	
	if foundx == -1 then
		for i = 1, self.level.map.h - 2 do
			if self.map_array[0][i] == true then
				self:floodfill(0,i)
				foundx = 0
			end
		end
	end
	
	if foundx == -1 then
		for i = 1, self.level.map.h - 2 do
			if self.map_array[self.level.map.w - 1][i] == true then
				self:floodfill(self.level.map.w - 1,i)
				foundx = self.level.map.w - 1
			end
		end
	end
	
	--[[if foundx == -1 then
		for i = 1, self.level.map.h - 2 do
			if self.level.map:checkEntity(self.level.map.w - 1, i, Map.TERRAIN, "level") < 1 then
				foundx = self.level.map.w - 1
				foundy = i
			end
		end
	end--]]
	
	if foundx == -1 then
		--Make stairs - add lots of particle effects!  Pretty sounds!
		local g = self.zone:makeEntityByName(self.level, "terrain", "STAIRS")
		if g then self.zone:addEntity(self.level, g, "terrain", math.floor(self.level.map.w/2), math.floor(self.level.map.h/2)) end
		self.stairs_generated = false
		-- Add crazy shader?
	else
		for i = 0, self.level.map.w - 1 do
			for j = 0, self.level.map.h - 1 do
				if self.map_array[i][j] == true then
					local g = self.zone:makeEntityByName(game.level, "terrain", "TILE"..(rng.range(1, 29)))
					if g then
						self.zone:addEntity(game.level, g, "terrain", i, j)
					end
				end
			end
		end
		-- Go through whole map and mark all unconnected squares of level < 1 level = 1
		-- Cool particle effect too
	end

end

function _M:floodfill(x,y)
	if self.map_array[x+1][y] == true then
		self.map_array[x+1][y] = false
		self:floodfill(x+1,y)
	end
	if self.map_array[x+1][y+1] then 
		self.map_array[x+1][y+1] = false
		self:floodfill(x+1,y+1)
	end
	if self.map_array[x+1][y-1] then 
		self.map_array[x+1][y-1] = false
		self:floodfill(x+1,y-1)
	end
	if self.map_array[x][y+1] then 
		self.map_array[x][y+1] = false
		self:floodfill(x,y+1)
	end
	if self.map_array[x-1][y] then 
		self.map_array[x-1][y] = false
		self:floodfill(x-1,y) 
	end
	if self.map_array[x-1][y-1] then 
		self.map_array[x-1][y-1] = false
		self:floodfill(x-1,y-1) 
	end
	if self.map_array[x][y-1] then 
		self.map_array[x][y-1] = false
		self:floodfill(x,y-1) 
	end
	if self.map_array[x-1][y+1] then 
		self.map_array[x-1][y+1] = false
		self:floodfill(x-1,y+1) 
	end	
end
Any advice, DarkGod?

Re: Module-making questions

Posted: Wed Mar 13, 2013 8:47 pm
by nate
You'd probably get faster replies if you included line numbers and the precise error message, which should say which value is nil. It's kind of hard to trace other peeps' code: knowing where to begin helps.

EDIT: But I suspect that lines like

Code: Select all

if self.map_array[x+1][y+1] then 
are causing you problems when applied to tiles on the border. If x+1 doesn't exist, then "if [x+1][y+1]" will throw an error, even though "if [x+1]" alone wouldn't.

Re: Module-making questions

Posted: Wed Mar 13, 2013 8:53 pm
by Grey
Nil on the array on the first line of the floodfill (the second procedure). But I'm guessing DarkGod will say my whole approach is wrong and that I'm doing terrible things to the code.... ;)

Re: Module-making questions

Posted: Wed Mar 13, 2013 8:56 pm
by nate
I edited at the same time as you posted. Check it out, I think that's what's going on.

Re: Module-making questions

Posted: Wed Mar 13, 2013 9:37 pm
by Grey
Aha! Did a quick initialisation tweak and it seems you're quite right. Needs a bit more tweaking to get perfect, but it's mostly working as intended :D

Re: Module-making questions

Posted: Fri Mar 15, 2013 12:02 am
by Grey
Okay okay, another dumb question:

There's a function I've nicked which goes along the lines of util.findFreeGrid(a, b, 1, true, {[Map.ACTOR]=true})

I want it to also find a square that is of a certain terrain type - how do I do that? I'm not sure what all the variables in the brackets actually mean except for a and b and possibly 1.

Re: Module-making questions

Posted: Fri Mar 15, 2013 12:47 am
by darkgod
This wont work for you.
What you want is a project that checks whatever you wish; something like:

Code: Select all


local grids = {}
actor:project({type="ball", radius=3}, x, y, function(px, py)
  local g = game.level.map(px, py, Map.TERRAIN)
  local a = game.level.map(px, py, Map.ACTOR)
  if g and g.define_as == "WHATEVER" and not a then grids[#grids+1] = {x=px, y=py} end
end)
if #grids > 0 then
  local spot = rng.table(grids)
  .... here you can use spot.x and spot.y ....
end


Re: Module-making questions

Posted: Fri Mar 15, 2013 4:04 am
by Grey
Ah! Lovely, a bit of jiggling and I got that into what I wanted. I really must learn to use that lists thing more often... If you're curious, this is the current state of the game:

http://gamesofgrey.com/games/mosaic-mod.zip

Not sure how obvious the gameplay is yet ;)

More questions to come! The biggest of which is likely around how to make multiple sound files play simultaneously...

Re: Module-making questions

Posted: Fri Mar 15, 2013 9:01 am
by darkgod
Looks really neat!
But I dont see the challenge though, it seems to me the best way is always to simply circle the outer border as fast as you can, and if it gets damaged (rarely) to simply repair it

Re: Module-making questions

Posted: Fri Mar 15, 2013 2:00 pm
by Grey
Only the two easiest enemies are in at present - there will be 8 progressively harder enemies to add. Actually the version I posted up is way easier than my initial design for these enemies. When I realised that even I was struggling to beat levels I thought I should make things a bit easier, so I halved their speed and reduced their spawn rate by 90%... When I add some player abilities I can ramp up their difficulty again.

On the subject of player abilities, I have a move sequence system that isn't working quite as I want. It tracks each move the player makes and if they make the right 4-sequence move then it activates a special ability. The way this is working is storing the moves in an array {1,2,3,4}. But when it comes to comparing that array against the preset special ability patterns I don't quite see an easy way to do it. Simply "if sequence == {2,2,2,2}" doesn't work. The only way I can find that does work is "if sequence[1] == 2 and sequence[2] == 2 and sequence[3] == 2 and sequence[4] == 2". This is hugely cumbersome to code! I'd have to do about 20 such checks against the sequence patterns to cater for all directions of sequences etc. Is there a shortcut I don't know about?