wilderness.cc

musicmud-2.1.6/src/extras/wilderness.cc · 65.6 KB

From the archive musicmud2.1.6.tar.gz

Download the archive   « Back to the archive contents

/* 
 * MusicMUD - Wilderness Module
 * Copyright (C) 1998-2003 Abigail Brady
 * Copyright (C) 2001-2003 Paul Lettington
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 */

#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <regex.h>

#include "musicmud.h"
#include "trap.h"
#include "misc.h"
#include "pflags.h"
#include "verbs.h"
#include "prep.h"
#include "paths.h"
#include "misc.h"
#include "emsg.h"
#include "hooks.h"

#include <list>
#include <algorithm>
#include <vector>
#include <set>


//! The wilderness module
namespace wild {

#define MODULE "wild"
#define WILD(which, x, y) ((which->data)[(x%which->sizex) * which->sizey + (y%which->sizey)])

extern const char *sameall[], *samenear[], *singnear[], *plurnear[], *singfar[], *plurfar[];

  //! The actual type of terrain here
enum Terrain {
  T_NONE=-1,
  T_FIRST=0,
  T_SEA=T_FIRST, //0
  T_BEACH, //1
  T_WILDERNESS, //2
  T_BUILTUP, //3
  T_FOREST, //4
  T_GATEWAY,//5
  T_ROAD,//6
  T_FRESHWATER,//7
  T_BRIDGE, //8
  T_SCRUB,//9
  T_SWAMP,//10
  T_CLIFFTOP,//11
  T_DESERT,//12
  T_PRAIRIE,//13
  T_ICE,//14
  T_ROCKYPLAIN,//15
  T_HIGHGROUND,//16
  T_DRYCLIFF,//17
  T_OUTCROPS,//18
  T_GPLAIN,//19
  T_DRYRIVER,//20
  T_DOMEWALL,//21
  
  T_LAST,
};

#define O_NONE        0
#define O_MAGLEV    1
#define O_TUNNEL 2

  void operator++(Terrain &t) {
    t = Terrain(int(t) + 1);
  }
  void operator++(Terrain &t, int) {
    t = Terrain(int(t) + 1);
  }

//! A cardinal compass direction.
enum Dir {
  D_NONE,
  D_NORTH,
  D_EAST,
  D_SOUTH,
  D_WEST,
};

//! A cell on the grid.
struct Loc {
  Terrain t;
  Dir d;
  char alt;
  char overlay;
  
  Loc() : t(T_NONE), d(D_NONE), alt(0), overlay(O_NONE) {
  }
};

class Wilderness;

static Terrain terrain(Wilderness *, int x, int y);

  //! A wilderness.
class Wilderness : public Object {
public:
  unsigned int sizex, sizey;
  Loc *data;
  Terrain baseterrain;
  string zoneobject;
  bool needssave;
  int hasair;
  
  //! Constructs a new (blank) wilderness, with given properties.
  Wilderness(string which, unsigned int x, unsigned int y, 
	     Terrain base, int defalt, const char *zon) : Object(which.c_str()) {
    sizex = x;
    sizey = y;
    data = new Loc [x*y];
    baseterrain = base;
    hasair = 0;
    needssave = 1;
    for (x=0;x<sizex;x++) {
      for (y=0;y<sizey;y++) {
	WILD(this, x, y).t = base;
	WILD(this, x, y).alt = defalt;
      }
    }
    zoneobject = zon;
  }

  //! Loads the wilderness with name 'which'. Will throw an emsg if there is an error.
  Wilderness(string which) : Object(which.c_str()){
    log(PFL_SEEINFO,0,"wild", "loading %s",which.c_str());
    needssave=false;
    string name = DATA_WILD "/";
    name += which;
    name += ":meta";
    FILE *fp = fopen(name.c_str(), "r");
    if(!fp) {
      emsg e("can't open meta file");
      throw e;
    }

    char temp[1024];

    if(fscanf(fp,"%u %u\n%d\n%s\n%u", &sizex, &sizey, &(int)baseterrain,temp, &hasair) != 5) {
      emsg e("meta file isn't the right shape");
      fclose(fp);
      throw e;
    }
    zoneobject = temp;
    fclose(fp);
    
    name = DATA_WILD "/";
    name += which;
    name += "-0.0";
    fp = fopen(name.c_str(), "r");
    unsigned int x,y;
    data = new Loc[sizex*sizey];
    if(fp) {
      for (y=0;y<sizey;y++) {
	for (x=0;x<sizex;x++) {
	  int ch = fgetc(fp);
	  WILD(this,x,y).t = (Terrain)(ch - '0');
	}
	fgetc(fp);
      }
      fclose(fp);
    } else {
      emsg e("can't open terrain file");
      throw e;
    }
    
    name = DATA_WILD "/";
    name += which;
    name += "-0.0:d";
    fp = fopen(name.c_str(),"r");
    if(fp) {
      for(y=0;y<sizey;y++) {
	for(x=0;x<sizex;x++) {
	  int ch = fgetc(fp);
	  WILD(this,x,y).d = (Dir)(ch-'0');
	}
	fgetc(fp);
      }
      fclose(fp);
    } else {
      emsg e("can't open dir file");
      fclose(fp);
      throw e;
    }
    
    name = DATA_WILD "/";
    name += which;
    name += "-0.0:a";
    fp = fopen(name.c_str(), "r");
    if(fp) {
      for(y=0;y<sizey;y++) {
	for(x=0;x<sizex;x++) {
	  int ch = fgetc(fp);
	  WILD(this,x,y).alt = (ch-'@');
	}
	fgetc(fp);
      }
      fclose(fp);
    } else {
      emsg e("can't open alt file");
      throw e;
    }
    
    name = DATA_WILD "/";
    name += which;
    name += "-0.0:o";
    fp = fopen(name.c_str(), "r");
    if(fp) {
      for(y=0;y<sizey;y++) {
	for(x=0;x<sizex;x++) {
	  int ch = fgetc(fp);
	  WILD(this,x,y).overlay = (ch-' ');
	}
	fgetc(fp);
      }
      fclose(fp);
    }
    
  }
  
  //! Destructor. Will save the wilderness if needed.
  ~Wilderness() {
    log(PFL_SEEINFO,0,"wild","destroying %s",this->id);
    if(needssave) save();
    delete [] data;
  }

  //! Delete the files associated with this wilderness.
  void erase() {
    string name = ssprintf(DATA_WILD "/%s:meta", id);
    unlink(name.c_str());

    name = ssprintf(DATA_WILD "/%s-0.0", id);
    unlink(name.c_str());

    name = ssprintf(DATA_WILD "/%s-0.0:a", id);
    unlink(name.c_str());

    name = ssprintf(DATA_WILD "/%s-0.0:d", id);
    unlink(name.c_str());

    name = ssprintf(DATA_WILD "/%s-0.0:o", id);
    unlink(name.c_str());
  }
  
  //! Saves this wilderness
  void save() {
    string name = DATA_WILD "/";
    name += id;
    name += ":meta";
    FILE *fp = fopen(name.c_str(),"w");
    if (!fp) {
      log(PFL_SEEINFO,0,"wild","couldn't open %s",name.c_str());
      return;
    }
    fprintf(fp,"%u %u\n%d\n%s\n%u",sizex,sizey,(int)baseterrain,zoneobject.c_str(),hasair);
    fclose(fp);
    
    name = DATA_WILD "/";
    name += id;
    name += "-0.0";
    fp = fopen(name.c_str(),"w");
    if (!fp) {
      log(PFL_SEEINFO,0,"wild","couldn't open %s",name.c_str());
      return;
    }
    unsigned int x,y;
    for(y=0;y<sizey;y++) {
      for(x=0;x<sizex;x++) {
	fprintf(fp,"%c", terrain(this,x,y)+'0');
      }
      fprintf(fp,"\n");
    }
    fclose(fp);
    
    name = DATA_WILD "/";
    name += id;
    name += "-0.0:d";
    fp = fopen(name.c_str(),"w");
    if (!fp) {
      log(PFL_SEEINFO,0,"wild","couldn't open %s",name.c_str());
      return;
    }
    for(y=0;y<sizey;y++) {
      for(x=0;x<sizex;x++) {
	int d = WILD(this,x,y).d;
	if(d<0) d=0;
	fprintf(fp, "%i",d);
      }
      fprintf(fp,"\n");
    }
    fclose(fp);
    
    name = DATA_WILD "/";
    name += id;
    name += "-0.0:a";
    fp = fopen(name.c_str(),"w");
    if (!fp) {
      log(PFL_SEEINFO,0,"wild","couldn't open %s",name.c_str());
      return;
    }
    for(y=0;y<sizey;y++) {
      for(x=0;x<sizex;x++) {
	int d = WILD(this,x,y).alt;
	fprintf(fp, "%c",d+'@');
      }
      fprintf(fp,"\n");
    }
    fclose(fp);

    name = DATA_WILD "/";
    name += id;
    name += "-0.0:o";
    fp = fopen(name.c_str(),"w");
    if (!fp) {
      log(PFL_SEEINFO,0,"wild","couldn't open %s",name.c_str());
      return;
    }
    for(y=0;y<sizey;y++) {
      for(x=0;x<sizex;x++) {
	int o = WILD(this,x,y).overlay;
	fprintf(fp, "%c", o+' ');
      }
      fprintf(fp,"\n");
    }
    fclose(fp);

    needssave = 0;
  }
  
  MudObject *obj() {
    return planet->get(zoneobject);
  }
};

World<Wilderness> *wilderni = new World<Wilderness>();

#define DONT_INSTANTIATE
#include "../World.cc"
template World<Wilderness>;
#undef DONT_INSTANTIATE

regex_t tagmatch;

//! convert x, y, and z coords on a wilderness to a string.
static string rawwhereis(Wilderness *wild, int x, int y, int z, int noloop=0)
{
  if (y < 0 || y >= (int)wild->sizey) return "";
  if (x < 0)
    return rawwhereis(wild, x+wild->sizex, y, z, noloop);
  if (x >= (int)wild->sizex)
    return rawwhereis(wild, x-wild->sizex, y, z, noloop);

  if (!z) {
    return ssprintf("%i:%i", x, y);
  } else if (z < 0) {
    return ssprintf("%i:%i-%i", x, y, -z);
  } else if (z > 0) {
    return ssprintf("%i:%i+%i", x, y, +z);
  }
  
  return "";
}

  //! Returns the zone object associated with this wilderness.
static MudObject *zfor(Wilderness *wild) {
	if(wild) return planet->get(wild->zoneobject.c_str());
	return NULL;
}

  //! Returns the zone tag associated with this wilderness.
static const char *ztag(Wilderness *wild) {
	MudObject *zoneobj = zfor(wild);
	if(!zoneobj) return 0;
	return zoneobj->get("zone");
}

  //! Returns the id of the room at x,y,z in the wilderness, which may be a non-wilderness room that has been patched in, or may be "" to indicate that his room does not exist.
static string whereis(Wilderness *wild, int x, int y, int z, int noloop=0)
{
  const char *which = wild->id;
  if (y < 0 || y >= (int)wild->sizey) return "";
  if (x < 0)
    return whereis(wild, x+wild->sizex, y, z, noloop);
  if (x >= (int)wild->sizex)
    return whereis(wild, x-wild->sizex, y, z, noloop);

  string i = rawwhereis(wild, x, y, z, noloop);
  MudObject *o = zfor(wild);
  if (o) {
    const char *where = o->get(i.c_str());
    if (where)
      return where;
  }

  bool allowed = false;
  if (z == 0) 
    allowed = true;
  else
    {
      Terrain t = terrain(wild, x, y);
      if (z == -1 && (t == T_SEA || t == T_BRIDGE))
	allowed = true;
    }

  if (z == 3)
    allowed = 1;

  if (WILD(wild, x, y).overlay & O_MAGLEV) {
    if (z == 2 || z == 1)
      allowed = 1;
  }

  if (WILD(wild, x, y).overlay & O_TUNNEL) {
    if (z == -2)
      allowed = -1;
  }

  if (!allowed)
    return "";

  if (!z) {
    return ssprintf("@%s_%i:%i", which, x, y);
  } else if (z < 0) {
    return ssprintf("@%s_%i:%i-%i", which, x, y, -z);
  } else if (z > 0) {
    return ssprintf("@%s_%i:%i+%i", which, x, y, +z);
  }

  return "";
}

//! performs a transformation to find out what sort of terrain (x,y) looks like from (fx,fy)
static Terrain seems_terrain(int fx, int fy, Wilderness *wild, int x, int y) {
  int thisa = WILD(wild, fx, fy).alt;
  int nexta = WILD(wild, x, y).alt;
  if ((nexta-thisa)>1) {
    return T_CLIFFTOP;
  }
  return terrain(wild, x, y);
}

//! Obtain the terrain type from wild(x,y). If it's a patched in loc, will grab it from 'terrain' property.
static Terrain terrain(Wilderness *wild, int x, int y)
{
  while (x<0)
    x += wild->sizex;

  while (x>=(int)wild->sizex)
    x -= wild->sizex;

  if (y < 0 || y >= (int)(wild->sizey))
    return wild->baseterrain;

  string t = whereis(wild, x, y, 0, 1);
  Terrain t2 = WILD(wild, x, y).t;
  if (t[0] != '@' && t2 != T_BUILTUP) {
    MudObject *to = planet->get(t.c_str());
    if (to) {
      int te = to->get_int("terrain");
      if (te != -1)
       return (Terrain)te;
    }
    return T_GATEWAY;
  }
  return t2;
}


//! Get the room at wild(x,y,z), but only if it is patched in
static MudObject *getpatched(Wilderness *wild, int x, int y, int z) {
  string destid = whereis(wild, x, y, z);
  if (destid[0]!='@') {
    return planet->get(destid.c_str());
  }
  return NULL;
}

//! Return any landmark string associated with wild(x,y,z)
static const char *landmark(Wilderness *wild, int x, int y, int z) {
  MudObject *p = getpatched(wild, x, y, z);
  if (p) {
    const char *lm = p->get("landmark");
    if (lm)
      return lm;
  }
  
  string destid = rawwhereis(wild, x, y, z);
  destid += "_landmark";
  MudObject *o = zfor(wild);
  if (o) {
    const char *lm=o->get(destid.c_str());
    if (lm)
      return lm;
  }

  return NULL;
}

//! Convert a Dir to a string.
static const char *dirname(Dir d) {
  const char *dn[] = {"nowhere", "north", "east", "south", "west" };
  if (d < D_NONE || d > D_WEST)
    return "???";
  return dn[d];
}

//! convert a string to a Dir
static Dir getdir(const char *name) {
  if (toupper(name[0]) == 'N') return D_NORTH;
  if (toupper(name[0]) == 'E') return D_EAST;
  if (toupper(name[0]) == 'S') return D_SOUTH;
  if (toupper(name[0]) == 'W') return D_WEST;
  return D_NONE;
}

//! Return the wilderness associated with the tag given, loading it if necessary.
static Wilderness *tag2l(const char *a) {
  	if (!a)
    		return 0;

	Wilderness *w =wilderni->get(a);
	if (w) 
		return w;
	else {
		try {
			w = new Wilderness(a);
			if(w) {
				wilderni->add(*w);
				return w;
			}
		} catch (emsg &e) {
			log(PFL_SEEINFO,0,"wild","load: %s: %s",a,e.msg.c_str());
		}
	}
	return NULL;
}


//! Create an exit from @from@ to @dest@, going @dir@.  If a to-be-deleted exit already exists, just adapt that.
static MudObject *make_exit(MudObject *from, const char *dir, const char *dest) {
  const char *door=0;

  string id = ssprintf("%s_%s", from->id, dir);
  MudObject *already = planet->rawget(id.c_str());
  if (already) already->nuke_me = 1;

  if (dest && *dest) {
    if (dest[0] != '@') {
      MudObject *dst = planet->get(dest);
      if (dst) {
	if (streq(dir, "north") && dst->get_flag(FL_BARSOUTH)) return NULL;
	if (streq(dir, "south") && dst->get_flag(FL_BARNORTH)) return NULL;
	if (streq(dir, "east") && dst->get_flag(FL_BARWEST)) return NULL;
	if (streq(dir, "west") && dst->get_flag(FL_BAREAST)) return NULL;

	if (!door)
	  door = dst->get(ssprintf("door.%s", dir).c_str());

	if (!door)
	  door = dst->get(ssprintf("door%s", dir).c_str());
      }
    }

    MudObject *e = already;
    if (!e) {
      e = new MudObject(ssprintf("%s_%s", from->id, dir).c_str());
      planet->add(*e);
    }
    e->nuke_me = 0;
    e->set("short", dir);
    if (dest[0]=='-') {
      e->set("link", dest+1);
      e->set("lua.before_traverse", "if not getflag(pl, flag.Flying) then send(pl, \"You can't go there.\") return 1 end");
    } else {
      e->set("link", dest);
      e->unset("lua.before_traverse");
    }
    if (door)
      e->set("door", door);
    e->set_bflag(FL_EXIT, 1);
    e->set_bflag(FL_TRANSIENT, 1);
    set_owner(e, from);
    if (from)
      e->set("start", from->id);
    if (from)
      e->set("zone", from->get("zone"));
    return e;
  }

  return NULL;
}


//! does the given terrain type stop visibility?
static bool isobstruction(Terrain t)
{
	if(t==T_FOREST || t==T_BUILTUP || t==T_DOMEWALL)
		return 1;
	return 0;
}


//! is this terrain type a road?
static bool isroad(Terrain t) 
{
  if (t==T_ROAD || t==T_GATEWAY || t==T_BRIDGE)
    return 1;
  return 0;
}

#define same(a, b) (a==b || (isroad(b)?isroad(a):0))
#define SAMEN (same(neighbour[0], t))
#define SAMEW (same(neighbour[1], t))
#define SAMEE (same(neighbour[2], t))
#define SAMES (same(neighbour[3], t))
#define SAMENS (SAMEN && SAMES)
#define SAMEEW (SAMEE && SAMEW)
#define SAMENSEW (SAMEE && SAMEW && SAMEN && SAMES)

//! Set the room description and other properties on o, which is wild(x,y,z)
static void descit(Wilderness *wild, MudObject *o, int x, int y, int z) 
{
  	int i;
	Terrain t;

	bool done[4] = {0,0,0,0};
	Terrain neighbour[4]; // one square away
	Terrain neighbour2[4]; // two squares away
	/*
		 0
		1 2
		 3
	*/

	string name = "DEFAULT WILDERNESS";
	string desc = "This is the default description, you should never see this.";

	o->set_bflag(FL_ONWATER, 0);
	o->set_bflag(FL_BYWATER, 0);
	o->set_bflag(FL_HOT, 0);
	o->set_bflag(FL_COLD, 0);
	o->set_bflag(FL_AIRLESS, !wild->hasair);
	o->set_bflag(FL_TEMPEXTREME, 0);
	o->set_bflag(FL_ROOM, 1);
	o->unset("namestyle");

	if (z==1) {
	  o->set("name", "MagLev Tube");
	  o->set("namestyle", IN_THE);
	  o->set("desc", "You are in a wilderness maglev tube.");
	  o->set("terrain", terrain(wild,x,y));	
	  o->set_bflag(FL_OUTDOORS, 0);
	  dotrap(E_ONWILDCREATE, zfor(wild), zfor(wild), o);
	  return;
	}

	if (z==2) {
	  o->set("name", "MagLev Tube");
	  o->set("namestyle", ON_THE);
	  o->set("desc", "You are standing on top of a maglev tube.");
	  o->set("terrain", terrain(wild,x,y));	
	  dotrap(E_ONWILDCREATE, zfor(wild), zfor(wild), o);
	  return;
	}

	if (z==-2) {
	  o->set("name", "Tunnel");
	  o->set("namestyle", IN_THE);
	  string desc = "You are in an underground tunnel.";
	  o->set_bflag(FL_DARK, 1);
	  o->set_bflag(FL_OUTDOORS, 0);
	  o->set("terrain", terrain(wild,x,y));	
	  
	  vector<const char *> dirs;

	  if (whereis(wild, x, y-1, z)!="")    dirs.push_back("north");
	  if (whereis(wild, x+1, y, z)!="")    dirs.push_back("east");
	  if (whereis(wild, x, y+1, z)!="")    dirs.push_back("south");
	  if (whereis(wild, x-1, y, z)!="")    dirs.push_back("west");

	  if (dirs.size()==4) {
	    desc = "You are at a crossroads in an underground tunnel.";
	  }

	  if (dirs.size()==3) {
	    desc += ssprintf(" You are at a fork. The tunnel continues %s, %s and %s.",
	      dirs[0], dirs[1], dirs[2]);
	  }

	  if (dirs.size()==2) {
	    desc += ssprintf(" The tunnel continues %s and %s.\n", dirs[0], dirs[1]);
	  }

	  o->set("desc", desc);
	  if (whereis(wild, x, y, 0).length())
	    o->set("above", whereis(wild, x, y, 0));

	  dotrap(E_ONWILDCREATE, zfor(wild), zfor(wild), o);
	  return;
	}

	if (z==3) {
	  o->set("floor", "-");
	  static char *descs[T_LAST] = {
	    "the sea",
	    "a beach",
	    "a grassplain",
	    "a town",
	    "a forest",
	    "a gateway",
	    "a road",
	    "some freshwater",
	    "a bridge",
	    "some scrub",
	    "a swamp",
	    "a cliff-top",
	    "a desert",
	    "a prairie",
	    "ice",
	    "a rocky plain",
	    "high ground",
	    "a dry cliff",
	    "rocky outcrops",
	    "a gypsum plain",
	    "a dry river",
            "a dome wall"};
	  o->setf("name", "over %s", descs[terrain(wild, x, y)]);
	  o->set("namestyle", DIRECT);
	  o->set("desc", "You are over a wilderness location.");
	  o->set("terrain", terrain(wild,x,y));	
	  dotrap(E_ONWILDCREATE, zfor(wild), zfor(wild), o);
	  return;
	}

	o->set("floor", "template_ground_1");

	/* Custom types of locations that aren't handled very well at the moment 

	    Clearings on forests/scrub
            River mouths
            Islands
            Islands in rivers
            T-junctions
            Riverbanks
            |X  X = where two rivers merge (is there a name for that?)
            +-  

	*/


	switch (terrain(wild,x,y)) {
		case T_SEA:
		  if (z==0) {
			name = "Sea";
			desc = "You are at sea.";
			o->set("namestyle", ON_OR_IN);
			if(random_chance(10)) desc += " You hear the sound of a seagull from somewhere a long way above you.";
			if(random_chance(100)) desc += " You hear the sound of a mermaid singing a long way in the distance.";
			o->set_bflag(FL_ONWATER,1);
			o->set("floor", "template_sea_1");
			o->setf("seabed", whereis(wild, x, y, -1).c_str());
		  } else {
			name = "Underwater";
			o->set("namestyle", DIRECT);
			desc = "You are under the sea.";
			o->set_bflag(FL_UNDERWATER,1);
			o->set_bflag(FL_OUTDOORS, 0);
			o->set("floor", "template_seabed_1");
			o->setf("surface", whereis(wild, x, y, 0).c_str());
		  }
			break;
		case T_GATEWAY:
		case T_ROAD:
			name = "Road";
			desc = "You are on a road.";
			o->set("namestyle", ON_THE);
			o->set("floor", "template_surface_1");
			break;
	case T_OUTCROPS:
	  name = "Rocky Outcrops";
	  desc = "You are among some rocky outcrops.";
	  o->set("namestyle", ON);
	  break;
		case T_BEACH:
			// doesn't deal with crinkly fjords very well.
			o->set("namestyle", ON_OR_BY);
			name = "Beach";
			desc = "You are on a beach looking out to sea.";
			o->set_bflag(FL_BYWATER,1);
			o->set("floor", "template_beach_1");
			o->array_set("tele", 0, "template_sea_2");
			if(random_chance(5)) {
				const char * what;
				switch(random_number(3)) {
					case 0:
						what = "template_pebble_1";
						break;
					case 1:
						what = "template_seaweed_1";
						break;
					default:
					case 2:
						what = "template_driftwood_1";
						break;
				}

				MudObject *sign = planet->get(what);
				if(!sign) {
					log(PFL_SEEINFO, 0, "wild", "%s is NULL!", what);
					break;
				}
				MudObject *thing = clone_object(sign, o, 0);
				thing->set("zone","@auto");
				thing->set_bflag(FL_TRANSIENT,1);
			}
			break;
		case T_CLIFFTOP:
		case T_DRYCLIFF:
			// idea: random signs advertising the Samaritans?
			o->set("namestyle", ON_THE);
			name = "Cliff";
			desc = "You are on top of a cliff.";
			if(random_chance(10)) {
				MudObject *sign = planet->get("template_sign_1");
				if(!sign) {
					log(PFL_SEEINFO, 0, "wild", "NULL sign!");
					break;
				}
				MudObject *thing = clone_object(sign, o, 0);
				thing->set("zone","@auto");
				thing->set_bflag(FL_TRANSIENT,1);
			}
			o->set("treatas","template_sign_1");
			break;
		case T_WILDERNESS:
			name = "Grassland";
			o->set("namestyle", ON_THE);
			desc = "You are in an area of grassland.";
			break;
		case T_DESERT:
			name = "Desert";
			o->set("namestyle", IN_THE);
			desc = "You are in a desert.";
			if(random_chance(10)) desc += " You see tracks in the sand which could have been made by a scorpion.";
			o->set_bflag(FL_TEMPEXTREME, 1);
			break;
		case T_GPLAIN:
			name = "Gypsum Plain";
			o->set("namestyle", ON_THE);
			desc = "You are on a white gypsum plain.";
			o->set_bflag(FL_TEMPEXTREME, 1);
			break;
		case T_PRAIRIE:
			name = "Prairie";
			o->set("namestyle", ON_THE);
			desc = "You are on a prairie.";
			if(random_chance(10)) desc += " You see tracks in the ground which could have been made by a snake.";
			break;
		case T_ICE:
			name = "Ice";
			o->set("namestyle", ON_THE);
			desc = "You are on an icefield.";
			o->set_bflag(FL_COLD, 1);
			break;
		case T_ROCKYPLAIN:
  	        case T_HIGHGROUND:
			name = "Rocky Plain";
			o->set("namestyle", ON_THE);
			desc = "You are on a rocky plain.";
			break;
		case T_SCRUB:
			name = "Scrub";
			o->set("namestyle", IN_THE);
			desc = "You are in an area of scrub.";
			break;
		case T_SWAMP:
			name = "Swamp";
			o->set("namestyle", IN_THE);
			desc = "You are in a swamp.";
			if(random_chance(10)) desc += " Midges and flies swarm all around you.";
			break;
		case T_FOREST:
			o->array_set("tele", 0, "template_trees_1");
			name = "Forest";
			o->set("namestyle", IN_THE);
			desc= "You are in a forest.";
			if(random_chance(10)) desc += " You hear the sound of birdsong from the treetops.";
			if(random_chance(10)) {
				int r = random_number(10);
				const char * which="template_mushroom_3";
				const char *taste = 0;
				if(r<5)
					which="template_mushroom_3"; // boring
				else if(r<7) {
					which="template_mushroom_2"; // poisonous
					taste="It tastes slightly bitter.";
				}
				else
					which="template_mushroom_1"; // magic

				MudObject *shroom = planet->get("template_mushroom_1");
				if(!shroom) {
					log(PFL_SEEINFO, 0, "wild", "NULL mushroom!");
					break;
				}
				MudObject *thing = clone_object(shroom, o, 0);
				thing->set("zone","@auto");
				thing->set("treatas",which);
				thing->set_bflag(FL_TRANSIENT,1);
				if (taste)
				  thing->set("taste", taste);
			}
			break;
		case T_FRESHWATER:
			o->array_set("tele", 0, "template_freshwater_1");
			if(WILD(wild,x,y).d == D_NONE)
			{
				name = "Freshwater";
				o->set("namestyle", ON_OR_IN);
				desc = "You are in an area of still freshwater.";
				if(random_chance(10)) desc += " You catch a glimpse of a frog hopping away as you arrive.";
				o->set_bflag(FL_ONWATER,1);
			} else
			{
				name = "River";
				o->set("namestyle", ON_OR_IN);
				desc = "You are in a ";
				desc += dirname(WILD(wild,x,y).d);
				desc += " flowing river.";
				o->set_bflag(FL_ONWATER,1);
			}
			break;
                case T_DRYRIVER:
			name = "Dry Riverbed";
			o->set("namestyle", IN_THE);
			desc = "You are standing in a dry riverbed..";
			break;
		case T_BRIDGE:
		  	if (z==0) {
				name = "Bridge";
				o->set("namestyle", ON_THE);
				o->set("name.broken", "Broken Bridge.");
				o->set("treatas", "template_bridge_1");
				desc = "You are on a bridge over a river, which is flowing ";
				desc += dirname(WILD(wild,x,y).d);
				desc += ".";
				o->set("floor", "template_surface_1");
				o->set("namestyle", 4);
			} else {
			  	o->array_set("tele", 0, "template_freshwater_1");
				name = "Under the Bridge";
				desc = "You are under a bridge over a a ";
				desc += dirname(WILD(wild,x,y).d);
				desc += " flowing river.";
				o->set_bflag(FL_ONWATER, 1);
			}
			break;
                case T_DOMEWALL:
		case T_BUILTUP:
		default:
			break;
	}

	o->set("altitude", WILD(wild,x,y).alt);

	//XXX BEGIN PASTE HERE
	t= terrain(wild,x,y);
	neighbour[0] = seems_terrain(x, y,wild,x,y-1);
	neighbour[1] = seems_terrain(x, y,wild,x-1,y);
	neighbour[2] = seems_terrain(x, y,wild,x+1,y);
	neighbour[3] = seems_terrain(x, y,wild,x,y+1);

	neighbour2[0] = seems_terrain(x,y,wild,x,y-2);
	neighbour2[1] = seems_terrain(x, y,wild,x-2,y);
	neighbour2[2] = seems_terrain(x, y,wild,x+2,y);
	neighbour2[3] = seems_terrain(x, y, wild,x,y+2);

	if (neighbour[0] == T_CLIFFTOP) neighbour2[0] = T_NONE;
	if (neighbour[1] == T_CLIFFTOP) neighbour2[1] = T_NONE;
	if (neighbour[2] == T_CLIFFTOP) neighbour2[2] = T_NONE;
	if (neighbour[3] == T_CLIFFTOP) neighbour2[3] = T_NONE;

#define LANDMARK(xd, yd, dirn, number) \
	if (const char *lm=landmark(wild, x + xd, y + yd, z)) { \
	    desc += " To the "dirn", you see "; \
	    desc += lm; \
	    desc += "."; \
	    neighbour[number] = T_NONE; \
	  }

#define LANDMARK2(xd, yd, dirn, number) \
	if (const char *lm=landmark(wild, x + xd, y +yd, z)) { \
	    if (neighbour[number]!=T_NONE) { \
	      desc += " Far in the "dirn", you see "; \
	      desc += lm; \
	      desc += "."; \
	    } \
	    neighbour2[number] = T_NONE; \
	}

 LANDMARK(  0, -1, "north", 0)
 LANDMARK( +1, 0, "east", 2)
 LANDMARK(  0, +1, "south", 3)
 LANDMARK( -1, 0, "west", 1)

 LANDMARK2(  0, -2, "north", 0)
 LANDMARK2( +2, 0, "east", 2)
 LANDMARK2(  0, +2, "south", 3)
 LANDMARK2( -2, 0, "west", 1)

	//SPECIAL CASES:
	switch(t)
	{
		case T_FRESHWATER:
			if(WILD(wild,x,y).d != D_NONE)
			{
				for(i=0;i<4;i++)
				{
					if(neighbour[i]==T_SEA)
					{
						name = "River Mouth";
						// existing desc should do for this one
					}
				}
			}
			break;
		case T_SCRUB:
		case T_PRAIRIE:
		case T_WILDERNESS:
			{
				int temp=0;
				for(i=0;i<4;i++)
				{
					if(neighbour[i] != T_FOREST) temp=1;
				}
				if(!temp)
				{
					name = "Clearing";
					desc = "You are in a clearing.";
				}
			}
			break;
		case T_FOREST:
			{
				int isedge=0;
				for(i=0;i<4;i++)
				{
					if(neighbour[i] != T_FOREST && neighbour2[i] != T_FOREST) isedge=1;
				}
				if(isedge)
				{
					name = "Edge of Forest";
					desc = "You are at the edge of a forest.";
					if(random_chance(2)) desc += " The trees are slightly less dense here.";
				}
			}
		default:
			break;
	}
	//END OF SPECIAL CASES:

	if(SAMENSEW)
	{
		if (t != T_NONE && sameall[t]) {
	  		desc += " ";
			desc += sameall[t];
		}
		if(isobstruction(t))
		{
			for(int i=0;i<4;i++)
				done[i]=1;
		}
	} else if(SAMEN || SAMES || SAMEE || SAMEW) {
		string temp="???";
		if(SAMENS) {
			temp="north and south";
			if(SAMEE||SAMEW) {
				temp = "north, south and ";
				temp += SAMEE?"east":"west";
			}
		} else if(SAMEEW) {
			temp="east and west";
			if(SAMEN||SAMES){
				temp = SAMEN?"north":"south";
				temp += ", east and west";
			}
		} else {
			if(SAMEN) temp="north";
			if(SAMES) temp="south";
			if(SAMEE) if(SAMEN||SAMES) temp+= " and east"; else temp="east";
			if(SAMEW) if(SAMEN||SAMES) temp+= " and west"; else temp="west";
		}

		if (t != T_NONE && samenear[t]) {
		  desc += " ";
		  desc += ssprintf(samenear[t], temp.c_str());
		}
	}

	for(i=T_SEA;i<T_LAST;i++)
	{
	  	if (t == i)
		  continue;
		if (isroad(t) && isroad((Terrain)i))
		  continue;
		int foundcount=0;
		string where="";
		string last="";
		int plural=0;
		if(neighbour[0]==i) 
		{
			if(!foundcount)
				where="north";
			if(foundcount==1)
				last="north";
			if(foundcount>1) {
				where += ", ";
				where += last;
				last = "north";
			}
		
			foundcount++;
		}
		if(neighbour[3]==i) 
		{
			if(!foundcount)
				where="south";
			if(foundcount==1)
				last="south";
			if(foundcount>1) {
				where += ", ";
				where += last;
				last = "south";
			}
			foundcount++;
		}
		if(neighbour[2]==i) 
		{
			if(!foundcount)
				where="east";
			if(foundcount==1)
				last="east";
			if(foundcount>1) {
				where += ", ";
				where += last;
				last = "east";
			}
			foundcount++;
		}
		if(neighbour[1]==i) 
		{
			if(!foundcount)
				where="west";
			if(foundcount==1)
				last="west";
			if(foundcount>1) {
				where += ", ";
				where += last;
				last = "west";
			}
			foundcount++;
		}

		if(foundcount)
		{
			if(foundcount>1) {
				where += " and ";
				where += last;
				plural=1;
			} else {
				where += last;
				plural=0;
			}
			if (plural && plurnear[i]) {
			  desc += " ";
			  desc += ssprintf(plurnear[i], where.c_str());
			} else if (singnear[i]) {
			  desc += " ";
			  desc += ssprintf(singnear[i], where.c_str());
			}
		}
	}

	for(i=T_SEA;i<T_LAST;i++)
	{
	  	if (t == i)
		  continue;
		if (isroad(t) && isroad((Terrain)i))
		  continue;
		int foundcount=0;
		string where="";
		string last="";
		int plural=0;
		int j;
		for(j=0;j<4;j++)
		{
			if(neighbour2[j]==i && neighbour[j]!=neighbour2[j]) 
			{
				if(!done[j])
				{
					string temp = "";
					switch(j)
					{
						case 0:
							temp="north";
							break;
						case 1:
							temp="west";
							break;
						case 2:	
							temp="east";
							break;
						case 3:
							temp="south";
							break;
						default:
							temp="???";
							break;
					}

					if(!foundcount)
						where=temp;
					if(foundcount==1)
						last=temp;
					if(foundcount>1) {
						where += ", ";
						where += last;
						last = temp;
					}
					foundcount++;
				}
			}
		}
		if(foundcount)
		{
			if(foundcount>1) {
				where += " and ";
				where += last;
				plural=1;
			} else {
				where += last;
				plural=0;
			}
			if (plural && plurfar[i]) {
			  desc += " ";
			  desc += ssprintf(plurfar[i], where.c_str());
			} else if (singfar[i]) {
			  desc += " ";
			  desc += ssprintf(singfar[i], where.c_str());
			}
		}
	}
	//XXX END PASTE HERE

	if (WILD(wild, x, y).overlay & O_MAGLEV) {
	  desc += " MagLev.";
	}
#if 0
	if (WILD(wild, x, y).overlay & O_TUNNEL) {
	  desc += " Tunnel.";
	}
#endif

	o->setf("name", name.c_str());
	o->setf("desc", desc.c_str());

	o->set("terrain", terrain(wild,x,y));	

	MudObject *zo = wild->obj();
	if (zo) {
	  if (zo->get_flag(FL_HOT)) {
	    o->set_bflag(FL_HOT, 1);
	    o->set_bflag(FL_COLD, 0);
	    o->set_bflag(FL_TEMPEXTREME, 0);
	  }
	  if (zo->get_flag(FL_COLD)) {
	    o->set_bflag(FL_COLD, 1);
	    o->set_bflag(FL_HOT, 0);
	    o->set_bflag(FL_TEMPEXTREME, 0);
	  }
	  if (zo->get_flag(FL_TEMPEXTREME)) {
	    o->set_bflag(FL_COLD, 0);
	    o->set_bflag(FL_HOT, 0);
	    o->set_bflag(FL_TEMPEXTREME, 1);
	  }
	  if (zo->get_flag(FL_AIRLESS)) {
	    o->set_bflag(FL_AIRLESS, 1);
	  }
	}

	dotrap(E_ONWILDCREATE, zfor(wild), zfor(wild), o);
	/* ::: wild_create o1==the zone object, o2==the wilderness that has just had a description made from it; probably before exits are made. */
}

//! Normalise the x coordinate.
static void norm(Wilderness *wild, int &x)
{
  while (x<0) x += wild->sizex;
  while (x>=(int)wild->sizex) x -= wild->sizex;
}

//! Find out whether we can construct an exit from /fro/ to tx,ty in /dir/, and if so return the destintion.
static string towhere(Wilderness *wild, MudObject *fro, int fx, int fy, int fz, int tx, int ty, 
		      const char *dir) {
  Terrain from = terrain(wild, fx, fy);
  Terrain to = terrain(wild, tx, ty);

  norm(wild,fx);
  norm(wild,tx);

  if (fz==-2) {
    return whereis(wild, tx, ty, fz);
  }

  if (fz>0) {
    return whereis(wild, tx, ty, fz);
  }

  if (from==T_DOMEWALL||to==T_DOMEWALL) {
    return "";
  }
  
  if (from==T_SEA && to==T_SEA && fz==-1) {
    return whereis(wild, tx, ty, -1);
  }

  if (from==T_FRESHWATER && to==T_BRIDGE) {
    return whereis(wild, tx, ty, -1);
  }

  if (from==T_CLIFFTOP && to==T_SEA) {
    return "-"+whereis(wild, tx, ty, 0);
    // allow for fliers
  }

  if (from==T_SEA && to==T_CLIFFTOP) {
    return "-"+whereis(wild, tx, ty, 0);
    // allow for fliers
  }

  if (from==T_BRIDGE && fz == -1 && to != T_FRESHWATER && to != T_BRIDGE) {
    return "";
  }

  if (from==T_BRIDGE && fz == 0 && to == T_FRESHWATER) {
    return "-"+whereis(wild, tx, ty, 0);
    // allow for fliers
  }

  if (abs(fro->get_int("altitude")-(WILD(wild, tx, ty).alt))>1) {
    return "-"+whereis(wild, tx, ty, 0);
    // allow for fliers
  }

  string a = whereis(wild, tx, ty, 0);

  if (a[0] == '@' && to==T_BUILTUP) {
    return "";
  }

  if (a[0] != '@') {
    MudObject *w = planet->get(a.c_str());
    string ap = ssprintf("%spatch", dir);
    if (w && w->get_object(ap.c_str())) {
      a = w->get(ap.c_str());
    }
  }
  
  return a;
}

#define NUMMATCHES 10

//! Create the standard exits for w(x,y,z)
static void make_exits(MudObject *o, Wilderness *w, int x, int y, int z) {
  make_exit(o, "north", towhere(w, o, x, y, z, x, y-1, "north").c_str());
  make_exit(o, "south", towhere(w, o, x, y, z, x, y+1, "south").c_str());
  make_exit(o, "west", towhere(w, o, x, y, z, x-1, y, "west").c_str());
  make_exit(o, "east", towhere(w, o, x, y, z, x+1, y, "east").c_str());
}


//! The actual hook.  Given a tag, creates, populates and returns a new wilderness room, if possible.
static MudObject *make_wild(const char *tag) {
	if (!tag) return NULL;

	regmatch_t match[NUMMATCHES];
	/*
	* 0 = whole thing
	* 1 = wtag
	* 2 = x
	* 3 = y
	* 4 = +/-z
	* 5 = +/-
	* 6 = z
	*/
	int r = regexec(&tagmatch,tag,NUMMATCHES,match,0);
	if(r) {
		//log(PFL_SEEINFO,0,"wild", "regexec returned %d", r);
		return NULL;
	} else {
		/*
		for(int i=0;i<NUMMATCHES;i++) {
			if(match[i].rm_so == -1) continue;
			log(PFL_SEEINFO,0,"wild", "regexec: %s %d: %d %d",tag,i,match[i].rm_so,match[i].rm_eo);
		}
		*/
	}

	string wtag = string(tag+match[1].rm_so, match[1].rm_eo-match[1].rm_so);
	long x = atoi(tag+match[2].rm_so);
	long y = atoi(tag+match[3].rm_so);
	long z = 0;
	if(match[4].rm_so!=-1) {
		int sign = (tag[match[5].rm_so] == '-')?-1:1;
		z = sign * atoi(tag+match[6].rm_so);
	}

	Wilderness *wild = tag2l(wtag.c_str());
	if(!wild) {
		return NULL;
	}

	string where = whereis(wild, x, y, z);
	if (where == "" || where[0] != '@') {
		return NULL;
	}

	//log(PFL_NONE,0,"wild", "make_wild: making %s",tag);
	MudObject *o = new MudObject(tag);
	o->set("zone", ztag(wild));
	o->set_bflag(FL_WILDERNESS, 1);
	o->set_bflag(FL_STICKYROOM, 1);
	o->set_bflag(FL_OUTDOORS, 1);
	planet->add(*o);

	o->set("wild.x", x);
	o->set("wild.y", y);
	o->set("wild.z", z);
	o->set("wild.tag", wtag);

	descit(wild, o, x, y, z);

	MudObject *zn = zfor(wild);
	if (zn) {
		set_owner(o, zn);
		o->set("start", zn->id);
	}

	make_exits(o, wild, x, y, z);

	return o;
}

const char *compass_key[] =
  {
    "  ",        "N ",               "  ",
    "  ",        "^#x2191; ",        "  ",
    "W^#x2190;", "^#x25ef;^#x2192;", "E ",
    "  ",        "^#x2193; ",        "  ",
    "  ",        "S ",               "  ",    
  };

const char *singnear[T_LAST] = {
  "You can see the ocean to the %s of here.", // sea
  "You can see beach to the %s of here.", // beach
  "There is grassland to the %s of here.", // grass
  "You can see a built-up area to the %s of here.", // built
  "You can see trees to the %s of here.", // forest
  "You can see a gateway to the %s of here.", // gateway
  "There is a road to the %s of here.", // road
  "You can see water to the %s of here.", // water
  "You can see a bridge to the %s of here.", // bridge
  "You can see scrub to the %s of here.", // scrub
  "There is a swamp to the %s of here.", // swamp
  "There is a cliff-face to the %s of here.", // cliff
  "There is desert to the %s of here.", // desert
  "There is prairie to the %s of here.",  // prairie
  "There is ice to the %s of here.", // ice
  "There is a rocky plain to the %s of here.",   // rocky plain
  "There is rocky high ground to the %s of here.", // high ground
  "There is a cliff to the %s of here.", // dry cliff
  "There is a patch of rocky ground to the %s of here.", // outcrops
  "There is a white gypsum plain to the %s of here.", //gplain
  "There is a dry riverbed to the %s of here.", //dryriver
  "There is a dome wall to the %s of here.", //domewall
};


const char *plurnear[T_LAST] = {
  NULL,
  NULL,
  NULL,
  "You can see built-up areas to the %s of here.",
  NULL,
  "You can see gateways to the %s of here.",
  "There are roads to the %s of here.",
  NULL,
  "You can see bridges to the %s of here.",
  NULL, // scr
  NULL, // swam
  "There are cliff-faces to the %s of here.",
  NULL, // desert
  NULL, // praire
  NULL, // ice
  NULL, // plain
  NULL, // rocks
  "There are cliffs to the %s of here.",
  "There are patches of rocky ground to the %s of here.",
  NULL, //gplain
  NULL, //dryriver
  NULL, //domewall
};

const char *singfar[T_LAST] = {
  "You can see the ocean in the distance to the %s of here.",
  "You can see beach in the distance to the %s of here.",
  "There is grassland in the distance to the %s of here.",
  "You can see a built-up area in the distance to the %s of here.",
  "You can see trees in the distance to the %s of here.",
  "You can see a gateway in the distance to the %s of here.",
  "There is a road in the distance to the %s of here.",
  "You can see water in the distance to the %s of here.",
  "You can see a bridge in the distance to the %s of here.",
  "You can see scrub in the distance to the %s of here.",
  "There is a swamp in the distance to the %s of here.",
  "There is a cliff-face in the distance to the %s of here.",
  "There is desert in the distance to the %s of here.",
  "There is prairie in the distance to the %s of here.",
  "There is ice in the distance to the %s of here.",
  "There is a rocky plain in the distance to the %s of here.",  
  "There is rocky high ground in the distance to the %s of here.",
  "There is a cliff in the distance to the %s of here.",
  "There is a patch of rocky ground in the distance to the %s of here.",
  "There is a white gypsum plain in the distance to the %s of here.",
  "There is a dry riverbed in the distance to the %s of here.",
  "You can see a dome wall in the distance to the %s of here.",
};


const char *plurfar[T_LAST] = {
  NULL,
  NULL,
  NULL,
  "You can see built-up areas in the distance to the %s of here.",
  NULL,
  "You can see gateways in the distance to the %s of here.",
  "There are roads in the distance to the %s of here.",
  NULL,
  "You can see bridges in the distance to the %s of here.",
  NULL, // scr
  NULL, // swam
  "There are cliff-faces in the distance to the %s of here.",
  NULL, // desert
  NULL, // praire
  NULL, // ice
  NULL, // plain
  NULL, // rocks
  "There is a cliff in the distance to the %s of here.",
  "There are patches of rocky ground in the distance to the %s of here.",
  NULL, // gplain
  NULL, // dryriver
  NULL, // domewall
};

const char *samenear[T_LAST] = {
  "There is more sea to the %s of here.",
  "There is more beach to the %s of here.",
  "There is more grassland to the %s of here.",
  NULL, /* builtup */
  "There is more forest to the %s of here.",
  NULL, /* gateway */
  "The road continues %s from here.", /* road */
  "The water continues to the %s of here.",
  "The road continues %s from here.", /* bridge */
  "There is more scrub to the %s of here.",
  "The swamp continues to the %s.",
  NULL, /* clifftop */
  "The desert continues to the %s.",
  "The prairie continues to the %s.",
  "The ice continues to the %s.",
  "The plain continues to the %s.",
  "The plain continues to the %s.",
  "The cliff continues to the %s.",
  "The there are more rocky outcrops to the %s of here.",
  "The gypsum plain continues to the %s.",
  "The dry riverbed continues to the %s.",
  "The dome wall continues to the %s.",
};

const char *sameall[T_LAST] = {
  "There is no land anywhere near you.",
  "You are surrounded by sand.",
  "The grassland extends around you on all sides.",
  NULL, /* builtup */
  "Trees surround you on all sides and prevent you seeing anything beyond them.",
  NULL, /* gateway */
  NULL, /* road */
  "The water extends around you on all sides.",
  NULL, /* bridge */
  "The scrub extends around you on all sides.",
  "The swamp extends around you on all sides.",
  NULL, /* clifftop */
  "The desert extends around you on all sides.",
  "The prairie extends around you on all sides.",
  "The ice extends around you on all sides.",
  "The plain extends around you on all sides.",
  "The rocks extend around you on all sides.",
  NULL,
  "The rocky outcrops surround you on all sides.",
  "The gypsum plain extends away in all directions.",
  "The dry riverbed extends away in all directions.",
  "The dome wall extends away in all directions.",
};

//! Terrain traits.
struct terraininfo {
  //! default altitude for this type of terrain
  int normal_alt;

  //! the usual name
  const char *shortname;

  //! a longer alternative name (optional)
  const char *longname;

  //! the normal map graphic
  const char *gr;

  //! the alternate map graphic (optional)
  const char *agr;
} tinfo[T_LAST] = {
  { 0, "sea", NULL, "^C::", }, // sea
  { 1, "beach", NULL, "^y;;", }, // beach
  { 2, "grass", "wilderness", "^g..", }, // grass
  { 2, "town", "built-up", "^R##", }, // builtup
  { 2, "forest", NULL, "^G^^^^", }, // forest
  { 2, "zone", "gateway", "^W[]", }, // gateway
  { 2, "road", NULL, "^R__"}, // road
  { 1, "river", NULL, "^B::", }, // fresh
  { 1, "bridge", NULL, "^B:^R_"}, // bridge
  { 2, "scrub", NULL, "^Goo"}, // scrub
  { 1, "swamp", NULL, "^B_^G_", "^G_^B_" }, // swamp
  { 2, "cliff", "clifftop", "^C:^W|"}, // clifftop
  { 2, "desert", NULL,"^Y::"}, // desert
  { 2, "prairie", NULL, "^g%%"}, // prairie
  { 1, "ice", NULL, "^W##"}, // ice
  { 2, "plain", "rocky plain", "^r,,"}, // rocky plain
  { 4, "rocks", "rocky ground", "^r::"}, // high ground
  { 4, "cliff", "drycliff", "^w|^^"}, // dry cliff
  { 3, "outcrop", "rocky outcrop", "^w^^^^"}, // rocky outcrops
  { 2, "gplain", "gypsum plain", "^W::"}, //gypsum plain
  { 1, "dryriver", "dry riverbed", "^w,,"}, //dry riverbed
  { 2, "domewall", "dome wall", "^w%%"} //dome wall
  
};

terraininfo gettinfo(int i)
{
  if (i < 0 || i > T_LAST)
    return (terraininfo){0, "unknown", 0, "^W??" };
  return tinfo[i];
}

string overlay(const char *str, const char *c1, const char *pre);

inline int X(Wilderness *w, int x) {
  while (x >= (int)w->sizex)
    x -= w->sizex;

  while (x < 0)
    x += w->sizex;

  return x;
}

static bool verb_bigmap(MudObject *who, int argc, const char **argv) {
  MudObject *where = who->owner;

  if (where->get_object("ship")) {
    where = where->get_object("ship")->owner;
  }

  Wilderness *wild = tag2l(where->get("wild.tag"));
  int x = where->get_int("wild.x");
  int y = where->get_int("wild.y");

  if (x == -1 || y == -1 || !wild) {
    who->printf("There is no map available for the current area.\n");
    return true;
  }

  int xmin = columns(who)/4 - 2;
  int xmax = columns(who)/4 - 1;

  int ymin = rows(who)/2 - 2;
  int ymax = rows(who)/2 - 2;

  enum {
    MODE_NORM,
    MODE_ALT,
    MODE_TUNNEL,
  } mode = MODE_NORM;

  if (streq(argv[1], "alt"))
    mode = MODE_ALT;
  
  if (streq(argv[1], "tunnel"))
    mode = MODE_TUNNEL;

  int needh = 0;
  for (int mx=x-xmin;mx<x+xmax;mx++) {
    if ((X(wild, mx)/100)%10) {
      needh = 1;
      break;
    }
  }

  if (needh) {
    who->printf("    ");
    for (int mx=x-xmin;mx<x+xmax;mx++) {
      who->printf("^W%i ", (X(wild, mx)/100)%10);
    }
    who->printf("\n");
  } else {
    ymin++;
  }

  who->printf("    ");
  for (int mx=x-xmin;mx<x+xmax;mx++) {
    who->printf("^W%i ", (X(wild, mx)/10)%10);
  }
  who->printf("\n");

  who->printf("    ");
  for (int mx=x-xmin;mx<x+xmax;mx++) {
    who->printf("^W%i ", (X(wild, mx)/1)%10);
  }
  who->printf("\n");

  for (int my=y-ymin;my<y+ymax;my++) {
    string buffer = "";
    
    if (my < 0 || my > (int)wild->sizey)
      buffer += ssprintf("    ", my);
    else
      buffer += ssprintf("^W%3i^n ", my);

    for (int mx=x-xmin;mx<x+xmax;mx++) {
      const char *thing = "^w  ", *olay_pre=0, *olay_post=0;
      if (my == y && mx == x)
	olay_pre = "^Wo";

      if (mode == MODE_NORM) {
	Terrain th=terrain(wild, mx, my);
	if (my & 1)
	  thing = gettinfo(th).gr;
	else
	  thing = gettinfo(th).agr?gettinfo(th).agr:gettinfo(th).gr;
      }

      if (mode == MODE_ALT) {
	  switch (WILD(wild, mx, my).alt) {
	  case 0 : thing = "^B__"; break;
	  case 1 : thing = "^Y__"; break;
	  case 2 : thing = "^g--"; break;
	  case 3 : thing = "^G--"; break;
	  case 4 : thing = "^R¯¯"; break;
	  case 5 : thing = "^M¯¯"; break;
	  default: thing = "^n??";
	  }
      }

      if (mode == MODE_TUNNEL) {
	if (WILD(wild, mx, my).overlay & O_TUNNEL) {
	  thing = "^W##";
	}

	string s = whereis(wild, mx, my, -2);
	if (s.length() && s[0] != '@')
	  thing = "^R[]";
      }

      buffer += overlay(thing, olay_post, olay_pre);
    }
    who->printf("%s\n", buffer.c_str());
  }
  return true;
}


struct splitresult {
  char cols[2];
  char chars[2];
};

splitresult split(const char *b)
{
  splitresult a;
  int i = 0;

  while (*b) {
    
    if (i==2)
      return a;

    if (b[0]=='^' && b[1]!='^') {
      a.cols[i] = b[1];
      if (!i)
	a.cols[1] = b[1];

      b += 2;
      continue;
    }

    if (b[0]=='^' && b[1]=='^') {
      a.chars[i] = b[1];
      i++;
      b += 2;
      continue;
    }

    a.chars[i] = *b;
    i++;

    b++;
  }

  return a;
}

string overlay(const char *str, const char *c1, const char *pre)
{
  splitresult b = split(str);
  string s;

  if (c1 && pre) {
    s += pre;
    s += c1;
    return s;
  }

  if (c1) {
    s += "^";
    s += b.cols[0];
    s += b.chars[0];
    if (b.chars[0]=='^')
      s += b.chars[0];
    s += c1;
    return s;
  }

  if (pre) {
    s += pre;
    s += "^";
    s += b.cols[1];
    s += b.chars[1];
    if (b.chars[1]=='^')
      s += b.chars[1];
    return s;
  }

  return str;
}


//! Shows to the user an overhead map of the area of wilderness they are in. Will call BEFOREMAP trap, also send files mentioned in "map" property of the owner in preference to doing this.
static bool verb_map(MudObject *who, int argc, const char **argv) {

  if (!who->owner) return 1;

  MudObject *where = who->owner;
  if (where->get_object("ship")) {
    where = where->get_object("ship")->owner;
  }

  if (!where) return 1;

  if (dotrap(E_BEFOREMAP, who, where)) {
    /* ::: before_map o1==where; return 1 to not do any map handling */
    return true;
  }

  int x = where->get_int("wild.x");
  int y = where->get_int("wild.y");

  const char *mp = where->owner?where->owner->get("map"):0;
  if (where->get("map")) {
    mp = where->get("map");
  }

  Wilderness *wild = tag2l(where->get("wild.tag"));

  if (!wild || x==-1 || y==-1)
    if (mp && !streq(mp, "-")) {
      if (who->spewfile(DATA_INFO, mp, false)) return true;
    }

  if (x == -1 || y == -1 || !wild) {
    who->printf("There is no map available for the current area.\n");
    return true;
  }
  
  int my;
  int mx;

  int aa = 0;

  set<Terrain> t;
  list<string> s;

  for (my=y-10;my<y+11;my++) {
    for (mx=x-10;mx<x+11;mx++) {
      if (((my-y)*(my-y)+(mx-x)*(mx-x))<=105) {
	Terrain th=terrain(wild, mx, my);
	if (t.find(th)==t.end())
	  t.insert(th);
      }
    }
  }

  int wildkeylength = 2;
  s.insert(s.end(), "         ^/^-^-^-^-^\\");
  for (set<Terrain>::iterator i=t.begin();i!=t.end();i++) {
    s.insert(s.end(), ssprintf("%8s ^| %s^n ^|", gettinfo(*i).shortname, gettinfo(*i).gr));
    wildkeylength++;
  }
  s.insert(s.end(), "         ^[^-^-^-^-^]");

  list<string>::iterator si = s.begin();

  int wildkeystart = 12-wildkeylength/2;
    
  for (my=y-10;my<y+11;my++) {
    int ry=(my-y)+10;

    if (my==y)
      who->send_data();
    
    aa++;
    
    string buffer = "";

    for (mx=x-10;mx<x+11;mx++) {
      int rx=(mx-x)+10;

      if (((my-y)*(my-y)+(mx-x)*(mx-x))>105) {
	if (rx<3 && ry<5) {
	  buffer += compass_key[(ry * 3) + rx];
	}
	else {
	  buffer += "  ";
	}
	continue;
      }

      const char *olay_pre = 0, *olay = 0;

      if (mx==x && my==y) {
	olay_pre = "^Wo";
      }

      if (!olay_pre && IS_CAPTAIN(who)) {
	int d = 0;
	MudObject *o;
	int i;
	foreach(players, o, i) if (o != who && o->owner) {
	  if (!streq(o->owner->get("wild.tag"), wild->id))
	    continue;
	  int px = o->owner->get_int("wild.x");
	  int py = o->owner->get_int("wild.y");
	  if (px != -1 && py != -1 && px == mx && py == my) {
	    buffer += "^C";
	    buffer += toupper(o->id[0]);
	    buffer += tolower(o->id[1]);
	    d = 1;
	    break;
	  }
	}
	if (d)
	  continue;
      }

      const char *ter = "??";

      if (WILD(wild, mx, my).overlay & O_MAGLEV) {
	olay = "^W|";
      }
#if 0      
      if (WILD(wild, mx, my).overlay & O_TUNNEL) {
	olay = "^w.";
      }
#endif      
      Terrain th=terrain(wild, mx, my);
      if (th >= T_SEA && th < T_LAST)
	if (my & 1)
	  ter = gettinfo(th).gr;
	else
	  ter = gettinfo(th).agr?gettinfo(th).agr:gettinfo(th).gr;

      if (olay || olay_pre)
	buffer += overlay(ter, olay, olay_pre);
      else
	buffer += ter;
    }
    
    int wildl = aa - wildkeystart;
    if (wildl >= 0 && wildl < wildkeylength) {
      who->printf("%s ^n%s\n", buffer.c_str(), si->c_str());
      si++;
    } else {
      who->printf("%s\n", buffer.c_str());
    }
  }
  return true;
}


//! Explicitly save the wilderness
static bool verb_wildwrite(MudObject *who, int argc, const char **argv)
{
	Wilderness *w;
	int i;
	foreach(wilderni,w,i)
		if(w->needssave) {
			who->printf("Saving %s\n",w->id);
			w->save();
		}
	return true;
}

//! internal command executed by a wilderness object, to delete itself if safe
static bool verb_timeout(MudObject *who, int argc, const char **argv)
{
  if (!who->get_flag(FL_WILDERNESS))
    return true;
  if ((now - who->reftime) < 60) {
    who->printf("You were last referenced less than a minute ago.\n");
    who->reftime = now;
    return true;
  }
  MudObject *o;
  int i;
  foreach(who->children, o, i) {
    if (!o->get_flag(FL_TRANSIENT)) 
      {
	who->printf("You are occupied by a non-instrinsic!\n");
	return true;
      }
  }


  who->printf("Ok, looks like we can say goodbye to you.\n");
  //log(PFL_NONE, 0, "wild", "Killing %s and exits.", who->id);
  foreach(who->children, o, i) {
    o->nuke_me = 1;
  }
  who->nuke_me = 1;
  return true;
}

//! update room desc and exits of wild(x,y,z) and all its neighbours
static void update_and_neighbours(Wilderness *wild, MudObject *o, int x, int y, int z, int rec=0) {
  if (o->get_flag(FL_WILDERNESS)) {
    descit(wild, o, x, y, z);
    make_exits(o, wild, x, y, z);
  }

  if (!rec) {
    MudObject *o;

    o = planet->rawget(whereis(wild, x+1, y, z).c_str());
    if (o && o->get_flag(FL_WILDERNESS))
      update_and_neighbours(wild, o, x+1, y, z, 1);

    o = planet->rawget(whereis(wild, x-1, y, z).c_str());
    if (o && o->get_flag(FL_WILDERNESS))
      update_and_neighbours(wild, o, x-1, y, z, 1);

    o = planet->rawget(whereis(wild, x, y+1, z).c_str());
    if (o && o->get_flag(FL_WILDERNESS))
      update_and_neighbours(wild, o, x, y+1, z, 1);

    o = planet->rawget(whereis(wild, x, y-1, z).c_str());
    if (o && o->get_flag(FL_WILDERNESS))
      update_and_neighbours(wild, o, x, y-1, z, 1);
  }
}

Terrain gettype(const char *a)
{
  for (Terrain i=T_FIRST;i<T_LAST;i++) {
    if ((strncmp(a, tinfo[i].shortname, strlen(a))==0) ||
	(tinfo[i].longname && (strncmp(a, tinfo[i].longname, strlen(a))==0))) {
      return i;
      break;
    }
  }
  return T_NONE;
}

//! change the terrain type of current loc
static bool verb_form(MudObject *who, int argc, const char **argv)
{
  if (argc < 2) {
    for (int i=0;i<T_LAST;i++) {
      who->printf("%i %10s %s^n\n", tinfo[i].normal_alt, 
		  (tinfo[i].shortname&&(i!=T_DRYCLIFF))?tinfo[i].shortname:tinfo[i].longname,
		  tinfo[i].gr);
    }
    return 1;
  }

  int now = 0;
  int towhat = 0;

  if (streq(argv[1], "now")) {
    now = 1;
  }
  
  if (!now) {
    towhat = -1;
    for (int i=0;i<T_LAST;i++) {
      if ((strncmp(argv[1], tinfo[i].shortname, strlen(argv[1]))==0) ||
	  (tinfo[i].longname && (strncmp(argv[1], tinfo[i].longname, strlen(argv[1]))==0))) {
	towhat = i;
	break;
      }
    }
    if (towhat == -1) {
      who->printf("What type of terrain?\n");
      return true;
    }
  }

  if (!who->owner) 
    return 1;

  int x = who->owner->get_int("wild.x");
  int y = who->owner->get_int("wild.y");

  if (x == -1 || y == -1) 
    return 1;

  Wilderness *wild = tag2l(who->owner->get("wild.tag"));
  if (!wild)
    return true;

  who->printf("You terraform here.\n");
  who->oprintf(cansee, "%#M terraforms here.\n", who);
  
  if (!now) {
    WILD(wild, x, y).t = (Terrain)towhat;
    WILD(wild, x, y).d = D_NONE;
    WILD(wild, x, y).alt = tinfo[towhat].normal_alt;
    wild->needssave = 1;
  }

  MudObject *o = planet->get(whereis(wild, x, y, 0));
  if (o && o->get_flag(FL_WILDERNESS)) { 
    update_and_neighbours(wild, o, x, y, 0);
  }

  return true;
}


//! change altitude of current loc
static bool verb_alt(MudObject *who, int argc, const char **argv)
{
  if (argc < 2) {
    who->printf("Make the altitude how many metres?\n");
    return 1;
  }
  
  int alt = atoi(argv[1]);

  int x = who->owner->get_int("wild.x");
  int y = who->owner->get_int("wild.y");

  if (x == -1 || y == -1) 
    return 1;  

  Wilderness *wild = tag2l(who->owner->get("wild.tag"));
  if (!wild)
    return true;

  WILD(wild, x, y).alt = alt;
  wild->needssave = 1;

  MudObject *o = planet->get(whereis(wild, x, y, 0));
  if (o && o->get_flag(FL_WILDERNESS)) { 
    update_and_neighbours(wild, o, x, y, 0);
  }
  return true;
}

//! change direction of flow of current loc
static bool verb_flow(MudObject *who, int argc, const char **argv)
{
  if (argc < 2) {
    who->printf("Flow in what direction?\n");
    return 1;
  }

  Dir towhat = getdir(argv[1]);

  if (!who->owner) 
    return 1;

  int x = who->owner->get_int("wild.x");
  int y = who->owner->get_int("wild.y");

  if (x == -1 || y == -1) 
    return 1;

  who->printf("You terraform here.\n");
  who->oprintf(cansee, "%#M terraforms here.\n", who);
  
  Wilderness *wild = tag2l(who->owner->get("wild.tag"));
  if (!wild)
    return true;

  WILD(wild, x, y).d = towhat;
  wild->needssave = 1;

  MudObject *o = planet->get(whereis(wild, x, y, 0));
  if (o && o->get_flag(FL_WILDERNESS)) { 
    update_and_neighbours(wild, o, x, y, 0);
  }
  
  return true;
}

//! dive below the water
static bool verb_dive(MudObject *who, int, const char **) 
{
  MudObject *where = who->owner;
  MudObject *seabed = where->get_object("seabed");

  if (!seabed) {
    who->printf("You can't dive here.\n");
    return true;
  }

  if (MudObject *m=mount(who)) {
    who->printf("You can't dive whilst riding %Y.\n", m);
    return true;
  }

  if (!wf_wears_with_flag(who, FL_GILLS)) {
    who->printf("You can't swim underwater.\n");
    return true;
  }

  if (!seabed) {
    if (seabed->get_flag(FL_BARUP)) {
      who->printf("It's too shallow to dive here.\n");
      return true;
    }
  }

  who->printf("You dive below the surface.\n");
  who->oprintf(cansee, "%#M dives below the surface.\n", who);


  set_owner(who, seabed);
  who->oprintf(cansee, "%#M arrives from the surface.\n", who);
  who->interpret("look");
  return true;
}

//! surface above the water
static bool verb_surface(MudObject *who, int, const char **) 
{
  MudObject *where = who->owner;
  MudObject *surface = where->get_object("surface");

  if (!surface) {
    who->printf("You can't surface here.\n");
    return true;
  }

  if (MudObject *m=mount(who)) {
    who->printf("You can't surface whilst riding %Y.\n", m);
    return true;
  }

  who->printf("You rise up to the surface.\n");
  who->oprintf(cansee, "%#M rises up to the surface.\n", who);

  set_owner(who, surface);
  who->oprintf(cansee, "%#M arrives from below the surface.\n", who);
  who->interpret("look");
  return true;
}

//! turns d (in degrees) into a broad compass direction.
static const char *compass(int d) 
{
  if (d < 0)
    d += 360;
  if (d > 360)
    d -= 360;

  if (d==0) return "due north";
  if (d==180) return "due sorth";
  if (d==90) return "due east";
  if (d==270) return "due west";

  if (d < 22+(46*0)) return "north";
  if (d < 22+(45*1)) return "north-east";
  if (d < 22+(45*2)) return "east";
  if (d < 22+(45*3)) return "south-east";
  if (d < 22+(45*4)) return "south";
  if (d < 22+(45*5)) return "south-west";
  if (d < 22+(45*6)) return "west";
  if (d < 22+(45*7)) return "north-west";
  return "north";

}

//! angle, distance, and name of a landmark
struct landmark_pos {
  int angle;
  int dist;
  string of;
  
  bool operator< (const struct landmark_pos &w) const {
    return dist < w.dist || (dist == w.dist && angle < w.angle);
  }
};


//! convert distance in wilderness grids to english.
static const char *dist(int dist)
{
  if (dist < 3)
    return "just ";
  if (dist < 7)
    return "";
  if (dist < 11)
    return "quite a way ";
  return "a long way ";
}

//! obtain a random piece of road.
static MudObject *random_road(const char *desc) {
  const char *wtag = "wild";
  Wilderness *wild = tag2l(wtag);
  unsigned int roads = 0;
  for (unsigned int x=0;x<wild->sizex;x++) for (unsigned int y=0;y<wild->sizey;y++) {
    if (terrain(wild, x, y)==T_ROAD) {
      roads++;
    }
  }
  
  unsigned int x=0, y=0, r = 0;

  roads = random_number(roads);
  for (unsigned int mx=0;mx<wild->sizex;mx++) 
    for (unsigned int my=0;my<wild->sizey;my++) {
    if (terrain(wild, mx, my)==T_ROAD) {
      if (roads==r) {
	x = mx; 
	y = my;
      }
      r++;
    }
  }
  MudObject *where = planet->get(ssprintf("@wild_%i:%i", x, y).c_str());
  if (where)
    return where;
  return 0;
}

float atan2(int a, int b)
{
  return ::atan2((float)a, (float)b);
}

float sqrt(int a)
{
  return ::sqrt((float)a);
}

//! given a wilderness location, describe where it is in relation to landmarks.
static string describe_location(MudObject *where, int max) {

  int x = where->get_int("wild.x");
  int y = where->get_int("wild.y");

  if (x == -1) return "nowhere";
  if (y == -1) return "nowhere";

  Wilderness *w = tag2l(where->get("wild.tag"));

  if (!w)
    return "nowhere";

  MudObject *o = zfor(w);

  vector<landmark_pos> wl;

  Object::strit i = o->strs.begin();
  while (i != o->strs.end()) {
    if (endswith(i->first.c_str(), "_placename") && strchr(i->first.c_str(), ':')) {
      int ix = atoi(i->first.c_str());
      int iy = atoi(strchr(i->first.c_str(), ':')+1);
      
      landmark_pos ww;
      ww.angle = (int)(atan2(x-ix, iy-y) * 180 / M_PI);
      ww.dist = (int)sqrt(((x-ix)*(x-ix))+((y-iy)*(y-iy)));

      ww.of = i->second;

      wl.push_back(ww);
    }
    i++;
  }

  sort(wl.begin(), wl.end());

  string sofar = "";

  vector<landmark_pos>::iterator wit = wl.begin();
  while (wit != wl.end()) {
    if (sofar.length())
      sofar += ", ";
    sofar += ssprintf("%s%s of %s", dist(wit->dist), compass(wit->angle), wit->of.c_str());
    wit++;
    max--;
    if (!max)
      break;
  }
  return sofar;
}

//! create wilderness 
static bool verb_makewild(MudObject *who, int argc, const char **argv)
{
  if (argc != 6) {
    who->printf("makewild tag x y zone base.\n");
    return true;
  }
  
  int x = atoi(argv[2]);
  if (x < 2 || x > 400) {
    who->printf("x must be between 2 and 400.\n");
    return true;
  }

  int y = atoi(argv[3]);
  if (y < 2 || y > 400) {
    who->printf("y must be between 2 and 400.\n");
    return true;
  }

  MudObject *z = planet->get(argv[4]);
  if (!z) z = getzone(argv[4]);
  if (!z || z->owner != mud || !z->get("zone")) {
    who->printf("%s doesn't exist or is not a zone.\n", argv[4]);
    return true;
  }

  const char *w = argv[1];
  int bad = 0;
  while (*w) {
    if (!isalpha(*w))
      bad = 1;
    w++;
  }

  if (strlen(argv[1])!=4 || bad) {
    who->printf("Wilderness tags must be 4 letters.\n");
    return true;
  }

  int towhat = -1;
  for (int i=0;i<T_LAST;i++) {
    if ((strncmp(argv[5], tinfo[i].shortname, strlen(argv[5]))==0) ||
	(tinfo[i].longname && (strncmp(argv[5], tinfo[i].longname, strlen(argv[5]))==0))) {
      towhat = i;
      break;
    }
  }
  if (towhat == -1) {
    who->printf("What type of terrain?\n");
    return true;
  }

  Wilderness *s = tag2l(argv[1]);
  if (s) {
    who->printf("A wilderness called '%s' already exists.\n", argv[1]);
    return true;
  }

  s = new Wilderness(argv[1], 
		     x,
		     y,
		     (Terrain)towhat,
		     tinfo[towhat].normal_alt,
		     z->id);
  wilderni->add(*s);
  s->save();
  log(PFL_SEEINFO, 0, "wild", "created %s", argv[1]);
  return true;
}

//! set a wilderness property
static bool verb_setwild(MudObject *who, int argc, const char **argv)
{
  if (!argv[1]) {
    who->printf("Set which wilderness?\n");
    return true;
  }

  Wilderness *w = tag2l(argv[1]);
  if (!w) {
    who->printf("No such wilderness.\n");
    return true;
  }

  if (argc<4) {
    who->printf("Set which property?\n");
    return true;
  }

  if (!streq(argv[2], "airless")) {
    who->printf("Only property supported at the moment is 'airless'.\n");
    return true;
  }

  if (yes(argv[3])) {
    who->printf("Airless set.\n");
    w->hasair = 0;
  } else if (no(argv[3])) {
    who->printf("Airless unset.\n");
    w->hasair = 1;
  } else {
    who->printf("Set it to what?\n");
  }

  return true;
}

//! delete wilderness 
static bool verb_killwild(MudObject *who, int argc, const char **argv)
{
  if (argc < 2) {
    who->printf("Delete which wilderness?\n");
    return true;
  }

  const char *w = argv[1];
  int bad = 0;
  while (*w) {
    if (!isalpha(*w))
      bad = 1;
    w++;
  }

  if (strlen(argv[1])!=4 || bad) {
    who->printf("Wilderness tags must be 4 letters.\n");
    return true;
  }

  Wilderness *s = tag2l(argv[1]);
  if (!s) {
    who->printf("No such wilderness.\n");
    return true;
  }
  
  wilderni->remove(*s);
  s->erase();
  delete s;
  
  return true;
}

static void docleanup() {
	PREFAB_OBJECT = NULL;
	Wilderness *w;
	int i;
	foreach(wilderni,w,i) {
		delete w;
	}
	delete wilderni;
	DESCRIBE_LOCATION=NULL;
	RANDOM_ROAD=NULL;
	regfree(&tagmatch);
}

class WildLoc {
public:
  Wilderness *w;
  int x, y, z;
  MudObject *where;
  
  WildLoc(MudObject *where) : where(where) {
    w = tag2l(where->get("wild.tag"));
    if (!w)
      throw new emsg("You aren't in a wilderness.");
    
    x = where->get_int("wild.x");
    y = where->get_int("wild.y");
    z = where->get_int("wild.z");
  }

  void update() {
    MudObject *o = planet->get(whereis(w, x, y, 0));
    if (o && o->get_flag(FL_WILDERNESS)) {
      update_and_neighbours(w, o, x, y, 0);
    }
  }
};

bool verb_rail(MudObject *o, int argc, const char **argv)
{
  WildLoc w(o->owner);
  WILD(w.w, w.x, w.y).overlay ^= O_MAGLEV;

  w.update();

  w.w->needssave = 1;
  return true;
}

bool verb_tunnel(MudObject *o, int argc, const char **argv)
{
  WildLoc w(o->owner);
  WILD(w.w, w.x, w.y).overlay ^= O_TUNNEL;

  w.update();

  w.w->needssave = 1;
  return true;
}

bool verb_formrect(MudObject *o, int argc, const char **argv)
{
  if (argc < 4) {
    o->printf("Formrect x-size y-size type.\n");
    return true;
  }

  int xdim = atoi(argv[1]);

  if (xdim < 1) {
    o->printf("x-size must be at least 1.\n");
    return true;
  }

  int ydim = atoi(argv[2]);

  if (ydim < 1) {
    o->printf("y-size must be at least 1.\n");
    return true;
  }

  Terrain t = gettype(argv[3]);
  if (t == T_NONE) {
    o->printf("terrain invalid.\n");
    return true;
  }

  WildLoc w(o->owner);

  for (int x=w.x;x<w.x+xdim;x++) {
    for (int y=w.y;y<w.y+ydim;y++) {
      if (y<0)
	continue;
      if (y>=(int)w.w->sizey)
	continue;

      WILD(w.w, x, y).t = t;
      WILD(w.w, x, y).d = D_NONE;
      WILD(w.w, x, y).alt = tinfo[t].normal_alt;
    }
  }

  for (int x=w.x;x<w.x+xdim;x++) {
    for (int y=w.y;y<w.y+ydim;y++) {
      if (y<0)
	continue;
      if (y>=(int)w.w->sizey)
	continue;
      string s = whereis(w.w, x, y, 0);
      if (MudObject *o=planet->rawget(s.c_str())) {
	update_and_neighbours(w.w, o, x, y, 0);
      }
    }
  }

  w.w->needssave = 1;

  return true;
}

bool verb_wilds(MudObject *who, int argc, const char **argv)
{
  Divert d(who, "wilds");
  Header h(who, "Wildernesses Loaded", 1);
  
  Wilderness *w;
  int i;
  who->printf("%-4s %-3s %-3s %-3s %-10s %s\n", "Tag", "X", "Y", "Air", "Terrain", "Zone");
  foreach(wilderni, w, i) {
    who->printf("%-4s %3ix%3i %-3s %-10s %s\n", 
		w->id, 
		w->sizex, 
		w->sizey,
		w->hasair?"Yes":"No",
		gettinfo(w->baseterrain).shortname, 
		w->zoneobject);
  }
  return true;
}

template<class T> T sqr(T a) { return a * a; }

bool verb_formcircle(MudObject *o, int argc, const char **argv)
{
  if (argc < 3) {
    o->printf("Formcircle radius type.\n");
    return true;
  }

  int dim = atoi(argv[1]);

  if (dim < 1) {
    o->printf("x-size must be at least 1.\n");
    return true;
  }

  Terrain t = gettype(argv[2]);
  if (t == T_NONE) {
    o->printf("terrain invalid.\n");
    return true;
  }

  WildLoc w(o->owner);

  for (int x=w.x-dim;x<=w.x+dim;x++) {
    for (int y=w.y-dim;y<=w.y+dim;y++) {

      if (sqrt(sqr(x-w.x)+sqr(y-w.y))>dim)
	continue;

      if (y<0)
	continue;
      if (y>=(int)w.w->sizey)
	continue;

      WILD(w.w, x, y).t = t;
      WILD(w.w, x, y).d = D_NONE;
      WILD(w.w, x, y).alt = tinfo[t].normal_alt;
    }
  }

  for (int x=w.x-dim;x<w.x+dim;x++) {
    for (int y=w.y-dim;y<w.y+dim;y++) {
      if (y<0)
	continue;
      if (y>=(int)w.w->sizey)
	continue;
      string s = whereis(w.w, x, y, 0);
      if (MudObject *o=planet->rawget(s.c_str())) {
	update_and_neighbours(w.w, o, x, y, 0);
      }
    }
  }

  w.w->needssave = 1;

  return true;
}

#define CLEANUP docleanup();

}

using namespace wild;

#include "verbmodule.h"

void startup() {
  PREFAB_OBJECT = make_wild;
  DESCRIBE_LOCATION = describe_location;
  RANDOM_ROAD = random_road;

  {
    int r=0;
    r = regcomp(&tagmatch,"^@([[:alnum:]]+)_([[:digit:]]+):([[:digit:]]+)(([+-])([[:digit:]]+))?$",REG_EXTENDED);
    if(r) log(PFL_SEEINFO,0,"wild","regcomp returned non-zero");
  }

  AUTO_VERB(map, 3, 0, PFL_NONE);
  AUTO_VERB(bigmap, 6, 0, PFL_GOD);
  AUTO_VERB(timeout, 5, 0, PFL_NONE);

  AUTO_VERB(wildwrite, 5, 0, PFL_GOD);
  AUTO_VERB(form, 4, 0, PFL_GOD);
  AUTO_VERB(flow, 4, 0, PFL_GOD);
  AUTO_VERB(alt, 3, 0, PFL_GOD);
  AUTO_VERB(formrect, 5, 0, PFL_GOD);
  AUTO_VERB(formcircle, 5, 0, PFL_GOD);

  AUTO_VERB(makewild, 5, 0, PFL_GOD);
  AUTO_VERB(killwild, 5, 0, PFL_GOD);
  AUTO_VERB(setwild, 5, 0, PFL_GOD);

  AUTO_VERB(dive, 3, 0, PFL_AWAKE);
  AUTO_VERB(surface, 2, 0, PFL_AWAKE);

  AUTO_VERB(rail, 4, 0, PFL_GOD);
  AUTO_VERB(tunnel, 4, 0, PFL_GOD);

  AUTO_VERB(wilds, 5, 0, PFL_GOD);
}

Site Stats

Snippets: 1791
Codebases: 571