key playerkey;
string playername;
float timeout = 120.0;
integer minbet = 1;
integer maxbet = 10000;
integer maxpayout = 10000;
integer amountbet = 0;
integer amountpaid = 0;
integer inplay = FALSE;
integer playerBJ = FALSE;
integer dealerBJ = FALSE;
integer player1a = FALSE;
integer player5 = FALSE;
integer dealer1a = FALSE;
integer dealer5 = FALSE;
integer bet = 0;
integer playercount = 0;
integer dealercount = 0;
integer playerscore = 0;
integer dealerscore = 0;
integer playeraces = 0;
integer dealeraces = 0;
string command;
string command1;
string command2;
parse(string bulk)
{
list chunks = llParseString2List(bulk, [" "], []);
command = llList2String(chunks, 0);
command1 = llList2String(chunks, 1);
command2 = llList2String(chunks, 2);
//command = llDeleteSubString(command, 0, prefix_length - 1);
}
reset_game()
{
llSetTimerEvent(0.0);
inplay = FALSE;
playerBJ = FALSE;
dealerBJ = FALSE;
player1a = FALSE;
player5 = FALSE;
dealer1a = FALSE;
dealer5 = FALSE;
bet = 0;
playercount = 0;
dealercount = 0;
playerscore = 0;
dealerscore = 0;
playeraces = 0;
dealeraces = 0;
}
integer card_idvalue(integer num) {
integer v;
if (num < 13) {
v = num + 2;
} else {
if (num < 26) {
v = num - 11;
} else {
if (num < 39) {
v = num - 24;
} else {
if (num < 53) {
v = num - 37;
} else {
v = 15;
}
}
}
}
return v;
}
hide_cards()
{
integer cardnum = 1;
do
{
cardnum += 1;
llMessageLinked(cardnum, 0, "Hide", "");
} while (cardnum < 11);
}
deal_card(integer cardnum)
{
integer rand_card;
rand_card = llRound(llFrand(51));
llMessageLinked(llRound(cardnum + 1), 0, "Card " + (string)rand_card, "");
if (cardnum < 6)
{
if (card_idvalue(rand_card) == 14)
{
if (cardnum == 1) {
player1a = TRUE;
}
playerscore += 11;
playeraces += 1;
}
else if (card_idvalue(rand_card) > 10) playerscore += 10;
else playerscore += card_idvalue(rand_card);
if (playerscore == 21)
{
if (cardnum == 2)
{
llWhisper(0, playername + "; Black Jack! Let's see if the dealer can get 21!");
playerBJ = TRUE;
llMessageLinked(7, 0, "Card " + (string)rand_card, "");
if (card_idvalue(rand_card) == 14)
{
dealer1a == TRUE;
dealerscore += 11;
dealeraces += 1;
}
else if (card_idvalue(rand_card) > 10) dealerscore += 10;
else dealerscore += card_idvalue(rand_card);
}
else
{
llWhisper(0, playername + "; You have " + (string)playerscore + ".");
}
dealer_play();
return;
}
else if (playerscore > 21)
{
if (playeraces > 0)
{
playeraces -= 1;
playerscore -= 10;
}
else
{
llWhisper(0, playername + "; Sorry, you busted with " + (string)playerscore + ".
Better luck next time!");
reset_game();
return;
}
}
playercount += 1;
}
if (cardnum == 5)
{
llWhisper(0, playername + "; You have " + (string)playerscore + " with a 5 card
trick! Let's see if the Dealer can beat that!");
player5 = TRUE;
dealer_play();
}
else if (cardnum > 1)
{
llWhisper(0, playername + "; You have " + (string)playerscore + ". 'Hit' or
'Stand'?");
}
if (cardnum == 2)
{
integer rand_card = llRound(llFrand(51));
llMessageLinked(7, 0, "Card " + (string)rand_card, "");
if (card_idvalue(rand_card) == 14)
{
dealer1a == TRUE;
dealerscore += 11;
dealeraces += 1;
}
else if (card_idvalue(rand_card) > 10) dealerscore += 10;
else dealerscore += card_idvalue(rand_card);
llMessageLinked(8, 0, "Card 53", "");
}
}
dealer_play()
{
integer cardnum = 7;
integer rand_card;
do
{
rand_card = llRound(llFrand(51));
llMessageLinked(llRound(cardnum + 1), 0, "Card " + (string)rand_card, "");
if (card_idvalue(rand_card) == 14)
{
dealerscore += 11;
dealeraces += 1;
}
else if (card_idvalue(rand_card) > 10) dealerscore += 10;
else dealerscore += card_idvalue(rand_card);
if ((dealerscore == 21) && (cardnum == 7))
{
dealerBJ = TRUE;
llWhisper(0, playername + "; Dealer has Black Jack!");
dealer_stand();
return;
}
if (dealerscore > 21)
{
if (dealeraces > 0)
{
dealeraces -= 1;
dealerscore -= 10;
}
else
{
dealer_bust();
return;
}
}
if ((dealerscore == 16) && (dealerscore > playerscore)) {
dealer_stand();
return;
}
cardnum += 1;
} while ((cardnum < 11) && (dealerscore < 17));
if (cardnum == 11)
{
dealer5 = TRUE;
llWhisper(0, playername + "; Dealer has " + (string)dealerscore + " with a 5
card trick.");
}
dealer_stand();
}
dealer_stand()
{
integer payout;
if (!dealer5) llWhisper(0, playername + "; Dealer has " + (string)dealerscore +
".");
if (player5)
{
if (dealer5)
{
if (playerscore > dealerscore)
{
payout = (bet * 2);
llWhisper(0, playername + "; You won $" + (string)payout + "!");
llGiveMoney(playerkey, payout);
amountpaid += payout;
}
else
{
llWhisper(0, playername + "; Sorry, you lost. Better luck next time!");
}
}
else
{
payout = (bet * 2);
llWhisper(0, playername + "; You won $" + (string)payout + "!");
llGiveMoney(playerkey, payout);
amountpaid += payout;
}
}
else if (dealer5)
{
if ((playerBJ) && (player1a))
{
payout = (bet * 3);
llWhisper(0, playername + "; You won $" + (string)payout + "!");
llGiveMoney(playerkey, payout);
amountpaid += payout;
}
else
{
llWhisper(0, playername + "; Sorry, you lost. Better luck next time!");
}
}
else if (playerscore > dealerscore)
{
if (playerBJ) payout = (bet * 3);
else payout = (bet * 2);
llWhisper(0, playername + "; You won $" + (string)payout + "!");
llGiveMoney(playerkey, payout);
amountpaid += payout;
}
else if (playerscore == dealerscore)
{
llGiveMoney(playerkey, bet);
amountpaid += bet;
llWhisper(0, playername + "; You tied with the Dealer! You won $" + (string)bet
+ "!");
}
else
{
llWhisper(0, playername + "; Sorry, you lost. Better luck next time!");
}
reset_game();
}
dealer_bust()
{
integer payout;
if (playerBJ) payout = (bet * 3);
else payout = (bet * 2);
llWhisper(0, playername + "; The Dealer busted with " + (string)dealerscore + "!
You won $" + (string)payout + "!");
llGiveMoney(playerkey, payout);
amountpaid += payout;
reset_game();
}
default
{
on_rez(integer sparam)
{
llResetScript();
}
state_entry()
{
hide_cards();
llListen(0, "", "", "");
llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);
}
listen(integer channel, string name, key id, string message)
{
parse(message);
if ((id == playerkey) && (inplay))
{
if (llToLower(message) == "hit")
{
llSetTimerEvent(timeout);
deal_card(llRound(playercount + 1));
}
if (llToLower(message) == "stand")
{
llSetTimerEvent(timeout);
dealer_play();
}
}
if(command == "status" && command1 == "blackjack")
{
llSay(0,"Minimum Bet: " + (string)minbet);
llSay(0,"Maximum Bet: " + (string)maxbet);
llSay(0,"Max Payout: " + (string)maxpayout);
llSay(0,"Time Out: " + (string)timeout);
}
if(!inplay && id == llGetOwner())
{
if(command == "minbet")
{
if((integer)command1 <= maxbet)
{
minbet = (integer)command1;
llSay(0,"Minimum BET set to " + command1);
}
else if((integer)command1 > maxbet)
{
llSay(0,"Minimum Bet must be less than or equal to Maximum Bet");
}
}
else if(command == "maxbet")
{
if((integer)command1 >= minbet)
{
maxbet = (integer)command1;
llSay(0,"Maximum BET set to " + command1);
}
else if((integer)command1 < minbet)
{
llSay(0,"Maximum Bet must be greater than or equal to Minimum Bet");
}
}
else if(command == "maxpayout")
{
maxpayout = (integer)command1;
llSay(0,"Maximum PAYOUT set to " + command1);
}
else if(command == "timeout")
{
timeout = (float)command1;
llSay(0,"Timeout set to " + command1);
}
}
else if(inplay)
{
llSay(0,"Someone is currently playing this game. Please wait until they finish
this hand to
change the settings.");
}
if(id == llGetOwner() && llToLower(message) == "reset blackjack")
{
llResetScript();
}
}
link_message(integer sender_num, integer num, string str, key id)
{
if ((id == playerkey) && (inplay))
{
if (llToLower(str) == "hit")
{
llSetTimerEvent(timeout);
deal_card(llRound(playercount + 1));
}
if (llToLower(str) == "stand")
{
llSetTimerEvent(timeout);
dealer_play();
}
}
}
money(key giver, integer amount)
{
if (inplay) {
llGiveMoney(giver, amount);
llWhisper(0, "Another game is in play.");
return;
}
else if ((amount > maxbet) || (amount < minbet))
{
llGiveMoney(giver, amount);
llWhisper(0, "Min Bet: $" + (string)minbet);
llWhisper(0, "Max Bet: $" + (string)maxbet);
return;
}
else if ((amountpaid > maxpayout) || (amountpaid == maxpayout))
{
llGiveMoney(giver, amount);
llWhisper(0, "This machine is currently down for servicing");
return;
}
amountbet += amount;
hide_cards();
playerkey = giver;
playername = llKey2Name(giver);
inplay = TRUE;
bet = amount;
llWhisper(0, "$" + (string)bet + " bet! Good luck!");
deal_card(1);
deal_card(2);
llSetTimerEvent(timeout);
}
touch_start(integer total_number)
{
if (llDetectedKey(0) == llGetOwner())
{
llWhisper(0, "Total Amount Bet: $" + (string)amountbet);
llWhisper(0, "Total Amount Paid: $" + (string)amountpaid);
}
else
{
llWhisper(0, "Black Jack! Right-Click and 'Pay' to Play!");
}
}
timer()
{
integer refund = bet;
llWhisper(0, "Black Jack has timed out. Please contact the owner for a
refund.");
reset_game();
llInstantMessage(llGetOwner(), "Black Jack timed out for " + playername + " on a
$" + (string)refund + " bet.");
}
}