/
mud++0.33/etc/
mud++0.33/etc/guilds/
mud++0.33/help/propert/
mud++0.33/mudC/
mud++0.33/player/
mud++0.33/src/
mud++0.33/src/bcppbuilder/
mud++0.33/src/unix/
mud++0.33/src/vm/
/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project.  All 
................................................ contributions are welcome. 
....Copyright(C).1995.Melvin.Smith.............. Enjoy. 
------------------------------------------------------------------------------
Melvin Smith (aka Fusion)         msmith@hom.net
MUD++ development mailing list    mudpp-list@mailhost.net
------------------------------------------------------------------------------
interface1.cpp
*/

#include "interface.h"
#include "io.h"
#include "area.h"
#include "global.h"
#include "pc.h"

void dump_string__pPC_s( VMachine * vm )
{
	vmstack * args = vm->initp(2);
	String * str = args[1].val.s;
	Cout << "VMachine interface function dumped string: " << *str << endl;
}

void out_string__pPC_s( VMachine * vm )
{
	vmstack * args = vm->initp(2);
	PC * pc = (PC*) args[0].val.o;
	String * str = args[1].val.s;
	pc->out( *str );
}


void out_time__pPC( VMachine * vm )
{
	char buf[ 256 ];
	struct rusage r;
	getrusage( RUSAGE_SELF, &r );
	vmstack * args = vm->initp(1);
	PC * pc = (PC*) args[0].val.o;
	sprintf( buf, "USER: %d.%dusec\n\rSYS : %d.%dusec\n\r", r.ru_utime.tv_sec, r.ru_utime.tv_usec,
											r.ru_stime.tv_sec, r.ru_stime.tv_usec );
	pc->out( buf );	
}


void getObjPrototype__s ( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	String * str = args[0].val.s;

	// cut & paste from do_ofind

	String strName;
	String strArea;
	Object * ptr;
	Area *area;
	bool wildcard = false;
	bool global = false;

	if( !(bool)str )
	{
		vm->push_null_obj( VMT_OBJECT);
		return;
	}

	// Use index object to parse arg
	Index tempIndex( *str );
	strName = tempIndex.getKey();
	strArea = tempIndex.getScope();
	
	HashTable<Object> *objHashTable;

	if( strArea == "*" )
		global = true;

	if( strName == "*" )
		wildcard = true;


	if( !global )
	{
		if( !( area = lookupArea( strArea ) ) )
		{
			vm->push_null_obj( VMT_OBJECT );
			return;
		}

		objHashTable = &area->objIndex;
	
		for_each( (*objHashTable), ptr )
		{
			if( !wildcard && !ptr->isName( strName ) )
			{
				objHashTable->next();
				continue;
    		}
 
			vm->push_obj( VMT_OBJECT, ptr );
			return;
		}
	}
	else
	{
		for_each( areas, area )
		{
			objHashTable = &area->objIndex;
			for_each( (*objHashTable), ptr )
			{
				if( !wildcard && !ptr->isName( strName ) )
					continue;
				
				vm->push_obj( VMT_OBJECT, ptr );
				return;

			}
		}
	}
	vm->push_null_obj( VMT_OBJECT );
}


void copyObject__pObject( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	Object * obj = (Object *) args[0].val.o;
	if ( !obj )
	{
		vm->push_null_obj( VMT_OBJECT );
		return;
	}
	obj = obj->clone();
	obj->fromCWorld();
	vm->push_obj( VMT_OBJECT, obj );
}


void toRoom__pObject_pRoom( VMachine * vm )
{
	vmstack * args = vm->initp(2);
	Object * obj = (Object *) args[0].val.o;
	Room * room = (Room *) args[1].val.o;
	if ( !obj || !room )
		return;
	// it is error prone, we need to work on security
	room->addObjInv(obj);
	obj->toCWorld();
}

void inRoom__pThing( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	Thing * thing = (Thing*) args[0].val.o;
	if ( !thing )
	{
		// crit_error ? it is a method - should be not null 'this'
		vm->push_null_obj( VMT_ROOM );
		return;
	}
	vm->push_obj( VMT_ROOM, thing->inRoom() );
}


void getChar__pRoom_s( VMachine * vm )
{
	vmstack * args = vm->initp(2);
	Room * room = (Room*) args[0].val.o;
	String * str = (String*) args[1].val.s;
	if( !room )
	{
		vm->push_null_obj( VMT_CHAR );
		return;
	}
	vm->push_obj( VMT_CHAR, room->getChar( *str ) );
}


void say__pChar_s( VMachine * vm )
{
	vmstack * args = vm->initp(2);
	Char * ch = (Char*) args[0].val.o;
	String * str = (String*) args[1].val.s;
	if( !ch )
		return;

	ch->say( *str );
}


void new_array_of_int__i( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	int size = args[0].val.i;
	vmptr arr;
	if ( size <=0 ) size = 1;
	arr.a =  new VMarray( VMT_INT, size );
	int i;
	for ( i =0; i < size; i++ )
		arr.a->table[i].i =0;
	vm->push( VMT_ARRAY_INT, arr );
}

void new_array_of_float__i( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	int size = args[0].val.i;
	vmptr arr;
	if ( size <=0 ) size = 1;
	arr.a =  new VMarray( VMT_FLOAT, size );
	int i;
	for ( i =0; i < size; i++ )
		arr.a->table[i].f =0;
	vm->push( VMT_ARRAY_FLOAT, arr );
}

void new_array_of_string__i( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	int size = args[0].val.i;
	vmptr arr;
	if ( size <=0 ) 
		size = 1;
	arr.a =  new VMarray( VMT_STRING, size );
	int i;
	for ( i =0; i < size; i++ )
	{
		arr.a->table[i].s = new String();
		Cout << "Initialized array with " << *(arr.a->table[i].s) <<endl;
	}
	vm->push( VMT_ARRAY_STRING, arr );
}

void new_array_of_vmobject__i( VMachine * vm )
{
	vmstack * args = vm->initp(1);
	int size = args[0].val.i;
	vmptr arr;
	if ( size <=0 ) size = 1;
	arr.a =  new VMarray( VMT_VMOBJECT, size );
	int i;
	for ( i =0; i < size; i++ )
		arr.a->table[i].o =0;
	vm->push( VMT_ARRAY_VMOBJECT, arr );
}