//================= Hercules Script =======================================
//=       _   _                     _
//=      | | | |                   | |
//=      | |_| | ___ _ __ ___ _   _| | ___  ___
//=      |  _  |/ _ \ '__/ __| | | | |/ _ \/ __|
//=      | | | |  __/ | | (__| |_| | |  __/\__ \
//=      \_| |_/\___|_|  \___|\__,_|_|\___||___/
//================= License ===============================================
//= This file is part of Hercules.
//= http://herc.ws - http://github.com/HerculesWS/Hercules
//=
//= Copyright (C) 2012-2015  Hercules Dev Team
//= Copyright (C)  AnnieRuru
//= Copyright (C)  Emistry
//= Copyright (C)  Euphy
//= Copyright (C)  Paradox924X
//= Copyright (C)  L0ne_W0lf
//= Copyright (C)  Evera
//= Copyright (C)  kobra_k88
//= Copyright (C)  Lupus
//=
//= Hercules is free software: you can redistribute it and/or modify
//= it under the terms of the GNU General Public License as published by
//= the Free Software Foundation, either version 3 of the License, or
//= (at your option) any later version.
//=
//= This program is distributed in the hope that it will be useful,
//= but WITHOUT ANY WARRANTY; without even the implied warranty of
//= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//= GNU General Public License for more details.
//=
//= You should have received a copy of the GNU General Public License
//= along with this program.  If not, see <http://www.gnu.org/licenses/>.
//=========================================================================
//= Global Functions
//================= Description ===========================================
//= General script functions.
//================= Current Version =======================================
//= 2.20
//=========================================================================

//== Function F_InsertComma ================================
// Returns a number with commas based on precision of digits and custom
// separator.
// callfunc "F_InsertComma",<number>{,<precision>,<separator>}
// Examples:
//    callfunc("F_InsertComma",7777777)           // returns "7,777,777"
//    callfunc("F_InsertComma",1000000000,3,",") // returns "1,000,000,000"
//    callfunc("F_InsertComma",1000000000,3,"_") // returns "1_000_000_000"
//    callfunc("F_InsertComma",1000000000,4)      // returns "10,0000,0000"
function	script	F_InsertComma	{
	.@value = getarg(0);
	.@precision = getarg(1,3);
	.@separator$ = getarg( 2,"," );

	.@str$ = ""+.@value;
	.@is_negative = ( .@value < 0 );

	.@length = getstrlen( .@str$ ) - .@precision - .@is_negative;
	while ( .@length > 0 ) {
		.@str$ = insertchar( .@str$, .@separator$ , ( .@length + .@is_negative ) );
		.@length -= .@precision;
	}
	return .@str$;
}

//== Function F_ClearJobVar ================================
// Clears job quest variables
function	script	F_ClearJobVar	{
	// Misc ---------------------------------
	JBLVL = 0;
	FIRSTAID = 0;
	PLAYDEAD = 0;
	got_bandage = 0;
	got_novnametag = 0;
	// First Class Jobs ---------------------
	job_acolyte_q = 0; job_acolyte_q2 = 0;
	job_archer_q = 0;
	job_magician_q = 0;
	job_merchant_q = 0; job_merchant_q2 = 0; job_merchant_q3 = 0;
	job_sword_q = 0; SWTEST = 0;
	job_thief_q = 0;
	// Super Novice
	SUPNOV_Q = 0;
	// 2-1 Jobs ------------------------------
	ASSIN_Q = 0; ASSIN_Q2 = 0; ASSIN_Q3 = 0;
	BSMITH_Q = 0; BSMITH_Q2 = 0;
	HNTR_Q = 0; HNTR_Q2 = 0;
	KNIGHT_Q = 0; KNIGHT_Q2 = 0;
	PRIEST_Q = 0; PRIEST_Q2 = 0; PRIEST_Q3 = 0;
	WIZ_Q = 0; WIZ_Q2 = 0;
	// 2-2 Jobs ------------------------------
	ROGUE_Q = 0; ROGUE_Q2 = 0;
	ALCH_Q = 0; ALCH_Q2 = 0;
	CRUS_Q = 0;
	MONK_Q = 0; JOB_MONK_C = 0;
	SAGE_Q = 0; SAGE_Q2 = 0;
	DANC_Q = 0;
	BARD_Q = 0;
	// Extended Classes
	TAEK_Q = 0; TK_Q = 0;
	STGL_Q = 0;
	SOUL_Q = 0;
	GUNS_Q = 0;
	NINJ_Q = 0;
	return;
}

function	script	F_ClearGarbage	{
	// Clear outdated global VARS
	//Clear TURTLE var if the Turtle Islans Quest is already done
		if(TURTLE==20) MISC_QUEST |= 65536;
		if(MISC_QUEST & 65536) TURTLE = 0; //now clear the var if the quest is over
	//Clear previously saved Quest Skills (now it isn't used)
		ADV_QSK = 0;
		ADV_QSK2 = 0;
	//Old Seset Skill Event used to use RES_SKILL var
		RES_SKILL = 0;
	//Here you put outdated variables from your outdated EVENTS
	//e.g. Is XMAS done? Add the EVENT var clearing code here.

	//due to bugs in BS quest:
	wizard_m2 = 0;

	// Old Novice Ground Variables.
	NEW_MES_FLAG0 = 0;
	NEW_MES_FLAG1 = 0;
	NEW_MES_FLAG2 = 0;
	NEW_MES_FLAG3 = 0;
	NEW_MES_FLAG4 = 0;
	NEW_MES_FLAG5 = 0;
	NEW_LVUP0 = 0;
	NEW_LVUP1 = 0;
	NEW_JOBLVUP = 0;

	// Old DTS variables that are no longer used.
	dtseligible = 0;
	MISC_QUEST = MISC_QUEST & ~128;

	return;
}

//== Function Job_Change ===================================
// Used in REBIRTH scripts
function	script	Job_Change	{
	jobchange getarg(0),Upper; //Jobchange based on Upper
	logmes "CLASS CHANGE: " +strcharinfo(PC_NAME)+ " become a "+jobname(Class);
	return;
}

//== Function F_Rand =======================================
// Returns a random argument
// Example: (see also F_Bye or F_Hi functions)
// .@itemIDfromList = callfunc("F_Rand",1129,1222,1163,1357,1360,1522,1811,1410);
function	script	F_Rand	{
	return getarg(rand(getargcount()));
}

//== Function F_Hi =========================================
// Returns random HELLO message
function	script	F_Hi	{
	return callfunc("F_Rand","Hi!","Hello!","Good day!","How are you?","Hello there.");
}

//== Function F_Bye ========================================
// Returns random BYE message
function	script	F_Bye	{
	return callfunc("F_Rand","Bye. See you again.","Later.","Goodbye.","Good luck!","Have a nice day!","Byebye!!!");
}

//== Function F_ItemName ===================================
// Returns expanded item name string
// Arguments:
// - 0 - Item ID
// - 1 - Element N (0=none,1=Ice,2=Earth,3=Fire,4=Wind)
// - 2 - VVS meter 0...3
// - 3 - Refine
// Example: mes "Show me your "+callfunc("F_ItemName",1201,1,2,5)+"...";
//  is equal to: mes "Show me your ^000090 +5 VVS Fire Knife ^000000..."
function	script	F_ItemName	{
	if(getarg(3))
		.@t$ += "+"+getarg(3)+" ";
	switch(getarg(2)) {
		case 0: break;
		case 1: .@t$ += "VS "; break;
		case 2: .@t$ += "VVS "; break;
		case 3: .@t$ += "VVVS "; break;
		default: .@t$ += getarg(2)+"xVS "; break;
	}
	switch(getarg(1)) {
		case 1: .@t$ += "Ice "; break;
		case 2: .@t$ += "Earth "; break;
		case 3: .@t$ += "Fire "; break;
		case 4: .@t$ += "Wind "; break;
		default: .@t$ += "Strange "; break;
	}
	return "^000090"+.@t$+getitemname(getarg(0))+"^000000";
}

//== Function F_SaveQuestSkills ============================
// Stores learned quest skills
function	script	F_SaveQuestSkills	{
	ADV_QSK = 0; ADV_QSK2 = 0;
	//1st classes quest skills
	for (.@i = 0; .@i < 14; ++.@i) {
		if(getskilllv(144+.@i)) ADV_QSK |= (2 ** .@i);
	}
	//2nd classes quest skills
	for (.@i = 0; .@i < 19; ++.@i) {
		if(getskilllv(1001+.@i)) ADV_QSK2 |= (2 ** .@i);
	}
	return;
}

//== Function F_Load1Skills ================================
// Restores learned 1st class quest skills
function	script	F_Load1Skills	{
	//1st classes quest skills
	for(.@i = 0; .@i < 14; ++.@i) {
		if(ADV_QSK|(2 ** .@i) == ADV_QSK) skill 144+.@i,1,0;
	}
	ADV_QSK = 0; //Clear var
	return;
}

//== Function F_Load2Skills ================================
// Restores learned 2nd class quest skills
function	script	F_Load2Skills	{
	//2nd classes quest skills
	for (.@i = 0; .@i < 19; ++.@i) {
		if(ADV_QSK2|(2 ** .@i) == ADV_QSK2) skill 1001+.@i,1,0;
	}
	ADV_QSK2 = 0; //Clear var
	return;
}

//== Function F_GetWeaponType ==============================
// Determines weapon type by subtype
function	script	F_GetWeaponType	{
	if (getiteminfo(getarg(0), ITEMINFO_TYPE) != IT_WEAPON)
		return "Not a weapon";

	switch (getiteminfo(getarg(0), ITEMINFO_SUBTYPE)) {
		case W_DAGGER: return "Dagger"; break;
		case W_1HSWORD: return "One-handed Sword"; break;
		case W_2HSWORD: return "Two-handed Sword"; break;
		case W_1HSPEAR: return "One-handed Spear"; break;
		case W_2HSPEAR: return "Two-handed Spear"; break;
		case W_1HAXE: return "One-handed Axe"; break;
		case W_2HAXE: return "Two-handed Axe"; break;
		case W_MACE: return "Mace"; break;
		//case W_2HMACE: return "Two-handed Mace"; break; // Unused
		case W_STAFF: return "Staff"; break;
		case W_BOW: return "Bow"; break;
		case W_KNUCKLE: return "Knuckle"; break;
		case W_MUSICAL: return "Instrument"; break;
		case W_WHIP: return "Whip"; break;
		case W_BOOK: return "Book"; break;
		case W_KATAR: return "Katar"; break;
		case W_REVOLVER: return "Revolver"; break;
		case W_RIFLE: return "Rifle"; break;
		case W_GATLING: return "Gatling gun"; break;
		case W_SHOTGUN: return "Shotgun"; break;
		case W_GRENADE: return "Grenade Launcher"; break;
		case W_HUUMA: return "Shuriken"; break;
		case W_2HSTAFF: return "Two-handed Staff"; break;
		default: return "Unable to Determine Equip Type"; break;
	}
	end;
}

//== Function F_GetAmmoType ================================
// Determines ammunition type by subtype
function	script	F_GetAmmoType	{
	if (getiteminfo(getarg(0), ITEMINFO_TYPE) != IT_AMMO)
		return "Not an ammunition";

	switch (getiteminfo(getarg(0), ITEMINFO_SUBTYPE)) {
		case A_ARROW: return "Arrow"; break;
		case A_DAGGER: return "Throwing Dagger"; break;
		case A_BULLET: return "Bullet"; break;
		case A_SHELL: return "Shell"; break;
		case A_GRENADE: return "Grenade"; break;
		case A_SHURIKEN: return "Huuma Shuriken"; break;
		case A_KUNAI: return "Kunai"; break;
		case A_CANNONBALL: return "Cannon Ball"; break;
		case A_THROWWEAPON: return "Throwing Weapon"; break;
		default: return "Unable to Determine Ammunition Type"; break;
	}
	end;
}

//== Function F_GetArmorType ===============================
// Determines equipment type by equip location
function	script	F_GetArmorType	{
	switch (getiteminfo(getarg(0), ITEMINFO_TYPE)) {
	case IT_WEAPON:
		return callfunc("F_GetWeaponType", getarg(0));
	case IT_AMMO:
		return callfunc("F_GetAmmoType", getarg(0));
	case IT_ARMOR:
		break;
	default:
		return "Unknown Equip Type";
	}

	.@loc = getiteminfo(getarg(0), ITEMINFO_LOC);

	if ((.@loc & EQP_HELM) != 0) {
		.@name$ = "";
		if ((.@loc & EQP_HEAD_TOP) != 0) {
			.@name$ += "Top";
		}
		if ((.@loc & EQP_HEAD_MID) != 0) {
			if ((.@loc & EQP_HEAD_TOP) != 0)
				.@name$ += " + ";
			.@name$ += "Middle";
		}
		if ((.@loc & EQP_HEAD_LOW) != 0) {
			if ((.@loc & (EQP_HEAD_TOP | EQP_HEAD_MID)) != 0)
				.@name$ += " + ";
			.@name$ += "Lower";
		}
		.@name$ += " Headgear";
		return .@name$;
	}
	if ((.@loc & EQP_GARMENT) != 0)
		return "Garment";
	if ((.@loc & EQP_ACC) != 0)
		return "Accessory";
	if ((.@loc & EQP_ARMOR) != 0)
		return "Armor";
	if ((.@loc & EQP_SHIELD) != 0)
		return "Shield";
	if ((.@loc & EQP_SHOES) != 0)
		return "Shoes";
	if ((.@loc & EQP_COSTUE_GARMENT) != 0)
		return "Costume Garment";
	if ((.@loc & EQP_COSTUME) != 0) {
		.@name = "Costume ";
		if ((.@loc & EQP_COSTUME_HEAD_TOP) != 0) {
			.@name$ += "Top";
		}
		if ((.@loc & EQP_COSTUME_HEAD_MID) != 0) {
			if ((.@loc & EQP_COSTUME_HEAD_TOP) != 0)
				.@name$ += " + ";
			.@name$ += "Middle";
		}
		if ((.@loc & EQP_COSTUME_HEAD_LOW) != 0) {
			if ((.@loc & (EQP_COSTUME_HEAD_TOP | EQP_COSTUME_HEAD_MID)) != 0)
				.@name$ += " + ";
			.@name$ += "Lower";
		}
		.@name$ += " Headgear";
		return .@name$;
	}
	if ((.@loc & EQP_SHADOW_ARMOR) != 0)
		return "Shadow Armor";
	if ((.@loc & EQP_SHADOW_WEAPON) != 0)
		return "Shadow Weapon";
	if ((.@loc & EQP_SHADOW_SHIELD) != 0)
		return "Shadow Shield";
	if ((.@loc & EQP_SHADOW_SHOES) != 0)
		return "Shadow Shoes";
	if ((.@loc & EQP_SHADOW_ACC) != 0)
		return "Shadow Accessory";

	return "Unknown Equip Type";
}

//== Function Time2Str =====================================
// Time calculation Function
function	script	Time2Str	{
	.@time_left = getarg(0) - gettimetick(2);

	.@Days = .@time_left / 86400;
	.@time_left -= (.@Days * 86400);
	.@Hours = .@time_left / 3600;
	.@time_left -= (.@Hours * 3600);
	.@Minutes = .@time_left / 60;
	.@time_left -= (.@Minutes * 60);

	.@Time$ = "";
	if( .@Days > 1 )
		.@Time$ += .@Days + " days, ";
	else if( .@Days > 0 )
		.@Time$ += .@Days + " day, ";

	if( .@Hours > 1 )
		.@Time$ += .@Hours + " hours, ";
	else if( .@Hours > 0 )
		.@Time$ += .@Hours + " hour, ";

	if( .@Minutes > 1 )
		.@Time$ += .@Minutes + " minutes, ";
	else if( .@Minutes > 0 )
		.@Time$ += .@Minutes + " minute, ";

	if( .@time_left > 1 || .@time_left == 0 )
		.@Time$ += .@time_left + " seconds";
	else if( .@time_left == 1 )
		.@Time$ += .@time_left + " second";

	return .@Time$;
}

//== Function F_ShuffleNumbers =============================
// Generate a set of numbers in random order that the numbers are not repeated
//    callfunc "F_ShuffleNumbers",<start num>,<last num>,<output array>{,<count>};
// Examples: 
//    callfunc("F_ShuffleNumbers", 0, 5, .@output)      // possible output 4,1,3,2,0,5
//    callfunc("F_ShuffleNumbers", -5, 1, .@output)     // possible output -3,-5,-4,-2,-1,1,0
//    callfunc("F_ShuffleNumbers", 0, 100, .@output, 5) // possible output 9,55,27,84,33
// Reminder: Use *freeloop command when appropriate !
function	script	F_ShuffleNumbers	{
	deletearray getarg(2);
	.@static = getarg(0);
	.@range = getarg(1) +1 - .@static;
	.@count = getarg(3, .@range);
	if (.@range <= 0 || .@count <= 0)
		return 0;
	if (.@count > .@range)
		.@count = .@range;
	for (.@i = 0; .@i < .@range; ++.@i)
		.@temparray[.@i] = .@i;
	for (.@i = 0; .@i < .@count; ++.@i) {
		.@rand = rand(.@range);
		set getelementofarray( getarg(2), .@i ), .@temparray[.@rand] + .@static;
		.@temparray[.@rand] = .@temparray[--.@range];
	}
	return .@count;
}

//== Function F_MesColor ===================================
// Function to colorize npc dialog without having to memorize the color code
// Examples:
//    mes callfunc("F_MesColor", C_BLUE) +"This message is now in BLUE";
function	script	F_MesColor	{
	return sprintf("^%06X", min(getarg(0), 0xFFFFFF));
}
