inherit "/d/Imperium/std/room";
#include "defs.h"

private int how_many=0;
private int ile_serc=0;
static int alarm_id;

string opis_pudelka();
string opis_maszynerii();
int pobierz(string str);
int wrzuc(string str);
void grind_potion();

void
create_imperium_room()
{
	set_short("Pracownia");
	set_long("Znajdujesz sie w pracowni wilkolakow. Zauwazasz dziwna maszynerie do ktorej wrzucisz "+
	         "serce, cos z tego zrobi.. tylko co. Pudelko stojace obok maszynerii przechowuje "+
	         "jej produtky.\n\n");
	add_exit(RPATH + "srodek", "poludniowy-zachod");
	add_item("pudelko", "@@opis_pudelka");
	add_item("maszynerie", "@@opis_maszynerii");
	restore_object(RACKI);
	alarm_id = set_alarm(20.0, 200.0, &grind_potion());
}

void
save_me()
{
	save_object(RACKI);
}

void
load_me()
{
	restore_object(RACKI);
}

void
set_potion(int i)
{
	how_many = i;
	save_me();
}

int
query_potion()
{
	return how_many;
}

void
set_serca(int i)
{
	ile_serc = i;
	save_me();
}

int
query_serca()
{
	return ile_serc;
}

int
uruchom(string str)
{
	notify_fail("Co chcesz uruchomic?\n");
	if(!str && str != "maszynerie") return 0;
	notify_fail("Maszyneria juz pracuje.\n");
	if(get_alarm(alarm_id)) return 0;
	alarm_id = set_alarm(20.0, 200.0, &grind_potion());
	TP->catch_msg("Uruchomil" + TP->koncowka("es", "as") + " dziwna maszynerie.\n");
	tell_room(TO, QCIMIE(TP, PL_MIA) + " uruchomi" + TP->koncowka("l", "la") + " dziwna maszynerie.\n");
	return 1;

}

void
init()
{
  ::init();
  add_action("pobierz", "pobierz");
  add_action("wrzuc", "wrzuc");
  add_action("dorzuc", "dorzuc");
  add_action("uruchom", "uruchom");
}

string
opis_maszynerii()
{
	string str = "Dziwna maszyneria potrafiaca wytworzyc przydatne mikstury z serc ludzkich. Wystarczy dorzucic serc a sama stworzy z nich cos przydatnego. ";
	switch(ile_serc)
	{
		case 0: str+="W tej chwili maszyneria nie ma surowca by pracowac..\n"; break;
		case 1..150: str+="W tej chwili maszyneria ma malutko surowca i zapewnie niedlugo sie jej skonczy.\n"; break;
		case 151..500: str+="W tej chwili maszyneria ma troche surowca jednak przydaloby sie uzupelnic zapasy.\n"; break;
		case 5001..1000: str+="W tej chwili maszyneria ma duzo surowca do pracy.\n"; break;
		default: str+="W tej chwili maszyneria jest prawie cala zapelniona sercami ludzkimi..\n"; break;
	}
	return str;
}

string
opis_pudelka()
{
	string str = "Drewniane pudelko stojace obok dziwnej maszynerii. W tej chwili ";
	switch(how_many)
	{
		case 0: str += "jest puste.\n"; break;
		default: str+= "zawiera [" + how_many + "] czerwonawych mikstur ktore z pewnoscia mozesz pobrac.\n"; break;

	}
	return str;
}

int
pobierz(string str)
{
	int ile;
	int i;
	notify_fail("Co chcesz pobrac?\n");
	if(!str) return 0;
	notify_fail("Drewniane pudelko jest puste!\n");
	if(!how_many) return 0;
	parse_command(str, environment(TP), "%d 'mikstur'", ile);
	notify_fail("Pobierz [ile] mikstur?\n");
	if(!ile) return 0;
	notify_fail("Nie ma tyle mikstur w pudelku.\n");
	if(ile > how_many) return 0;
	setuid();
	seteuid(getuid());
	for(i = 0; i<ile; i++)
	{
		clone_object(EPATH + "mana")->move(TP);
		how_many-=1;
	}
	save_me();
	TP->catch_msg("Pobral" + TP->koncowka("es", "as") + " " + ile + " mikstur z pudelka!\n");
	tell_room(environment(TP), QCIMIE(TP, PL_MIA) + " pobra" + TP->koncowka("l", "la") + " troche mikstur z pudelka.\n", ({ TP }));
	return 1;
}

int
wrzuc(string str)
{
	int ile, ile_txt, i, ile_mamy=0;
	object *itemy;
	notify_fail("Co chcesz pobrac?\n");
	if(!str) return 0;
	parse_command(str, environment(TP), "%d 'mikstur'", ile);
	notify_fail("Wrzuc [ile] mikstur?\n");
	if(!ile) return 0;
	ile_txt = ile;
	itemy = deep_inventory(TP);
	for(i=0; i<sizeof(itemy); i++)
	   if(itemy[i]->query_garou_mana_potion()) ile_mamy+=1;
	notify_fail("Nie masz tylu mikstur przy sobie!\n");
	if(ile_mamy<ile) return 0;
	for(i=0; i<sizeof(itemy); i++)
	   if(itemy[i]->query_garou_mana_potion() && ile)
	     { itemy[i]->remove_object(); ile-=1; set_potion(query_potion() + 1); }

	save_me();
	TP->catch_msg("Wrzucil" + TP->koncowka("es", "as") + " " + ile_txt + " mikstur do drewnianego pudelka.\n");
	tell_room(environment(TP), QCIMIE(TP, PL_MIA) + " wrzuci" + TP->koncowka("l", "la") + " troche mikstur do pudelka.\n", ({ TP }));
	return 1;
}



int
dorzuc(string str)
{
	object *itemy;
	int ile, i;
	notify_fail("Co chcesz dorzucic do maszynerii?\n");
	if(!str) return 0;
	if(str != "serc" && str != "serca" && str != "serce") return 0;
	itemy = deep_inventory(TP);
	for(i=0; i<sizeof(itemy); i++)
	   if(itemy[i]->query_serce_garou())
	   { ile+=1; itemy[i]->remove_object(); }
	notify_fail("Nie masz przy sobie zadnego serca!\n");
	if(!ile) return 0;
	set_serca(query_serca() + ile);
	save_me();
	TP->catch_msg("Dorzucil" + TP->koncowka("es", "as") + " troche serc do dziwnej maszynerii.\n");
	tell_room(environment(TP), QCIMIE(TP, PL_MIA) + " dorzuci" + TP->koncowka("l", "la") + " troche serc do maszynerii.\n", ({ TP }));
	return 1;
}

void
grind_potion()
{
	int i;
	if(!ile_serc) return;
	i = query_serca() / 10;
	if(!i) return;

	if(i > 10) { set_potion(query_potion() + 10); set_serca(query_serca() - 100); }
	else { set_potion(query_potion() + i); set_serca(query_serca() - i*10); }
	tell_room(TO, "Z maszynerii dochodza dziwne dzwieki.\n");
	save_me();
}