#include "bleeding_config"
#include "nw_i0_tool"
#include "prc_inc_racial"

void main()
{

    object oPC = GetEnteringObject();

    if (GetIsPC(oPC))
    {
        // Player is assumed to be alive when connecting
        SetLocalInt(oPC, "PlayerHealth", PC_HEALTH_ALIVE);

        // if 0 XP, strip items and give starting gold and items
        if ((GetXP(oPC) == 0) && (!GetIsDM(oPC)))
        {
            // Unequip items
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_CHEST, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_ARMS, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_ARROWS, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_BELT, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_BOLTS, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_BOOTS, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_BULLETS, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_CARMOUR, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_CLOAK, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_CWEAPON_B, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_CWEAPON_L, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_CWEAPON_R, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_HEAD, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_LEFTRING, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_NECK, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC)));
            //AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_RIGHTRING, oPC)));

            DestroyObject(GetItemInSlot(INVENTORY_SLOT_CHEST, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_ARMS, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_ARROWS, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_BELT, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_BOLTS, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_BOOTS, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_BULLETS, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_CARMOUR, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_CLOAK, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_CWEAPON_B, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_CWEAPON_L, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_CWEAPON_R, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_HEAD, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_LEFTRING, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_NECK, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC));
            DestroyObject(GetItemInSlot(INVENTORY_SLOT_RIGHTRING, oPC));

            // Destroy all items (except healing potions and torch)
            object oItem = GetFirstItemInInventory(oPC);
            string sTag;
            while (oItem != OBJECT_INVALID)
            {
                sTag = GetTag(oItem);
                // Destroy everything but torchand potions
                if (sTag != "NW_IT_MPOTION001" && sTag != "NW_IT_TORCH001")
                    DestroyObject(oItem);
                oItem = GetNextItemInInventory(oPC);
            }
            oItem = GetFirstItemInInventory(oPC);
            while (oItem != OBJECT_INVALID)
            {
                sTag = GetTag(oItem);
                // Destroy one potion
                if (sTag == "NW_IT_MPOTION001")
                {
                    DestroyObject(oItem);
                    break;
                }
                oItem = GetNextItemInInventory(oPC);
            }

            // Give appropriate amount of gold based on class
            int nGold = 0;
            switch (GetClassByPosition(1, oPC))
            {
                case CLASS_TYPE_FIGHTER:
                case CLASS_TYPE_PALADIN:
                case CLASS_TYPE_RANGER:
                    nGold = d4(6) * 10;
                    break;
                case CLASS_TYPE_CLERIC:
                case CLASS_TYPE_ROGUE:
                    nGold = d4(5) * 10;
                    break;
                case CLASS_TYPE_BARBARIAN:
                case CLASS_TYPE_BARD:
                    nGold = d4(4) * 10;
                    break;
                case CLASS_TYPE_SORCERER:
                case CLASS_TYPE_WIZARD:
                    nGold = d4(3) * 10;
                    break;
                case CLASS_TYPE_DRUID:
                    nGold = d4(2) * 10;
                    break;
                case CLASS_TYPE_MONK:
                    nGold = d4(5);
                    break;
            }
            SendMessageToPC(oPC, "You have received " + IntToString(nGold) + " starting gold.");
            int nOriginalGold = GetGold(oPC);
            AssignCommand(oPC, TakeGoldFromCreature(nOriginalGold, oPC, TRUE));
            GiveGoldToCreature(oPC, nGold);

            // Give player some clothes
            object oClothes = CreateItemOnObject("jhr_bp_clothes0" + IntToString(d4()), oPC);

            // Give player a medkit +1
            CreateItemOnObject("nw_it_medkit001", oPC, 1);

            // Put the clothes on
            AssignCommand(oPC, ActionEquipItem(oClothes, INVENTORY_SLOT_CHEST));

            // Give the player a race-appropriate weapon
            switch (MyPRCGetRacialType(oPC))
            {
                case RACIAL_TYPE_ELF:
                case RACIAL_TYPE_HALFELF:
                case RACIAL_TYPE_HALFORC:
                case RACIAL_TYPE_HUMAN:
                    // Give player a walking stick
                    CreateItemOnObject("jhr_bp_walkingst", oPC);
                    // Equip walking stick
                    AssignCommand(oPC, ActionEquipItem(GetObjectByTag("JHR_IT_WALKINGSTICK"), INVENTORY_SLOT_RIGHTHAND));
                    break;

                case RACIAL_TYPE_DWARF:
                case RACIAL_TYPE_GNOME:
                case RACIAL_TYPE_HALFLING:
                    // Give player a knife
                    CreateItemOnObject("jhr_bp_knife", oPC);
                    // Equip and unequip the knife (so it's in the first inventory page)
                    AssignCommand(oPC, ActionEquipItem(GetObjectByTag("JHR_IT_KNIFE"), INVENTORY_SLOT_RIGHTHAND));
                    AssignCommand(oPC, ActionUnequipItem(GetObjectByTag("JHR_IT_KNIFE")));
                    break;
            }
            // Give player 1 xp so they don't go through this again
            SetXP(oPC, 1);
        }

        // Give player an emote wand
        if (!(HasItem(oPC, "EmoteWand")))
            CreateItemOnObject("emotewand", oPC);


    }

    if (GetIsDM(oPC))
    {
        SetLocalInt(GetModule(), "bDMPresent", 1);

        // Give DM Helper and FX wands
        if (!(HasItem(oPC, "DMsHelper")))
            CreateItemOnObject("dmshelper", oPC);
        if (!(HasItem(oPC, "WandOfFX")))
            CreateItemOnObject("wandoffx", oPC);
        //DEBUG Give player a plot wand and conversation toggler for testing
        if (!(HasItem(oPC, "JHR_IT_PLOTWAND")))
            CreateItemOnObject("jhr_bp_plotwand", oPC);
        if (!(HasItem(oPC, "JHR_IT_CONVERSATIONTOGGLER")))
        CreateItemOnObject("conversationtogg", oPC);
    }
}
