Re: Help with Mapedit.txt

From: Welcor (welcor@dune.net)
Date: 03/25/02


From: "Moppe Sewerin" <mans@GRAPHICLIFE.COM>
> Any1, who are using this map thing, succeeded in making a frame around
> the map?
> [...]

I've taken the liberty to dissect your code a bit. It should be both
faster and more readable now:

I assume the map[][] array is initialized as

char map[MAP_X+1][MAP_Y+1];

or you'll have to change the loops below to use 'MAP_X-1' etc.

<mailer code>
/* corners */
map[0][0] = map[MAP_X][0] = map[0][MAP_Y] = map[MAP_X][MAP_Y] = ' ';

/* coloumns */
for (x = 1; x < MAP_X; x++) /* skip first and last row */
  map[x][0] = map[x][MAP_Y] = '|';

/* lines */
for (y = 1; y < MAP_Y; y++) /* skip first and last col */
  map[0][y] = map[MAP_X][y] = '-';

/* blanks */
for (x = 1; x < MAP_X; x++)  /* skip first and last row */
  for (y = 1; y < MAP_Y; y++)  /* skip first and last col */
    map[x][y] = ' ';

/* plot in zones */
for (i = 0; i <= top_of_zone_table; i++)
  if (GET_XPOS(ch) == zone_table[i].xpos &&
      GET_YPOS(ch) == zone_table[i].ypos)
    map[zone_table[i].xpos][zone_table[i].ypos] = 'X';
  else
    map[zone_table[i].xpos][zone_table[i].ypos] = zone_table[i].sy;
}
</mailer code>

The output should look like this:
 ---
|   |
|   |
 ---

Just to pretty things up a bit (and skip a comparison[1]) try this:

<mailer code>
ACMD(do_map)
{
   int x, y, slen = 0;

   *buf='\0';

   draw_map(ch);

   for (x = 0; x <= MAP_X; x++) {
     for (y = 0; y <= MAP_Y; y++)
       slen += sprintf(buf + slen, "%c", map[x][y]);
     slen += sprintf(buf + slen, "\r\n");
   }

   page_string(ch->desc, buf, 1);
}

</mailer code>

Welcor

[1] It's good coding practice to limit the amount of
    comparisons in a for/while loop, since you have to
    iterate through that comparison a lot of times.
    Same thing goes for nested for/while loops - be sure
    the total computations stay as low as possible.
    Consider the following (stupid) example:

    int x,y,count = 0;

    for (x = 0;x < 50;x++)
      for (y = 0; y < 25; y++)
        count++;

    printf("%d", count);

    As the output shows, the loop iterates 25*50 (= 1250) times.
    Any comparisons, nested for/while loops, or other statements
    are executed 1250 times too. In other words - to not slow
    down the program avoid nesting for/while loops, or filling
    them with lots of comparisons, whenever possible.

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT