/* weapon.c */

#include <sys.h>
#include <weapon.h>

object wielder,owner;
string name,short_desc,long_desc;
int hit_bonus,dam_num_die,dam_die_size,dam_bonus,flags,special,value;
string msg_array1[MAX_INTENSITY];
string msg_array2[MAX_INTENSITY];
string msg_array3[MAX_INTENSITY];
string msg_array4[MAX_INTENSITY];
string msg_array5[MAX_INTENSITY];

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

replicate() {
  object o;
  int loop;

  o=clone_object("/obj/weapon");
  call_other(o,"set_hit_bonus",hit_bonus);
  call_other(o,"set_damage_bonus",dam_num_die,dam_die_size,dam_bonus);
  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);
  loop=0;
  while (loop<MAX_INTENSITY) {
    call_other(o,"set_hit_msg",loop,msg_array1[loop],msg_array2[loop],
               msg_array3[loop],msg_array4[loop],msg_array5[loop]);
    loop++;
  }
  move_object(o,location(this_object()));
  return o;
}

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

get_type() { return TYPE_WEAPON; }
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; }

intensity_string(intensity) {
  if (intensity==I_PUNY) return "Puny";
  if (intensity==I_WEAK) return "Weak";
  if (intensity==I_POOR) return "Poor";
  if (intensity==I_AVERAGE) return "Average";
  if (intensity==I_GOOD) return "Good";
  if (intensity==I_VERYGOOD) return "VeryGood";
  return "Massacre";
}

stat() {
  int loop;

  tell_player(this_player(),"Object Type: WEAPON\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(),"HitBonus: "+itoa(hit_bonus)+"\n");
  tell_player(this_player(),"Damage: "+itoa(dam_num_die)+"d"+
              itoa(dam_die_size)+"+"+itoa(dam_bonus)+"\n");
  tell_player(this_player(),"Value: "+itoa(value)+"\n");
  if (wielder)
    tell_player(this_player(),"Wielded By: "+make_num(wielder)+"\n");
  loop=0;
  if (owner)
    tell_player(this_player(),"Owner: "+make_num(owner)+"\n");
  while (loop<MAX_INTENSITY) {
    if (msg_array1[loop] || msg_array2[loop] || msg_array3[loop] ||
        msg_array4[loop] || msg_array5[loop])
      tell_player(this_player(),intensity_string(loop)+": "+
                  msg_array1[loop]+"*"+msg_array2[loop]+";"+
                  msg_array3[loop]+";"+
                  msg_array4[loop]+"*"+msg_array5[loop]+"\n");
    loop++;
  }
  return 1;
}

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

get_short() { return wielder? short_desc+" (wielded)":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;

  name=";"+arg+";";
  pos=instr(arg,1,";");
  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 wielder;
}

dead_drop() {
  if (wielder) call_other(wielder,"force_unwield");
  wielder=0;
  return 0;
}

get() { return 1; }

wield() {
  if (wielder) return 0;
  wielder=caller_object();
  return 1;
}

unwield() {
  if (!wielder) return 1;
  if (caller_object()!=wielder) return 1;
  wielder=0;
  return 0;
}

get_hit_bonus(monster) {
  return hit_bonus;
}

set_hit_bonus(bonus) {
  hit_bonus=bonus;
  return 1;
}

get_damage() { return itoa(dam_num_die)+"d"+itoa(dam_die_size)+"+"+
                      itoa(dam_bonus); }

get_damage_bonus(monster) {
  int count;
  int bonus;

  count=0;
  while (count++<dam_num_die)
    bonus+=random(dam_die_size)+1;
  return bonus+dam_bonus;
}

set_damage_bonus(x,y,z) {
  dam_num_die=x;
  dam_die_size=y;
  dam_bonus=z;
  return 1;
}

set_hit_msg(intensity,msg1,msg2,msg3,msg4,msg5) {
  if (intensity<0 || intensity>=MAX_INTENSITY) return;
  msg_array1[intensity]=msg1;
  msg_array2[intensity]=msg2;
  msg_array3[intensity]=msg3;
  msg_array4[intensity]=msg4;
  msg_array5[intensity]=msg5;
  return 1;
}

get_hit_msg1(intensity) {
  if (intensity<0 || intensity>=MAX_INTENSITY) return;
  return msg_array1[intensity];
}

get_hit_msg2(intensity) {
  if (intensity<0 || intensity>=MAX_INTENSITY) return;
  return msg_array2[intensity];
}

get_hit_msg3(intensity) {
  if (intensity<0 || intensity>=MAX_INTENSITY) return;
  return msg_array3[intensity];
}

get_hit_msg4(intensity) {
  if (intensity<0 || intensity>=MAX_INTENSITY) return;
  return msg_array4[intensity];
}

get_hit_msg5(intensity) {
  if (intensity<0 || intensity>=MAX_INTENSITY) return;
  return msg_array5[intensity];
}