#library "Musicrand"
#include "zcommon.acs"
str muspool[147] = {
"MUS_AB01","MUS_AB02","MUS_AB03","MUS_AB04","MUS_AB05","MUS_AB07","MUS_AB10","MUS_AB15","MUS_AB21","MUS_AB22","MUS_AB31","MUS_AB32",
"MUS_ALH1","MUS_ALH2","MUS_ALH3","MUS_ALH4","MUS_ALH5",
"ANNIE01","ANNIE02","ANNIE03",
"MUS_BTK1","MUS_BTK2","MUS_BTK3",
"MUS_BW01","MUS_BW02","MUS_BW03","MUS_BW04","MUS_BW05","MUS_BW06","MUS_BW07","MUS_BW08","MUS_BW09","MUS_BW10","MUS_BW11","MUS_BW12","MUS_BW13","MUS_BW14","MUS_BW15","MUS_BW16","MUS_BW17","MUS_BW18","MUS_BW31",
"MUS_BR01","MUS_BR02","MUS_BR03","MUS_BR04","MUS_BR05","MUS_BR06","MUS_BR07","MUS_BR08","MUS_BR09","MUS_BR10","MUS_BR11",
"MUS_BTH1","MUS_BTH2","MUS_BTH3","MUS_BTH4","MUS_BTH5","MUS_BTH6","MUS_BTH7","MUS_BTH8","MUS_BTH9",
"MUS_CIT1","MUS_CIT2","MUS_CIT3","MUS_CIT4","MUS_CIT5","MUS_CIT6","MUS_CIT7","MUS_CIT8","MUS_CIT9","MUS_CITA","MUS_CITB","MUS_CITI",
"MUS_CD01","MUS_CD02","MUS_CD03","MUS_CD04","MUS_CD05","MUS_CD06","MUS_CD07","MUS_CD08","MUS_CD09","MUS_CD10","MUS_CD11","MUS_CD12",
"MM_01","MM_02","MM_03","MM_04","MM_05","MM_06","MM_07",
"MUS_DC01","MUS_DC02","MUS_DC03","MUS_DC04","MUS_DC05","MUS_DC06","MUS_DC07","MUS_DC08","MUS_DC09","MUS_DC10","MUS_DC11","MUS_DC12","MUS_DCIN",
"MUS_DR01","MUS_DR02","MUS_DR03","MUS_DR04","MUS_DR05","MUS_DR06","MUS_DR07","MUS_DR08","MUS_DR09","MUS_DR10","MUS_DR11",
"MUS_DK01","MUS_DK02","MUS_DK03","MUS_DK04","MUS_DK05","MUS_DK06","MUS_DK07","MUS_DK08","MUS_DK09","MUS_DK10","MUS_DK11","MUS_DK12","MUS_DK13","MUS_DK14","MUS_DK15","MUS_DK16","MUS_DK17","MUS_DK18","MUS_DK19","MUS_DK20","MUS_DK21","MUS_DK22","MUS_DK23","MUS_DK24",
"MUS_DIST",
"MUS_DYS1","MUS_DYS2","MUS_DYS3","MUS_DYS4","MUS_DYS5"
//Earth
};
script "RollMusic" OPEN
{
  while(True)
  {
  int musnum=random(1,147);
  SetMusic(muspool[musnum]);
  delay(300000);
  }
}

#libdefine PLAYER_MIN_TID 1010
#libdefine MAX_PLAYERS 8
#define PERSISTENT_INVENTORY_LEN 1
global int 1:inmap;
int persistentInventory[PERSISTENT_INVENTORY_LEN] =
{
	"ModifiedCoin"
};
int playerHasLoadedInventory[MAX_PLAYERS] = {0,0,0,0,0,0,0,0};
int loginMessageShown[MAX_PLAYERS] = {0,0,0,0,0,0,0,0};

//Inventory saving/loading in-memory cache (for speed - saving to DB is slow):
str g_accountNameForPlayer[MAX_PLAYERS][127];                         //Need to keep track of the account names for all players
int g_inventoryCountForPlayer[MAX_PLAYERS][PERSISTENT_INVENTORY_LEN]; //Need to keep track of all inventory items for all logged in players.
script "setupPlayerTid" ENTER
{
	inmap=1;
	Thing_ChangeTID(0, PLAYER_MIN_TID+PlayerNumber());
	int playerTid = PLAYER_MIN_TID+PlayerNumber();
	ACS_NamedExecute("loopShit",0,PlayerNumber());
}
script "unloadshit" UNLOADING
{
inmap=0;
}
script "onRespawnScript" RESPAWN
{
    Delay(1);
	Thing_ChangeTID(PLAYER_MIN_TID+PlayerNumber(),0);
	Thing_ChangeTID(0,PLAYER_MIN_TID+PlayerNumber());
	int playerTid = PLAYER_MIN_TID+PlayerNumber();
	SetActorProperty(playerTid, APROP_Invulnerable, 0);
	Delay(1);
}
/*
script restoreInventoryKey (void)
{
 Log(s:"Requested inventory restoring by account ",s:GetPlayerAccountName(playerIdx));
 restorePersistentInventory(PlayerNumber());
}*/
function bool check_some_shit (int playerIdx)
{
  if(inmap==0)
  {
  if(PlayerInGame(playerIdx))
  {
  return True;
  }
  }
  return False;
}
script "loopShit" (int playerIdx) // wait until player finally logs in
{
do{delay(1000);
Log(s:"not logged not cool");}//testing purposes
until(check_some_shit(playerIdx) || PlayerIsLoggedIn(playerIdx));
restorePersistentInventory(playerIdx);
}
function bool restorePersistentInventory (int playerIdx)
{   
	if (!PlayerIsLoggedIn(playerIdx))
	{
	if (loginMessageShown[playerIdx]==0)
	{
		Print(s:"Log in to access saving / restoring your inventory!\n(options > multiplayer > log in)");
		loginMessageShown[playerIdx] = 1;
		//return false;
	}
	}
	str accountName = GetPlayerAccountName(playerIdx);
	str namespace = strparam(s:accountName, s:"_inventory");
    int playerTid = PLAYER_MIN_TID + playerIdx;
    ClearActorInventory(playerTid);
    for (int i = 0; i < PERSISTENT_INVENTORY_LEN; i++) {
        GiveActorInventory(playerTid, persistentInventory[i], GetDBEntry(namespace, persistentInventory[i]));
    }
    bool bShowHUDMessage = true;
    if (bShowHUDMessage)
    {
        HudMessage(s:"Inventory restored"; 0, 0, CR_GOLD, 0.5, 0.5, 3.0);
		playerHasLoadedInventory[playerIdx] = 1;
    }
	return true;
}

function bool validateSaveAction(int playerIdx)
{
    str accountName = StrParam(a:g_accountNameForPlayer[playerIdx]);
    if (PlayerIsLoggedIn(playerIdx) == 0) {
        Log(s:"The player wasn't logged in, aborting save.");
        return false; 
    }
    return true;
}

script "saveInventory" (int playerIdx, int noHUDMessage)
{
	int playerTid = PLAYER_MIN_TID + playerIdx;
    str accountName = StrParam(a:g_accountNameForPlayer[playerIdx]);
    if (!validateSaveAction(playerIdx))
    {
        Log(s:"Save for player aborted.");
    	//print(s:"You should load your inventory before you save over it.");
        ActivatorSound("menu/invalid", 127);
		SetResultValue(false);
		terminate;
    }
    Log(s:"saveInventory: Saving inventory with namespace: ", s:accountName, s:" from ", a:g_accountNameForPlayer[playerIdx]);
	str namespace = strparam(s:accountName, s:"_inventory");
    Print(s:"Saving your inventory...");
	Log(s:"Saving inventory for player ", d:playerIdx, s:": ", s:accountName);
	BeginDBTransaction();
    for (int i = 0; i < PERSISTENT_INVENTORY_LEN; i++) {
        SetDBEntry(namespace, persistentInventory[i], g_inventoryCountForPlayer[playerIdx][i]);
    }
	EndDBTransaction();
    if (noHUDMessage == 0)
    {
        HudMessage(s:"Inventory saved!"; 0, 0, CR_GOLD, 0.5, 0.5, 3.0);
    }
    g_accountNameForPlayer[playerIdx][0] = '\0';
	setResultValue(true);
}
script "saveInventoryKey" (void)
{
 Log(s:"Requested inventory saving by account ",s:GetPlayerAccountName(PlayerNumber()));
 ACS_NamedExecuteWithResult("saveInventory", PlayerNumber(), 0);
}
script "saveAndTakeInventory" (void)
{
	if (!ACS_NamedExecuteWithResult("saveInventory", 0, PlayerNumber())) {
		terminate;
	}
}
/*
script "saveAllPlayersInventoriesAtMapUnload" UNLOADING
{
    Log(s:"saveInventory running");
    for (int playerIdx = 0; playerIdx < MAX_PLAYERS; playerIdx++) {
        //Save your inventory
        //if (PlayerInGame(playerIdx)) {
            ACS_NamedExecuteAlways("saveInventory", 0, playerIdx);    
        //}
    }
}*/