Initial Commit
Initial Commit
This commit is contained in:
473
_module/nss/bjeventhndlr.nss
Normal file
473
_module/nss/bjeventhndlr.nss
Normal file
@@ -0,0 +1,473 @@
|
||||
/**
|
||||
* The main event handler, this handler will take events,
|
||||
* calculate hands, set CustomTokens and then call up a
|
||||
* new conversation.
|
||||
*
|
||||
* Events Handled:
|
||||
* 200: Initial deal
|
||||
* 201: PC Hit
|
||||
* 202: PC Stand
|
||||
* 203: PC DoubleDown
|
||||
* 210: Someone sat down
|
||||
* 211: Player plays hand
|
||||
* 212: Dealer plays hand
|
||||
* 213: Betting
|
||||
* 214: Get Started
|
||||
* 215: Play again
|
||||
*
|
||||
* Copyright (C) 2002-2003 Jim Woodgate - woody@realtime.net
|
||||
*/
|
||||
|
||||
#include "cards"
|
||||
|
||||
void HandleDeal();
|
||||
void HandleHit();
|
||||
void HandleStand();
|
||||
void HandleSit();
|
||||
void HandlePlaying();
|
||||
void HandleDealerPlay();
|
||||
void HandleBetting();
|
||||
void HandleStart();
|
||||
void HandlePlayAgain();
|
||||
void HandleDoubleDown();
|
||||
|
||||
void SpeakDelay(string message);
|
||||
|
||||
string getFullHand(string hand);
|
||||
|
||||
string cardSer(int card);
|
||||
int calculateTotal(string hand);
|
||||
|
||||
void main() {
|
||||
switch(GetUserDefinedEventNumber()) {
|
||||
case 200:
|
||||
PrintString("Received event for Deal!");
|
||||
HandleDeal();
|
||||
break;
|
||||
case 201:
|
||||
PrintString("Received event for Hit!");
|
||||
HandleHit();
|
||||
break;
|
||||
case 202:
|
||||
PrintString("Received event for Stand!");
|
||||
HandleStand();
|
||||
break;
|
||||
case 203:
|
||||
PrintString("Received event for DoubleDown!");
|
||||
HandleDoubleDown();
|
||||
break;
|
||||
case 210:
|
||||
PrintString("Received event for sat down!");
|
||||
HandleSit();
|
||||
break;
|
||||
case 211:
|
||||
PrintString("Received event for handle playing!");
|
||||
HandlePlaying();
|
||||
break;
|
||||
case 212:
|
||||
PrintString("Received event for handle dealer play!");
|
||||
HandleDealerPlay();
|
||||
break;
|
||||
case 213:
|
||||
PrintString("Received event for handle betting!");
|
||||
HandleBetting();
|
||||
break;
|
||||
case 214:
|
||||
PrintString("Received event for handle start!");
|
||||
HandleStart();
|
||||
break;
|
||||
case 215:
|
||||
PrintString("Received event for play again");
|
||||
HandlePlayAgain();
|
||||
break;
|
||||
default:
|
||||
PrintString("Unknown Event: "+IntToString(GetUserDefinedEventNumber()));
|
||||
}
|
||||
}
|
||||
|
||||
void HandleSit() {
|
||||
if (GetLocalInt(OBJECT_SELF, "BJPLAYING") == FALSE) {
|
||||
SetLocalInt(OBJECT_SELF, "BJPLAYING", TRUE);
|
||||
ActionWait(2.0);
|
||||
ActionDoCommand(SignalEvent(OBJECT_SELF, EventUserDefined(214)));
|
||||
}
|
||||
}
|
||||
|
||||
void HandleDealerPlay() {
|
||||
string dealerHand = GetLocalString(OBJECT_SELF, "BJDLRHAND");
|
||||
int dealerHoleCard = GetLocalInt(OBJECT_SELF, "BJDLRHOLE");
|
||||
|
||||
int total = calculateTotal(dealerHand);
|
||||
|
||||
// First need to check and see if anyone is left
|
||||
int numPlayers = GetLocalInt(OBJECT_SELF, "BJNUMPLYRS");
|
||||
int i;
|
||||
int bFound = FALSE;
|
||||
for (i=0; i<numPlayers; i++) {
|
||||
if (GetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(i)) <= 21) {
|
||||
// Still in
|
||||
bFound = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if (bFound == FALSE) {
|
||||
SpeakDelay("Everyone else is out, dealer's hold card is "+getLongValue(dealerHoleCard)+" of "
|
||||
+getLongSuit(dealerHoleCard));
|
||||
SpeakDelay("Dealer's total is "+IntToString(total));
|
||||
}
|
||||
else {
|
||||
// Dealer needs to play out his hand
|
||||
SpeakDelay("Dealer's hole card is "+getLongValue(dealerHoleCard)+" of "
|
||||
+getLongSuit(dealerHoleCard));
|
||||
SpeakDelay("Dealer's total is "+IntToString(total));
|
||||
|
||||
while (total < 17) {
|
||||
int newCard = pickCard(OBJECT_SELF);
|
||||
SpeakDelay("Dealer draws "+getLongValue(newCard)+" of "
|
||||
+getLongSuit(newCard));
|
||||
dealerHand += cardSer(newCard);
|
||||
total = calculateTotal(dealerHand);
|
||||
SpeakDelay("Dealer's new total is "+IntToString(total));
|
||||
}
|
||||
|
||||
SetLocalString(OBJECT_SELF, "BJDLRHAND", dealerHand);
|
||||
SetCustomToken(51, getFullHand(dealerHand));
|
||||
SetCustomToken(53, IntToString(total));
|
||||
|
||||
if (total > 21) {
|
||||
SpeakDelay("Dealer busts, everyone is a winner!");
|
||||
for (i=0; i<numPlayers; i++) {
|
||||
if (GetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(i)) <= 21) {
|
||||
// Player wins
|
||||
int bet = GetLocalInt(OBJECT_SELF, "BJBET"+IntToString(i));
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(i));
|
||||
GiveGoldToCreature(oPlayer, bet*2);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i=0; i<numPlayers; i++) {
|
||||
int playerTotal = GetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(i));
|
||||
if (playerTotal <= 21) {
|
||||
int bet = GetLocalInt(OBJECT_SELF, "BJBET"+IntToString(i));
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(i));
|
||||
if (playerTotal == total) {
|
||||
SpeakDelay(GetName(oPlayer)+" pushes");
|
||||
GiveGoldToCreature(oPlayer, bet);
|
||||
}
|
||||
else if (playerTotal > total) {
|
||||
SpeakDelay(GetName(oPlayer)+" with a total of "+
|
||||
IntToString(playerTotal)+" wins!");
|
||||
GiveGoldToCreature(oPlayer, bet*2);
|
||||
}
|
||||
else {
|
||||
SpeakDelay(GetName(oPlayer)+" with a total of "+
|
||||
IntToString(playerTotal)+" loses");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", 0);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(215));
|
||||
}
|
||||
|
||||
void HandleHit() {
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(currentPlayer));
|
||||
string playerHand = GetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(currentPlayer));
|
||||
|
||||
int newCard = pickCard(OBJECT_SELF);
|
||||
playerHand += cardSer(newCard);
|
||||
SetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(currentPlayer), playerHand);
|
||||
int numCards = GetLocalInt(OBJECT_SELF, "BJPLYRNUMCRDS"+IntToString(currentPlayer));
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRNUMCRDS"+IntToString(currentPlayer), numCards+1);
|
||||
SetCustomToken(52, getFullHand(playerHand));
|
||||
|
||||
// Calculate the player's hand
|
||||
int total = calculateTotal(playerHand);
|
||||
SetCustomToken(54, IntToString(total));
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(currentPlayer), total);
|
||||
|
||||
// Start talking to the bettor again
|
||||
if (oPlayer != OBJECT_INVALID) {
|
||||
if (total > 21) {
|
||||
SpeakDelay(GetName(oPlayer)+" drew "+getLongValue(newCard)+" of "
|
||||
+getLongSuit(newCard)+" and has busted with a total of "
|
||||
+IntToString(total));
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", currentPlayer+1);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(211));
|
||||
}
|
||||
else {
|
||||
SpeakDelay(GetName(oPlayer)+" drew "+getLongValue(newCard)+" of "
|
||||
+getLongSuit(newCard)+" new total "+IntToString(total));
|
||||
ActionStartConversation(oPlayer, "bjplyrplay", FALSE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
PrintString("Bummer, bettor not set correctly!");
|
||||
}
|
||||
}
|
||||
|
||||
void HandleStart() {
|
||||
// Check for chairs and if anyone is sitting in them
|
||||
int bFound = FALSE;
|
||||
|
||||
int index = 1;
|
||||
int playerIndex = 0;
|
||||
|
||||
object oChair = GetObjectByTag(GetTag(OBJECT_SELF)+IntToString(index));
|
||||
while (oChair != OBJECT_INVALID) {
|
||||
object oPlayer = GetSittingCreature(oChair);
|
||||
if (oPlayer != OBJECT_INVALID) { // We have someoneone!
|
||||
if (GetGold(oPlayer) == 0) {
|
||||
SpeakDelay(GetName(oPlayer)+" is out of money, please give up your seat for someone who has money deadbeat!");
|
||||
SetCommandable(TRUE, oPlayer);
|
||||
AssignCommand(oPlayer, ClearAllActions());
|
||||
}
|
||||
else {
|
||||
SetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(playerIndex), oPlayer);
|
||||
|
||||
playerIndex++;
|
||||
bFound = TRUE;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
oChair = GetObjectByTag(GetTag(OBJECT_SELF)+IntToString(index));
|
||||
}
|
||||
|
||||
if (bFound == FALSE) { // Noone is playing, reset bPlaying
|
||||
SetLocalInt(OBJECT_SELF, "BJPLAYING", FALSE);
|
||||
}
|
||||
else {
|
||||
SetLocalInt(OBJECT_SELF, "BJNUMPLYRS", playerIndex);
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", 0);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(213));
|
||||
}
|
||||
}
|
||||
|
||||
void HandleDeal() {
|
||||
SpeakDelay("Shuffling...");
|
||||
shuffle(OBJECT_SELF);
|
||||
int playerIndex = GetLocalInt(OBJECT_SELF, "BJNUMPLYRS");
|
||||
|
||||
// Deal to everyone
|
||||
int i;
|
||||
for (i=0; i<playerIndex; i++) {
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(i));
|
||||
int card = pickCard(OBJECT_SELF);
|
||||
SpeakDelay(GetName(oPlayer)+" gets "+getLongValue(card)+" of "
|
||||
+getLongSuit(card));
|
||||
SetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(i),
|
||||
cardSer(card));
|
||||
}
|
||||
|
||||
int dealerHole = pickCard(OBJECT_SELF);
|
||||
SetLocalInt(OBJECT_SELF, "BJDLRHOLE", dealerHole);
|
||||
SpeakDelay("Dealer gets face down card...");
|
||||
|
||||
for (i=0; i<playerIndex; i++) {
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(i));
|
||||
int card = pickCard(OBJECT_SELF);
|
||||
SpeakDelay(GetName(oPlayer)+" gets "+getLongValue(card)+" of "
|
||||
+getLongSuit(card));
|
||||
SetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(i),
|
||||
GetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(i))
|
||||
+cardSer(card));
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(i), calculateTotal(GetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(i))));
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRNUMCRDS"+IntToString(i), 2);
|
||||
|
||||
}
|
||||
int dealerFace = pickCard(OBJECT_SELF);
|
||||
SetLocalInt(OBJECT_SELF, "BJDLRFACE", dealerFace);
|
||||
SpeakDelay("Dealer has "+getLongValue(dealerFace)+" of "
|
||||
+getLongSuit(dealerFace)+" showing");
|
||||
SetLocalString(OBJECT_SELF, "BJDLRHAND", cardSer(dealerHole)
|
||||
+cardSer(dealerFace));
|
||||
|
||||
// check for blackjacks
|
||||
int dealerTotal = calculateTotal(cardSer(dealerHole)+cardSer(dealerFace));
|
||||
for (i=0; i<playerIndex; i++) {
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(i));
|
||||
int plyrTotal = GetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(i));
|
||||
|
||||
if (plyrTotal == 21) { // player has blackjack!
|
||||
SpeakDelay(GetName(oPlayer)+" has a blackjack!");
|
||||
if (dealerTotal == 21) {
|
||||
// just a push
|
||||
GiveGoldToCreature(oPlayer, GetLocalInt(OBJECT_SELF, "BJBET"+IntToString(i)));
|
||||
}
|
||||
else {
|
||||
// pay at 3/2
|
||||
int bet = GetLocalInt(OBJECT_SELF, "BJBET"+IntToString(i));
|
||||
GiveGoldToCreature(oPlayer, (bet*5)/2);
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(i), 22); // disable hand
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dealerTotal == 21) {
|
||||
SpeakDelay("Dealer's hole card is "
|
||||
+getLongValue(dealerHole)+" of "
|
||||
+getLongSuit(dealerHole)+" dealer has BlackJack. Everyone loses!");
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", 0);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(215));
|
||||
}
|
||||
else {
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", 0);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(211));
|
||||
}
|
||||
}
|
||||
|
||||
// Since we don't have an easy way to prepend 0's
|
||||
// on an in, simple card serializer
|
||||
string cardSer(int card) {
|
||||
if (card < 10)
|
||||
return "0"+IntToString(card);
|
||||
return IntToString(card);
|
||||
}
|
||||
|
||||
int calculateTotal(string hand) {
|
||||
int total = 0;
|
||||
int aces = 0;
|
||||
int i;
|
||||
for (i=0; i<GetStringLength(hand); i+=2) {
|
||||
int value = StringToInt(GetSubString(hand, i, 2))%13;
|
||||
|
||||
if (value == 0) {
|
||||
aces++;
|
||||
total+=11;
|
||||
}
|
||||
else if (value < 10) {
|
||||
total += value+1;
|
||||
}
|
||||
else {
|
||||
total += 10;
|
||||
}
|
||||
}
|
||||
|
||||
while (total > 21 && aces > 0) {
|
||||
aces--;
|
||||
total -= 10;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Given a hand like 012513, turn it into text
|
||||
// like 2C KH AS
|
||||
string getFullHand(string hand) {
|
||||
string fullName;
|
||||
int i;
|
||||
for (i=0; i<GetStringLength(hand); i+=2) {
|
||||
int card = StringToInt(GetSubString(hand, i, 2));
|
||||
fullName += getValue(card)+getSuit(card)+" ";
|
||||
}
|
||||
return fullName;
|
||||
}
|
||||
|
||||
void SpeakDelay(string message) {
|
||||
ActionDoCommand(SpeakString(message));
|
||||
ActionWait(2.0);
|
||||
}
|
||||
|
||||
void HandleBetting() {
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
int maxPlayer = GetLocalInt(OBJECT_SELF, "BJNUMPLYRS");
|
||||
|
||||
if (currentPlayer >= maxPlayer) {
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(200)); // deal!
|
||||
}
|
||||
else {
|
||||
ActionStartConversation(GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(currentPlayer)), "bjbetting", FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
void HandlePlaying() {
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
int maxPlayer = GetLocalInt(OBJECT_SELF, "BJNUMPLYRS");
|
||||
|
||||
if (currentPlayer >= maxPlayer) {
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(212)); // let dealer finish up
|
||||
}
|
||||
else {
|
||||
if (GetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(currentPlayer)) < 22) { // don't show if there was a blackjack
|
||||
SetCustomToken(50, IntToString(GetLocalInt(OBJECT_SELF, "BJBET"+IntToString(currentPlayer))));
|
||||
int card = GetLocalInt(OBJECT_SELF, "BJDLRFACE");
|
||||
SetCustomToken(51, "XX "+getValue(card)+getSuit(card));
|
||||
SetCustomToken(52, getFullHand(GetLocalString(OBJECT_SELF, "BJPLYRHAND"
|
||||
+IntToString(currentPlayer))));
|
||||
SetCustomToken(54, IntToString(calculateTotal(GetLocalString(OBJECT_SELF, "BJPLYRHAND"
|
||||
+IntToString(currentPlayer)))));
|
||||
|
||||
ActionStartConversation(GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(currentPlayer)), "bjplyrplay", FALSE);
|
||||
}
|
||||
else {
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", currentPlayer+1);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(211)); // next player
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleStand() {
|
||||
// increment the current player and signal play
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", currentPlayer+1);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(211));
|
||||
}
|
||||
|
||||
void HandleDoubleDown() {
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(currentPlayer));
|
||||
string playerHand = GetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(currentPlayer));
|
||||
|
||||
// double the bet
|
||||
int bet = GetLocalInt(OBJECT_SELF, "BJBET"+IntToString(currentPlayer));
|
||||
TakeGoldFromCreature(bet, oPlayer, TRUE);
|
||||
SetLocalInt(OBJECT_SELF, "BJBET"+IntToString(currentPlayer), bet*2);
|
||||
|
||||
int newCard = pickCard(OBJECT_SELF);
|
||||
playerHand += cardSer(newCard);
|
||||
SetLocalString(OBJECT_SELF, "BJPLYRHAND"+IntToString(currentPlayer), playerHand);
|
||||
SetCustomToken(52, getFullHand(playerHand));
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRNUMCRDS"+IntToString(currentPlayer), 3);
|
||||
|
||||
// Calculate the player's hand
|
||||
int total = calculateTotal(playerHand);
|
||||
SetCustomToken(54, IntToString(total));
|
||||
SetLocalInt(OBJECT_SELF, "BJPLYRTOTAL"+IntToString(currentPlayer), total);
|
||||
|
||||
SpeakDelay(GetName(oPlayer)+" doubles down, and draws "+getLongValue(newCard)+" of "
|
||||
+getLongSuit(newCard)+" for a total of "+IntToString(total));
|
||||
|
||||
// increment the current player and signal play
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", currentPlayer+1);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(211));
|
||||
}
|
||||
|
||||
void HandlePlayAgain() {
|
||||
// increment the current player and signal play
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
object oPlayer = GetLocalObject(OBJECT_SELF, "BJPLYR"+IntToString(currentPlayer));
|
||||
if (GetGold(oPlayer) == 0) {
|
||||
SpeakDelay(GetName(oPlayer)+" is out of money, please give up your seat for someone who has money deadbeat!");
|
||||
SetCommandable(TRUE, oPlayer);
|
||||
AssignCommand(oPlayer, ClearAllActions());
|
||||
|
||||
int numPlayers = GetLocalInt(OBJECT_SELF, "BJNUMPLYRS");
|
||||
int currentPlayer = GetLocalInt(OBJECT_SELF, "BJCURRENTPLYR");
|
||||
currentPlayer++;
|
||||
if (currentPlayer < numPlayers) {
|
||||
SetLocalInt(OBJECT_SELF, "BJCURRENTPLYR", currentPlayer);
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(215));
|
||||
}
|
||||
else {
|
||||
SignalEvent(OBJECT_SELF, EventUserDefined(214));
|
||||
}
|
||||
}
|
||||
else {
|
||||
ActionStartConversation(oPlayer, "bjplayagain", FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user