#include <std.h>
#include "../tecqumin.h"

inherit "/std/bag_logic";
int cloned;

string owner;
string short_desc();
string long_desc();

void create(){
  ::create();
  set_name("many-legged table");
  set_id(({"table", "many-legged table", "tablexxx"}));
  set_short((:TO, "short_desc":));
  set_long((:TO, "long_desc":));
  cloned = 0;
}

void init(){
  if (cloned ==1){// This check prevents abuse by dropping and insta-getting
    remove();
    return;
  }
  ::init();
  add_action("put_into", "put");
  add_action("get_from", "get");
}

int is_living(){
  return 0;
}

int move(object dest){
  object table, *conts;
  int i, count, res;
  res = ::move(dest);
  if (!living(ETO)){
    table = new (OBJ + "table");
    conts = all_inventory(TO);
    count = sizeof(conts);
    if (count>0){
      for (i=count;i>-1;i--){   //normal for loop reversed to retain 
        conts[i]->move(table);  //order of items in inv
      }
    }
    cloned = 1;               //
    set_short("");            // These lines avoid any duping 
    set_long("");             // problems
    set_id(({"clonedtable"}));// Lujke
    call_out("remove", 1);
    table->move(ETO);
    return res;
  }
  return res;
}

int get_from(string str){
  if (cloned == 1){
    return 0;
    remove();
  }
  return bag_logic::get_from(str);
}

int put_into(string str){
  string this, that;
  if (cloned == 1){
    return 0;
    remove();
  }
  if(stringp(str) && sscanf(str,"%s on %s",this,that) == 2 
                                       &&(that != "ground")){
    return bag_logic::put_into(this + " in "+ that);
  } 
  return notify_fail("Do you want to put it <on> the table?");
}

int is_table(){
  return 1;
}



string short_desc(){
  string desc, pile, top_ob, article;
  object * conts;
  int count;
  desc = "%^RESET%^many-legged stone table with ";
  conts = all_inventory(TO);
  count = sizeof(conts);
  if (count<1){
    pile = "";
    top_ob ="nothing";
  } else {
    top_ob = conts[0]->query_short() + "%^RESET%^";
  }
  if (count == 2){
    pile = " and " + conts[1]->query_short() + "%^RESET%^";
  }
  if (count >2 && count <4){
    pile = " %^RESET%^and a few other things";
  }
  if (count > 3 && count <6){
    pile  = " %^RESET%^and a small pile of other stuff";
  }
  if (count > 5){
    pile = " %^RESET%^and a big pile of other stuff";
  }
  if (top_ob[0] == "a" || top_ob[0] == "e" || top_ob[0] == "i"|| top_ob[0] == "o"|| top_ob[0] == "u"){
    article = "an ";
  } else {
    article = "a ";
  }
  if (count<1){ article = "";}
  desc += article + top_ob;
  if (stringp(pile)){ desc += pile;}
  desc += " on it";
  return desc;
}

int set_owner(string own){
  object leader;
  if (!stringp(own)){ return -1;}
  owner = own;
  leader = find_player(owner);
  if (!objectp(leader)){
    leader = present(owner, ETO);
  }
  if (!objectp(leader)){
    return -1;
  }
  leader->add_follower(TO);
  tell_player(leader,"The table will now follow you, when you put it down.");
  return 1;
}

string query_owner(){
  return owner;
}

string long_desc(){

  string desc;

  desc = "%^CYAN%^This carved %^RESET%^stone table%^CYAN%^ would be just above"
    +" knee height on the average human.  It is rectangular in shape, with"
    +" multiple pairs of %^ORANGE%^curved legs%^CYAN%^ supporting it on the"
    +" long edges. Unlike most tables, this one is clearly%^BOLD%^%^YELLOW%^"
    +" magically animated%^RESET%^%^CYAN%^ and is almost constantly moving," 
    +" whether following its owner or simply fidgeting restlessly on the"
    +" spot.  Despite the movement, nothing ever falls off the table,"
    +" which seems able to lengthen or shorten its legs to angle tilt its"
    +" top with exquisite timing to keep everything balanced.%^RESET%^ You"
    +" could probably put stuff on the table, or get things from it.\n\n";

  return desc;
}
