/* sys.c */

#include <flags.h>

make_flags(f) {
  string s;

  if (f & F_PERMANENT) { s+=" permanent"; f&=~F_PERMANENT; }
  if (f & F_FORSALE) { s+=" forsale"; f&=~F_FORSALE; }
  if (f & F_HOSTILE) { s+=" hostile"; f&=~F_HOSTILE; } 
  if (f & F_TEMPLE) { s+=" temple"; f&=~F_TEMPLE; }
  if (f & F_DARK) { s+=" dark"; f&=~F_DARK; }
  if (f) s+=" "+itoa(f);
  return s;
}

make_num(o) {
  int type,owner,f;
  string name,num;
  object rcv;

  name=call_other(o,"get_short");
  type=call_other(o,"get_type");
  if (type==4) name="Exit: "+call_other(o,"get_name");
  owner=call_other(o,"get_owner");
  if (!(rcv=this_player())) rcv=caller_object();
  if (owner!=rcv && call_other(rcv,"get_type")==8) return name;
  num="(#"+itoa(otoi(o));
  if (type==1) num+="P";
  else if (type==2) num+="w";
  else if (type==3) num+="R";
  else if (type==4) num+="E";
  else if (type==5) num+="W";
  else if (type==6) num+="A";
  else if (type==7) num+="p";
  else if (type==8) num+="b";
  else if (type==9) num+="M";
  else if (type==10) num+="O";
  else if (type==11) num+="G";
  else if (type==12) num+="S";
  else if (type==13) num+="C";
  else num="("+otoa(o);
  if (f=call_other(o,"get_flags")) {
    num+=" ";
    if (f & F_PERMANENT) num+="P";
    if (f & F_FORSALE) num+="S";
    if (f & F_HOSTILE) num+="H";
    if (f & F_TEMPLE) num+="T";
    if (f & F_DARK) num+="D";
  }
  return name+num+")";
}

present(name,loc) {
  object curr;
  int count,pos;

  if (!loc) return 0;
  pos=instr(name,1,".");
  if (pos) {
    count=leftstr(name,pos-1);
    count=atoi(count);
  }
  if (count)
    name=rightstr(name,strlen(name)-pos);
  else
    count=1;
  if (!name) return 0;
  curr=contents(loc);
  while (curr) {
    if (call_other(curr,"id",name))
      if (!(--count))
        return curr;
    curr=next_object(curr);
  }
  return 0;
}

tell_room(room,msg) {
  if (!room) return;
  call_other(room,"listen",msg);
  iterate(contents(room),0,0,"listen",msg);
}

tell_room_except(room,player,msg) {
  if (!room) return;
  if (room!=player) call_other(room,"listen",msg);
  iterate(contents(room),player,0,"listen",msg);
}

tell_room_except2(room,p1,p2,msg) {
  if (!room) return;
  if (p1!=room && p2!=room) call_other(room,"listen",msg);
  iterate(contents(room),p1,p2,"listen",msg);
}

make_space(arg,len) {
  int count;

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

who_list() {
  object curr;
  int count;
  string name;

  count=0;
  call_other(this_player(),"listen","Active Connections:\n");
  call_other(this_player(),"listen","------------------\n");
  curr=next_who();
  while (curr) {
    name=call_other(curr,"get_short");
    if (name) {
      count++;
      call_other(this_player(),"listen",name+"\n");
    }
    curr=next_who(curr);
  }
  if (count==1)
    call_other(this_player(),"listen","1 player connected.\n");
  else
    call_other(this_player(),"listen",itoa(count)+" players connected.\n");
}

wiz_who_list() {
  object curr;
  int count;

  count=0;
  curr=next_who();
  call_other(this_player(),"listen",make_space("Address",16)+
                                    make_space("Name",32)+
                                    "Location\n");
  call_other(this_player(),"listen",make_space("-------",16)+
                                    make_space("----",32)+
                                    "--------\n");
  while (curr) {
    count++;
    call_other(this_player(),"listen",make_space(get_devconn(curr),16)+
                                        make_space(make_num(curr),32)+
                                        (location(curr) ? 
                                         make_num(location(curr)) : "void")+
                                        "\n");
    curr=next_who(curr);
  }
  if (count==1)
    call_other(this_player(),"listen","1 player connected.\n");
  else
    call_other(this_player(),"listen",itoa(count)+ " players connected.\n");
}

static init() {
  if (!prototype(this_object()))
    destruct(this_object());
}

find_player(name) {
  string low_name;
  object curr;

  curr=atoo("/obj/player");
  low_name=downcase(name);
  while (curr) {
    if (low_name==call_other(curr,"get_uid"))
      return curr;
    curr=next_child(curr);
  }
  return 0;
}

find_wiz(name) {
  string low_name;
  object curr;

  curr=atoo("/obj/wiz");
  low_name=downcase(name);
  while (curr) {
    if (low_name==call_other(curr,"get_uid"))
      return curr;
    curr=next_child(curr);
  }
  return 0;
}