PDA

View Full Version : FPC RPG style map won't even startup!



Robert Kosek
24-10-2004, 04:34 PM
Hello, I'm new here and thanks for the help (in advance).

I was trying to get a simple 80x19 map to display in console mode and am getting runtime errors. I don't have the GNU debugger, nor do I know where to get it.

Here's the app:

{
COLORS:
White,LightGray,DarkGray,Black
Brown
Yellow
Magenta,LightMagenta
Green,LightGreen
Red,LightRed
Blue,LightBlue
Cyan,LightCyan
}
program rpgmap;

uses crt,dos;

type
map_tile = record
tile: char;
color: integer;
move_cost: integer;
end;

function rolldie(size,addto: integer): integer;
begin
rolldie := (1+random(size))+addto;
end;

function rolldice(size,count,addto: integer): integer;
var i: integer;
begin
rolldice := addto;
for i := 1 to count do
rolldice := rolldice+(random(size)+1);
end;

const tiles: array[1..5] of map_tile = (
( tile: '.'; color: Green; move_cost: 250; ),
( tile: '*'; color: Brown; move_cost: 750; ),
( tile: 'v'; color: Green; move_cost: 500; ),
( tile: 'T'; color: Green; move_cost: 250; ),
( tile: 'f'; color: Brown; move_cost: 250; ));

desc: array[1..5] of string = ('You walk through the grass.',
'You slog through the mud.',
'You wander through the tall grass.',
'You pass beneath a green tree.',
'You walk under the dead tree.');
move_speed = 250;

procedure drawtile(n: byte; x,y: byte);
begin
gotoXY(X,Y);
TextColor(tiles[n].color);
write(tiles[n].tile);
end;

var map: array[1..80,1..19] of byte;
c,r: integer;
turn: integer = 0;
quit: boolean;
key: char;
x,y: byte;

begin
for r := 1 to 19 do
for c := 1 to 80 do
case rolldie(100,0) of
1..40: map[c,r] := 1;
41..50: map[c,r] := 2;
51..70: map[c,r] := 3;
71..100: if rolldie(100,0) >= 33 then map[c,r] := 4 else map[c,r] := 5;
default: map[c,r] := 1;
end;
repeat
key := ReadKey;
if key = 'Q' then quit := true;
turn := turn + 1;
gotoxy(1,20);
write(desc[map[x,y]]);
gotoxy(75,20);
write(turn);
until quit = true;
end.


What did I do wrong? I know I'm not a good programmer, but I'm trying.

Robert

Useless Hacker
25-10-2004, 02:21 PM
Hi, I can't see anything obviously wrong with your program, perhaps you could post the error message(s) you are getting?

Robert Kosek
25-10-2004, 02:57 PM
Sadly I don't recieve any. It simply crashes w/o one. See why it's annoying me? :?

EDIT, I have a theory but can't test it for a little bit. It has to do with the vars X,Y = 0 and being less than the Low(desc).

EDIT2, Now I fixed the bug ... Didn't set the x and y vars like I thought so... I've got a very baseline rpg which draws a map.

Anonymous
31-12-2004, 12:39 PM
Sadly I don't recieve any. It simply crashes w/o one. See why it's annoying me? :?

EDIT, I have a theory but can't test it for a little bit. It has to do with the vars X,Y = 0 and being less than the Low(desc).

EDIT2, Now I fixed the bug ... Didn't set the x and y vars like I thought so... I've got a very baseline rpg which draws a map.


A "default" clause in a case statement doesn't exist. This wouldn't even compile]

EmbranceII
01-01-2005, 05:12 PM
So could you post the full working code?
I would be interested on seeing how it worked.

Robert Kosek
27-01-2005, 10:33 PM
I'm sorry but I missed the posts here. I lost the application in a reformat, but I'll see what I can do.

Robert Kosek
27-01-2005, 11:40 PM
Just for the record, I wasn't ignoring anyone ... I simply missed the responses ... sorry.

Ok, here it is. It was no problem, and was good to return to ... I was having a break from FPC for a little. The map is 80x19 and generated randomly. I added comments so folks will understand what's going on a bit better.

http://img139.exs.cx/img139/1966/rpgmap3rt.gif

Free for any use, just mention me in the credits please.

(*
* COLORS:
* White,LightGray,DarkGray,Black
* Brown
* Yellow
* Magenta,LightMagenta
* Green,LightGreen
* Red,LightRed
* Blue,LightBlue
* Cyan,LightCyan
*)
program rpgmap;

uses crt,dos;

type
map_tile = record
tile: char; // The ASCII representation
color: integer; // The Color, just an int. See top for list
move_cost: byte; // The number of turns to move through
end;
// Numpad key:
// "1" "2" "3" "4" "5" "6" "7" "8" "9"
direction = (SouthWest,South,SouthEast,West,none,East,NorthWes t,North,NorthEast);

// Roll a die of *size* sides and add the result to *addto*. IE, 1d6+4
function rolldie(size,addto: integer): integer;
begin
rolldie := (1+random(size))+addto;
end;

// Roll dice of count *count* and *size* sides and add the result to *addto*.
// IE, 1d6+4
function rolldice(size,count,addto: integer): integer;
var i: integer;
begin
rolldice := addto;
for i := 1 to count do
rolldice := rolldice+(random(size)+1);
end;

const tiles: array[1..5] of map_tile = (
( tile: '.'; color: Green; move_cost: 1; ), //Grass, easy to move through
( tile: '*'; color: Brown; move_cost: 4; ), //Mud, nasty stuff! Grips the boots!
( tile: 'v'; color: Green; move_cost: 2; ), //Tall grass, hard to see where you're going
( tile: 'T'; color: Green; move_cost: 1; ), //A tall tree, just for astetics
( tile: 'f'; color: Brown; move_cost: 1; ));//A dead tree, just for astetics

//Descriptions for moving through the terrain.
desc: array[1..5] of string = ('You walk through the grass.',
'You slog through the mud.',
'You wander through the tall grass.',
'You pass beneath a green tree.',
'You walk under the dead tree.');
move_speed = 1; // I cannot remember WHY I have this... Maybe you would?

// A simple procedure to draw the tile
procedure drawtile(n: byte; x,y: byte);
begin
gotoXY(X,Y);
TextColor(tiles[n].color);
write(tiles[n].tile);
end;

// Declare the vars
var map: array[1..80,1..19] of byte; // Refrences the type of the terrain.
c,r: integer; // temp value for Column and Row, used in map generation
turn: integer; // Current turn, reference mostly
quit: boolean; // If the player has hit "Q" then this is triggered. Quits.
key: char; // Key that was hit is stored here.
x,y: byte; // Player's current map position

(************************************************* **************************************
* This is the toughest function to write in the entire game! This controls movement,
* and even prevents the player from exiting the map area.
*
* This function also draws the maptile where the player is and draws the player.
************************************************** *************************************)
procedure moveplayer(dir: direction; var xvar,yvar: byte);
var xmove,ymove: boolean;
begin
xmove := true; // Assume that we can move. We've no data so this is safest.
ymove := true;
if dir <> none then begin // If we're going to move, IE direction hit and not 5 &#40;stationary&#41;
drawtile&#40;map&#91;xvar,yvar&#93;,xvar,yvar&#41;; // Hide the player! He is no longer here and shouldn't be shown
&#40;************************************************* ******************************
* This section of code is essential! It determines if the player can move
* on the x and y axis. If he can't then it is flagged for later.
************************************************** *****************************&#41;
if dir in &#91;West,NorthWest,SouthWest&#93; then
if xvar-1 < 1 then xmove &#58;= false;
if dir in &#91;East,NorthEast,SouthEast&#93; then
if xvar+1 > 80 then xmove &#58;= false;
if dir in &#91;North,NorthWest,NorthEast&#93; then
if yvar-1 < 1 then ymove &#58;= false;
if dir in &#91;South,SouthWest,SouthEast&#93; then
if yvar+1 > 19 then ymove &#58;= false;
if &#40;xmove&#41; then // if movement is allowed on the X axis then do it based on DIR
case dir of
NorthWest,West,SouthWest&#58; xvar &#58;= xvar-1;
NorthEast,East,SouthEast&#58; xvar &#58;= xvar+1;
end;
if &#40;ymove&#41; then // if movement is allowed on the Y axis then do it based on DIR
case dir of
NorthWest,North,NorthEast&#58; yvar &#58;= yvar-1;
SouthWest,South,SouthEast&#58; yvar &#58;= yvar+1;
end;
end;
TextColor&#40;white&#41;; // The Player is the WHITE "@" symbol on the screen.
gotoXY&#40;xvar,yvar&#41;; // go to the new spot
write&#40;'@'&#41;; // and draw the player there
end;

begin
turn &#58;= 0; // Since we just began the player has just become "consious"
x &#58;= rolldie&#40;80,0&#41;; // Set X & Y to be on the map. Somewhere, though ;&#41;
y &#58;= rolldie&#40;19,0&#41;;
for r &#58;= 1 to 19 do
for c &#58;= 1 to 80 do begin
case rolldie&#40;100,0&#41; of
1..70&#58; map&#91;c,r&#93; &#58;= 1;
71..85&#58; map&#91;c,r&#93; &#58;= 2;
86..90&#58; map&#91;c,r&#93; &#58;= 3; // mud is kinda prevelent
91..100&#58; if rolldie&#40;100,0&#41; >= 15 then map&#91;c,r&#93; &#58;= 4 else map&#91;c,r&#93; &#58;= 5; // Trees are mainly alive here
else map&#91;c,r&#93; &#58;= 1;
end;
drawtile&#40;map&#91;c,r&#93;,c,r&#41;; // Draw the current tile.
end;
moveplayer&#40;none,x,y&#41;; //Draw the player so he's visible, though don't move him!
repeat
key &#58;= ReadKey;
case key of
'Q'&#58; quit &#58;= true;
'1'&#58; MovePlayer&#40;SouthWest, x, y&#41;; // SW
'2'&#58; MovePlayer&#40;South, x, y&#41;; // S
'3'&#58; MovePlayer&#40;SouthEast, x, y&#41;; // SE
'4'&#58; MovePlayer&#40;West, x, y&#41;; // W
'5'&#58; MovePlayer&#40;none, x, y&#41;; // NO MOVEMENT
'6'&#58; MovePlayer&#40;East, x, y&#41;; // E
'7'&#58; MovePlayer&#40;NorthWest, x, y&#41;; // NW
'8'&#58; MovePlayer&#40;North, x, y&#41;; // N
'9'&#58; MovePlayer&#40;NorthEast, x, y&#41;; // NE
end;
turn &#58;= turn + tiles&#91;map&#91;x,y&#93;&#93;.move_cost; // increment time by transit cost
gotoxy&#40;1,20&#41;; // go to the status line
clreol; // Clear previous message
write&#40;desc&#91;map&#91;x,y&#93;&#93;&#41;;
gotoxy&#40;75,20&#41;;// Go further right
write&#40;turn&#41;; // and draw the turn number
until quit = true;
end.