Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ok, I'm lost

Status
Not open for further replies.

rjr9999

Programmer
Apr 7, 2001
183
0
0
US
Ok, I'm about to post a big honky piece of code in here. This thing has boggled me for 3 days now. The idea is to read from a Map file (100X100) and print out a 12X32 map each time "Printmap()" is called....here it is.

#include "conf.h"
#include "sysdep.h"

#include "structs.h"
#include "utils.h"
#include "comm.h"
#include "interpreter.h"
#include "handler.h"
#include "db.h"
#include "spells.h"
#include "screen.h"
#include "maputils.h"

extern struct room_data *world;

char mainmap[99][99];

char mapchar;

void printmap(int rnum, struct char_data * ch) {
int x=0;
int y=0;
int tx = 0;
int ty = 0;
int xcord = 0;
int ycord = 0;
FILE *fopen(), *mfile;
int letter;
int loop;
char buf[512];
char colorof[50];

ty = (rnum / 100);
tx = (rnum % 100);

xcord = tx;
ycord = ty;

ty = (ty - 6);
tx = (tx - 17);

if(ty < 0)
{
ty = (ty + 100);
}
if(ty > 99)
{
ty = (ty - 100);
}
if(tx < 0)
{
tx = (tx + 100);
}
if(tx > 99)
{
tx = (tx - 100);
}

strcpy(buf, &quot;\r\n &quot;);

ty = 1;
tx = 1;

for (y = (ycord - 6); y <= (ycord + 5); y++) {
for (x = (xcord - 16); x <= (xcord + 15); x++) {
if (x==xcord && y==ycord) {
sprintf(colorof, &quot;%s&quot;, CCGRN(ch, C_NRM));
strcat(buf, colorof);
strcat(buf, &quot;@&quot;);
sprintf(colorof, &quot;%s&quot;, CCNRM(ch, C_NRM));
strcat(buf, colorof);
} else {
if( ((x - 1) % 100) == 99)
{
tx = (x - 100);
} else {
tx = x;
}
if( ((y - 1) % 100) == 99)
{
ty = (y - 100);
} else {
ty = y;
}
if (world[( (ty * 100) + tx)].people) {
if (IS_NPC(world[( (ty * 100) + tx)].people)) {
sprintf(colorof, &quot;%s&quot;, CCRED(ch, C_NRM));
strcat(buf, colorof);
strcat(buf, &quot;*&quot;);
sprintf(colorof, &quot;%s&quot;, CCNRM(ch, C_NRM));
strcat(buf, colorof);
} else {
sprintf(colorof, &quot;%s&quot;, CCBLU(ch, C_NRM));
strcat(buf, colorof);
strcat(buf, &quot;*&quot;);
sprintf(colorof, &quot;%s&quot;, CCNRM(ch, C_NRM));
strcat(buf, colorof);
}
} else {
loop = 0;
if ( (mfile = fopen(&quot;earth&quot;, &quot;rt&quot;)) == NULL)
{
log(&quot;file doesn't exist?&quot;);
}
while (!feof(mfile))
{
loop++;
letter = fgetc(mfile);
if (loop == ( (ty * 100) + tx) )
{
mapchar = letter;
}
}
if (mapchar != NULL)
{
strcat(buf, &mapchar);
} else {
mapchar = '.';
strcat(buf, &mapchar);
}
fclose(mfile);
}
}
}
strcat(buf, &quot;\r\n &quot;);
}
strcat(buf, &quot;\r\n&quot;);
strcat(buf, &quot;TESTING\r\n\r\n&quot;);
send_to_char(buf, ch);
}

yes, it's extreamly sloppy, because i've been tearing it apart and rebuilding it numorous times....I've pinpointed that it's just not reading from the file...it opens it fine, and if you see where it says &quot;If (mapchar == NULL) mapchar = '.'&quot;...I end up getting a 12x32 map of .'s, so obviously, mapchar's not reading anything, and it has nothing to do with the loop, because i've tried reading even individual lines or characters one at a time from the file, and no luck. It's being compiled on a UNIX BSD server, and it's not stand alone, obviously. It's part of a MUD for those of you that know what that is :p. Any help would be appriciated.

Thanks,
Rob
 
Heh, been a while since I looked at mud code. Also been a while since I used fopen but i think it is in your declaration of it.

FILE* mfile; // not FILE* mfile(),*mfile;

then try the

mfile = fopen(&quot;earth&quot;, &quot;rt&quot;)

Also make sure that the &quot;earth&quot; file exists where you are opening it from and it is not a directory up/down etc

Matt


 
Tried that :p, and it's opening the file fine, otherwise i'd get the error i have there in my log...it's just not reading it
 
I think the problem lies here

while (!feof(mfile))
{//begin while
loop++;
letter = fgetc(mfile);
if (loop == ( (ty * 100) + tx) )
{
mapchar = letter;
}
}//end while

if (mapchar != NULL)
{
strcat(buf, &mapchar);
}
else
{
mapchar = '.';
strcat(buf, &mapchar);
}

We go through the whole file and occasionally set mapchar. Then the while ends and i dont follow mapchar != NULL as you are testing the value of mapchar == 0? Bit confusing here. Look into this loop and see what you come up with.

Matt
 
ty and tx are the current coordinates set, they're originated from; ty = (rnum / 100); tx = (rnum % 100);...so if rnum is 1520, ty is 15, tx is 20. So, the program will check for a 12x32 area from rnum in y and x coordinates system (works better for what i'm doing). So what's happeneing is i'm reading characters untill the loop is equal to the rnum(which is the current room number being read). I probably shouldn't even read anything into loop untill it gets to that rnum, but as i said, it's been torn apart and rebuilt many times. Anyway, basically it's saying letter = (whatever char we're on in the file, unless it's untill EOF)...if we get to &quot;rnum&quot;, then make mapchar = letter. If mapchar == NULL (which is not 0, it's \0, or basically there's nothing there) then mapchar = '.';...and i'm getting a 12x32 map of .'s...this tells me that mapchar's not reading anything from letter, when i KNOW loop will get to rnum (the files 10000 char's long, and we're only looking at a 12x32 area)....so again thanks, but still not the problem :)....keep em comeing though.
 
Alright... after looking at this. Is what you are doing:

Opening the file

Looping like this
for (y = 0; y <= 12; y++) {
for (x = 0; x <= 31; x++) {

31^12 times through the loop just to get one character each time? There has to be an easier way. What about a static buffer you read the WHOLE file into once? If the file is TOO big this prolly wont work. This may be the wrong way to look at it but opening and closing the infile over and over again seems a bit overboard

(this is, of course, iff loop == (ty * 100) + tx) executes ONCE which I dont see it happening any more then that.

Basically this is the same as loop == rnum. If you are going to go through all the trouble of looping one character at a time, why not instead look into fgets, dynamically malloc a character array and free it (where the array is the size of rnum) after reading it into the array take the last character in the array (the one you are looking for) and free the memory close the file and do it all again. This will speed up the process a bit at least.

Verify with a printf that the character you get, is, infact not null.

THIS MAY BE ANOTHER PATH TO TAKE.

for kicks, memset(buf,0,sizeof(buf)); just to see what happens and...

try:

char mapchar[2];
mapchar[1] = '\0';


any assingments of mapchar should look like this

*mapchar = letter;

the other problem may be that strcat may not be working properly.


Lemme know how these ideas turn out

matt

 
By the way, what kind of style is it?
{
tx = (tx + 100);
}
if(tx > 99)
{
tx = (tx - 100);
}
C/C++ has the operator +=. The diference is
tx =(tx - 100);
means put tx-100 in memory somewhere and after it tx= thisvalue from that somethere. The faster, directly adding is
tx-=100;
It compilles in fewer machine commands. John Fill
1c.bmp


ivfmd@mail.md
 
Ok, again, this code is extreamly sloppy. At one point I did read the entire map into a var (see mainmap[99][99]). But it still was not reading the file. My problem isn't in the loops at all, as this program works fine in Windows, my problem lies in the fact that, once run under UNIX BSD, the file is not read at all. And as far as tx -= 100 yes I agree it will compile faster, and in turn run faster. At this point, however it's made for me to be able to read easily. It will be organized when i figure out WHY I can't get it to read the file. I will however try the *mapchar = letter...

thanks again,
Rob
 
I got it working, although i feel really stupid now...The reason it wasn't working was that the program was running from a seperate directory as the source map file was...so i had to define the filename as char *filename = &quot;../src/earth.map&quot;; Everything else just fell into place...although i did have to modify some of the loops to make it wrap around like i wanted it to...thanks for your help, guys.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top