Combining 2D Arrays?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Davidobot wrote:
Ranguna259 wrote:
Davidobot wrote:Thank you! :D
Works perfectly! Infinite worlds, here I come! :joker:
infinite :ultrashocked: well.. don't forget to optimize it so it will discard maps as you move forward or backward and reload them once you get close to them again because if you don't there'll be a massive impact on the performance ;)
Now that is another problem, I will now need a remove function?! :shock:
Hmmm.. let's both think about that, it shouldn't be that hard if each map block has a fixed width and height, say 14 width 13 height as you have in your exemple ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Combining 2D Arrays?

Post by Davidobot »

Lets just say it's 60x100. So removing shouldn't be that much of a problem, but that means after removing you will need to save it somewhere, I already have a table serializer.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Davidobot wrote:Lets just say it's 60x100. So removing shouldn't be that much of a problem, but that means after removing you will need to save it somewhere, I already have a table serializer.
Actualy it wont be easy, removing vars from a table is actually a delicate matter but luckly I've already done something like this, I just gotta search for my code, I'm going to have dinner now so I'll post back in an hour or so.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Oh great :| I can't code anything unless I know how you manage you tables and player coordinates so take a look at the code in this page which is this one:

Code: Select all

local input = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p' }
local remove = { f=true, g=true, j=true, n=true, o=true, p=true }

local n=#input

for i=1,n do
        if remove[input[i]] then
                input[i]=nil
        end
end

local j=0
for i=1,n do
        if input[i]~=nil then
                j=j+1
                input[j]=input[i]
        end
end
for i=j+1,n do
        input[i]=nil
end
And try to make it so that it removes a mapblock from the mainmap when the player moves forward ir backward to a new mapblock. Here's a a representation:
Attachments
infmap.png
infmap.png (10.83 KiB) Viewed 1837 times
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Combining 2D Arrays?

Post by Davidobot »

Here is the .love file I am working on. The map combining is located under states/gameState.lua at the very bottom. BTW the map can get very big.
Attachments
AoB.love
Development Build
(237.71 KiB) Downloaded 54 times
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Ok I'm gonna look into it, but basicly what you want is something like a random generated map right ? like minecraft or terraria ?
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Combining 2D Arrays?

Post by Ranguna259 »

Here ya go, now all you need to do is to implement this to your code:

Code: Select all

print('back remover')
mapa = {
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
       }
mapb = {
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
       }


for i=1,#mapb do
	 for ii=1, #mapb[i] do
		 table.insert(mapa[i],mapb[i][ii])
	 end
end
print('two maps')
for i=1, #mapa do
	 print(unpack(mapa[i]))
end

--back remover
local remove = 14 --mapblock width in this exemple
local n = #mapa --length of the table
local nn = #mapa[1] --length of the table

for i=1,#mapa do
	 for ii=1,remove do
		 mapa[i][ii]=nil
	 end
end
print('\none map with nil garbedge')
for i=1, #mapa do
	 print(unpack(mapa[i]))
end
local j=0
local jj=0
for i=1,n do
	 j=j+1
	 for ii=15,nn do
         jj=jj+1
         mapa[j][jj]=mapa[i][ii]
	 end
	 jj=0
end
print('\none map with number garbedge')
for i=1, #mapa do
	 print(unpack(mapa[i]))
end
for i=1,n do
	 for ii=nn-13,nn do
		 mapa[i][ii]=nil
	 end
end
print('\none map')
for i=1, #mapa do
	 print(unpack(mapa[i]))
end

print('\nfront remover')
mapa = {
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
       }
mapb = {
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
           { 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
           { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
           { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
           { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
       }


for i=1,#mapb do
	 for ii=1, #mapb[i] do
		 table.insert(mapa[i],mapb[i][ii])
	 end
end
print('two maps')
for i=1, #mapa do
	 print(unpack(mapa[i]))
end

--front remover
local remove = #mapa[1] --end of mainmap in this exemple
local n = #mapa --length of the table
local nn = #mapa[1] --length of the table

for i=1,#mapa do
	 for ii=15,remove do --15=mapblock width + 1 (width of the mapblocks that are before the last one)
		 mapa[i][ii]=nil
	 end
end
print('\none map')
for i=1, #mapa do
	 print(unpack(mapa[i]))
end
EDIT: When the code discards mapblocks make it save them in a file they can be restored when the player approaches them again, or not if you want the map to be totally random generated even if the player has already past throught a mapblock.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], Semrush [Bot] and 72 guests