Bennu Game Development

English Forums => Helpdesk => Topic started by: MisterN on October 15, 2011, 09:09:33 PM

Title: trying to make an ascii map editor...
Post by: MisterN on October 15, 2011, 09:09:33 PM
..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
Title: Re: trying to make an ascii map editor...
Post by: gecko on October 15, 2011, 09:30:59 PM
You could try using FGETS (http://wiki.bennugd.org/index.php?title=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...
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 15, 2011, 09:43:06 PM
ill try that but could you make an example :3?
Title: Re: trying to make an ascii map editor...
Post by: gecko on October 15, 2011, 11:08:27 PM
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
Title: Re: trying to make an ascii map editor...
Post by: handsource-dyko on October 16, 2011, 08:47:57 AM
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 (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.
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 16, 2011, 04:37:47 PM
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?
Title: Re: trying to make an ascii map editor...
Post by: hardyx on October 17, 2011, 07:45:45 AM
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
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 18, 2011, 02:05:40 AM
but how would i do that? i tried something like this

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

and got some errors lol
Title: Re: trying to make an ascii map editor...
Post by: handsource-dyko on October 18, 2011, 02:34:38 PM
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.
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 18, 2011, 11:32:28 PM
heres what I got, I get an error at line 45: "<" expected <"LINES">
Title: Re: trying to make an ascii map editor...
Post by: josebita on October 18, 2011, 11:39:51 PM
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.
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 19, 2011, 03:24:11 AM
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
Title: Re: trying to make an ascii map editor...
Post by: josebita on October 19, 2011, 01:10:03 PM
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
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 19, 2011, 10:18:06 PM
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

Title: Re: trying to make an ascii map editor...
Post by: SplinterGU on October 19, 2011, 10:29:38 PM
line is string... you can't multiple it by 16... multiple operations only are valid with data numbers.
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 20, 2011, 12:27:02 AM
so do you know what i should do?
Title: Re: trying to make an ascii map editor...
Post by: handsource-dyko on October 20, 2011, 07:08:31 AM
You should use the "line" variable as a counter (nummerical type) as use a differently named, string type variable on fgets.

Maybe you should call the counters, line_count and character_count, to avoid confusion.
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 22, 2011, 04:44:28 AM
how might i do that?
Title: Re: trying to make an ascii map editor...
Post by: MisterN on October 26, 2011, 08:12:20 PM
Hello? Is this site dead? How might I complete this?