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!

P#36604 2017-01-24 15:34 ( Edited 2017-02-16 13:48)

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!

P#36617 2017-01-24 19:16 ( Edited 2017-01-25 00:16)

Thanks, this helps quite a bit!

P#37458 2017-02-14 15:57 ( Edited 2017-02-14 20:57)


This is finally working great, thanks to krajzeg.

P#37488 2017-02-15 10:04 ( Edited 2017-02-15 15:05)

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.


P#37493 2017-02-15 12:58 ( Edited 2017-02-15 17:58)

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


P#37518 2017-02-16 08:48 ( Edited 2017-02-16 13:48)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 08:15:02 | 0.016s | Q:23