// cas_fishstart.nss
#include "cas_fishinc"

void main()
{
    object oPC = GetLastUsedBy();
    if (!GetIsPC(oPC) || GetIsDM(oPC))
        return;

    object oToken = GetItemPossessedBy(oPC, TOKEN_TAG);
    if (!GetIsObjectValid(oToken))
    {
        SendMessageToPC(oPC, "You need a casino token to play.");
        return;
    }

    int nDifficulty = GetLocalInt(oToken, VAR_DIFFICULTY);
    if (nDifficulty < 1 || nDifficulty > 3)
    {
        SendMessageToPC(oPC, "Set a difficulty on your token first (1-3).");
        return;
    }

    if (GetLocalInt(oPC, "MATH_GAME_ACTIVE"))
    {
        SendMessageToPC(oPC, "Game already running. Use the harpoon on your fish.");
        return;
    }
    

    int nTarget = Random(100); // 0..99
    SetLocalInt(oPC, "MATH_GAME_TARGET", nTarget);
    SetLocalInt(oPC, "MATH_GAME_CURRENT", 0);
    SetLocalInt(oPC, "MATH_GAME_ACTIVE", 1);

    CleanupOwnedFish(oPC); // in case of leftovers

    // Give spear if they don't already have it
    object oSpear = GetFirstItemByResRef(oPC, SPEAR_RESREF);
    if (!GetIsObjectValid(oSpear))
    {
        CreateItemOnObject(SPEAR_RESREF, oPC);
        RegisterVariableEvent(oSpear, "EVENT_USE", "cas_fish_spear_use");
    }

    SendMessageToPC(oPC, "Target number: " + IntToString(nTarget));
    SendMessageToPC(oPC, "Use your Fishing Harpoon on fish to add their value.");

    // Kickstart spawning (define loop var outside loop)
    int i;
    for (i = 0; i < DESIRED_FISH_COUNT; i++)
    {
        SpawnFishForPlayer(oPC);
    }
}
