/* Jems Scripting Language Demo */


#include "demo.jsh"


// MainFunction points to script main function
var export MainFunction = @main;

// global NPC database
NPC npcs[MAX_NPCS];


// host API functions

host Print(1); // 1 parameter
host Gets(); // no parameters


// main() is not exported but it can be accessed by using its index, stored in MainFunction
func main()
{
	Print("\n |==========================================|\n");
	Print(" | Welcome to Jems Scripting Language Demo! |\n");
	Print(" |==========================================|\n\n");

	// clear NPC slots
	init_slots();

	while (true)
	{
		Print(" You can:\n");
		Print("\t'a' --> Add a new NPC\n");
		Print("\t'r' --> Remove a NPC\n");
		Print("\t'v' --> View a NPC\n");
		Print("\t'e' --> Exit\n");

		Print("\n Your choice > ");

		var choice = Gets();

		if (choice == "a")
			add_npc();
		else if (choice == "r")
			remove_npc();
		else if (choice == "v")
			view_npc();
		else if (choice == "e")
			break;
		else
			Print("\n Bad choice!\n");
	}

	Print("\n");

	return 0; // all went ok
}

// marks all NPC slots as free
func init_slots()
{
	// loop through each NPC slot
	var i = 0;
	while (i < MAX_NPCS)
	{
		npcs[i].active = false; // set current slot as inactive

		++i;
	}
}

// returns the index of the first free NPC slot
func get_slot()
{
	// loop through each NPC slot
	var i = 0;
	while (i < MAX_NPCS)
	{
		if (!npcs[i].active) // slot is free
			return i; // return slot index

		++i;
	}

	return -1; // no free slots found
}

// returns slot index of the NPC with the given name
func search_npc(name)
{
	// loop through each NPC slot
	var i = 0;
	while (i < MAX_NPCS)
	{
		if (npcs[i].active) // ignore inactive slots
			if (npcs[i].name == name) // name matches
				return i; // return current slot index

		++i;
	}

	return -1; // NPC not found
}

// adds a new NPC to the database
func add_npc()
{
	var slot = get_slot(); // get index of the first free NPC slot in the database

	if (slot == -1)
	{
		Print("\n\nNo more room for new NPCs!\n\n");
		return;
	}

	Print("\n\n NPC name > ");
	var name = Gets();

	if (search_npc(name) != -1)
	{
		Print("\nNPC already added! Try a different name!\n\n");
		return;
	}

	npcs[slot].name = name;

	Print("\n Life > ");
	npcs[slot].attrs.life = Gets();

	Print("\n Mana > ");
	npcs[slot].attrs.mana = Gets();

	Print("\n Strength > ");
	npcs[slot].attrs.strength = Gets();

	Print("\n Dexterity > ");
	npcs[slot].attrs.dexterity = Gets();

	Print("\n Position (x,y,z separated by spaces) > ");
	npcs[slot].position[0] = Gets();
	npcs[slot].position[1] = Gets();
	npcs[slot].position[2] = Gets();

	npcs[slot].active = true; // this slot is now active

	Print("\n\n");
}

// removes a NPC from the database
func remove_npc()
{
	Print("\n\n NPC name > ");
	var name = Gets();

	var slot = search_npc(name);

	if (slot == -1)
	{
		Print("\nNPC not found!\n\n");
		return;
	}

	npcs[slot].active = false; // this slot is now free

	Print("\n\n");
}

// prints NPC data
func view_npc()
{
	Print("\n\n NPC name > ");
	var name = Gets();

	var slot = search_npc(name);

	if (slot == -1)
	{
		Print("\nNPC not found!\n\n");
		return;
	}

	Print("\n\n NPC data about " + name + ":\n");

	Print("\n\tLife: " + npcs[slot].attrs.life);

	Print("\n\tMana: " + npcs[slot].attrs.mana);

	Print("\n\tStrength: " + npcs[slot].attrs.strength);

	Print("\n\tDexterity: " + npcs[slot].attrs.dexterity);

	Print("\n\tPosition: ( " +
		npcs[slot].position[0] + " , " +
		npcs[slot].position[1] + " , " +
		npcs[slot].position[2] + " )" );

	Print("\n\n");
}