Log In  


Cart #36603 | 2017-01-24 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

I'm trying to place 32 yellow sprites in any open (green) areas. This code is very close, except that it will occasionally place yellow sprites just 1 map tile outside of the green areas. Any ideas how to troubleshoot this one?

Thanks!



Well, two things. First I'll try to explain why it doesn't work properly:

del(...,v) removes the first element from the table that is the same as v. This means that del(spritesx, ex) and del(spritesy, ey) do not remove the index you want, necessarily

Imagine:
spritesx = {1,1,2,2,3,3}
spritesy = {1,2,3,1,2,3}

Now if r=6, you'd like to remove the last element from both arrays, but you won't - the first 3 from both will be removed.

===

Ok, now - your problem is actually putting stuff on the map in green spaces. This can be solved in a simpler manner:

local items_left=32
while items_left > 0 do
 local x = flr(rnd(16))
 local y = flr(rnd(16)) -- get a set of random coordinates
 if mget(x,y) == 1 then -- is this grass?
  mset(x,y,16) -- yes, put an item down!
  items_left -= 1
 end
end

Every time we succesfully find an empty space, we can put down an item and decrease a counter, simple as that :).

Hope that helps!


Thanks, this helps quite a bit!



This is finally working great, thanks to krajzeg.


So to take this to the next level, I am trying to find and fill a set number of open spaces on each rendered map screen. It isn't working, and I can't quite figure out why. It seems to not be detecting the map tiles correctly, but all the debugging I've done seems to indicate that I'm looking in the right places. Please excuse the single letter variable naming, I was trying to avoid naming conflicts.



I just had to add the map offset after getting each set of random coordinates.




[Please log in to post a comment]