murk.cpp

murk++/murk.cpp · 168.9 KB

From the archive murk++-1.6r41.tar.gz

Download the archive   « Back to the archive contents

/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Merc Diku Mud, you must comply with   *
 *  both the original Diku license in 'license.doc' as well the Merc       *
 *  license in 'license.txt'.  In particular, you may not remove either of *
 *  these copyright notices.                                               *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/

/*
 MurkMUD++ - A Windows compatible, C++ compatible Merc 2.2 Mud.

 \author Jon A. Lambert
 \date 01/02/2007
 \version 1.5
 \remarks
  This source code copyright (C) 2005, 2006, 2007 by Jon A. Lambert
  All rights reserved.

  Use governed by the MurkMUD++ public license found in license.murk++
*/

/*-----------------------------------------------------------------------*/
/* OS DEPENDENT INCLUDES AND DEFINITIONS                                 */
/*-----------------------------------------------------------------------*/
#include "os.hpp"

/*-----------------------------------------------------------------------*/
/* MurkMUD++ BEGINS HERE                                                 */
/*-----------------------------------------------------------------------*/

#include "config.hpp"
#include "utils.hpp"
#include "io.hpp"
#include "database.hpp"
#include "symbols.hpp"
#include "world.hpp"

/*
 * Globals.
 */
World *g_world = NULL;
Database *g_db = NULL;
Area *area_last = NULL;
std::list<Ban *> ban_list;
std::list<Character *> char_list;
std::list<Descriptor *> descriptor_list;       /* All open descriptors     */
std::list<Note *> note_list;
std::list<Object *> object_list;
std::list<Shop *> shop_list;

// These iterators used on loops where the next iterator can be invalidated
// because of a nested method that erases an object in the list.
CharIter deepchnext, deeprmnext;
ObjIter deepobnext;
DescIter deepdenext;
//bool character_invalidated = false;  // This is set in Mprogs if we

std::map<int, MobPrototype *> mob_table;
std::map<int, ObjectPrototype *> obj_table;
std::map<int, Room *> room_table;

struct kill_data kill_table[MAX_LEVEL];

/*
 * Global variables.
 */
bool merc_down;                 /* Shutdown         */
bool wizlock;                   /* Game is wizlocked        */
std::string str_boot_time;
std::string help_greeting;
short g_port;
SOCKET g_listen;

/*
 * The kludgy global is for spells who want more stuff from command line.
 */
std::string target_name;

std::ifstream * fpArea;
std::string strArea;

/*
 * Array of containers read for proper re-nesting of objects.
 */
Object *rgObjNest[MAX_NEST];

bool MOBtrigger;

std::string dir_name[] = {
  "north", "east", "south", "west", "up", "down"
};

sh_int rev_dir[] = {
  2, 3, 0, 1, 5, 4
};

std::string where_name[] = {
  "<used as light>     ",
  "<worn on finger>    ",
  "<worn on finger>    ",
  "<worn around neck>  ",
  "<worn around neck>  ",
  "<worn on body>      ",
  "<worn on head>      ",
  "<worn on legs>      ",
  "<worn on feet>      ",
  "<worn on hands>     ",
  "<worn on arms>      ",
  "<worn as shield>    ",
  "<worn about body>   ",
  "<worn about waist>  ",
  "<worn around wrist> ",
  "<worn around wrist> ",
  "<wielded>           ",
  "<held>              "
};

/*
 * Class table.
 */
struct class_type class_table[] = {
  { "Mag", APPLY_INT, OBJ_VNUM_SCHOOL_DAGGER,
    3018, 95, 18, 10, 6, 8, true},
  { "Cle", APPLY_WIS, OBJ_VNUM_SCHOOL_MACE,
    3003, 95, 18, 12, 7, 10, true},
  { "Thi", APPLY_DEX, OBJ_VNUM_SCHOOL_DAGGER,
    3028, 85, 18, 8, 8, 13, false},
  { "War", APPLY_STR, OBJ_VNUM_SCHOOL_SWORD,
    3022, 85, 18, 6, 11, 15, false}
};

/*
 * Attribute bonus structures.
 */
struct str_app_type str_app[26] = {
  {-5, -4, 0, 0}, {-5, -4, 3, 1}, {-3, -2, 3, 2}, {-3, -1, 10, 3},
  {-2, -1, 25, 4}, {-2, -1, 55, 5}, {-1, 0, 80, 6}, {-1, 0, 90, 7},
  {0, 0, 100, 8}, {0, 0, 100, 9}, {0, 0, 115, 10},  {0, 0, 115, 11},
  {0, 0, 140, 12}, {0, 0, 140, 13}, {0, 1, 170, 14}, {1, 1, 170, 15}, /* 15  */
  {1, 2, 195, 16}, {2, 3, 220, 22}, {2, 4, 250, 25}, {3, 5, 400, 30},
  {3, 6, 500, 35}, {4, 7, 600, 40}, {5, 7, 700, 45}, {6, 8, 800, 50},
  {8, 10, 900, 55}, {10, 12, 999, 60}      /* 25   */
};

struct int_app_type int_app[26] = {
  {3}, {5}, {7}, {8}, {9}, {10},      /*  5 */
  {11}, {12}, {13}, {15}, {17},       /* 10 */
  {19}, {22}, {25}, {28}, {31},       /* 15 */
  {34}, {37}, {40}, {44}, {49},       /* 20 */
  {55}, {60}, {70}, {85}, {99}        /* 25 */
};

struct wis_app_type wis_app[26] = {
  {0}, {0}, {0}, {0}, {0}, {1},       /*  5 */
  {1}, {1}, {1}, {2}, {2},            /* 10 */
  {2}, {2}, {2}, {2}, {3},            /* 15 */
  {3}, {4}, {4}, {5}, {5},            /* 20 */
  {6}, {7}, {7}, {7}, {8}             /* 25 */
};

struct dex_app_type dex_app[26] = {
  {60}, {50}, {50}, {40}, {30}, {20}, /* 5 */
  {10}, {0}, {0}, {0}, {0},           /* 10 */
  {0}, {0}, {0}, {0}, {-10},          /* 15 */
  {-15}, {-20}, {-30}, {-40}, {-50},  /* 20 */
  {-65}, {-75}, {-90}, {-105}, {-120} /* 25 */
};

struct con_app_type con_app[26] = {
  {-4, 20}, {-3, 25}, {-2, 30}, {-2, 35}, {-1, 40}, {-1, 45}, /*  5 */
  {-1, 50}, {0, 55}, {0, 60}, {0, 65}, {0, 70},               /* 10 */
  {0, 75}, {0, 80}, {0, 85}, {0, 88}, {1, 90},                /* 15 */
  {2, 95}, {2, 97}, {3, 99}, {3, 99}, {4, 99},                /* 20 */
  {4, 99}, {5, 99}, {6, 99}, {7, 99}, {8, 99}                 /* 25 */
};

/*
 * Liquid properties.
 */
struct liq_type liq_table[LIQ_MAX] = {
  {"water", "clear", {0, 1, 10}},       /*  0 */
  {"beer", "amber", {3, 2, 5}},
  {"wine", "rose", {5, 2, 5}},
  {"ale", "brown", {2, 2, 5}},
  {"dark ale", "dark", {1, 2, 5}},

  {"whisky", "golden", {6, 1, 4}},      /*  5 */
  {"lemonade", "pink", {0, 1, 8}},
  {"firebreather", "boiling", {10, 0, 0}},
  {"local specialty", "everclear", {3, 3, 3}},
  {"slime mold juice", "green", {0, 4, -8}},

  {"milk", "white", {0, 3, 6}}, /* 10 */
  {"tea", "tan", {0, 1, 6}},
  {"coffee", "black", {0, 1, 6}},
  {"blood", "red", {0, 2, -1}},
  {"salt water", "clear", {0, 1, -2}},

  {"cola", "cherry", {0, 1, 5}} /* 15 */
};

/* prototypes */
void multi_hit(Character *ch, Character *victim, int dt);
void damage(Character *ch, Character *victim, int dam, int dt);

bool dragon(Character *ch, char *spell_name);

void mprog_act_trigger(const std::string & buf, Character *mob, Character *ch, Object *obj, void *vo);
void mprog_bribe_trigger(Character *mob, Character *ch, int amount);
void mprog_death_trigger(Character *mob);
void mprog_entry_trigger(Character *mob);
void mprog_fight_trigger(Character *mob, Character *ch);
void mprog_give_trigger(Character *mob, Character *ch, Object *obj);
void mprog_greet_trigger(Character *mob);
void mprog_hitprcnt_trigger(Character *mob, Character *ch);
void mprog_random_trigger(Character *mob);
void mprog_speech_trigger(char *txt, Character *mob);

bool spec_breath_any(Character *ch);
bool spec_breath_acid(Character *ch);
bool spec_breath_fire(Character *ch);
bool spec_breath_frost(Character *ch);
bool spec_breath_gas(Character *ch);
bool spec_breath_lightning(Character *ch);
bool spec_cast_adept(Character *ch);
bool spec_cast_cleric(Character *ch);
bool spec_cast_judge(Character *ch);
bool spec_cast_mage(Character *ch);
bool spec_cast_undead(Character *ch);
bool spec_executioner(Character *ch);
bool spec_fido(Character *ch);
bool spec_guard(Character *ch);
bool spec_janitor(Character *ch);
bool spec_mayor(Character *ch);
bool spec_poison(Character *ch);
bool spec_thief(Character *ch);

/* Structs */
/*
 * MOBprogram block
*/
class MobProgramActList {
public:
  MobProgramActList *next;
  std::string buf;
  Character *ch;
  Object *obj;
  void *vo;

  MobProgramActList() :
    next(NULL), ch(NULL), obj(NULL), vo(NULL) {
  }

};

class MobProgram {
public:
  MobProgram *next;
  int type;
  std::string arglist;
  std::string comlist;

  MobProgram() :
    next(NULL), type(0) {
  }

};

#include "shop.hpp"
#include "note.hpp"
#include "affect.hpp"
#include "ban.hpp"
#include "exit.hpp"
#include "reset.hpp"
#include "area.hpp"
#include "room.hpp"
#include "objproto.hpp"
#include "object.hpp"
#include "mobproto.hpp"
#include "descriptor.hpp"
#include "pcdata.hpp"
#include "extra.hpp"

struct cmd_type cmd_table[] = {
  /*
   * Common movement commands.
   */
  {"north", &Character::do_north, POS_STANDING, 0},
  {"east", &Character::do_east, POS_STANDING, 0},
  {"south", &Character::do_south, POS_STANDING, 0},
  {"west", &Character::do_west, POS_STANDING, 0},
  {"up", &Character::do_up, POS_STANDING, 0},
  {"down", &Character::do_down, POS_STANDING, 0},

  /*
   * Common other commands.
   * Placed here so one and two letter abbreviations work.
   */
  {"buy", &Character::do_buy, POS_RESTING, 0},
  {"cast", &Character::do_cast, POS_FIGHTING, 0},
  {"exits", &Character::do_exits, POS_RESTING, 0},
  {"get", &Character::do_get, POS_RESTING, 0},
  {"inventory", &Character::do_inventory, POS_DEAD, 0},
  {"kill", &Character::do_kill, POS_FIGHTING, 0},
  {"look", &Character::do_look, POS_RESTING, 0},
  {"order", &Character::do_order, POS_RESTING, 0},
  {"rest", &Character::do_rest, POS_RESTING, 0},
  {"sleep", &Character::do_sleep, POS_SLEEPING, 0},
  {"stand", &Character::do_stand, POS_SLEEPING, 0},
  {"tell", &Character::do_tell, POS_RESTING, 0},
  {"wield", &Character::do_wear, POS_RESTING, 0},
  {"wizhelp", &Character::do_wizhelp, POS_DEAD, L_HER},

  /*
   * Informational commands.
   */
  {"areas", &Character::do_areas, POS_DEAD, 0},
  {"bug", &Character::do_bug, POS_DEAD, 0},
  {"commands", &Character::do_commands, POS_DEAD, 0},
  {"compare", &Character::do_compare, POS_RESTING, 0},
  {"consider", &Character::do_consider, POS_RESTING, 0},
  {"credits", &Character::do_credits, POS_DEAD, 0},
  {"equipment", &Character::do_equipment, POS_DEAD, 0},
  {"examine", &Character::do_examine, POS_RESTING, 0},
  {"help", &Character::do_help, POS_DEAD, 0},
  {"idea", &Character::do_idea, POS_DEAD, 0},
  {"report", &Character::do_report, POS_DEAD, 0},
  {"pagelength", &Character::do_pagelen, POS_DEAD, 0},
  {"score", &Character::do_score, POS_DEAD, 0},
  {"slist", &Character::do_slist, POS_DEAD, 0},
  {"socials", &Character::do_socials, POS_DEAD, 0},
  {"time", &Character::do_time, POS_DEAD, 0},
  {"typo", &Character::do_typo, POS_DEAD, 0},
  {"weather", &Character::do_weather, POS_RESTING, 0},
  {"who", &Character::do_who, POS_DEAD, 0},
  {"wizlist", &Character::do_wizlist, POS_DEAD, 0},

  /*
   * Configuration commands.
   */
  {"auto", &Character::do_auto, POS_DEAD, 0},
  {"autoexit", &Character::do_autoexit, POS_DEAD, 0},
  {"autoloot", &Character::do_autoloot, POS_DEAD, 0},
  {"autosac", &Character::do_autosac, POS_DEAD, 0},
  {"blank", &Character::do_blank, POS_DEAD, 0},
  {"brief", &Character::do_brief, POS_DEAD, 0},
  {"channels", &Character::do_channels, POS_DEAD, 0},
  {"combine", &Character::do_combine, POS_DEAD, 0},
  {"config", &Character::do_config, POS_DEAD, 0},
  {"description", &Character::do_description, POS_DEAD, 0},
  {"password", &Character::do_password, POS_DEAD, 0},
  {"prompt", &Character::do_prompt, POS_DEAD, 0},
  {"title", &Character::do_title, POS_DEAD, 0},
  {"wimpy", &Character::do_wimpy, POS_DEAD, 0},

  /*
   * Communication commands.
   */
  {"answer", &Character::do_answer, POS_SLEEPING, 0},
  {"auction", &Character::do_auction, POS_SLEEPING, 0},
  {"chat", &Character::do_chat, POS_SLEEPING, 0},
  {".", &Character::do_chat, POS_SLEEPING, 0},
  {"emote", &Character::do_emote, POS_RESTING, 0},
  {",", &Character::do_emote, POS_RESTING, 0},
  {"gtell", &Character::do_gtell, POS_DEAD, 0},
  {";", &Character::do_gtell, POS_DEAD, 0},
  {"music", &Character::do_music, POS_SLEEPING, 0},
  {"note", &Character::do_note, POS_SLEEPING, 0},
  {"question", &Character::do_question, POS_SLEEPING, 0},
  {"reply", &Character::do_reply, POS_RESTING, 0},
  {"say", &Character::do_say, POS_RESTING, 0},
  {"'", &Character::do_say, POS_RESTING, 0},
  {"shout", &Character::do_shout, POS_RESTING, 3},
  {"yell", &Character::do_yell, POS_RESTING, 0},
  {"gag", &Character::do_gag, POS_DEAD, 0},
  {"ignore", &Character::do_gag, POS_DEAD, 0},

  /*
   * Object manipulation commands.
   */
  {"brandish", &Character::do_brandish, POS_RESTING, 0},
  {"close", &Character::do_close, POS_RESTING, 0},
  {"drink", &Character::do_drink, POS_RESTING, 0},
  {"drop", &Character::do_drop, POS_RESTING, 0},
  {"eat", &Character::do_eat, POS_RESTING, 0},
  {"fill", &Character::do_fill, POS_RESTING, 0},
  {"give", &Character::do_give, POS_RESTING, 0},
  {"hold", &Character::do_wear, POS_RESTING, 0},
  {"list", &Character::do_list, POS_RESTING, 0},
  {"lock", &Character::do_lock, POS_RESTING, 0},
  {"open", &Character::do_open, POS_RESTING, 0},
  {"pick", &Character::do_pick, POS_RESTING, 0},
  {"put", &Character::do_put, POS_RESTING, 0},
  {"quaff", &Character::do_quaff, POS_RESTING, 0},
  {"recite", &Character::do_recite, POS_RESTING, 0},
  {"remove", &Character::do_remove, POS_RESTING, 0},
  {"sell", &Character::do_sell, POS_RESTING, 0},
  {"take", &Character::do_get, POS_RESTING, 0},
  {"sacrifice", &Character::do_sacrifice, POS_RESTING, 0},
  {"unlock", &Character::do_unlock, POS_RESTING, 0},
  {"value", &Character::do_value, POS_RESTING, 0},
  {"wear", &Character::do_wear, POS_RESTING, 0},
  {"zap", &Character::do_zap, POS_RESTING, 0},

  /*
   * Combat commands.
   */
  {"backstab", &Character::do_backstab, POS_STANDING, 0},
  {"bs", &Character::do_backstab, POS_STANDING, 0},
  {"disarm", &Character::do_disarm, POS_FIGHTING, 0},
  {"flee", &Character::do_flee, POS_FIGHTING, 0},
  {"kick", &Character::do_kick, POS_FIGHTING, 0},
  {"murde", &Character::do_murde, POS_FIGHTING, 5},
  {"murder", &Character::do_murder, POS_FIGHTING, 5},
  {"rescue", &Character::do_rescue, POS_FIGHTING, 0},

  /*
   * Miscellaneous commands.
   */
  {"follow", &Character::do_follow, POS_RESTING, 0},
  {"group", &Character::do_group, POS_SLEEPING, 0},
  {"hide", &Character::do_hide, POS_RESTING, 0},
  {"practice", &Character::do_practice, POS_SLEEPING, 0},
  {"qui", &Character::do_qui, POS_DEAD, 0},
  {"quit", &Character::do_quit, POS_DEAD, 0},
  {"recall", &Character::do_recall, POS_FIGHTING, 0},
  {"/", &Character::do_recall, POS_FIGHTING, 0},
  {"rent", &Character::do_rent, POS_DEAD, 0},
  {"save", &Character::do_save, POS_DEAD, 0},
  {"sleep", &Character::do_sleep, POS_SLEEPING, 0},
  {"sneak", &Character::do_sneak, POS_STANDING, 0},
  {"spells", &Character::do_spells, POS_SLEEPING, 0},
  {"split", &Character::do_split, POS_RESTING, 0},
  {"steal", &Character::do_steal, POS_STANDING, 0},
  {"train", &Character::do_train, POS_RESTING, 0},
  {"visible", &Character::do_visible, POS_SLEEPING, 0},
  {"wake", &Character::do_wake, POS_SLEEPING, 0},
  {"where", &Character::do_where, POS_RESTING, 0},

  /*
   * Immortal commands.
   */
  {"advance", &Character::do_advance, POS_DEAD, L_GOD},
  {"trust", &Character::do_trust, POS_DEAD, L_GOD},

  {"allow", &Character::do_allow, POS_DEAD, L_SUP},
  {"ban", &Character::do_ban, POS_DEAD, L_SUP},
  {"deny", &Character::do_deny, POS_DEAD, L_SUP},
  {"disconnect", &Character::do_disconnect, POS_DEAD, L_SUP},
  {"freeze", &Character::do_freeze, POS_DEAD, L_SUP},
  {"hotboo", &Character::do_hotboo, POS_DEAD, L_SUP},
  {"hotboot", &Character::do_hotboot, POS_DEAD, L_SUP},
  {"shutdow", &Character::do_shutdow, POS_DEAD, L_SUP},
  {"shutdown", &Character::do_shutdown, POS_DEAD, L_SUP},
  {"users", &Character::do_users, POS_DEAD, L_SUP},
  {"wizify", &Character::do_wizify, POS_DEAD, L_SUP},
  {"wizlock", &Character::do_wizlock, POS_DEAD, L_SUP},

  {"force", &Character::do_force, POS_DEAD, L_DEI},
  {"mload", &Character::do_mload, POS_DEAD, L_DEI},
  {"mset", &Character::do_mset, POS_DEAD, L_DEI},
  {"noemote", &Character::do_noemote, POS_DEAD, L_DEI},
  {"notell", &Character::do_notell, POS_DEAD, L_DEI},
  {"oload", &Character::do_oload, POS_DEAD, L_DEI},
  {"oset", &Character::do_oset, POS_DEAD, L_DEI},
  {"owhere", &Character::do_owhere, POS_DEAD, L_DEI},
  {"pardon", &Character::do_pardon, POS_DEAD, L_DEI},
  {"peace", &Character::do_peace, POS_DEAD, L_DEI},
  {"purge", &Character::do_purge, POS_DEAD, L_DEI},
  {"restore", &Character::do_restore, POS_DEAD, L_DEI},
  {"rset", &Character::do_rset, POS_DEAD, L_DEI},
  {"silence", &Character::do_silence, POS_DEAD, L_DEI},
  {"sla", &Character::do_sla, POS_DEAD, L_DEI},
  {"slay", &Character::do_slay, POS_DEAD, L_DEI},
  {"sset", &Character::do_sset, POS_DEAD, L_DEI},
  {"transfer", &Character::do_transfer, POS_DEAD, L_DEI},
  {"mpstat", &Character::do_mpstat, POS_DEAD, L_DEI},

  {"at", &Character::do_at, POS_DEAD, L_ANG},
  {"bamfin", &Character::do_bamfin, POS_DEAD, L_ANG},
  {"bamfout", &Character::do_bamfout, POS_DEAD, L_ANG},
  {"echo", &Character::do_echo, POS_DEAD, L_ANG},
  {"goto", &Character::do_goto, POS_DEAD, L_ANG},
  {"holylight", &Character::do_holylight, POS_DEAD, L_ANG},
  {"memory", &Character::do_memory, POS_DEAD, L_ANG},
  {"mfind", &Character::do_mfind, POS_DEAD, L_ANG},
  {"mstat", &Character::do_mstat, POS_DEAD, L_ANG},
  {"mwhere", &Character::do_mwhere, POS_DEAD, L_ANG},
  {"ofind", &Character::do_ofind, POS_DEAD, L_ANG},
  {"ostat", &Character::do_ostat, POS_DEAD, L_ANG},
  {"recho", &Character::do_recho, POS_DEAD, L_ANG},
  {"return", &Character::do_return, POS_DEAD, L_ANG},
  {"rstat", &Character::do_rstat, POS_DEAD, L_ANG},
  {"slookup", &Character::do_slookup, POS_DEAD, L_ANG},
  {"switch", &Character::do_switch, POS_DEAD, L_ANG},

  {"immtalk", &Character::do_immtalk, POS_DEAD, L_ANG},
  {":", &Character::do_immtalk, POS_DEAD, L_ANG},

  /*
   * MOBprogram commands.
   */
  {"mpasound", &Character::do_mpasound, POS_DEAD, 41},
  {"mpjunk", &Character::do_mpjunk, POS_DEAD, 41},
  {"mpecho", &Character::do_mpecho, POS_DEAD, 41},
  {"mpechoat", &Character::do_mpechoat, POS_DEAD, 41},
  {"mpechoaround", &Character::do_mpechoaround, POS_DEAD, 41},
  {"mpkill", &Character::do_mpkill, POS_DEAD, 41},
  {"mpmload", &Character::do_mpmload, POS_DEAD, 41},
  {"mpoload", &Character::do_mpoload, POS_DEAD, 41},
  {"mppurge", &Character::do_mppurge, POS_DEAD, 41},
  {"mpgoto", &Character::do_mpgoto, POS_DEAD, 41},
  {"mpat", &Character::do_mpat, POS_DEAD, 41},
  {"mptransfer", &Character::do_mptransfer, POS_DEAD, 41},
  {"mpforce", &Character::do_mpforce, POS_DEAD, 41},

  /*
   * End of list.
   */
  {"", 0, POS_DEAD, 0}
};

/*
 * The skill and spell table.
 * Slot numbers must never be changed as they appear in #OBJECTS sections.
 * Skills include spells as a particular case.
 */
struct skill_type skill_table[MAX_SKILL] = {

/*
 * Magic spells.
 */

  {   "reserved", {99, 99, 99, 99},
      NULL, TAR_IGNORE, POS_STANDING,
      0, 0, "", ""},
  {   "acid blast", {20, 37, 37, 37},
      &Character::spell_acid_blast, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      20, 12, "acid blast", "!Acid Blast!"},
  {   "armor", {5, 1, 37, 37},
      &Character::spell_armor, TAR_CHAR_DEFENSIVE, POS_STANDING,
      5, 12, "", "You feel less protected."},
  {   "bless", {37, 5, 37, 37},
      &Character::spell_bless, TAR_CHAR_DEFENSIVE, POS_STANDING,
      5, 12, "", "You feel less righteous."},
  {   "blindness", {8, 5, 37, 37},
      &Character::spell_blindness, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      5, 12, "", "You can see again."},
  {   "burning hands", {5, 37, 37, 37},
      &Character::spell_burning_hands, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "burning hands", "!Burning Hands!"},
  {   "call lightning", {37, 12, 37, 37},
      &Character::spell_call_lightning, TAR_IGNORE, POS_FIGHTING,
      15, 12, "lightning bolt", "!Call Lightning!"},
  {   "cause critical", {37, 9, 37, 37},
      &Character::spell_cause_critical, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      20, 12, "spell", "!Cause Critical!"},
  {   "cause light", {37, 1, 37, 37},
      &Character::spell_cause_light, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "spell", "!Cause Light!"},
  {   "cause serious", {37, 5, 37, 37},
      &Character::spell_cause_serious, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      17, 12, "spell", "!Cause Serious!"},
  {   "change sex", {37, 37, 37, 37},
      &Character::spell_change_sex, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      15, 12, "", "Your body feels familiar again."},
  {   "charm person", {14, 37, 37, 37},
      &Character::spell_charm_person, TAR_CHAR_OFFENSIVE, POS_STANDING,
      5, 12, "", "You feel more self-confident."},
  {   "chill touch", {3, 37, 37, 37},
      &Character::spell_chill_touch, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "chilling touch", "You feel less cold."},
  {   "colour spray", {11, 37, 37, 37},
      &Character::spell_colour_spray, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "colour spray", "!Colour Spray!"},
  {   "continual light", {4, 2, 37, 37},
      &Character::spell_continual_light, TAR_IGNORE, POS_STANDING,
      7, 12, "", "!Continual Light!"},
  {   "control weather", {10, 13, 37, 37},
      &Character::spell_control_weather, TAR_IGNORE, POS_STANDING,
      25, 12, "", "!Control Weather!"},
  {   "create food", {37, 3, 37, 37},
      &Character::spell_create_food, TAR_IGNORE, POS_STANDING,
      5, 12, "", "!Create Food!"},
  {   "create spring", {10, 37, 37, 37},
      &Character::spell_create_spring, TAR_IGNORE, POS_STANDING,
      20, 12, "", "!Create Spring!"},
  {   "create water", {37, 2, 37, 37},
      &Character::spell_create_water, TAR_OBJ_INV, POS_STANDING,
      5, 12, "", "!Create Water!"},
  {   "cure blindness", {37, 4, 37, 37},
      &Character::spell_cure_blindness, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      5, 12, "", "!Cure Blindness!"},
  {   "cure critical", {37, 9, 37, 37},
      &Character::spell_cure_critical, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      20, 12, "", "!Cure Critical!"},
  {   "cure light", {37, 1, 37, 37},
      &Character::spell_cure_light, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      10, 12, "", "!Cure Light!"},
  {   "cure poison", {37, 9, 37, 37},
      &Character::spell_cure_poison, TAR_CHAR_DEFENSIVE, POS_STANDING,
      5, 12, "", "!Cure Poison!"},
  {   "cure serious", {37, 5, 37, 37},
      &Character::spell_cure_serious, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      15, 12, "", "!Cure Serious!"},
  {   "curse", {12, 12, 37, 37},
      &Character::spell_curse, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      20, 12, "curse", "The curse wears off."},
  {   "detect evil", {37, 4, 37, 37},
      &Character::spell_detect_evil, TAR_CHAR_SELF, POS_STANDING,
      5, 12, "", "The red in your vision disappears."},
  {   "detect hidden", {37, 7, 37, 37},
      &Character::spell_detect_hidden, TAR_CHAR_SELF, POS_STANDING,
      5, 12, "", "You feel less aware of your suroundings."},
  {   "detect invis", {2, 5, 37, 37},
      &Character::spell_detect_invis, TAR_CHAR_SELF, POS_STANDING,
      5, 12, "", "You no longer see invisible objects."},
  {   "detect magic", {2, 3, 37, 37},
      &Character::spell_detect_magic, TAR_CHAR_SELF, POS_STANDING,
      5, 12, "", "The detect magic wears off."},
  {   "detect poison", {37, 5, 37, 37},
      &Character::spell_detect_poison, TAR_OBJ_INV, POS_STANDING,
      5, 12, "", "!Detect Poison!"},
  {   "dispel evil", {37, 10, 37, 37},
      &Character::spell_dispel_evil, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "dispel evil", "!Dispel Evil!"},
  {   "dispel magic", {26, 31, 37, 37},
      &Character::spell_dispel_magic, TAR_CHAR_OFFENSIVE, POS_STANDING,
      15, 12, "", "!Dispel Magic!"},
  {   "earthquake", {37, 7, 37, 37},
      &Character::spell_earthquake, TAR_IGNORE, POS_FIGHTING,
      15, 12, "earthquake", "!Earthquake!"},
  {   "enchant weapon", {12, 37, 37, 37},
      &Character::spell_enchant_weapon, TAR_OBJ_INV, POS_STANDING,
      100, 24, "", "!Enchant Weapon!"},
  {   "energy drain", {13, 37, 37, 37},
      &Character::spell_energy_drain, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      35, 12, "energy drain", "!Energy Drain!"},
  {   "faerie fire", {4, 2, 37, 37},
      &Character::spell_faerie_fire, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      5, 12, "faerie fire", "The pink aura around you fades away."},
  {   "faerie fog", {10, 14, 37, 37},
      &Character::spell_faerie_fog, TAR_IGNORE, POS_STANDING,
      12, 12, "faerie fog", "!Faerie Fog!"},
  {   "fireball", {15, 37, 37, 37},
      &Character::spell_fireball, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "fireball", "!Fireball!"},
  {   "flamestrike", {37, 13, 37, 37},
      &Character::spell_flamestrike, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      20, 12, "flamestrike", "!Flamestrike!"},
  {   "fly", {7, 12, 37, 37},
      &Character::spell_fly, TAR_CHAR_DEFENSIVE, POS_STANDING,
      10, 18, "", "You slowly float to the ground."},
  {   "gate", {37, 37, 37, 37},
      &Character::spell_gate, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      50, 12, "", "!Gate!"},
  {   "giant strength", {7, 37, 37, 37},
      &Character::spell_giant_strength, TAR_CHAR_DEFENSIVE, POS_STANDING,
      20, 12, "", "You feel weaker."},
  {   "harm", {37, 15, 37, 37},
      &Character::spell_harm, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      35, 12, "harm spell", "!Harm!"},
  {   "heal", {37, 14, 37, 37},
      &Character::spell_heal, TAR_CHAR_DEFENSIVE, POS_FIGHTING,
      50, 12, "", "!Heal!"},
  {   "identify", {10, 10, 37, 37},
      &Character::spell_identify, TAR_OBJ_INV, POS_STANDING,
      12, 24, "", "!Identify!"},
  {   "infravision", {6, 9, 37, 37},
      &Character::spell_infravision, TAR_CHAR_DEFENSIVE, POS_STANDING,
      5, 18, "", "You no longer see in the dark."},
  {   "invis", {4, 37, 37, 37},
      &Character::spell_invis, TAR_CHAR_DEFENSIVE, POS_STANDING,
      5, 12, "", "You are no longer invisible."},
  {   "know alignment", {8, 5, 37, 37},
      &Character::spell_know_alignment, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      9, 12, "", "!Know Alignment!"},
  {   "lightning bolt", {9, 37, 37, 37},
      &Character::spell_lightning_bolt, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "lightning bolt", "!Lightning Bolt!"},
  {   "locate object", {6, 10, 37, 37},
      &Character::spell_locate_object, TAR_IGNORE, POS_STANDING,
      20, 18, "", "!Locate Object!"},
  {   "magic missile", {1, 37, 37, 37},
      &Character::spell_magic_missile, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "magic missile", "!Magic Missile!"},
  {   "mass invis", {15, 17, 37, 37},
      &Character::spell_mass_invis, TAR_IGNORE, POS_STANDING,
      20, 24, "", "!Mass Invis!"},
  {   "pass door", {18, 37, 37, 37},
      &Character::spell_pass_door, TAR_CHAR_SELF, POS_STANDING,
      20, 12, "", "You feel solid again."},
  {   "poison", {37, 8, 37, 37},
      &Character::spell_poison, TAR_CHAR_OFFENSIVE, POS_STANDING,
      10, 12, "poison", "You feel less sick."},
  {   "protection", {37, 6, 37, 37},
      &Character::spell_protection, TAR_CHAR_SELF, POS_STANDING,
      5, 12, "", "You feel less protected."},
  {   "refresh", {5, 3, 37, 37},
      &Character::spell_refresh, TAR_CHAR_DEFENSIVE, POS_STANDING,
      12, 18, "refresh", "!Refresh!"},
  {   "remove curse", {37, 12, 37, 37},
      &Character::spell_remove_curse, TAR_CHAR_DEFENSIVE, POS_STANDING,
      5, 12, "", "!Remove Curse!"},
  {   "sanctuary", {37, 13, 37, 37},
      &Character::spell_sanctuary, TAR_CHAR_DEFENSIVE, POS_STANDING,
      75, 12, "", "The white aura around your body fades."},
  {   "shield", {13, 37, 37, 37},
      &Character::spell_shield, TAR_CHAR_DEFENSIVE, POS_STANDING,
      12, 18, "", "Your force shield shimmers then fades away."},
  {   "shocking grasp", {7, 37, 37, 37},
      &Character::spell_shocking_grasp, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      15, 12, "shocking grasp", "!Shocking Grasp!"},
  {   "sleep", {14, 37, 37, 37},
      &Character::spell_sleep, TAR_CHAR_OFFENSIVE, POS_STANDING,
      15, 12, "", "You feel less tired."},
  {   "stone skin", {17, 37, 37, 37},
      &Character::spell_stone_skin, TAR_CHAR_SELF, POS_STANDING,
      12, 18, "", "Your skin feels soft again."},
  {   "summon", {37, 8, 37, 37},
      &Character::spell_summon, TAR_IGNORE, POS_STANDING,
      50, 12, "", "!Summon!"},
  {   "teleport", {8, 37, 37, 37},
      &Character::spell_teleport, TAR_CHAR_SELF, POS_FIGHTING,
      35, 12, "", "!Teleport!"},
  {   "ventriloquate", {1, 37, 37, 37},
      &Character::spell_ventriloquate, TAR_IGNORE, POS_STANDING,
      5, 12, "", "!Ventriloquate!"},
  {   "weaken", {7, 37, 37, 37},
      &Character::spell_weaken, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      20, 12, "spell", "You feel stronger."},
  {   "word of recall", {37, 37, 37, 37},
      &Character::spell_word_of_recall, TAR_CHAR_SELF, POS_RESTING,
      5, 12, "", "!Word of Recall!"},
/*
 * Dragon breath
 */
  {   "acid breath", {33, 37, 37, 37},
      &Character::spell_acid_breath, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 4, "blast of acid", "!Acid Breath!"},
  {   "fire breath", {34, 37, 37, 37},
      &Character::spell_fire_breath, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 4, "blast of flame", "!Fire Breath!"},
  {   "frost breath", {31, 37, 37, 37},
      &Character::spell_frost_breath, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 4, "blast of frost", "!Frost Breath!"},
  {   "gas breath", {35, 37, 37, 37},
      &Character::spell_gas_breath, TAR_IGNORE, POS_FIGHTING,
      0, 4, "blast of gas", "!Gas Breath!"},
  {   "lightning breath", {32, 37, 37, 37},
      &Character::spell_lightning_breath, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 4, "blast of lightning", "!Lightning Breath!"},
/*
 * Fighter and thief skills.
 */
  {   "backstab", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_STANDING,
      0, 24, "backstab", "!Backstab!"},
  {   "disarm", {37, 37, 10, 37},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 24, "", "!Disarm!"},
  {   "dodge", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 0, "", "!Dodge!"},
  {   "enhanced damage", {37, 37, 37, 1},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 0, "", "!Enhanced Damage!"},
  {   "hide", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_RESTING,
      0, 12, "", "!Hide!"},
  {   "kick", {37, 37, 37, 1},
      &Character::spell_null, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 8, "kick", "!Kick!"},
  {   "parry", {37, 37, 37, 1},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 0, "", "!Parry!"},
  {   "peek", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_STANDING,
      0, 0, "", "!Peek!"},
  {   "pick lock", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_STANDING,
      0, 12, "", "!Pick!"},
  {   "rescue", {37, 37, 37, 1},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 12, "", "!Rescue!"},
  {   "second attack", {37, 37, 1, 1},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 0, "", "!Second Attack!"},
  {   "sneak", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_STANDING,
      0, 12, "", NULL},
  {   "steal", {37, 37, 1, 37},
      &Character::spell_null, TAR_IGNORE, POS_STANDING,
      0, 24, "", "!Steal!"},
  {   "third attack", {37, 37, 37, 1},
      &Character::spell_null, TAR_IGNORE, POS_FIGHTING,
      0, 0, "", "!Third Attack!"},
/*
 *  Spells for mega1.are from Glop/Erkenbrand.
*/
  {   "general purpose", {37, 37, 37, 37},
      &Character::spell_general_purpose, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 12, "general purpose ammo", "!General Purpose Ammo!"},
  {   "high explosive", {37, 37, 37, 37},
      &Character::spell_high_explosive, TAR_CHAR_OFFENSIVE, POS_FIGHTING,
      0, 12, "high explosive ammo", "!High Explosive Ammo!"}
};

///////////////////
// start of code //
///////////////////

/*
 * Lowest level output function.
 * Write a block of text to the file descriptor.
 * If this gives errors on very long blocks (like 'ofind all'),
 *   try lowering the max block size.
 */
bool write_to_descriptor (SOCKET desc, const char *txt, int length)
{
  int iStart;
  int nWrite;
  int nBlock;

  if (length <= 0)
    length = strlen (txt);

  for (iStart = 0; iStart < length; iStart += nWrite) {
    nBlock = std::min (length - iStart, 4096);
    if ((nWrite = send (desc, txt + iStart, nBlock, 0)) == SOCKET_ERROR) {
      perror ("Write_to_descriptor");
      return false;
    }
  }

  return true;
}

/*
 * Return ascii name of an affect location.
 */
std::string affect_loc_name (int location)
{
  switch (location) {
  case APPLY_NONE:
    return "none";
  case APPLY_STR:
    return "strength";
  case APPLY_DEX:
    return "dexterity";
  case APPLY_INT:
    return "intelligence";
  case APPLY_WIS:
    return "wisdom";
  case APPLY_CON:
    return "constitution";
  case APPLY_SEX:
    return "sex";
  case APPLY_CLASS:
    return "class";
  case APPLY_LEVEL:
    return "level";
  case APPLY_AGE:
    return "age";
  case APPLY_MANA:
    return "mana";
  case APPLY_HIT:
    return "hp";
  case APPLY_MOVE:
    return "moves";
  case APPLY_GOLD:
    return "gold";
  case APPLY_EXP:
    return "experience";
  case APPLY_AC:
    return "armor class";
  case APPLY_HITROLL:
    return "hit roll";
  case APPLY_DAMROLL:
    return "damage roll";
  case APPLY_SAVING_PARA:
    return "save vs paralysis";
  case APPLY_SAVING_ROD:
    return "save vs rod";
  case APPLY_SAVING_PETRI:
    return "save vs petrification";
  case APPLY_SAVING_BREATH:
    return "save vs breath";
  case APPLY_SAVING_SPELL:
    return "save vs spell";
  }

  bug_printf ("Affect_location_name: unknown location %d.", location);
  return "(unknown)";
}

/*
 * Return ascii name of an affect bit vector.
 */
std::string affect_bit_name (int vector)
{
  std::string buf;

  if (vector & AFF_BLIND)
    buf.append(" blind");
  if (vector & AFF_INVISIBLE)
    buf.append(" invisible");
  if (vector & AFF_DETECT_EVIL)
    buf.append(" detect_evil");
  if (vector & AFF_DETECT_INVIS)
    buf.append(" detect_invis");
  if (vector & AFF_DETECT_MAGIC)
    buf.append(" detect_magic");
  if (vector & AFF_DETECT_HIDDEN)
    buf.append(" detect_hidden");
  if (vector & AFF_SANCTUARY)
    buf.append(" sanctuary");
  if (vector & AFF_FAERIE_FIRE)
    buf.append(" faerie_fire");
  if (vector & AFF_INFRARED)
    buf.append(" infrared");
  if (vector & AFF_CURSE)
    buf.append(" curse");
  if (vector & AFF_POISON)
    buf.append(" poison");
  if (vector & AFF_PROTECT)
    buf.append(" protect");
  if (vector & AFF_SLEEP)
    buf.append(" sleep");
  if (vector & AFF_SNEAK)
    buf.append(" sneak");
  if (vector & AFF_HIDE)
    buf.append(" hide");
  if (vector & AFF_CHARM)
    buf.append(" charm");
  if (vector & AFF_FLYING)
    buf.append(" flying");
  if (vector & AFF_PASS_DOOR)
    buf.append(" pass_door");
  if (buf.empty())
    buf.append("none");
  else
    buf.erase(0,1);
  return buf;
}

/*
 * Return ascii name of extra flags vector.
 */
std::string extra_bit_name (int extra_flags)
{
  std::string buf;

  if (extra_flags & ITEM_GLOW)
    buf.append(" glow");
  if (extra_flags & ITEM_HUM)
    buf.append(" hum");
  if (extra_flags & ITEM_DARK)
    buf.append(" dark");
  if (extra_flags & ITEM_LOCK)
    buf.append(" lock");
  if (extra_flags & ITEM_EVIL)
    buf.append(" evil");
  if (extra_flags & ITEM_INVIS)
    buf.append(" invis");
  if (extra_flags & ITEM_MAGIC)
    buf.append(" magic");
  if (extra_flags & ITEM_NODROP)
    buf.append(" nodrop");
  if (extra_flags & ITEM_BLESS)
    buf.append(" bless");
  if (extra_flags & ITEM_ANTI_GOOD)
    buf.append(" anti-good");
  if (extra_flags & ITEM_ANTI_EVIL)
    buf.append(" anti-evil");
  if (extra_flags & ITEM_ANTI_NEUTRAL)
    buf.append(" anti-neutral");
  if (extra_flags & ITEM_NOREMOVE)
    buf.append(" noremove");
  if (extra_flags & ITEM_INVENTORY)
    buf.append(" inventory");
  if (buf.empty())
    buf.append("none");
  else
    buf.erase(0,1);
  return buf;
}

/*
 * Lookup a skill by name.
 */
int skill_lookup (const std::string & name)
{
  int sn;

  for (sn = 0; sn < MAX_SKILL; sn++) {
    if (skill_table[sn].name == NULL)
      break;
    if (tolower(name[0]) == tolower(skill_table[sn].name[0])
      && !str_prefix (name, skill_table[sn].name))
      return sn;
  }

  return -1;
}

/*
 * Translates mob virtual number to its mob index struct.
 * Hash table lookup.
 */
MobPrototype *get_mob_index (int vnum)
{
  std::map<int,MobPrototype*>::iterator pMobIndex;

  pMobIndex = mob_table.find(vnum);

  if (pMobIndex != mob_table.end())
      return (*pMobIndex).second;

  if (g_db->fBootDb) {
    fatal_printf ("Get_mob_index: bad vnum %d.", vnum);
  }

  return NULL;
}

/*
 * Translates mob virtual number to its obj index struct.
 * Hash table lookup.
 */
ObjectPrototype *get_obj_index (int vnum)
{
  std::map<int,ObjectPrototype*>::iterator pObjIndex;

  pObjIndex = obj_table.find(vnum);

  if (pObjIndex != obj_table.end())
      return (*pObjIndex).second;

  if (g_db->fBootDb) {
    fatal_printf ("Get_obj_index: bad vnum %d.", vnum);
  }

  return NULL;
}

/*
 * Translates mob virtual number to its room index struct.
 * Hash table lookup.
 */
Room *get_room_index (int vnum)
{
  std::map<int,Room*>::iterator pRoomIndex;

  pRoomIndex = room_table.find(vnum);

  if (pRoomIndex != room_table.end())
      return (*pRoomIndex).second;

  if (g_db->fBootDb) {
    fatal_printf ("Get_room_index: bad vnum %d.", vnum);
  }

  return NULL;
}

std::string get_title (int klass, int level, int sex)
{
  sqlite3_stmt *stmt = NULL;
  char *sql = sqlite3_mprintf(
    "SELECT title FROM titles WHERE class = %d AND level = %d and sex = %d",
    klass, level, sex == SEX_FEMALE ? 1 : 0);
  std::string str("");
  Database* db = Database::instance();

  if (sqlite3_prepare(db->database, sql, -1, &stmt, 0) != SQLITE_OK) {
    bug_printf("Could not prepare statement: %s", sqlite3_errmsg(db->database));
    sqlite3_free(sql);
    return str;
  }

  if (sqlite3_step(stmt) == SQLITE_ROW) {
    str.assign((const char*)sqlite3_column_text( stmt, 0 ));
  }

  sqlite3_finalize(stmt);
  sqlite3_free(sql);
  return str;
}

/*
 * Get an extra description from a list.
 */
std::string get_extra_descr (const std::string & name, std::list<ExtraDescription *> & ed)
{
  std::list<ExtraDescription *>::iterator e;
  for (e = ed.begin(); e != ed.end(); e++) {
    if (is_name (name, (*e)->keyword))
      return (*e)->description;
  }
  return "";
}

/*
 * Given a name, return the appropriate spec fun.
 */
SPEC_FUN *spec_lookup (const std::string & name)
{
  if (!str_cmp (name, "spec_breath_any"))
    return spec_breath_any;
  if (!str_cmp (name, "spec_breath_acid"))
    return spec_breath_acid;
  if (!str_cmp (name, "spec_breath_fire"))
    return spec_breath_fire;
  if (!str_cmp (name, "spec_breath_frost"))
    return spec_breath_frost;
  if (!str_cmp (name, "spec_breath_gas"))
    return spec_breath_gas;
  if (!str_cmp (name, "spec_breath_lightning"))
    return spec_breath_lightning;
  if (!str_cmp (name, "spec_cast_adept"))
    return spec_cast_adept;
  if (!str_cmp (name, "spec_cast_cleric"))
    return spec_cast_cleric;
  if (!str_cmp (name, "spec_cast_judge"))
    return spec_cast_judge;
  if (!str_cmp (name, "spec_cast_mage"))
    return spec_cast_mage;
  if (!str_cmp (name, "spec_cast_undead"))
    return spec_cast_undead;
  if (!str_cmp (name, "spec_executioner"))
    return spec_executioner;
  if (!str_cmp (name, "spec_fido"))
    return spec_fido;
  if (!str_cmp (name, "spec_guard"))
    return spec_guard;
  if (!str_cmp (name, "spec_janitor"))
    return spec_janitor;
  if (!str_cmp (name, "spec_mayor"))
    return spec_mayor;
  if (!str_cmp (name, "spec_poison"))
    return spec_poison;
  if (!str_cmp (name, "spec_thief"))
    return spec_thief;
  return 0;
}

/*
 * MOBprogram code block
*/
/* the functions */

/* This routine transfers between alpha and numeric forms of the
 *  mob_prog bitvector types. This allows the use of the words in the
 *  mob/script files.
 */
int mprog_name_to_type (const std::string & name)
{
  if (!str_cmp (name, "in_file_prog"))
    return IN_FILE_PROG;
  if (!str_cmp (name, "act_prog"))
    return ACT_PROG;
  if (!str_cmp (name, "speech_prog"))
    return SPEECH_PROG;
  if (!str_cmp (name, "rand_prog"))
    return RAND_PROG;
  if (!str_cmp (name, "fight_prog"))
    return FIGHT_PROG;
  if (!str_cmp (name, "hitprcnt_prog"))
    return HITPRCNT_PROG;
  if (!str_cmp (name, "death_prog"))
    return DEATH_PROG;
  if (!str_cmp (name, "entry_prog"))
    return ENTRY_PROG;
  if (!str_cmp (name, "greet_prog"))
    return GREET_PROG;
  if (!str_cmp (name, "all_greet_prog"))
    return ALL_GREET_PROG;
  if (!str_cmp (name, "give_prog"))
    return GIVE_PROG;
  if (!str_cmp (name, "bribe_prog"))
    return BRIBE_PROG;
  return (ERROR_PROG);
}

/* This routine reads in scripts of MOBprograms from a file */
MobProgram *mprog_file_read (const std::string & f, MobProgram *mprg, MobPrototype *pMobIndex)
{
  MobProgram *mprg2;
  std::ifstream progfile;
  bool done = false;
  char MOBProgfile[MAX_INPUT_LENGTH];

  snprintf (MOBProgfile, sizeof MOBProgfile, "%s%s", MOB_DIR, f.c_str());
  progfile.open (MOBProgfile, std::ifstream::in | std::ifstream::binary);
  if (!progfile.is_open()) {
    fatal_printf ("Mob:%d couldnt open mobprog file", pMobIndex->vnum);
  }
  mprg2 = mprg;
  switch (fread_letter (progfile)) {
  case '>':
    break;
  case '|':
    fatal_printf ("empty mobprog file.");
    break;
  default:
    fatal_printf ("in mobprog file syntax error.");
    break;
  }
  while (!done) {
    mprg2->type = mprog_name_to_type (fread_word (progfile));
    switch (mprg2->type) {
    case ERROR_PROG:
      fatal_printf ("mobprog file type error");
      break;
    case IN_FILE_PROG:
      fatal_printf ("mprog file contains a call to file.");
      break;
    default:
      pMobIndex->progtypes = pMobIndex->progtypes | mprg2->type;
      mprg2->arglist = fread_string (progfile);
      mprg2->comlist = fread_string (progfile);
      switch (fread_letter (progfile)) {
      case '>':
        mprg2->next = new MobProgram();
        mprg2 = mprg2->next;
        mprg2->next = NULL;
        break;
      case '|':
        done = true;
        break;
      default:
        fatal_printf ("in mobprog file syntax error.");
        break;
      }
      break;
    }
  }
  progfile.close();
  return mprg2;
}

/* This procedure is responsible for reading any in_file MOBprograms.
 */
void mprog_read_programs (std::ifstream & fp, MobPrototype *pMobIndex)
{
  MobProgram *mprg;
  bool done = false;

  if ((fread_letter (fp)) != '>') {
    fatal_printf ("Load_mobiles: vnum %d MOBPROG char", pMobIndex->vnum);
  }
  pMobIndex->mobprogs = new MobProgram();
  mprg = pMobIndex->mobprogs;
  while (!done) {
    mprg->type = mprog_name_to_type (fread_word (fp));
    switch (mprg->type) {
    case ERROR_PROG:
      fatal_printf ("Load_mobiles: vnum %d MOBPROG type.", pMobIndex->vnum);
      break;
    case IN_FILE_PROG:
      mprg = mprog_file_read (fread_string (fp), mprg, pMobIndex);
      fread_to_eol (fp);
      switch (fread_letter (fp)) {
      case '>':
        mprg->next = new MobProgram();
        mprg = mprg->next;
        mprg->next = NULL;
        break;
      case '|':
        mprg->next = NULL;
        fread_to_eol (fp);
        done = true;
        break;
      default:
        fatal_printf ("Load_mobiles: vnum %d bad MOBPROG.", pMobIndex->vnum);
        break;
      }
      break;
    default:
      pMobIndex->progtypes = pMobIndex->progtypes | mprg->type;
      mprg->arglist = fread_string (fp);
      fread_to_eol (fp);
      mprg->comlist = fread_string (fp);
      fread_to_eol (fp);
      switch (fread_letter (fp)) {
      case '>':
        mprg->next = new MobProgram();
        mprg = mprg->next;
        mprg->next = NULL;
        break;
      case '|':
        mprg->next = NULL;
        fread_to_eol (fp);
        done = true;
        break;
      default:
        fatal_printf ("Load_mobiles: vnum %d bad MOBPROG.", pMobIndex->vnum);
        break;
      }
      break;
    }
  }
}

/*
 * Create a 'money' obj.
 */
Object *create_money (int amount)
{
  char buf[MAX_STRING_LENGTH];
  Object *obj;

  if (amount <= 0) {
    bug_printf ("Create_money: zero or negative money %d.", amount);
    amount = 1;
  }

  if (amount == 1) {
    obj = get_obj_index(OBJ_VNUM_MONEY_ONE)->create_object(0);
  } else {
    obj = get_obj_index(OBJ_VNUM_MONEY_SOME)->create_object(0);
    snprintf (buf, sizeof buf, obj->short_descr.c_str(), amount);
    obj->short_descr = buf;
    obj->value[0] = amount;
  }

  return obj;
}

/*
 * It is very important that this be an equivalence relation:
 * (1) A ~ A
 * (2) if A ~ B then B ~ A
 * (3) if A ~ B  and B ~ C, then A ~ C
 */
bool is_same_group (Character * ach, Character * bch)
{
  if (ach->leader != NULL)
    ach = ach->leader;
  if (bch->leader != NULL)
    bch = bch->leader;
  return ach == bch;
}

bool Note::is_note_to (Character * ch)
{
  if (!str_cmp (ch->name, sender))
    return true;

  if (is_name ("all", to_list))
    return true;

  if (ch->is_hero() && is_name ("immortal", to_list))
    return true;

  if (is_name (ch->name, to_list))
    return true;

  return false;
}

void note_attach (Character * ch)
{
  Note *pnote;

  if (ch->pnote != NULL)
    return;

  pnote = new Note();

  pnote->sender = ch->name;
  ch->pnote = pnote;
  return;
}

void note_remove (Character * ch, Note * pnote)
{
  std::string to_new, to_one, to_list;

  /*
   * Build a new to_list.
   * Strip out this recipient.
   */
  to_list = pnote->to_list;
  while (!to_list.empty()) {
    to_list = one_argument (to_list, to_one);
    if (!to_list.empty() && str_cmp (ch->name, to_one)) {
      to_new.append(" ");
      to_new.append(to_one);
    }
  }

  /*
   * Just a simple recipient removal?
   */
  if (str_cmp (ch->name, pnote->sender) && !to_new.empty()) {
    pnote->to_list = to_new.substr(1);
    return;
  }

  /*
   * Remove note from linked list.
   */
  note_list.erase(find(note_list.begin(),note_list.end(),pnote));
  delete pnote;

  /*
   * Rewrite entire list.
   */
  std::ofstream notefile;

  notefile.open (NOTE_FILE, std::ofstream::out | std::ofstream::binary);
  if (!notefile.is_open()) {
    perror (NOTE_FILE);
  } else {
    for (std::list<Note*>::iterator p = note_list.begin();
      p != note_list.end(); p++) {
      notefile << "Sender  " << (*p)->sender << "~\n";
      notefile << "Date    " << (*p)->date << "~\n";
      notefile << "Stamp   " << (*p)->date_stamp << "\n";
      notefile << "To      " << (*p)->to_list << "~\n";
      notefile << "Subject " << (*p)->subject << "~\n";
      notefile << "Text\n" << (*p)->text << "~\n\n";
    }
    notefile.close();
  }
  return;
}

/* This routine handles the variables for command expansion.
 * If you want to add any go right ahead, it should be fairly
 * clear how it is done and they are quite easy to do, so you
 * can be as creative as you want. The only catch is to check
 * that your variables exist before you use them. At the moment,
 * using $t when the secondary target refers to an object
 * i.e. >prog_act drops~<nl>if ispc($t)<nl>sigh<nl>endif<nl>~<nl>
 * probably makes the mud crash (vice versa as well) The cure
 * would be to change act() so that vo becomes vict & v_obj.
 * but this would require a lot of small changes all over the code.
 */
void mprog_translate (char ch, std::string & t, Character * mob, Character * actor,
  Object * obj, void *vo, Character * rndm)
{
  static char *he_she[] = { "it", "he", "she" };
  static char *him_her[] = { "it", "him", "her" };
  static char *his_her[] = { "its", "his", "her" };
  Character *vict = (Character *) vo;
  Object *v_obj = (Object *) vo;

  t.erase();
  switch (ch) {
  case 'i':
    one_argument (mob->name, t);
    break;

  case 'I':
    t = mob->short_descr;
    break;

  case 'n':
    if (actor && mob->can_see(actor)) {
      one_argument (actor->name, t);
      if (!actor->is_npc ())
        t[0] = toupper(t[0]);
    }
    break;

  case 'N':
    if (actor) {
      if (mob->can_see(actor)) {
        if (actor->is_npc ())
          t = actor->short_descr;
        else
          t = actor->name + " " + actor->pcdata->title;
      } else {
        t = "someone";
      }
    }
    break;

  case 't':
    if (vict && mob->can_see(vict)) {
      one_argument (vict->name, t);
      if (!vict->is_npc ())
        t[0] = toupper(t[0]);
    }
    break;

  case 'T':
    if (vict) {
      if (mob->can_see(vict)) {
        if (vict->is_npc ())
          t = vict->short_descr;
        else
          t = vict->name + " " + vict->pcdata->title;
      } else {
        t = "someone";
      }
    }
    break;

  case 'r':
    if (rndm && mob->can_see(rndm)) {
      one_argument (rndm->name, t);
      if (!rndm->is_npc ())
        t[0] = toupper(t[0]);
    }
    break;

  case 'R':
    if (rndm) {
      if (mob->can_see(rndm)) {
        if (rndm->is_npc ())
          t = rndm->short_descr;
        else
          t = rndm->name + " " + rndm->pcdata->title;
      } else {
        t = "someone";
      }
    }
    break;

  case 'e':
    if (actor) {
      if (mob->can_see(actor))
        t = he_she[actor->sex];
      else
        t = "someone";
    }
    break;

  case 'm':
    if (actor) {
      if (mob->can_see(actor))
        t = him_her[actor->sex];
      else
        t = "someone";
    }
    break;

  case 's':
    if (actor) {
      if (mob->can_see(actor))
        t = his_her[actor->sex];
      else
        t = "someone's";
    }
    break;

  case 'E':
    if (vict) {
      if (mob->can_see(vict))
        t = he_she[vict->sex];
      else
        t = "someone";
    }
    break;

  case 'M':
    if (vict) {
      if (mob->can_see(vict))
        t = him_her[vict->sex];
      else
        t = "someone";
    }
    break;

  case 'S':
    if (vict) {
      if (mob->can_see(vict))
        t = his_her[vict->sex];
      else
        t = "someone's";
    }
    break;

  case 'j':
    t = he_she[mob->sex];
    break;

  case 'k':
    t = him_her[mob->sex];
    break;

  case 'l':
    t = his_her[mob->sex];
    break;

  case 'J':
    if (rndm) {
      if (mob->can_see(rndm))
        t = he_she[rndm->sex];
      else
        t = "someone";
    }
    break;

  case 'K':
    if (rndm) {
      if (mob->can_see(rndm))
        t = him_her[rndm->sex];
      else
        t = "someone";
    }
    break;

  case 'L':
    if (rndm) {
      if (mob->can_see(rndm))
        t = his_her[rndm->sex];
      else
        t = "someone's";
    }
    break;

  case 'o':
    if (obj) {
      if (mob->can_see_obj(obj))
        one_argument (obj->name, t);
      else
        t = "something";
    }
    break;

  case 'O':
    if (obj) {
      if (mob->can_see_obj(obj))
        t = obj->short_descr;
      else
        t = "something";
    }
    break;

  case 'p':
    if (v_obj) {
      if (mob->can_see_obj(v_obj))
        one_argument (v_obj->name, t);
      else
        t = "something";
    }
    break;

  case 'P':
    if (v_obj) {
      if (mob->can_see_obj(v_obj))
        t = v_obj->short_descr;
      else
        t = "something";
    }
    break;

  case 'a':
    if (obj)
      switch (obj->name[0]) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        t = "an";
        break;
      default:
        t = "a";
      }
    break;

  case 'A':
    if (v_obj)
      switch (v_obj->name[0]) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        t = "an";
        break;
      default:
        t = "a";
      }
    break;

  case '$':
    t = "$";
    break;

  default:
    bug_printf ("Mob: %d bad $var", mob->pIndexData->vnum);
    break;
  }

  return;

}

/* This procedure simply copies the cmnd to a buffer while expanding
 * any variables by calling the translate procedure.  The observant
 * code scrutinizer will notice that this is taken from act()
 */
void mprog_process_cmnd (const std::string & cmnd, Character * mob,
  Character * actor, Object * obj, void *vo, Character * rndm)
{
  std::string buf;
  std::string tmp;
  std::string::const_iterator str;
  str = cmnd.begin();

  while (str != cmnd.end()) {
    if (*str != '$') {
      buf.append(1, *str);
      str++;
      continue;
    }
    str++;
    mprog_translate (*str, tmp, mob, actor, obj, vo, rndm);
    buf.append(tmp);
    str++;
  }
  mob->interpret (buf);

  return;

}

/* Used to get sequential lines of a multi line string (separated by "\r\n")
 * Thus its like one_argument(), but a trifle different. It is destructive
 * to the multi line string argument, and thus clist must not be shared.
 */
std::string mprog_next_command (std::string & clist, std::string & cmd)
{
  std::string::iterator pointer = clist.begin();

  while (*pointer != '\n' && *pointer != '\r' && pointer != clist.end())
    pointer++;
  cmd.assign(clist.begin(), pointer);
  while ((*pointer == '\n' || *pointer == '\r') && pointer != clist.end())
    pointer++;
  return std::string(pointer, clist.end());
}

/* These two functions do the basic evaluation of ifcheck operators.
 *  It is important to note that the string operations are not what
 *  you probably expect.  Equality is exact and division is substring.
 *  remember that lhs has been stripped of leading space, but can
 *  still have trailing spaces so be careful when editing since:
 *  "guard" and "guard " are not equal.
 */
bool mprog_seval (const std::string & lhs, const std::string & opr, const std::string & rhs)
{

  if (!str_cmp (opr, "=="))
    return (bool) (!str_cmp (lhs, rhs));
  if (!str_cmp (opr, "!="))
    return (bool) (str_cmp (lhs, rhs));
  if (!str_cmp (opr, "/"))
    return (bool) (!str_infix (rhs, lhs));
  if (!str_cmp (opr, "!/"))
    return (bool) (str_infix (rhs, lhs));

  bug_printf ("Improper MOBprog operator");
  return 0;

}

bool mprog_veval (int lhs, const std::string & opr, int rhs)
{

  if (!str_cmp (opr, "=="))
    return (lhs == rhs);
  if (!str_cmp (opr, "!="))
    return (lhs != rhs);
  if (!str_cmp (opr, ">"))
    return (lhs > rhs);
  if (!str_cmp (opr, "<"))
    return (lhs < rhs);
  if (!str_cmp (opr, ">="))
    return (lhs <= rhs);
  if (!str_cmp (opr, ">="))
    return (lhs >= rhs);
  if (!str_cmp (opr, "&"))
    return (lhs & rhs);
  if (!str_cmp (opr, "|"))
    return (lhs | rhs);

  bug_printf ("Improper MOBprog operator\r\n");
  return 0;

}

/* This function performs the evaluation of the if checks.  It is
 * here that you can add any ifchecks which you so desire. Hopefully
 * it is clear from what follows how one would go about adding your
 * own. The syntax for an if check is: ifchck ( arg ) [opr val]
 * where the parenthesis are required and the opr and val fields are
 * optional but if one is there then both must be. The spaces are all
 * optional. The evaluation of the opr expressions is farmed out
 * to reduce the redundancy of the mammoth if statement list.
 * If there are errors, then return -1 otherwise return boolean 1,0
 */
bool mprog_do_ifchck (const std::string & ifchck, Character * mob,
  Character * actor, Object * obj, void *vo, Character * rndm)
{

  char buf[MAX_INPUT_LENGTH];
  char arg[MAX_INPUT_LENGTH];
  char opr[MAX_INPUT_LENGTH];
  char val[MAX_INPUT_LENGTH];
  Character *vict = (Character *) vo;
  Object *v_obj = (Object *) vo;
  char *bufpt = buf;
  char *argpt = arg;
  char *oprpt = opr;
  char *valpt = val;
  std::string::const_iterator point = ifchck.begin();
  int lhsvl;
  int rhsvl;

  if (ifchck.empty()) {
    bug_printf ("Mob: %d null ifchck", mob->pIndexData->vnum);
    return -1;
  }
  /* skip leading spaces */
  while (*point == ' ')
    point++;

  /* get whatever comes before the left paren.. ignore spaces */
  while (*point != '(')
    if (point == ifchck.end()) {
      bug_printf ("Mob: %d ifchck syntax error", mob->pIndexData->vnum);
      return -1;
    } else if (*point == ' ')
      point++;
    else
      *bufpt++ = *point++;

  *bufpt = '\0';
  point++;

  /* get whatever is in between the parens.. ignore spaces */
  while (*point != ')')
    if (point == ifchck.end()) {
      bug_printf ("Mob: %d ifchck syntax error", mob->pIndexData->vnum);
      return -1;
    } else if (*point == ' ')
      point++;
    else
      *argpt++ = *point++;

  *argpt = '\0';
  point++;

  /* check to see if there is an operator */
  while (*point == ' ')
    point++;
  if (point == ifchck.end()) {
    *opr = '\0';
    *val = '\0';
  } else {                      /* there should be an operator and value, so get them */

    while ((*point != ' ') && (!isalnum (*point)))
      if (point == ifchck.end()) {
        bug_printf ("Mob: %d ifchck operator without value", mob->pIndexData->vnum);
        return -1;
      } else
        *oprpt++ = *point++;

    *oprpt = '\0';

    /* finished with operator, skip spaces and then get the value */
    while (*point == ' ')
      point++;
    for (;;) {
      if ((*point != ' ') && (point == ifchck.end()))
        break;
      else
        *valpt++ = *point++;
    }

    *valpt = '\0';
  }
//  bufpt = buf;
//  argpt = arg;
//  oprpt = opr;
//  valpt = val;

  /* Ok... now buf contains the ifchck, arg contains the inside of the
   *  parentheses, opr contains an operator if one is present, and val
   *  has the value if an operator was present.
   *  So.. basically use if statements and run over all known ifchecks
   *  Once inside, use the argument and expand the lhs. Then if need be
   *  send the lhs,opr,rhs off to be evaluated.
   */

  if (!str_cmp (buf, "rand")) {
    return (number_percent () <= atoi (arg));
  }

  if (!str_cmp (buf, "ispc")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return 0;
    case 'n':
      if (actor)
        return (!actor->is_npc ());
      else
        return -1;
    case 't':
      if (vict)
        return (!vict->is_npc ());
      else
        return -1;
    case 'r':
      if (rndm)
        return (!rndm->is_npc ());
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'ispc'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "isnpc")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return 1;
    case 'n':
      if (actor)
        return actor->is_npc ();
      else
        return -1;
    case 't':
      if (vict)
        return vict->is_npc ();
      else
        return -1;
    case 'r':
      if (rndm)
        return rndm->is_npc ();
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'isnpc'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "isgood")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return mob->is_good ();
    case 'n':
      if (actor)
        return actor->is_good ();
      else
        return -1;
    case 't':
      if (vict)
        return vict->is_good ();
      else
        return -1;
    case 'r':
      if (rndm)
        return rndm->is_good ();
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'isgood'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "isfight")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return (mob->fighting) ? 1 : 0;
    case 'n':
      if (actor)
        return (actor->fighting) ? 1 : 0;
      else
        return -1;
    case 't':
      if (vict)
        return (vict->fighting) ? 1 : 0;
      else
        return -1;
    case 'r':
      if (rndm)
        return (rndm->fighting) ? 1 : 0;
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'isfight'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "isimmort")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return (mob->get_trust () > LEVEL_IMMORTAL);
    case 'n':
      if (actor)
        return (actor->get_trust () > LEVEL_IMMORTAL);
      else
        return -1;
    case 't':
      if (vict)
        return (vict->get_trust () > LEVEL_IMMORTAL);
      else
        return -1;
    case 'r':
      if (rndm)
        return (rndm->get_trust () > LEVEL_IMMORTAL);
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'isimmort'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "ischarmed")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return mob->is_affected (AFF_CHARM);
    case 'n':
      if (actor)
        return actor->is_affected (AFF_CHARM);
      else
        return -1;
    case 't':
      if (vict)
        return vict->is_affected (AFF_CHARM);
      else
        return -1;
    case 'r':
      if (rndm)
        return rndm->is_affected (AFF_CHARM);
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'ischarmed'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "isfollow")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return (mob->master != NULL && mob->master->in_room == mob->in_room);
    case 'n':
      if (actor)
        return (actor->master != NULL
          && actor->master->in_room == actor->in_room);
      else
        return -1;
    case 't':
      if (vict)
        return (vict->master != NULL
          && vict->master->in_room == vict->in_room);
      else
        return -1;
    case 'r':
      if (rndm)
        return (rndm->master != NULL
          && rndm->master->in_room == rndm->in_room);
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'isfollow'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "isaffected")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return (mob->affected_by & atoi (arg));
    case 'n':
      if (actor)
        return (actor->affected_by & atoi (arg));
      else
        return -1;
    case 't':
      if (vict)
        return (vict->affected_by & atoi (arg));
      else
        return -1;
    case 'r':
      if (rndm)
        return (rndm->affected_by & atoi (arg));
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'isaffected'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "hitprcnt")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->hit / mob->max_hit;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->hit / actor->max_hit;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->hit / vict->max_hit;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->hit / rndm->max_hit;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'hitprcnt'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "inroom")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->in_room->vnum;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->in_room->vnum;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->in_room->vnum;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->in_room->vnum;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'inroom'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "sex")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->sex;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->sex;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->sex;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->sex;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'sex'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "position")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->position;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->position;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->position;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->position;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'position'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "level")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->get_trust ();
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->get_trust ();
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->get_trust ();
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->get_trust ();
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'level'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "class")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->klass;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->klass;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->klass;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->klass;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'class'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "goldamt")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->gold;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        lhsvl = actor->gold;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 't':
      if (vict) {
        lhsvl = vict->gold;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'r':
      if (rndm) {
        lhsvl = rndm->gold;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'goldamt'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "objtype")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'o':
      if (obj) {
        lhsvl = obj->item_type;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'p':
      if (v_obj) {
        lhsvl = v_obj->item_type;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'objtype'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "objval0")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'o':
      if (obj) {
        lhsvl = obj->value[0];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'p':
      if (v_obj) {
        lhsvl = v_obj->value[0];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'objval0'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "objval1")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'o':
      if (obj) {
        lhsvl = obj->value[1];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'p':
      if (v_obj) {
        lhsvl = v_obj->value[1];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'objval1'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "objval2")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'o':
      if (obj) {
        lhsvl = obj->value[2];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'p':
      if (v_obj) {
        lhsvl = v_obj->value[2];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'objval2'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "objval3")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'o':
      if (obj) {
        lhsvl = obj->value[3];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'p':
      if (v_obj) {
        lhsvl = v_obj->value[3];
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'objval3'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "number")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      lhsvl = mob->gold;
      rhsvl = atoi (val);
      return mprog_veval (lhsvl, opr, rhsvl);
    case 'n':
      if (actor) {
        if (actor->is_npc ()) {
          lhsvl = actor->pIndexData->vnum;
          rhsvl = atoi (val);
          return mprog_veval (lhsvl, opr, rhsvl);
          }
      } else
        return -1;
    case 't':
      if (vict) {
        if (actor->is_npc ()) {
          lhsvl = vict->pIndexData->vnum;
          rhsvl = atoi (val);
          return mprog_veval (lhsvl, opr, rhsvl);
          }
      } else
        return -1;
    case 'r':
      if (rndm) {
        if (actor->is_npc ()) {
          lhsvl = rndm->pIndexData->vnum;
          rhsvl = atoi (val);
          return mprog_veval (lhsvl, opr, rhsvl);
          }
      } else
        return -1;
    case 'o':
      if (obj) {
        lhsvl = obj->pIndexData->vnum;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    case 'p':
      if (v_obj) {
        lhsvl = v_obj->pIndexData->vnum;
        rhsvl = atoi (val);
        return mprog_veval (lhsvl, opr, rhsvl);
      } else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'number'", mob->pIndexData->vnum);
      return -1;
    }
  }

  if (!str_cmp (buf, "name")) {
    switch (arg[1]) {           /* arg should be "$*" so just get the letter */
    case 'i':
      return mprog_seval (mob->name, opr, val);
    case 'n':
      if (actor)
        return mprog_seval (actor->name, opr, val);
      else
        return -1;
    case 't':
      if (vict)
        return mprog_seval (vict->name, opr, val);
      else
        return -1;
    case 'r':
      if (rndm)
        return mprog_seval (rndm->name, opr, val);
      else
        return -1;
    case 'o':
      if (obj)
        return mprog_seval (obj->name, opr, val);
      else
        return -1;
    case 'p':
      if (v_obj)
        return mprog_seval (v_obj->name, opr, val);
      else
        return -1;
    default:
      bug_printf ("Mob: %d bad argument to 'name'", mob->pIndexData->vnum);
      return -1;
    }
  }

  /* Ok... all the ifchcks are done, so if we didnt find ours then something
   * odd happened.  So report the bug and abort the MOBprogram (return error)
   */
  bug_printf ("Mob: %d unknown ifchck", mob->pIndexData->vnum);
  return -1;

}

/* Quite a long and arduous function, this guy handles the control
 * flow part of MOBprograms.  Basicially once the driver sees an
 * 'if' attention shifts to here.  While many syntax errors are
 * caught, some will still get through due to the handling of break
 * and errors in the same fashion.  The desire to break out of the
 * recursion without catastrophe in the event of a mis-parse was
 * believed to be high. Thus, if an error is found, it is bugged and
 * the parser acts as though a break were issued and just bails out
 * at that point. I havent tested all the possibilites, so I'm speaking
 * in theory, but it is 'guaranteed' to work on syntactically correct
 * MOBprograms, so if the mud crashes here, check the mob carefully!
 */
std::string mprog_process_if (const std::string & ifchck, std::string & com_list,
  Character * mob, Character * actor, Object * obj, void *vo, Character * rndm)
{
  std::string buf;
  std::string morebuf;
  std::string cmnd;
  bool loopdone = false;
  bool flag = false;
  int legal;

  /* check for trueness of the ifcheck */
  if ((legal = mprog_do_ifchck (ifchck, mob, actor, obj, vo, rndm))) {
    if (legal == 1)
      flag = true;
    else
      return "";
  }

  while (loopdone == false) {   /*scan over any existing or statements */
    com_list = mprog_next_command (com_list, cmnd);
    cmnd.erase(0, cmnd.find_first_not_of(" "));
    if (cmnd.empty()) {
      bug_printf ("Mob: %d no commands after IF/OR", mob->pIndexData->vnum);
      return "";
    }
    morebuf = one_argument (cmnd, buf);
    if (!str_cmp (buf, "or")) {
      if ((legal = mprog_do_ifchck (morebuf, mob, actor, obj, vo, rndm))) {
        if (legal == 1)
          flag = true;
        else
          return "";
      }
    } else
      loopdone = true;
  }

  if (flag)
    for (;;) {                  /*ifcheck was true, do commands but ignore else to endif */
      if (!str_cmp (buf, "if")) {
        com_list =
          mprog_process_if (morebuf, com_list, mob, actor, obj, vo, rndm);
        cmnd.erase(0, cmnd.find_first_not_of(" "));
        if (com_list.empty())
          return "";
        com_list = mprog_next_command (com_list, cmnd);
        morebuf = one_argument (cmnd, buf);
        continue;
      }
      if (!str_cmp (buf, "break"))
        return "";
      if (!str_cmp (buf, "endif"))
        return com_list;
      if (!str_cmp (buf, "else")) {
        while (str_cmp (buf, "endif")) {
          com_list = mprog_next_command (com_list, cmnd);
          cmnd.erase(0, cmnd.find_first_not_of(" "));
          if (cmnd.empty()) {
            bug_printf ("Mob: %d missing endif after else", mob->pIndexData->vnum);
            return "";
          }
          morebuf = one_argument (cmnd, buf);
        }
        return com_list;
      }
      mprog_process_cmnd (cmnd, mob, actor, obj, vo, rndm);
      com_list = mprog_next_command (com_list, cmnd);
      cmnd.erase(0, cmnd.find_first_not_of(" "));
      if (cmnd.empty()) {
        bug_printf ("Mob: %d missing else or endif", mob->pIndexData->vnum);
        return "";
      }
      morebuf = one_argument (cmnd, buf);
  } else {                      /*false ifcheck, find else and do existing commands or quit at endif */

    while ((str_cmp (buf, "else")) && (str_cmp (buf, "endif"))) {
      com_list = mprog_next_command (com_list, cmnd);
      cmnd.erase(0, cmnd.find_first_not_of(" "));
      if (cmnd.empty()) {
        bug_printf ("Mob: %d missing an else or endif", mob->pIndexData->vnum);
        return "";
      }
      morebuf = one_argument (cmnd, buf);
    }

    /* found either an else or an endif.. act accordingly */
    if (!str_cmp (buf, "endif"))
      return com_list;
    com_list = mprog_next_command (com_list, cmnd);
    cmnd.erase(0, cmnd.find_first_not_of(" "));
    if (cmnd.empty()) {
      bug_printf ("Mob: %d missing endif", mob->pIndexData->vnum);
      return "";
    }
    morebuf = one_argument (cmnd, buf);

    for (;;) {                  /*process the post-else commands until an endif is found. */
      if (!str_cmp (buf, "if")) {
        com_list = mprog_process_if (morebuf, com_list, mob, actor,
          obj, vo, rndm);
        cmnd.erase(0, cmnd.find_first_not_of(" "));
        if (com_list.empty())
          return "";
        com_list = mprog_next_command (com_list, cmnd);
        morebuf = one_argument (cmnd, buf);
        continue;
      }
      if (!str_cmp (buf, "else")) {
        bug_printf ("Mob: %d found else in an else section", mob->pIndexData->vnum);
        return "";
      }
      if (!str_cmp (buf, "break"))
        return "";
      if (!str_cmp (buf, "endif"))
        return com_list;
      mprog_process_cmnd (cmnd, mob, actor, obj, vo, rndm);
      com_list = mprog_next_command (com_list, cmnd);
      cmnd.erase(0, cmnd.find_first_not_of(" "));
      if (cmnd.empty()) {
        bug_printf ("Mob:%d missing endif in else section", mob->pIndexData->vnum);
        return "";
      }
      morebuf = one_argument (cmnd, buf);
    }
  }
}

/* The main focus of the MOBprograms.  This routine is called
 *  whenever a trigger is successful.  It is responsible for parsing
 *  the command list and figuring out what to do. However, like all
 *  complex procedures, everything is farmed out to the other guys.
 */
void mprog_driver (const std::string & com_list, Character * mob,
  Character * actor, Object * obj, void *vo)
{

  std::string tmpcmndlst;
  std::string buf;
  std::string morebuf;
  std::string command_list;
  std::string cmnd;
  Character *rndm = NULL;
  int count = 0;

  if (mob->is_affected (AFF_CHARM))
      return;

  /* get a random visable mortal player who is in the room with the mob */
  CharIter vch;
  for (vch = mob->in_room->people.begin(); vch != mob->in_room->people.end(); vch++)
    if (!(*vch)->is_npc ()
      && (*vch)->level < LEVEL_IMMORTAL && mob->can_see(*vch)) {
      if (number_range (0, count) == 0)
        rndm = *vch;
      count++;
    }

  tmpcmndlst = com_list;
  command_list = tmpcmndlst;
  command_list = mprog_next_command (command_list, cmnd);
  while (!cmnd.empty()) {
    morebuf = one_argument (cmnd, buf);
    if (!str_cmp (buf, "if"))
      command_list = mprog_process_if (morebuf, command_list, mob,
        actor, obj, vo, rndm);
    else
      mprog_process_cmnd (cmnd, mob, actor, obj, vo, rndm);
    command_list = mprog_next_command (command_list, cmnd);
  }

  return;

}

/* The next two routines are the basic trigger types. Either trigger
 *  on a certain percent, or trigger on a keyword or word phrase.
 *  To see how this works, look at the various trigger routines..
 */
void mprog_wordlist_check (const std::string & arg, Character * mob,
  Character * actor, Object * obj, void *vo, int type)
{
  std::string list;
  std::string dupl;
  std::string word;
  MobProgram *mprg;

  std::string::size_type start;
  std::string::size_type end;

  unsigned int i;

  for (mprg = mob->pIndexData->mobprogs; mprg != NULL; mprg = mprg->next)
    if (mprg->type & type) {
      list = mprg->arglist;
      for (i = 0; i < list.size(); i++)
        list[i] = tolower (list[i]);
      dupl = arg;
      for (i = 0; i < dupl.size(); i++)
        dupl[i] = tolower (dupl[i]);

      if (list.substr(0,2) == "p ") {
        list = list.substr(2);
        while ((start = dupl.find(list)) != std::string::npos)
          if ((start == 0 || dupl[start - 1] == ' ')
            && (dupl[end = start + list.size()] == ' '
              || dupl[end] == '\n' || dupl[end] == '\r' || dupl[end] == '\0')) {
            mprog_driver (mprg->comlist, mob, actor, obj, vo);
            break;
          } else
            dupl = dupl.substr(start + 1);
      } else {
        list = one_argument (list, word);
        for (; !word.empty(); list = one_argument (list, word))
          while ((start = dupl.find(word)) != std::string::npos)
            if ((start == 0 || dupl[start - 1] == ' ')
              && (dupl[end = start + word.size()] == ' '
              || dupl[end] == '\n' || dupl[end] == '\r' || dupl[end] == '\0')) {
              mprog_driver (mprg->comlist, mob, actor, obj, vo);
              break;
            } else
              dupl = dupl.substr(start + 1);
      }
    }

  return;
}

/*
 * Mob autonomous action.
 * This function takes 25% to 35% of ALL Merc cpu time.
 * -- Furey
 */
void mobile_update (void)
{
  Character *ch;
  Exit *pexit;
  int door;
try {
  /* Examine all mobs. */
  CharIter c;
  for (c = char_list.begin(); c != char_list.end(); c = deepchnext) {
    ch = *c;
    deepchnext = ++c;

    if (!ch->is_npc () || ch->in_room == NULL || ch->is_affected (AFF_CHARM))
      continue;

    /* Examine call for special procedure */
    if (ch->spec_fun != 0) {
      if ((*ch->spec_fun) (ch))
        continue;
    }

    /* That's all for sleeping / busy monster */
    if (ch->position < POS_STANDING)
      continue;

    /* MOBprogram random trigger */
    if (ch->in_room->area->nplayer > 0) {
      mprog_random_trigger (ch);
      /* If ch dies or changes
         position due to it's random
         trigger continue - Kahn */
      if (ch->position < POS_STANDING)
        continue;
    }

    /* Scavenge */
    if (IS_SET (ch->actflags, ACT_SCAVENGER)
      && !ch->in_room->contents.empty() && number_percent() <= 25) {
      Object *obj_best;
      int max;

      max = 1;
      obj_best = 0;
      ObjIter obj;
      for (obj = ch->in_room->contents.begin(); obj != ch->in_room->contents.end(); obj++) {
        if ((*obj)->can_wear(ITEM_TAKE) && (*obj)->cost > max) {
          obj_best = *obj;
          max = (*obj)->cost;
        }
      }

      if (obj_best) {
        obj_best->obj_from_room ();
        obj_best->obj_to_char (ch);
        ch->act ("$n gets $p.", obj_best, NULL, TO_ROOM);
      }
    }

    /* Wander */
    if (!IS_SET (ch->actflags, ACT_SENTINEL)
      && (door = number_range (0, 31)) <= 5
      && (pexit = ch->in_room->exit[door]) != NULL
      && pexit->to_room != NULL && !IS_SET (pexit->exit_info, EX_CLOSED)
      && !IS_SET (pexit->to_room->room_flags, ROOM_NO_MOB)
      && (!IS_SET (ch->actflags, ACT_STAY_AREA)
        || pexit->to_room->area == ch->in_room->area)) {
      ch->move_char (door);
      /* If ch changes position due
         to it's or someother mob's
         movement via MOBProgs,
         continue - Kahn */
      if (ch->position < POS_STANDING)
        continue;
    }

    /* Flee */
    if (ch->hit < (ch->max_hit / 2)
      && (door = number_range (0, 7)) <= 5
      && (pexit = ch->in_room->exit[door]) != NULL
      && pexit->to_room != NULL && !IS_SET (pexit->exit_info, EX_CLOSED)
      && !IS_SET (pexit->to_room->room_flags, ROOM_NO_MOB)) {
      bool found;

      found = false;
      CharIter rch;
      for (rch = pexit->to_room->people.begin(); rch != pexit->to_room->people.end(); rch++) {
        if (!(*rch)->is_npc ()) {
          found = true;
          break;
        }
      }
      if (!found)
        ch->move_char (door);
    }

  }

} catch (...) {
  fatal_printf("mobile_update() exception");
}
  return;
}

/*
 * Update the weather.
 */
void weather_update (void)
{
  std::string buf = g_world->weather_update();

  if (!buf.empty()) {
    for (DescIter d = descriptor_list.begin();
      d != descriptor_list.end(); d++) {
      if ((*d)->connected == CON_PLAYING && (*d)->character->is_outside()
        && (*d)->character->is_awake ())
        (*d)->character->send_to_char (buf);
    }
  }

  return;
}

/*
 * Update all chars, including mobs.
 * This function is performance sensitive.
 */
void char_update (void)
{
  Character *ch;
  Character *ch_save;
  Character *ch_quit;
  time_t save_time;

try {
  save_time = g_world->get_current_time();
  ch_save = NULL;
  ch_quit = NULL;
  CharIter c;
  for (c = char_list.begin(); c != char_list.end(); c = deepchnext) {
    Affect *paf;
    ch = *c;
    deepchnext = ++c;

    /*
     * Find dude with oldest save time.
     */
    if (!ch->is_npc ()
      && (ch->desc == NULL || ch->desc->connected == CON_PLAYING)
      && ch->level >= 2 && ch->save_time < save_time) {
      ch_save = ch;
      save_time = ch->save_time;
    }

    if (ch->position >= POS_STUNNED) {
      if (ch->hit < ch->max_hit)
        ch->hit += ch->hit_gain();

      if (ch->mana < ch->max_mana)
        ch->mana += ch->mana_gain ();

      if (ch->move < ch->max_move)
        ch->move += ch->move_gain();
    }

    if (ch->position == POS_STUNNED)
      ch->update_pos();

    if (!ch->is_npc () && ch->level < LEVEL_IMMORTAL) {
      Object *obj;

      if ((obj = ch->get_eq_char (WEAR_LIGHT)) != NULL
        && obj->item_type == ITEM_LIGHT && obj->value[2] > 0) {
        if (--obj->value[2] == 0 && ch->in_room != NULL) {
          --ch->in_room->light;
          ch->act ("$p goes out.", obj, NULL, TO_ROOM);
          ch->act ("$p goes out.", obj, NULL, TO_CHAR);
          obj->extract_obj ();
        }
      }

      if (++ch->timer >= 12) {
        if (ch->was_in_room == NULL && ch->in_room != NULL) {
          ch->was_in_room = ch->in_room;
          if (ch->fighting != NULL)
            ch->stop_fighting (true);
          ch->act ("$n disappears into the void.", NULL, NULL, TO_ROOM);
          ch->send_to_char ("You disappear into the void.\r\n");
          ch->save_char_obj();
          ch->char_from_room();
          ch->char_to_room(get_room_index (ROOM_VNUM_LIMBO));
        }
      }

      if (ch->timer > 30)
        ch_quit = ch;

      ch->gain_condition (COND_DRUNK, -1);
      ch->gain_condition (COND_FULL, -1);
      ch->gain_condition (COND_THIRST, -1);
    }

    AffIter af, next;
    for (af = ch->affected.begin(); af != ch->affected.end(); af = next) {
      paf = *af;
      next = ++af;
      if (paf->duration > 0)
        paf->duration--;
      else if (paf->duration < 0);
      else {
        if (next == ch->affected.end()
          || (*next)->type != paf->type || (*next)->duration > 0) {
          if (paf->type > 0 && skill_table[paf->type].msg_off[0] != '\0') {
            ch->send_to_char (skill_table[paf->type].msg_off);
            ch->send_to_char ("\r\n");
          }
        }

        ch->affect_remove (paf);
      }
    }

    /*
     * Careful with the damages here,
     *   MUST NOT refer to ch after damage taken,
     *   as it may be lethal damage (on NPC).
     */
    if (ch->is_affected (AFF_POISON)) {
      ch->act ("$n shivers and suffers.", NULL, NULL, TO_ROOM);
      ch->send_to_char ("You shiver and suffer.\r\n");
      damage (ch, ch, 2, skill_lookup("poison"));
    } else if (ch->position == POS_INCAP) {
      damage (ch, ch, 1, TYPE_UNDEFINED);
    } else if (ch->position == POS_MORTAL) {
      damage (ch, ch, 2, TYPE_UNDEFINED);
    }
  }

  /*
   * Autosave and autoquit.
   * Check that these chars still exist.
   */
  if (ch_save != NULL || ch_quit != NULL) {
    CharIter cnext;
    for (c = char_list.begin(); c != char_list.end(); c = cnext) {
      ch = *c;
      cnext = ++c;
      if (ch == ch_save)
        ch->save_char_obj();
      if (ch == ch_quit)
        ch->do_quit ("");
    }
  }
} catch (...) {
  fatal_printf("char_update() exception");
}

  return;
}

/*
 * Update all objs.
 * This function is performance sensitive.
 */
void obj_update (void)
{
  Object *obj;
  ObjIter o;

try {
  for (o = object_list.begin(); o != object_list.end(); o = deepobnext) {
    char *message;
    obj = *o;
    deepobnext = ++o;

    if (obj->timer <= 0 || --obj->timer > 0)
      continue;

    switch (obj->item_type) {
    default:
      message = "$p vanishes.";
      break;
    case ITEM_FOUNTAIN:
      message = "$p dries up.";
      break;
    case ITEM_CORPSE_NPC:
      message = "$p decays into dust.";
      break;
    case ITEM_CORPSE_PC:
      message = "$p decays into dust.";
      break;
    case ITEM_FOOD:
      message = "$p decomposes.";
      break;
    }

    if (obj->carried_by != NULL) {
      obj->carried_by->act (message, obj, NULL, TO_CHAR);
    } else if (obj->in_room != NULL && !obj->in_room->people.empty()) {
      Character *rch = obj->in_room->people.front();
      rch->act (message, obj, NULL, TO_ROOM);
      rch->act (message, obj, NULL, TO_CHAR);
    }

    obj->extract_obj ();
  }
} catch (...) {
  fatal_printf("obj_update() exception");
}

  return;
}

/*
 * Aggress.
 *
 * for each mortal PC
 *     for each mob in room
 *         aggress on some random PC
 *
 * This function takes 25% to 35% of ALL Merc cpu time.
 * Unfortunately, checking on each PC move is too tricky,
 *   because we don't the mob to just attack the first PC
 *   who leads the party into the room.
 *
 * -- Furey
 */
void aggr_update (void)
{
  Character *wch;
  Character *ch;
  Character *vch;
  Character *victim;

try {
  CharIter c;
  for (c = char_list.begin(); c != char_list.end(); c = deepchnext) {
    wch = *c;
    deepchnext = ++c;

    /* MOBProgram ACT_PROG trigger */
    if (wch->is_npc () && wch->mpactnum > 0 && wch->in_room->area->nplayer > 0) {
      MobProgramActList *tmp_act, *tmp2_act;
      for (tmp_act = wch->mpact; tmp_act != NULL; tmp_act = tmp_act->next) {
        mprog_wordlist_check (tmp_act->buf, wch, tmp_act->ch,
          tmp_act->obj, tmp_act->vo, ACT_PROG);
      }
      for (tmp_act = wch->mpact; tmp_act != NULL; tmp_act = tmp2_act) {
        tmp2_act = tmp_act->next;
        delete tmp_act;
      }
      wch->mpactnum = 0;
      wch->mpact = NULL;
    }

    if (wch->is_npc ()
      || wch->level >= LEVEL_IMMORTAL || wch->in_room == NULL)
      continue;

    Room* rlist = wch->in_room;

    CharIter rch, rcnext;
    for (rch = rlist->people.begin(); rch != rlist->people.end(); rch = deeprmnext) {
      ch = *rch;
      deeprmnext = ++rch;
      int count;

      if (!ch->is_npc ()
        || !IS_SET (ch->actflags, ACT_AGGRESSIVE)
        || ch->fighting != NULL || ch->is_affected (AFF_CHARM)
        || !ch->is_awake ()
        || (IS_SET (ch->actflags, ACT_WIMPY) && wch->is_awake ())
        || !ch->can_see(wch))
        continue;

      /*
       * Ok we have a 'wch' player character and a 'ch' npc aggressor.
       * Now make the aggressor fight a RANDOM pc victim in the room,
       *   giving each 'vch' an equal chance of selection.
       */
      count = 0;
      victim = NULL;
      for (CharIter vc = rlist->people.begin(); vc != rlist->people.end(); vc++) {
        if (!(*vc)->is_npc () && (*vc)->level < LEVEL_IMMORTAL
          && (!IS_SET (ch->actflags, ACT_WIMPY) || !(*vc)->is_awake ())
          && ch->can_see(*vc)) {
          if (number_range (0, count) == 0)
            victim = *vc;
          count++;
        }
      }

      if (victim == NULL) {
        bug_printf ("Aggr_update: null victim.", count);
        continue;
      }

      multi_hit (ch, victim, TYPE_UNDEFINED);
    }
  }

} catch (...) {
  fatal_printf("aggr_update() exception");
}

  return;
}

/*
 * Control the fights going on.
 * Called periodically by update_handler.
 */
void violence_update (void)
{
  Character *ch;
  Character *victim;
  Character *rch;

try {

  CharIter c;
  for (c = char_list.begin(); c != char_list.end(); c = deepchnext) {
    ch = *c;
    deepchnext = ++c;

    victim = ch->fighting;
    if (victim == NULL || ch->in_room == NULL)
      continue;

    if (ch->is_awake () && ch->in_room == victim->in_room)
      multi_hit (ch, victim, TYPE_UNDEFINED);
    else
      ch->stop_fighting (false);

    victim = ch->fighting;
    if (victim == NULL)
      continue;

    mprog_hitprcnt_trigger (ch, victim);
    mprog_fight_trigger (ch, victim);

    /*
     * Fun for the whole family!
     */

    if (ch == NULL || ch->in_room == NULL)
      continue;
    victim = ch->fighting;
    if (victim == NULL)
      continue;
    Room * rlist = ch->in_room;

    CharIter rc, rnext;
    for (rc = rlist->people.begin(); rc != rlist->people.end(); rc = deeprmnext) {
      rch = *rc;
      deeprmnext = ++rc;

      if (rch->fighting == NULL && rch->is_awake ()) {
        /*
         * PC's auto-assist others in their group.
         */
        if (!ch->is_npc () || ch->is_affected (AFF_CHARM)) {
          if ((!rch->is_npc () || rch->is_affected (AFF_CHARM))
            && is_same_group (ch, rch))
            multi_hit (rch, victim, TYPE_UNDEFINED);
          continue;
        }

        /*
         * NPC's assist NPC's of same type or 12.5% chance regardless.
         */
        if (rch->is_npc () && !rch->is_affected (AFF_CHARM)) {
          if (rch->pIndexData == ch->pIndexData || number_range (0, 7) == 0) {
            Character *target;
            int number;

            target = NULL;
            number = 0;
            CharIter vch;
            for (vch = rlist->people.begin(); vch != rlist->people.end(); vch++) {
              if (rch->can_see(*vch)
                && is_same_group (*vch, victim)
                && number_range (0, number) == 0) {
                target = *vch;
                number++;
              }
            }

            if (target != NULL) {
              if ((((target->level - rch->level <= 4)
                    && (target->level - rch->level >= -4))
                  && !(rch->is_good () && target->is_good ()))
                || (rch->is_evil () || target->is_evil ()))
                multi_hit (rch, target, TYPE_UNDEFINED);
            }
          }
        }
      }
    }
  }

} catch (...) {
  fatal_printf("violence_update() exception");
}

  return;
}

/*
 * Handle all kinds of updates.
 * Called once per pulse from game loop.
 * Random times to defeat tick-timing clients and players.
 */
void update_handler (void)
{
  static int pulse_area;
  static int pulse_mobile;
  static int pulse_violence;
  static int pulse_point;

try {
  if (--pulse_area <= 0) {
    pulse_area = number_range (PULSE_AREA / 2, 3 * PULSE_AREA / 2);
    g_world->area_update ();
  }

  if (--pulse_violence <= 0) {
    pulse_violence = PULSE_VIOLENCE;
    violence_update ();
  }

  if (--pulse_mobile <= 0) {
    pulse_mobile = PULSE_MOBILE;
    mobile_update ();
  }

  if (--pulse_point <= 0) {
    pulse_point = number_range (PULSE_TICK / 2, 3 * PULSE_TICK / 2);
    weather_update ();
    char_update ();
    obj_update ();
  }

  aggr_update ();
} catch (...) {
  fatal_printf("update_handler() exception");
}
  return;
}

/*
 * Shopping commands.
 */
Character *find_keeper (Character * ch)
{
  char buf[MAX_STRING_LENGTH];
  Shop *pShop;

  pShop = NULL;
  CharIter keeper;
  for (keeper = ch->in_room->people.begin(); keeper != ch->in_room->people.end(); keeper++) {
    if ((*keeper)->is_npc () && (pShop = (*keeper)->pIndexData->pShop) != NULL)
      break;
  }

  if (pShop == NULL) {
    ch->send_to_char ("You can't do that here.\r\n");
    return NULL;
  }

  /*
   * Undesirables.
   */
  if (!ch->is_npc () && IS_SET (ch->actflags, PLR_KILLER)) {
    (*keeper)->do_say ("Killers are not welcome!");
    snprintf (buf, sizeof buf, "%s the KILLER is over here!\r\n", ch->name.c_str());
    (*keeper)->do_shout (buf);
    return NULL;
  }

  if (!ch->is_npc () && IS_SET (ch->actflags, PLR_THIEF)) {
    (*keeper)->do_say ("Thieves are not welcome!");
    snprintf (buf, sizeof buf, "%s the THIEF is over here!\r\n", ch->name.c_str());
    (*keeper)->do_shout (buf);
    return NULL;
  }

  /*
   * Shop hours.
   */
  if (g_world->hour() < pShop->open_hour) {
    (*keeper)->do_say ("Sorry, come back later.");
    return NULL;
  }

  if (g_world->hour() > pShop->close_hour) {
    (*keeper)->do_say ("Sorry, come back tomorrow.");
    return NULL;
  }

  /*
   * Invisible or hidden people.
   */
  if (!(*keeper)->can_see(ch)) {
    (*keeper)->do_say ("I don't trade with folks I can't see.");
    return NULL;
  }

  return *keeper;
}

int get_cost (Character * keeper, Object * obj, bool fBuy)
{
  Shop *pShop;
  int cost;

  if (obj == NULL || (pShop = keeper->pIndexData->pShop) == NULL)
    return 0;

  if (fBuy) {
    cost = obj->cost * pShop->profit_buy / 100;
  } else {
    int itype;

    cost = 0;
    for (itype = 0; itype < MAX_TRADE; itype++) {
      if (obj->item_type == pShop->buy_type[itype]) {
        cost = obj->cost * pShop->profit_sell / 100;
        break;
      }
    }

    ObjIter o;
    for (o = keeper->carrying.begin(); o != keeper->carrying.end(); o++) {
      if (obj->pIndexData == (*o)->pIndexData)
        cost /= 2;
    }
  }

  if (obj->item_type == ITEM_STAFF || obj->item_type == ITEM_WAND)
    cost = cost * obj->value[2] / obj->value[1];

  return cost;
}

/*
 * Generic channel function.
 */
void talk_channel (Character * ch, const std::string & argument, int channel,
  const char *verb)
{
  char buf[MAX_STRING_LENGTH];
  int position;

  if (argument.empty()) {
    snprintf (buf, sizeof buf, "%s what?\r\n", verb);
    buf[0] = toupper (buf[0]);
    return;
  }

  if (!ch->is_npc () && IS_SET (ch->actflags, PLR_SILENCE)) {
    snprintf (buf, sizeof buf, "You can't %s.\r\n", verb);
    ch->send_to_char (buf);
    return;
  }

  REMOVE_BIT (ch->deaf, channel);

  switch (channel) {
  default:
    snprintf (buf, sizeof buf, "You %s '%s'.\r\n", verb, argument.c_str());
    ch->send_to_char (buf);
    snprintf (buf, sizeof buf, "$n %ss '$t'.", verb);
    break;

  case CHANNEL_IMMTALK:
    snprintf (buf, sizeof buf, "$n: $t.");
    position = ch->position;
    ch->position = POS_STANDING;
    ch->act (buf, argument.c_str(), NULL, TO_CHAR);
    ch->position = position;
    break;
  }

  for (DescIter d = descriptor_list.begin();
    d != descriptor_list.end(); d++) {
    Character *och;
    Character *vch;

    och = (*d)->original ? (*d)->original : (*d)->character;
    vch = (*d)->character;

    if ((*d)->connected == CON_PLAYING
      && vch != ch && !IS_SET (och->deaf, channel)) {
      if (channel == CHANNEL_IMMTALK && !och->is_hero())
        continue;
      if (channel == CHANNEL_YELL && vch->in_room->area != ch->in_room->area)
        continue;

      position = vch->position;
      if (channel != CHANNEL_SHOUT && channel != CHANNEL_YELL)
        vch->position = POS_STANDING;
      ch->act (buf, argument.c_str(), vch, TO_VICT);
      vch->position = position;
    }
  }

  return;
}

Room *find_location (Character * ch, const std::string & arg)
{
  Character *victim;
  Object *obj;

  if (is_number (arg))
    return get_room_index (atoi (arg.c_str()));

  if ((victim = ch->get_char_world (arg)) != NULL)
    return victim->in_room;

  if ((obj = ch->get_obj_world (arg)) != NULL)
    return obj->in_room;

  return NULL;
}

bool is_safe (Character * ch, Character * victim)
{
  if (ch->is_npc () || victim->is_npc ())
    return false;

  if (ch->get_age() < 21) {
    ch->send_to_char ("You aren't old enough.\r\n");
    return true;
  }

  if (IS_SET (victim->actflags, PLR_KILLER))
    return false;

  if (ch->level >= victim->level) {
    ch->send_to_char ("You may not attack a lower level player.\r\n");
    return true;
  }

  return false;
}

/*
 * See if an attack justifies a KILLER flag.
 */
void check_killer (Character * ch, Character * victim)
{
  /*
   * Follow charm thread to responsible character.
   * Attacking someone's charmed char is hostile!
   */
  while (victim->is_affected (AFF_CHARM) && victim->master != NULL)
    victim = victim->master;

  /*
   * NPC's are fair game.
   * So are killers and thieves.
   */
  if (victim->is_npc ()
    || IS_SET (victim->actflags, PLR_KILLER)
    || IS_SET (victim->actflags, PLR_THIEF))
    return;

  /*
   * Charm-o-rama.
   */
  if (IS_SET (ch->affected_by, AFF_CHARM)) {
    if (ch->master == NULL) {
      bug_printf ("Check_killer: %s bad AFF_CHARM",
        ch->is_npc () ? ch->short_descr.c_str() : ch->name.c_str());
      ch->affect_strip (skill_lookup("charm person"));
   

Showing the first 100 KB of 168.9 KB. Download the archive for the full file.

Site Stats

Snippets: 1791
Codebases: 571