#include <sys.h>
#include <flags.h>

string long_desc,short_desc;
object owner,dropto,shopkeeper,storeroom;
int special,flags;

self_destruct() { destruct(this_object()); return 1; }

get_flags() { return flags; }
get_special() { return special; }
set_flags(f) { flags=f; return 1; }
set_special(s) { special=s; return 1; }
get_dropto() { return dropto; }
set_dropto(o) { dropto=o; return 1; }
get_shopkeeper() { return shopkeeper; }
get_storeroom() { return storeroom; }
set_shopkeeper(x) { shopkeeper=x; return 1; }
set_storeroom(x) { storeroom=x; return 1; }

get_type() { return TYPE_SHOP; }

set_long(arg) {
  long_desc=arg;
  return 1;
}

set_short(arg) { short_desc=arg; return 1; }

get_long() { return (long_desc?long_desc:
                     "You are nowhere."); }

get_short() { return short_desc ? short_desc:"Gray Mist"; }

stat() {
  tell_player(this_player(),"Object Type: SHOP\n");
  if (flags) tell_player(this_player(),"Flags: "+make_flags(flags)+"\n");
  if (special) tell_player(this_player(),"Special: "+itoa(special)+"\n");
  if (short_desc)
    tell_player(this_player(),"Short: "+short_desc+"\n");
  if (long_desc)
    tell_player(this_player(),"Long: "+long_desc+"\n");
  if (dropto)
    tell_player(this_player(),"Dropto: "+make_num(dropto)+"\n");
  if (storeroom)
    tell_player(this_player(),"StoreRoom: "+make_num(storeroom)+"\n");
  if (shopkeeper)
    tell_player(this_player(),"ShopKeeper: "+make_num(shopkeeper)+"\n");
  if (owner)
    tell_player(this_player(),"Owner: "+make_num(owner)+"\n");
  return 1;
}

static init() {
  if (!prototype(this_object())) return;
  add_verb("buy","do_buy");
  add_verb("sell","do_sell");
  add_verb("list","do_list");
}

make_space(arg,len) {
  int count;

  count=len-strlen(arg);
  if (count<0) count=0;
  while (count--) arg+=" ";
  return arg;
}

do_list(arg) {
  object curr;
  int count,cost;

  if (!shopkeeper) {
    tell_player(this_player(),"The shopkeeper isn't here.\n");
    return 1;
  }
  if (location(shopkeeper)!=this_object()) {
    tell_player(this_player(),"The shopkeeper isn't here.\n");
    return 1;
  }
  if (storeroom) curr=contents(storeroom);
  tell_player(this_player(),"Cost     Item\n");
  tell_player(this_player(),"----     ----\n");
  while (curr) {
    cost=call_other(curr,"get_value");
    if (cost) {
      count++;
      tell_player(this_player(),make_space(itoa(cost),9)+
                  call_other(curr,"get_short")+"\n");
    }
    curr=next_object(curr);
  }
  tell_player(this_player(),itoa(count)+" object"+((count==1) ? "":"s")+
              " listed.\n");
  return 1;
}

static do_sell(arg) {
  int cost;
  object curr;

  if (!shopkeeper) {
    tell_player(this_player(),"The shopkeeper isn't here.\n");
    return 1;
  }
  if (location(shopkeeper)!=this_object()) {
    tell_player(this_player(),"The shopkeeper isn't here.\n");
    return 1;
  }
  if (!storeroom) {
    tell_player(this_player(),"The shopkeeper is uninterested in purchasing "+
                "anything at the moment.\n");
    return 1;
  }
  curr=present(arg,this_player());
  if (!curr) {
    tell_player(this_player(),"You're not carrying that.\n");
    return 1;
  }
  cost=call_other(curr,"get_value");
  cost=(cost*3)/4;
  if (cost>1000) cost=1000;
  if (!cost) {
    tell_player(this_player(),"The shopkeeper is uninterested in purchasing "+
                "that.\n");
    return 1;
  }
  if (call_other(curr,"drop")) {
    tell_player(this_player(),"You can't drop that object.\n");
    return 1;
  }
  tell_player(this_player(),"The shopkeeper pays you "+itoa(cost)+" gold "+
              "coins for it.\n");
  tell_room_except(this_object(),this_player(),call_other(this_player(),
                   "get_short")+" sells "+call_other(curr,"get_short")+".\n");
  call_other(this_player(),"add_gold",cost);
  move_object(curr,storeroom);
  if ((!(call_other(curr,"get_flags") & F_PERMANENT)) ||
      (call_other(curr,"get_flags") & F_FORSALE))
    call_other(curr,"self_destruct");
  return 1;
}  

static do_buy(arg) {
  int cost;
  object curr;

  if (!shopkeeper) {
    tell_player(this_player(),"The shopkeeper isn't here.\n");
    return 1;
  }
  if (location(shopkeeper)!=this_object()) {
    tell_player(this_player(),"The shopkeeper isn't here.\n");
    return 1;
  }
  if (!storeroom) {
    tell_player(this_player(),"There is no such object for sale here.\n");
    return 1;
  }
  curr=present(arg,storeroom);
  if (curr) cost=call_other(curr,"get_value");
  if (!cost) {
    tell_player(this_player(),"There is no such object for sale here.\n");
    return 1;
  }
  if (cost>call_other(this_player(),"get_gold")) {
    tell_player(this_player(),"You can't afford it.\n");
    return 1;
  }
  if (call_other(curr,"get_flags") & F_FORSALE)
    if (!(curr=call_other(curr,"replicate"))) {
      tell_player(this_player(),"That object cannot be bought.\n");
      return 1;
    }
  tell_room_except(this_object(),this_player(),call_other(this_player(),
                   "get_short")+" purchases "+call_other(curr,"get_short")+
                   ".\n");
  tell_player(this_player(),"You purchase "+call_other(curr,"get_short")+
              ".\n");
  move_object(curr,this_player());
  call_other(this_player(),"add_gold",-cost);
  return 1;
}