<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Fishing System Demo</title>
    <link rel="stylesheet" type="text/css" href="../MainStyle.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Greg 'Kefka' Baatard" />
    <style>
    td {text-align: center; vertical-align: top;}
    </style>
</head>
<body>

<form name="settings" method="get" action="fishing_demo.php">
<table style="width: 500px;" cellspacing="0" cellpadding="0" align="center">
    <tr>
        <td>ot.pe bonus:<br /><input size="20" type="text" name="p" maxlength="3" value="<?php echo isset($_GET['p']) ? $_GET['p'] : '100'; ?>" /></td>
        <td>cr.hu.fi bonus:<br /><input size="20" type="text" name="f" maxlength="3" value="<?php echo isset($_GET['f']) ? $_GET['f'] : '100'; ?>" /></td>
        <td>co.st.un bonus:<br /><input size="20" disabled type="text" name="s" maxlength="3" value="<?php echo isset($_GET['s']) ? $_GET['s'] : '100'; ?>" /></td>
    </tr>
    <tr><td colspan="3">&nbsp;</td></tr>
    <tr>
        <td>difficulty:<br /><input size="20" type="text" name="d" maxlength="3" value="<?php echo isset($_GET['d']) ? $_GET['d'] : '100'; ?>" /></td>
        <td>fish %:<br /><input size="20"type="text" name="fp" maxlength="3" value="<?php echo isset($_GET['fp']) ? $_GET['fp'] : '60'; ?>" /><br /></td>
        <td align="center">junk %:<br /><input size="20" type="text" name="jp" maxlength="3" value="<?php echo isset($_GET['jp']) ? $_GET['jp'] : '25'; ?>" /></td>
    </tr>
    <tr><td>&nbsp;</td><td colspan="2"><small>(fish % + junk % must total &lt;= 100)</small><br /><br /></td></tr>
    <tr>
    </tr>
    <tr><td colspan="3"><input type="submit" value="Submit" /></td></tr>
    <tr>
</table>
</form>
<br /><br />

<?php
//player data
$fishing_bonus = isset($_GET['f']) ? $_GET['f'] : 100;
$perception_bonus = isset($_GET['p']) ? $_GET['p'] : 100;
$underwater_stealth_bonus = isset($_GET['s']) ? $_GET['s'] : 100;

//fishing location data
$fp = isset($_GET['fp']) ? $_GET['fp'] : 60;
$jp = isset($_GET['jp']) ? $_GET['jp'] : 60;
$catch_ratio = array($fp, $jp);
$fishing_difficulty = isset($_GET['d']) ? $_GET['d'] : 100;
$fish_types = array('path/to/cod', 'path/to/trout', 'path/to/salmon', 'path/to/blowfish');
$fish_ratio = array(5, 3, 7, 1);
$junk_types = array('path/to/boot', 'path/to/reed', 'path/to/tin');
$basic_ok = true;
$area_name = 'well-stocked fishing pond';

//NOTE: the "isset..." parts of the lines above are just there to connect the code to the interface and allow values to be given.
//These sections can just be replaced with a value (e.g. $fishing_bonus = 100;) to dump the interface; no other part of the code is affected.


function skill_check($bonus, $diff)
{//simulates a skill check
    $bonus += rand(-($bonus*0.1), ($bonus*0.15)); //modify bonus by -10% to +15%

    if ($bonus < $diff) //fail
        return 0;

    elseif ($bonus < ($diff * 1.25)) //just pass
        return 1;

    elseif ($bonus < ($diff * 1.75)) //good pass
        return 2;

    else //pwned.
        return 3;
}

function fish_size()
{//determines the size of the fish you're attempting to reel in, and the difficulty modifier
    $fish_size = array('tiny', 'small', '', 'large', 'massive');
    $fish_size_diff = array(0.8, 0.9, 0, 1.1, 1.2);
    $num = rand(0, sizeof($fish_size) - 1);
    return array($fish_size[$num], $fish_size_diff[$num]);
}

function wait_msg()
{//produces a random waiting message
    $messages = array('You jiggle the line in the hope of attracting a fish.<br />', 'Time passes as you wait for a fish to bite.<br />', 'You wait patiently for a fish to bite.<br />', 'There\'s got to be a fish in there somewhere...<br />', 'Here, fishy fishy fishy.<br />');
    return $messages[rand(0, sizeof($fish_size) - 1)];
}

//CASTING

//check perception and store for later
$perception_check = skill_check($perception_bonus, 100);

//check if cast succeeds
if (skill_check($fishing_bonus, $fishing_difficulty) > 0)
{
    //special message if perception check passed
    if ($perception_check > 0)
        echo 'You cast your line into the '.$area_name.', carefully aiming it at an area likely to contain a lot of fish.<br />';
    else
        echo 'You cast your line into the '.$area_name.'.<br />';

    //imagine a pause here

    echo wait_msg();

    //WAITING
    //$i is initialised to 0, 10, 20 or 30 based on the perception check and incremented by 10 in each iteration
    for ($i=$perception_check*10; $i < 60; $i+=10)
    {
        if (rand(1, 100) > $i) //if a random number out of 100 is larger than $i, you keep jiggling for a fish
        {
            //imagine a pause here
            echo wait_msg();
        }
        else //if the number is smaller than $i, the loop ends
            break;
    }

    //CATCHING
    $catch = rand(1, 100);

    if ($catch <= $catch_ratio[0])
    {//you've caught a fish

        echo 'You feel sharp tug on the line as a fish takes your bait, and start reeling it in!<br />';

        //generate random number within sum of fish_ratio values
        $random = rand(1, array_sum($fish_ratio));

        $tally = 0;

        //go through fish_ratio until you find the one in which the random number falls
        for ($i=0; $i < sizeof($fish_types); $i++)
        {
            $tally += $fish_ratio[$i];

            if($random <= $tally)
            {
                //determine size and difficulty modifier of fish that has bitten
                $size = fish_size();

                //check for reeling in fish - including size modifier
                if (skill_check($fishing_bonus, ($fishing_difficulty * $size[1])) > 0)
                {
                    echo 'You manage to keep the fish on the line as you reel it in.<br />';

                    echo 'You caught a '.$size[0].' '.substr($fish_types[$i], strrpos($fish_types[$i], '/') +1).'!'; //cheap hack to get name of fish
                    break;
                }
                else
                {
                    echo 'As you struggle to reel in the line, the fish manages to escape.<br />';
                    echo 'You finish reeling in the line and pout.<br />';
                    break;
                }
            }
        }

    }
    elseif ($catch >= (100 - $catch_ratio[1]))
    {//you've caught junk

        echo 'The line goes taut and you start to reel it in!<br />';

        $j = rand(0, sizeof($junk_types) - 1);

        echo 'As you finish reeling in the line, you notice you haven\'t caught a fish after all.<br />';
        echo 'You caught a '.substr($junk_types[$j], strrpos($junk_types[$j], '/') +1).'.  Better luck next time!'; //cheap hack to get name of junk

    }
    else
    {//you've caught nothing
        echo 'You feel a brief tug as your bait is snatched away by a nimble fish.<br />';
        echo 'You reel in the line and pout.<br />';
    }

}
else
{
    echo 'You try to cast your line into the pond, but mess it up.<br />';
    echo 'You reel in the line and pout.<br />';
}

?>
<br /><br />
<div align="center">(<a href="fishing_demo_code.php" target="_blank">view code</a>)</div>
</body>
</html>