#include <mudlib.h>
#include <lsc.h>
inherit MONSTER ;
inherit LSC;

void setup() { setup = 0; }
create() {
	LSC_create();
	enable_commands();
	init_stats();
	init_living();
}

void error(mixed str) {
	object env;
	if (!this_play) {
		destruct(this_object());
		return;
	}

	if (!stderr) {
		env = environment(this_play);
		if (!env) return;
		tell_room(env,this_play->query("cap_name")+" says: ",this_play);
		tell_room(env,str+"\n",this_play);
	}
	_status = ERROR;
}

void Emote(string str) {
	object env;
	if (!this_play) {
		destruct(this_object());
		return;
	}

	env = environment(this_play);
	if (!env) return;
	tell_room(env,this_play->query("cap_name")+" "+str+"\n",this_play);
	_counter = 0;
}

done() {
    enable_commands();
    speed = 5;
    LSC_create();
    Reset();
}

void receive_message(string class, string str) {
	string what, tmp1, tmp2;
	if (!sscanf(str,"Square says: %s\n",what) &&
	    !sscanf(str,"Square tells you: %s\n",what) ) return;
	while(sscanf(what," %s",what));
	if (what=="") return;
	if (what[0]!='.') return;
	what = what[1..(strlen(what)-1)];
	Compile(what);
}

int _say() {
	mixed i;
	object env, ob;
	string out;
	i = Pop();
	if (!this_play) {
		destruct(this_object());
		return 0;
	}
	env = environment(this_play);
	if (!env) return 0;
	_counter = 0;
	if (Is_String(i)) {
		out = Parse_String(i);
	}
	else if (Is_Object(i)) {
		ob = find_object(i[2..strlen(i)-1]);
		if (!ob) out = "something";
		else out = (string) ob->query("short");
		if (!out) out = "some weird thing";
	}
	else if (objectp(i)) {
		if (!i) out = "something";
		else out = (string) i->query("short");
		if (!out) out = "some weird thing";
	}
	else if (intp(i)) {
		out = ""+i;
	}
	else if (pointerp(i)) {
		out = Restore_Array(i);
	}
	if (out) {
		if (this_play==this_object()) {
			command("say "+out);
			return 0;
		}
		this_play->lsc_command("say "+out);
		return 0;
	}

	tell_room(env,this_play->query("cap_name")+" says: ",
	    this_play);

	tell_room(env,i,this_play);
	tell_room(env,"\n",this_play);
	return 0;
}

int _randsay() {
	mixed i;
	i = Pop();
	if (!pointerp(i)) {
		error("randsay: not an [array]");
		return -1;
	}
	insert_input("say");
	return Push(i[random(sizeof(i))]);
}

int _cmd() {
	mixed i;
        if (child) i = (mixed) child->Pop();
        else i = Pop();
	if (!this_play) {
		destruct(this_object());
		return 0;
	}
	if (!Is_String(i)) {
		error("_cmd: Illegal argument");
		return -1;
	}
	i = Parse_String(i);
	if (this_play==this_object()) {
		command(i);
		return 0;
	}
	this_play->lsc_command(i);
}

int _randcmd() {
	mixed i;
        if (child) i = (mixed) child->Pop();
        else i = Pop();
	if (!pointerp(i)) {
		error("randcmd: not an [array]");
		return -1;
	}
	insert_input("cmd");
	return Push(i[random(sizeof(i))]);
}

int _me() {
	if (!this_play) {
		destruct(this_object());
		return -1;
	}
	return Push(this_play);
}

int _env() {
	object env;
	if (!this_play) {
		destruct(this_object());
		return -1;
	}
	return Push(environment(this_play));
}

int _players_here() {
	object *inv,*ret;
	int i,n;
	if (!this_play) {
		destruct(this_object());
		return -1;
	}
	inv = all_inventory(environment(this_play));
	ret = ({});
	n = sizeof(inv);
	for (i=0;i<n;i++) {
		if (inv[i]==this_play) continue;
		if (!living(inv[i])) continue;
		if ((int) inv[i]->query_npc()) continue;
		ret += ({ inv[i] });
	}
	if (!sizeof(ret)) return Push(0);
	return Push( ret );
}

int _livings_here() {
	object *inv,*ret;
	int i,n;
	if (!this_play) {
		destruct(this_object());
		return -1;
	}
	inv = all_inventory(environment(this_play));
	ret = ({});
	n = sizeof(inv);
	for (i=0;i<n;i++) {
		if (inv[i]==this_play) continue;
		if (!living(inv[i])) continue;
		ret += ({ inv[i] });
	}
	if (!sizeof(ret)) return Push(0);
	return Push( ret );
}

int _anything_here() {
	object *inv;
	int i,n;
	if (!this_play) {
		destruct(this_object());
		return -1;
	}
	inv = all_inventory(environment(this_play));
	inv -= ({ this_play });
	if (!sizeof(inv)) return Push(0);
	return Push( inv );
}

int _health() {
	mixed i;
	int stat,tmp;
	string obstr;
	object ob;
	i = Pop();
	if (Is_Object(i)) {
		ob = find_object(i[2..strlen(i)-1]);
		if(!ob) return Push(-1);
		if (!present(ob,environment(this_play))) return Push(-1);
	} else if(Is_String(i)) {
		obstr = i[1..strlen(i)-2];
		if(!parse_command(obstr,environment(this_play),"%l",ob))
			return Push(-1);
	} else {
		error("health: illegal argument");
		return -1;
	}

	stat=100*((int)ob->query_total_hp())/((int)ob->query_total_max_hp());
	tmp = (stat > 50) ? (100 - stat) : stat;
	return Push(stat+ random(tmp) - (tmp/2)); /* a little spice */
}

int Visible(mixed obj) {
	if ((string) obj->query("short")) return obj;
	return 0;
}

int _inv() {
	mixed i, *obl;
	object ob, *ret ;
	string obstr;

	i = Pop();
	if (objectp(i)) {
		ob = i;
	} else if (Is_Object(i)) {
		ob = find_object(i[2..strlen(i)-1]);
		if(!ob) return Push(-1);
		if (!present(ob, environment(this_play)) &&
		    !present(ob,this_play) ) return Push(-1);
	} else if(Is_String(i)) {
		obstr = i[1..strlen(i)-2];
		if(!parse_command(obstr,environment(this_play),"%i",obl))
			parse_command(obstr,this_play,"%i",obl);

		if (sizeof(obl)!=2) return Push(-1);
		ob = obl[1];
	} else {
		error("inv: illegal argument");
		return -1;
	}
	if (!ob) return Push(0);
	ret = all_inventory(ob);
	ret = map_array(ret,"Visible",this_object()) - ({ 0 });
	if (!sizeof(ret)) return Push(0);
	return Push(ret);
}

int _present() {
	mixed *ob1, *ob2;
	mixed i;
	string obstr, *Objstr;
	object ob;
	i = Pop();
	ob1=({});
	if (objectp(i)) {
		if (environment(i)==this_play ||
		    environment(i)==environment(this_play))
			return Push( ({ i }) );
		return Push(0);
	}
	if (Is_Object(i)) {
		ob = find_object(i[2..strlen(i)-1]);
		if (ob) return Push( ({ob}) );
		return Push(0);
	}
	if (Is_String(i)) {
		obstr = i[1..strlen(i)-2];
		if(parse_command(obstr,environment(this_play),"%i",ob1))
			ob1=ob1[1..sizeof(ob1)-1];
		if(parse_command(obstr,this_play,"%i",ob2))
			ob1+=ob2[1..sizeof(ob2)-1];
		if (sizeof(ob1)) return Push(ob1);
		return Push(0);
	}
	error("present: illegal argument");
	return 0;
}

int _logon() {
	mixed i;
	object u;
	int j,n;

	i = Pop();
	if (!Is_String(i)) {
		error("logon: illegal argument");
		return -1;
	}
	u=find_player(i[1..strlen(i)-2]);
	if (u && (string)u->query("short")) return Push(u);
	return Push(0);
}

void lsc_command(string str) {
	command(str);
}