//:: OnUsed script for casino math game placeable
//:: Checks for token, difficulty, and starts the game by spawning fish
//:: nw_casino_math_start.nss

const string TOKEN_TAG = "I_CAS_CASINOHANDLE";
const string VAR_DIFFICULTY = "INT_DIFFICULTY";
const string FISH_TEMPLATE = "casino_fish";
const string WAYPOINT_TAG_PREFIX = "iSpawnFishHere";
const int TOTAL_WAYPOINTS = 10;
const int DESIRED_FISH_COUNT = 3;
const int MAX_FISH = 10;
const float MIN_FISH_LIFETIME = 2.0;
const float MAX_FISH_LIFETIME = 10.0;


void DespawnFish(object oFish)
{
    if (GetIsObjectValid(oFish))
    {
        DestroyObject(oFish);
    }
}

void SpawnFishForPlayer(object oPC)
{
    // Count how many fish currently exist for this player
    int nFishCount = 0;
    object oArea = GetArea(oPC);
    object oCreature = GetFirstObjectInArea(oArea);
    while (GetIsObjectValid(oCreature))
    {
        if (GetTag(oCreature) == FISH_TEMPLATE && GetLocalObject(oCreature, "FISH_OWNER") == oPC)
        {
            nFishCount++;
        }
        oCreature = GetNextObjectInArea(oArea);
    }

    if (nFishCount >= DESIRED_FISH_COUNT)
        return;

    // Try to spawn one fish at a random open waypoint
    int iTries = 10;
    while (iTries > 0)
    {
        int iWP = Random(TOTAL_WAYPOINTS) + 1;
        string sWPTag = WAYPOINT_TAG_PREFIX + IntToString(iWP);
        object oWP = GetWaypointByTag(sWPTag);

        if (!GetIsObjectValid(oWP))
        {
            iTries--;
            continue;
        }

        // Make sure no fish is already at this spot
object oCheck = GetFirstObjectInShape(SHAPE_SPHERE, 1.0f, GetLocation(oWP), FALSE, OBJECT_TYPE_CREATURE);
while (GetIsObjectValid(oCheck))
{
    if (GetTag(oCheck) == FISH_TEMPLATE)
    {
        // A fish is already here, don't spawn another
        return;
    }

    oCheck = GetNextObjectInShape();
}

        {
            iTries--;
            continue;
        }

        // Generate value and display name
        int nFishValue = Random(10);
        string sDisplay = IntToString(nFishValue);

        // Create fish
        object oFish = CreateObject(OBJECT_TYPE_CREATURE, FISH_TEMPLATE, GetLocation(oWP));
        if (!GetIsObjectValid(oFish))
            return;

        SetName(oFish, sDisplay);
        SetLocalInt(oFish, "FISH_VALUE", nFishValue);
        SetLocalObject(oFish, "FISH_OWNER", oPC);

        // Despawn fish after random time
        float fDelay = IntToFloat(Random(9) + 2); // 2?10 seconds
        DelayCommand(fDelay, DespawnFish(oFish));
        // Schedule another fish to be spawned after this one despawns
        DelayCommand(fDelay + 0.2f, SpawnFishForPlayer(oPC));
        return;
    }
}





void main()
{
    object oPC = GetLastUsedBy();

    // Ensure it's a player and not a DM or creature
    if (!GetIsPC(oPC) || GetIsDM(oPC))
        return;

    // 1. Check for casino token in inventory
    object oToken = GetItemPossessedBy(oPC, TOKEN_TAG);
    if (!GetIsObjectValid(oToken))
    {
        SendMessageToPC(oPC, "You need a casino token to play.");
        return;
    }

    // 2. Check difficulty setting
    int nDifficulty = GetLocalInt(oToken, VAR_DIFFICULTY);
    if (nDifficulty < 1 || nDifficulty > 3)
    {
        SendMessageToPC(oPC, "You must set a difficulty before playing. Use your casino token to choose a difficulty.");
        return;
    }

    // 3. Generate a random target sum (0?99)
    int nTarget = Random(100);
    SetLocalInt(oPC, "MATH_GAME_TARGET", nTarget);
    SetLocalInt(oPC, "MATH_GAME_CURRENT", 0);

    SendMessageToPC(oPC, "Your target number is: " + IntToString(nTarget));

    // 4. Spawn fish at waypoints
    int i;
    for (i = 1; i <= MAX_FISH; i++)      // ? Valid in NWScript

    {
        string sWPTag = WAYPOINT_TAG_PREFIX + IntToString(i);
        object oWP = GetWaypointByTag(sWPTag);

        if (!GetIsObjectValid(oWP))
            continue;

        // Basic logic for spawning a fish with value 0?9
        int nFishValue = Random(10);
        string sDisplay = IntToString(nFishValue); // Name shown on fish

        // Spawn the fish
        object oFish = CreateObject(OBJECT_TYPE_CREATURE, FISH_TEMPLATE, GetLocation(oWP));

        if (GetIsObjectValid(oFish))
        {
            SetLocalInt(oFish, "FISH_VALUE", nFishValue);
            SetName(oFish, sDisplay);
            SetLocalObject(oFish, "FISH_OWNER", oPC); // Optional for click restrictions
        }
    }
}
