Author Topic: Map Code  (Read 2731 times)

Offline Tacitus

  • Acquaintance
  • *
  • Posts: 21
    • View Profile
Map Code
« on: September 28, 2006, 01:36:05 PM »
http://lpuni.net/node/120">Map code as requested.
Cody A.W. Somerville

Offline tigwyk

  • Acquaintance
  • *
  • Posts: 45
    • View Profile
Map Code
« Reply #1 on: September 28, 2006, 03:11:18 PM »
Well, I'll paste my code and you can tell me why it won't work. ;)



First, here is Tac's map code. Very cool.

code:


/* map.c

 Tacitus @ LPUniversity
 24-Aug-06
 An object that represents a map
 
*/

/* INTRODUCTION
 *
 * This object represents a map that is
 * parses from a file. Each line is a row (y)
 * in the map and each column (x) is also a
 * column in the map.
 *
 * This object provides a few simple accesor functions
 * to get information from the map and to minipulate the
 * map.
 *
 * This should load on any MudOS mud and most likely ldmud,
 * dgd, and others with ease.
 *
 */

   /* Gloval Variables */

mixed *map;         /* 2D array containing map tiles  */
string mapSource;   /* Map Source file (aka filename) */

   /* Function Prototypes */
   
void clear();
private int parseMap(string filename);

   /* Functions */

void create()
{
   clear();
}

/* Clears the map of all data */

void clear()
{
   map = ({});
   mapSource = "";
}

/* Load a map from file
 *
 * Will return 0 if file does not exist.
 * See comments on parseMap for further
 * error codes.
 *
 */

int load(string filename)
{
   if(!file_exists(filename)) return 0;
   else
   {
      clear();
      return parseMap(filename);
   }
}

/* Returns 1 if a map is loaded or 0 if false */

int isLoaded()
{
   if(sizeof(map) > 0) return 1;
   else return 0;
}

/* Returns the filename of current map */

string getMapSource()
{
   return mapSource;
}

/* Parses the map and creates 2D array
 *
 * This function can only be called internally.
 *
 * Returns -2 when unable to read file.
 * Returns -3 when file is empty.
 * Returns 1 on success
 *
 */
 
private int parseMap(string filename)
{
    string fileContents, *fileExploded;
    int y, x;

    fileContents = read_file(filename);
    if(!fileContents || !stringp(fileContents))
        return -2;

    fileExploded = explode(fileContents, "\n");
    if(!arrayp(fileExploded) || sizeof(fileExploded) <= 0)
        return -3;
       
    map = allocate(sizeof(fileExploded));
    mapSource = filename;
       
    for(y = 0; y < sizeof(fileExploded); y++)
    {
       map[y] = allocate(strlen(fileExploded[y]));
       
       for(x = 0; x < sizeof(map[y]); x++)
       {
          map[y]
  • = fileExploded[y][x..x];

       }
    }
   
    return 1;
}

/* Get the width of a row or if no argument is provided
 * then the row with the greatest width is returned.
 * -1 is returned when you provide an invalid argument.
 */
 
varargs int getWidth(int y)
{
   if(intp(y))
   {
      if(y >= sizeof(map) || y < 0) return -1;
      else return sizeof(map[y]);
   }
   
   else
   {
      int maxWidth = 0;
      
      foreach(mixed element in map)
         if(sizeof(element) > maxWidth) maxWidth = sizeof(element);
         
      return maxWidth;
   }
}

/* Returns the height of the map */

int getHeight()
{
   return sizeof(map);
}

/* Returns the tile type at the coordinates provided
 * Will return -1 for invalid or not existing coordinates
 */

mixed getTileAt(int x, int y)
{
   if(x < 0 || y < 0) return -1;
   if(y >= sizeof(map) || sizeof(map[y]) <= x) return -1;
   return map[y]
  • ;

}

/* Allows you to set the tile type at the coordinates provided
 * Returns 1 for success, 0 for failure, and -1 for invalid argument */

int setTileAt(mixed tileType, int x, int y)
{
   if(x < 0 || y < 0) return -1;
   if(y >= sizeof(map) || sizeof(map[y]) <= x) return 0;
   map[y]
  • = tileType;

   return 1;   
}

/* Returns 1 if tile type at coordinates match the tiletype
 * passed as the first argument. Returns 0 if false.
 * Returns -1 for invalid argument.
 */

int isTileType(mixed tileType, int x, int y)
{
   if(x < 0 || y < 0) return -1;
   if(y >= sizeof(map) || sizeof(map[y]) <= x) return -1;
   if(map[y]
  • == tileType) return 1;

   else return 0;
}




Now, here is my map command to display a map:



code:

#include <lib.h>

inherit LIB_DAEMON;

mixed cmd() {
   object map;
int x, y, xd, yd;

map = clone_object("/lib/map");
map->load("/lib/test.map");

/* Code to draw the entire map */

yd = map->get_height();
xd = map->get_width();

for(y = 0; y < yd; y++)
{
     for(x = 0; x < xd; x++)
          write(map->getTileAt(x, y));
     write("\n");
}

    return 1;
}

void help() {
    message("help", "Syntax: <map>\n\n"
      "Displays a map."
      "\n\n",this_player());
}




The problem? It returns a blank line. Ideas?