Loop display problem

Hello. I'm working on a MUD from scratch and we're designing the world map now. The map will be a csv file. I've got the csv parsing done. I don't think there are any issues in it, I can use a simple if to display it all fine, but please let me know if you do see any issues or improvements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void LoadMap ()
{
  ifstream worldfile(WORLD_FILE);

int col = 0;
int row = 0;

string tile;  // each actual value
string line; // to grab
stringstream linestream; //to manipulate further

while (getline(worldfile, line, ' ')) //Read each line, ends with nothing
{
  linestream << line;

  while (getline(linestream, tile, ',')) //read the stream, seperate tiles inbetween commas
  {
    worldmap[row][col] = tile;
    col++; //next item after a comma
  }
  row++;
  col = 0;
}


}


The real problem I'm having is when displaying the worldmap. It is not behavior properly. worldmap is 20x20, loaded into the string array above. to display I want to only display a section of it, in this case anything 5 less than and 6 more than (to center the player at all times). However when moving and looking, the 1st line is several characters off, and the code doesn't seem to be spacing out the left side correctly to center the player on the map. y centering seems okay, but X seems off. Currently player resides at 0x,0y (which in the game highlights his square red).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
          
            
            
            
      01 02 03 04 05 06 
      
01 02 03 04 05 06 
      
01 02 03 04 05 06 
      
01 02 03 04 05 06 
      
01 02 03 04 05 06 
      
01 02 03 04 05 06 

 
 
<0x 0y 1z>


As you can see, the spaces in the first line mess up the alignment. Also after geting to 6x or more, the code looses the spacing between lines, and after you get to 16 is does an odd line wrap and draws incorrect data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

            
            
            
            
            
            
10 11 12 13 14 15 16 17 18 19 20 
01 
10 11 12 13 14 15 16 17 18 19 20 
01 
10 11 12 13 14 15 16 17 18 19 20 
01 
10 11 12 13 14 15 16 17 18 19 20 
01 
10 11 12 13 14 15 16 17 18 19 20 
01 
10 11 12 13 14 15 16 17 18 19 20 
01 

 
 
<15x 0y 1z>




Here is the relevant dolook code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 for (int i = (p->y - 6); i < (p->y + 6); i++)
 {
  for (int j = (p->x - 6); j < (p->x + 6); j++)
  {
     if (j == p->x && i == p->y) // color player dot
     {
       *p << "\e[0;31m";
       *p << worldmap[i][j] << " ";
       *p << "\e[0m";
     }
   else 
   {
/* loop for other players */
   int others = 0;
    for (tPlayerListIterator listiter = playerlist.begin (); 
          listiter != playerlist.end (); 
          listiter++)
    {
     tPlayer *otherp = *listiter;

     if (otherp != p && i == otherp->y && j == otherp->x) // color other player dot
     {
      *p << "\e[0;36m";
      *p << worldmap[i][j] << " ";
      *p << "\e[0m";
      others++;
     }
    } /* end loop */

    if (j < 0 || i < 0)
     {
      *p << " ";
     }
     else if (others == 0)
     {
      *p << worldmap[i][j] << " ";
     }
   }

   }
    *p  << "\r\n";  //new line for each row.
   }
   }
*p << "\r\n \r\n \r\n";

Last edited on
*Bump* No one have any suggestions?
1
2
3
p->y
*p << worldmap[i][j] << " ";
*p  << "\r\n";  //new line for each row. 
¿what is `p'?

> after you get to 16 is does an odd line wrap
¿your screen is too narrow?
Screen for sure isn't too narrow, I can display the whole test map which is 20x20 and it looks fine, I'm trying to display a smaller subsection instead.

P point to the player class, *p specifically sends text for output. consider it the same as cout in a terminal app.
Last edited on
My initial hunch would make me dump the hex data in worldmap in a debugger.
I'm guessing some newlines are being stored in the array?
¿So a player is an stream? that's weird.

Use simply '\n' for newline
My initial hunch would make me dump the hex data in worldmap in a debugger.
I'm guessing some newlines are being stored in the array?


I thought about that too, but when i remove the relativity bit of the code

1
2
3
for (int i = (p->y - 6); i < (p->y + 6); i++)
 {
  for (int j = (p->x - 6); j < (p->x + 6); j++)


and just iterate 0 to 19 and write out the entire map it all seems to display fine. I don't get why iterating around the player's point rather than 0 to 19 would affect it displaying linebreaks and such.

1
2
3
 for (int i = 0; i <= 19; i++)
 {
  for (int j = 0; j <= 19; j++)



EDIT: Strange. I pulled the code out, rewrote it and now its working. I'll have to run a diff on it to see what might have been typo'd or something..

Thanks for the input guys.
Last edited on
Topic archived. No new replies allowed.