trying to make an ascii map editor...

Started by MisterN, October 15, 2011, 09:09:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MisterN

..that is if ascii is the correct word, if it isnt please correct me :3. i did it once on game maker and a couple times in flash (with assistance). I would have the game load up a .txt file containing code like this:

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0


each figure would represent some item on a 16x16 - 320x240 screen. 0 (or #) would be null, 1 would be the player, A would be an enemy, etc.

I think it would make a great tutorial for people who want to start coding for bennu, so if someone would like to help me get this to work they would also be contributing a good peice of code to the community :3. thanks
werg

gecko

You could try using FGETS (http://wiki.bennugd.org/index.php?title=Fgets) to read each line of the txt, and then saving each value into an array (something like array[line][column]).

And with the data in the array, then you can do whatever you want/need...
Torres Baldi Studio
http://torresbaldi.com

MisterN

ill try that but could you make an example :3?
werg

gecko

#3
I've made a basic example, and tried to comment it.

It reads a file, and split every line to get the values.

You can start from here and expand it to what you need.

EDIT: i add the code here to make it clear for everyone.


import "mod_string";
import "mod_file";
import "mod_say";

GLOBAL

int handle, position;

string line, value;

END

BEGIN

// load the file
handle = fopen("map.txt", O_READ);

// reads every line from the tet file
line = fgets(handle);
WHILE ( line <> "" )

// shows the current line
say(line);

// divide the line to obtain all the values
// (search for an "," and saves its position)
position = find(line, ",");
WHILE ( position <> -1 )

// loads the needed value here
value = substr(line, 0, position);

//and the rest of the line here
line = substr(line, position+2, 50);

//say("rest of line: " + line);
say("value: " + value);

// updates the position of the next "," in the line
position = find(line, ",");
END

// reads the next line
line = fgets(handle);
END

fclose(handle);

END
Torres Baldi Studio
http://torresbaldi.com

handsource-dyko

You can use the split() function to parse lines. Each line has 20 values with a comma a delimiter.
With the split function you can put these in a array. http://wiki.bennugd.org/index.php?title=Split

This is much easier then using a combination of find and substring. With the split function, it doesn't matter how many
characters a value has. So it would work fine on things like this: 100,0,2,10,3,500,0,0,0,0,0,0,100 etc.
With substr and find, that would be difficult since you need to manually count the length of the characters between the comma's.

Split is also great for reading spreadsheets in csv format.

MisterN

cool it works but it doesnt do anything at the moment. i made a process called block() every time the program reads # from the text menu. do you think you could supply some code that reads each value as a 16x16 place and spawns something?
werg

hardyx

#6
You can create the text file with fixed format, the easy way is use a char for each element and not use separators nor spaces. This is easiest to read in Bennu, because each character goes to a position in a matrix. For example, you can use X for walls, - for empty squares, P for player start, E for enemies start, and * for bonus items.

XXXXXXXXXXXXXXXXXXXXX
X------E------------X
X-*------*-------P--X
X---------------*---X
X-------------------X
X---E-----*---------X
X---------------E---X
XXXXXXXXXXXXXXXXXXXXX

MisterN

but how would i do that? i tried something like this

# = block(x,y);
P = player(x,y);

and got some errors lol
werg

handsource-dyko

With fgets() you read one line. If you stick to a fixed map size, you know how many characters there should be in a line.
If you want to use variable map sizes, you have to count the amount of characters per line. The len() functions will povide this data
for you. Then, you can create a FOR loop with a switch mechanism.

(this is pseudo code)



for lines=0; lines < end of file; lines+=1

     for character=0; character < len(fgets(file)); character+=1;

          switch (character)


                case a:
                      player ((lines*10), (character*10));
                end

                case b:
                      enemy ((lines*10), (character*10));
                end

                case c:
                     block ((lines*10), (character*10));
                end

                etc etc etc
          end

    end

end



Note: lines and chatacter are indexes, so they do map directly to screen coordinates. I recommend to multiplied them
with a factor of 10. This is complete dependant on the screen's resolution a the granlarity of the map.

MisterN

heres what I got, I get an error at line 45: "<" expected <"LINES">
werg

josebita

Quote from: DoctorN on October 18, 2011, 11:32:28 PM
heres what I got, I get an error at line 45: "<" expected <"LINES">
What handsource gave you is pseudo-code. You have to translate it into real code before trying to compile it.

MisterN

unknown identifier: case


GLOBAL

int handle, position;

string line, value;

END
BEGIN
set_title("Map Editor");
    Set_mode(320,240,16);
    Set_fps(20,0);
// load the file
handle = fopen("map.txt", O_READ);

// reads every line from the tet file
line = fgets(handle);
WHILE ( line <> "" )
 
  // shows the current line
  say(line);
 
  // divide the line to obtain all the values
  // (search for an "," and saves its position)
  position = find(line, ",");
  WHILE ( position <> -1 )
   
   // loads the needed value here
   value = substr(line, 0, position);
   
   //and the rest of the line here
   line = substr(line, position+2, 50);
   
   //say("rest of line: " + line);
   say("value: " + value);
   
   // updates the position of the next "," in the line
   position = find(line, ",");
   
   case #:
    block ((lines*16), (position*16));
   end
   case P:
    player ((lines*16), (position*16));
   end
   
  END

  // reads the next line
  line = fgets(handle);
end
END

fclose(handle);

loop
  if(key(_esc))
   Exit("Bye",0);
  end
frame;
end
END
PROCESS block(x,y);
begin
graph = load_png(".\MATERIAL\block.png");

loop
frame;
end
end
PROCESS player();
begin
graph = load_png(".\MATERIAL\player.png");

loop
frame;
end
end
werg

josebita

You probably have to quote the letters: "#", "P" and so on.
[Edit] And you need a switch statement before the "case" ones.
http://wiki.bennugd.org/index.php?title=Switch

MisterN

thanks, now I have this error: data types not compatible with operation <16>

GLOBAL

int handle, position;

string line, value;

END
BEGIN
set_title("Map Editor");
    Set_mode(320,240,16);
    Set_fps(20,0);
// load the file
handle = fopen("map.txt", O_READ);

// reads every line from the tet file
line = fgets(handle);
WHILE ( line <> "" )
 
  // shows the current line
  say(line);
 
  // divide the line to obtain all the values
  // (search for an "," and saves its position)
  position = find(line, ",");
  WHILE ( position <> -1 )
   
   // loads the needed value here
   value = substr(line, 0, position);
   
   //and the rest of the line here
   line = substr(line, position+2, 50);
   
   //say("rest of line: " + line);
   say("value: " + value);
   
   // updates the position of the next "," in the line
   position = find(line, ",");
 
  switch(value)
   case "#":
    block ((line*16), (position*16));
   end
   case "P":
    player ((line*16), (position*16));
   end
  end
   
  END

  // reads the next line
  line = fgets(handle);
end
END

fclose(handle);

loop
  if(key(_esc))
   Exit("Bye",0);
  end
frame;
end
END
PROCESS block(x,y);
begin
graph = load_png(".\MATERIAL\block.png");

loop
frame;
end
end
PROCESS player();
begin
graph = load_png(".\MATERIAL\player.png");

loop
frame;
end
end

werg

SplinterGU

line is string... you can't multiple it by 16... multiple operations only are valid with data numbers.
Download Lastest BennuGD Release: http://www.bennugd.org/node/2