Board Thread:Questions and Answers/@comment-24454571-20140517053531/@comment-9531161-20140518220215

First question: the code you posted is missing some stuff I think.

FOR J=0 TO 31 MAP(J,I)=0 IF MID$(MAPLINE$,J,1)=="1" NEXT

I think in my code, line 3 should be:

IF MID$(MAPLINE$,J,1)=="1" THEN MAP(J,I)=1

So you get:

RESTORE @MAP FOR I=0 TO 23 READ MAPLINE$ FOR J=0 TO 31 MAP(J,I)=0 IF MID$(MAPLINE$,J,1)=="1" THEN MAP(J,I)=1 NEXT NEXT

Before I go on, I want to state that the goal of this code is to translate map data which is stored in string form into map data which is stored in a numerical array. Working with structured arrays of numbers is easier than working with a bunch of strings when we're trying to perform collision detection. The map is 32 wide by 24 tall, and the DATA section stores the 24 rows. The outer loop iterates over all the rows, and the inner loop iterates over the columns within that row in order to build the map data from the strings. If you now understand this part, you can skip the next section.

How does this work? First, DATA is stored in parts. These parts can be any type, and are separated by commas (like DATA 1,2,3) or by the word "DATA" (like DATA 1 DATA 2 DATA 3, but each on their own line). Imagine that you have a big long table in a warehouse. Starting at the left side of the table, we continue to place DATA items as we move down the table towards the right side. In my COLITEST program, I placed a number of strings on the belt (something like 24, one for each line), so now they're all lined up on the table in order. But we're in a giant warehouse with lots of tables (memory), so how are we going to find the right table to get this DATA? We label the location of the first item with @MAP. It's like we placed a big neon sign above that first item in the DATA section. When you do RESTORE @MAP, a crane moves along to the neon sign and hovers above the first item. When you do READ MAPLINE$, the crane picks up the first item, puts it in a box labeled "MAPLINE$" (a variable), and moves down the table to the next item. This "crane" is the DATA pointer: RESTORE tells the crane where to start, and READ retrieves data from the table and moves down the line.

OK, let's simplify the MAP example and say we only had these lines:

RESTORE @MAP FOR I=0 TO 23 READ MAPLINE$ NEXT

We have 24 pieces of data, and there are 24 numbers from 0 to 23 (inclusive), so we read all 24 pieces of data from the map. Remember, it's not the individual characters within the strings that are data, but the sections divided by either a DATA call or a comma (in this case, it's a DATA call). However, MAPLINE$ is a single variable, and we're reading 24 items. This means that MAPLINE$ will get overwritten each time, and we lose all the map information except the last line. On top of this, we'd rather work with numbers instead of strings. Let's take a look at an example string which MAPLINE$ might store:

"10010100000100010001000000000101"

In my program, I treat 1 as an obstacle, and 0 as a non-obstacle. Our goal is to translate each of these map lines (rows) from a string into a row of the array. This requires us to extract information from each character in the string (each character in the string represents a row in the map). This is what the inner loop does:

FOR J=0 TO 31 MAP(J,I)=0 IF MID$(MAPLINE$,J,1)=="1" THEN MAP(J,I)=1 NEXT

What we're doing here is we're going through every character in the string we just read (MAPLINE$, which was a single row of the map) and assigning a numerical value to the MAP array based on the string. Here I'm taking a shortcut with this line:

MAP(J,I)=0

I'm saying "Assume all map sections are non-obstacles". Next, I do this:

IF MID$(MAPLINE$,J,1)=="1" THEN MAP(J,I)=1

Here I'm saying "Whoops, my assumption was wrong. The string told me that this particular map area was actually an obstacle, so set the map data accordingly". We do this for every character (column) in every string (row), so we eventually build up the entire map.

As for all this crap:

IF MAP(FLOOR(PLX/8),FLOOR(PLY/8)) THEN PLX=PLXO:PLY=PLYO IF MAP(FLOOR((PLX+5)/8),FLOOR(PLY/8)) THEN PLX=PLXO:PLY=PLYO IF MAP(FLOOR(PLX/8),FLOOR((PLY+5)/8)) THEN PLX=PLXO:PLY=PLYO IF MAP(FLOOR((PLX+5)/8),FLOOR((PLY+5)/8)) THEN PLX=PLXO:PLY=PLYO

What I'm actually doing in this program is I'm preemptively assuming that the place we can go is safe. These checks say "Oh crap, that area wasn't OK to go in (the MAP data isn't 0), so go back to my OLD coordinates (PLXO stands for Player X Old). I do all this before I draw, so the preemptive "phasing into a wall" isn't seen.