const string CAS_REWARD_TYPE = "THE_NAME_OF_THE_VARIABLE_FROM_THE_CHESTS";

string CAS_NameReagents(string sReagentName, string sReagentCost, string sReagentBag);
string CAS_NameReagents(string sReagentName, string sReagentCost, string sReagentBag)
{
    return "One bag of " + sReagentBag + " " + sReagentName + "s. Cost: " + sReagentCost + " tickets.";
}

void main()
{
    object oSelf = OBJECT_SELF;
    object oPC = GetLastUsedBy();
    object oReward;
    
    string s2da = "cas_rewards";
    string sResRef;
    string sPrefix;
    string sDescription;
    string sDollDescription;
    string sCost;
    string sType;
    string sVariable;
    string sContainerType = GetLocalString(oSelf, CAS_REWARD_TYPE);
    
    int iRows = Get2DARowCount(s2da);
    int iCount = 0;
    
    // Go through every line in the 2da, checking if TYPE matches the container.
    for (iCount; iCount <= iRows; iCount++)
    {
        sDollDescription = "";
        sVariable = "";
        sType = Get2DAString(s2da, "TYPE", iCount);
        
        if (sType == sContainerType)
        {
            sResRef = Get2DAString(s2da, "RESREF", iCount);
            
            if (sResRef == "reagents")
            {
                string sReagents = "cas_reagents";
                int iReagentCount = Get2DARowCount(sReagents);
                int iReagentCheck = 0;
                string sReagentName;
                string sReagentNumber;
                string sReagentResRef;
                
                for (iReagentCheck; iReagentCheck <= iReagentCount; iReagentCheck++)
                {
                    sReagentResRef = Get2DAString(sReagents, "RESREF", iReagentCheck);
                    sReagentName = Get2DAString(sReagents, "REAGENT_NAME", iReagentCheck);
                    sCost = Get2DAString(sReagents, "TICKET_COST", iReagentCheck);
                    sReagentNumber = Get2DAString(sReagents, "COUNT", iReagentCheck);
                    
                    oReward = CreateItemOnObject(sResRef, oSelf);
                    SetLocalInt(oReward, "iCostTickets", StringToInt(sCost));
                    SetLocalInt(oReward, "i2DARowIndex", iReagentCheck);
                    
                    SetName(oReward, CAS_NameReagents(sReagentName, sCost, sReagentNumber));
                }
            }
            
            else if (sResRef == "dolls")
            {
                string sDolls = "cas_dolls";
                string sName;
                string sSpeech;
                int iDollCount = Get2DARowCount(sDolls);
                int iDolls = 0;
                
                for (iDolls; iDolls <= iDollCount; iDolls++)
                {
                    oReward = CreateItemOnObject("cas_doll", oSelf);
                    SetLocalInt(oReward, "iCostTickets", StringToInt(sCost));
                    SetLocalInt(oReward, "iCopyMe", TRUE);
                    
                    sName = Get2DAString(sDolls, "NAME", iDolls);
                    sSpeech = Get2DAString(sDolls, "SPEECH", iDolls);
                    sDollDescription = Get2DAString(sDolls, "DESCRIPTION", iDolls);
                    
                    SetDescription(oReward, sDollDescription, TRUE);
                    SetDescription(oReward, sDollDescription + "\n\nTickets: " + sCost, FALSE);
                    
                    SetLocalString(oReward, "EVENT_ACQUIRE", "cas_acq_reward");
                    SetIdentified(oReward, FALSE);
                }
            }
            
            else
            {
                oReward = CreateItemOnObject(sResRef, oSelf);
                sCost = Get2DAString(s2da, "COST", iCount);
                SetLocalInt(oReward, "iCostTickets", StringToInt(sCost));
                SetLocalInt(oReward, "iCopyMe", TRUE);
                
                sDescription = GetDescription(oReward);
                SetDescription(oReward, sDescription + "\n\nTicket cost: " + sCost, FALSE);
                SetLocalString(oReward, "EVENT_ACQUIRE", "cas_acq_reward");
                SetIdentified(oReward, FALSE);
            }
        }
    }
    
    SetScript(oSelf, PLACEABLE_SCRIPT_USED, "");
}