/* weapon.c */

#include <sys.h>
#include <armor.h>

object wearer,owner;
string name,short_desc,long_desc;
int damreduce,hitreduce,protection,flags,special,value;

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

get_protection() { return protection; }

make_prot_number(x) {
  if (x=="head") return 0;
  if (x=="neck") return 1;
  if (x=="torso") return 3;
  if (x=="body") return 2;
  if (x=="arms") return 4;
  if (x=="hands") return 5;
  if (x=="groin") return 6;
  if (x=="legs") return 7;
  if (x=="feet") return 8;
  return -1;
}

make_prot_string(x) {
  if (x==0) return "head";
  if (x==1) return "neck";
  if (x==3) return "torso";
  if (x==2) return "body";
  if (x==4) return "arms";
  if (x==5) return "hands";
  if (x==6) return "groin";
  if (x==7) return "legs";
  if (x==8) return "feet";
  return 0;
}

replicate() {
  object o;

  o=clone_object("/obj/armor");
  call_other(o,"set_hitreduce",hitreduce);
  call_other(o,"set_damreduce",damreduce);
  call_other(o,"set_protection",protection);
  call_other(o,"set_name",get_name());
  call_other(o,"set_short",short_desc);
  call_other(o,"set_long",long_desc);
  call_other(o,"set_flags",flags);
  call_other(o,"set_special",special);
  call_other(o,"set_owner",owner);
  call_other(o,"set_value",value);
  move_object(o,location(this_object()));
  return o;
}

set_value(arg) { value=arg; return 1; }
get_value() { return value; }

get_type() { return TYPE_ARMOR; }
get_owner() { return owner; }
get_flags() { return flags; }
get_special() { return special; }
set_owner(o) { owner=o; return 1; }
set_flags(f) { flags=f; return 1; }
set_special(s) { special=s; return 1; }

set_protection(x) {
  if (x<0 || x>=NUM_PROT || wearer) return 0;
  protection=x;
  return 1;
}

stat() {
  tell_player(this_player(),"Object Type: ARMOR\n");
  if (flags) tell_player(this_player(),"Flags: "+make_flags(flags)+"\n");
  if (special) tell_player(this_player(),"Special: "+itoa(special)+"\n");
  if (name)
    tell_player(this_player(),"Name: "+get_name()+"\n");
  if (short_desc)
    tell_player(this_player(),"Short: "+short_desc+"\n");
  if (long_desc)
    tell_player(this_player(),"Long: "+long_desc+"\n");
  tell_player(this_player(),"HitReduce: "+itoa(hitreduce)+"\n");
  tell_player(this_player(),"DamReduce: "+itoa(damreduce)+"\n");
  tell_player(this_player(),"Protection: "+make_prot_string(protection)+"\n");
  tell_player(this_player(),"Value: "+itoa(value)+"\n");
  if (wearer)
    tell_player(this_player(),"Worn By: "+make_num(wearer)+"\n");
  if (owner)
    tell_player(this_player(),"Owner: "+make_num(owner)+"\n");
  return 1;
}

id(arg) { return instr(name,1,";"+arg+";"); }

get_short() { return wearer? short_desc+" (worn on "+
              make_prot_string(protection)+")":short_desc; }

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

get_long() { return long_desc; }

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

get_name() { if (name) return midstr(name,2,strlen(name)-2); }

set_name(arg) {
  int pos;

  pos=instr(arg,1,";");
  name=";"+arg+";";
  if (!short_desc)
    if (pos)
      short_desc="a "+leftstr(arg,pos-1);
    else
      short_desc="a "+arg;
  if (!long_desc)
    long_desc="You see nothing special.";
  return 1;
}

drop() {
  return wearer;
}

dead_drop() {
  if (wearer) call_other(wearer,"force_unwear",protection);
  wearer=0;
  return 0;
}

get() { return 1; }

wear() {
  if (wearer) return 0;
  wearer=caller_object();
  return 1;
}

unwear() {
  if (!wearer) return 1;
  if (caller_object()!=wearer) return 1;
  wearer=0;
  return 0;
}

get_hitreduce() {
  return hitreduce;
}

set_hitreduce(bonus) {
  hitreduce=bonus;
  return 1;
}

get_damreduce() { return damreduce; }

set_damreduce(x) { damreduce=x; return 1; }