----------------- Part 4.1 ----------------- Code Sample 4.1.1 " new ScriptClass(CheckerBoard); function CheckerBoard::isBlack(%this, %x, %y) { // this will decide if a checkerboard slot is black if( (%x % 2) && (%y % 2) ) return true; else if( %x % 2 || %y % 2 ) return false; else return true; } " Code Sample 4.1.2 " function CheckerBoard::setPiece(%this, %x, %y, %type) { // set the board's array position %this.board[%x,%y] = %type; } function CheckerBoard::getPiece(%this, %x, %y) { // return the piece stored at the position passed return %this.board[%x,%y]; } function CheckerBoard::removePiece(%this, %x, %y) { // set the board position to none %this.board[%x,%y] = $none; } " Code Sample 4.1.3 " function CheckerBoard::isLegalMove(%this, %fromX, %fromY, %toX, %toY) { // check if the move is to the same location, // if so we simply return "same" if(%fromX == %toX && %fromY == %toY) { return "same"; } // What color is this piece anyhow? %piece = %this.getPiece(%fromX, %fromY); // The trivial case, make sure the square is black. if(!%this.isBlack( %toX, %toY)) return false; // Now lets do another trivial check, lets make sure there // isn't a piece already in the spot we're trying to move to %moveToSlot = %this.getPiece(%toX, %toY); // If there is a piece there we can't move there if(%moveToSlot != $none) return false; // Check to make sure the move is in the right direction switch(%piece) { case 0: // Sanity check return false; case 1: // Red team, going in the positive Y direction if(%fromY > %toY) return false; case 2: // Blue team, going in negative Y direction if(%fromY < %toY) return false; // If it's a king, it will skip this, because kings can move in any // direction } // Now the tougher one, make sure the distance traveled is legal %xDistance = mAbs(%fromX - %toX); %yDistance = mAbs(%fromY - %toY); // Trivial case, the distance traveled is one. if(%xDistance == 1 && %yDistance == 1) { return true; } else if(%xDistance == 2 && %yDistance == 2) { // Check for jumps if(%fromX > %toX) { %xSign = -1; } else { %xSign = 1; } if(%fromY > %toY) { %ySign = -1; } else { %ySign = 1; } %newX = %fromX + %xSign; %newY = %fromY + %ySign; // get the spot in between where the client is trying to moveMap // and where the client's piece came from %midSpot = %this.getPiece(%newX, %newY); // here we set a value to check for king's to jump %king = getKing(%piece); %normal = getNormalPiece(%piece); // check if the midspot is either a teammate, a teammate king, or nothing, // if not then we jump it if((%midSpot == %normal) || (%midSpot == $none) || (%midSpot == %king)) { return false; } else { return %newX SPC %newY; } } return false; } " Code Sample 4.1.4 " function getKing(%team) { // check what team is being passed in and return the proper king if(%team == $red) { %king = $redKing; } else if(%team == $blue) { %king = $blueKing; } return %king; } function getNormalPiece(%team) { // check what team is being passed in and return the proper normal piece if(%team == $red || %team == $redKing) { %piece = $red; } else if(%team == $blue || %team == $blueKing) { %piece = $blue; } return %piece; } " ----------------- Part 4.2 ----------------- Code Sample 4.2.1 " function onServerCreated() { // toggle first connection to true $firstConnection = true; } " Code Sample 4.2.2 " function onClientConnected(%client) { // if this is the first connection then its a local connection, second // connection is the client connection, any beyond that we send an // "in match" message if($firstConnection) { $serverConnection = %client; setTeam(%client, $red); $firstConnection = false; initClient(); initServer(); Canvas.setContent($MainScreen); } else if(!$firstConnection) { if($inMatch) { commandToClient(%client, 'inMatch'); } else { setTeam(%client, $blue); $playerConnection = %client; commandToClient(%client, 'initClient'); serverLaunchGame(); } } } " Code Sample 4.2.3 " function initServer() { // Create an object to act as our Server check board new t2dStaticSprite(ServerCheckerBoard) { scenegraph = $checkersScene; class = CheckerBoard; }; // init the inmatch value to false $inMatch = false; } " Code Sample 4.2.4 " function ClientCheckerBoard::onLevelLoaded(%this, %scenegraph) { $checkersScene = %scenegraph; } function initClient() { Canvas.setContent($MainScreen); } " Code Sample 4.2.5 " function clientCmdInitClient() { initClient(); } " Code Sample 4.2.6 " function setTeam(%client, %team) { // store the team for the client %client.team = %team; // tell the client what team they are, server is boss commandToClient(%client, 'setTeam', %team); } " Code Sample 4.2.7 " function clientCmdSetTeam(%team) { // set the player's team $playerTeam = %team; } " Code Sample 4.2.8 " function clientCmdInMatch() { // spawn the message telling a client they are rejected, we are in a game without you MessageBoxOK("Server in a Match...", "The server you are trying to connect to is already in a match", ""); } " Code Sample 4.2.9 " function serverLaunchGame() { // we are no in a match $inMatch = true; // init the server's boards ServerCheckerBoard.init(); ClientCheckerBoard.init(); // tell the client to start the game commandToClient($playerConnection, 'StartGame'); } " Code Sample 4.2.10 " function clientCmdStartGame() { // call the start game on the client side clientLaunchGame(); } " Code Sample 4.2.11 " function clientLaunchGame() { Canvas.setContent($MainScreen); // init the client checker board ClientCheckerBoard.init(); // call the game started function on the server commandToServer('gameStarted'); } " Code Sample 4.2.12 " function serverCmdGameStarted(%client) { // tell the server to begin the game serverBeginGame(); } " Code Sample 4.2.13 " function serverBeginGame() { // set turn to the server's turn setTurn($serverConnection); } " Code Sample 4.2.14 " function setTurn(%player) { // get the other player %otherPlayer = getOtherPlayer(%player); // call the client's set turn function properly commandToClient(%otherPlayer, 'setPlayerTurn', $false); commandToClient(%player, 'setPlayerTurn', $true); // set the server's turn properly $playersTurn = %player; } " Code Sample 4.2.15 " function getOtherPlayer(%player) { // we check to see what play was passed and return the other player if(%player == $playerConnection) { %otherPlayer = $serverConnection; } else { %otherPlayer = $playerConnection; } return %otherPlayer; } " Code Sample 4.2.16 " function clientCmdSetPlayerTurn(%val) { // set the palyer's turn to the value passed $myTurn = %val; } " Code Sample 4.2.17 " function ServerCheckerBoard::init(%this) { // init the team counts $redCount = 0; $blueCount = 0; // loop through and create the server board for(%x = 0; %x < 8;%x++) { for(%y = 0;%y < 8;%y++) { // check if this is a black checkerboard spot if(%this.isBlack( %x, %y )) { // check if this is a checker's starting spot if(%y < 2) { // set the the red team's piece and add to the count %this.setPiece(%x, %y, $red); $redCount++; } else if( %y > 5 ) { // set the the blue team's piece and add to the count %this.setPiece(%x, %y, $blue); $blueCount++; } else { // set the blank spot %this.setPiece(%x, %y, $none); } } else { // set the blank spot %this.setPiece(%x, %y, $none); } } } } " Code Sample 4.2.18 " function ClientCheckerBoard::getTilePosition(%this, %x, %y) { // grab the board layer's position %tileMapPos = %this.boardLayer.getPosition(); // divide the position up into x and y variables %tileMapPosX = getWord(%tileMapPos, 0); %tileMapPosY = getWord(%tileMapPos, 1); // grab the board layer's size %tileMapSize = %this.boardLayer.getSize(); // divide the size up into x and y variables %tileMapSizeX = getWord(%tileMapSize, 0); %tilemapSizeY = getWord(%tileMapSize, 1); // calculate the start position %tileMapStartX = %tileMapPosX - (%tileMapSizeX / 2); %tileMapStartY = %tileMapPosY - (%tileMapSizeY / 2); // calculate the position and pass it back %pos = (%tileMapStartX + (%x * 64)) + 32 SPC (%tileMapStartY + (%y * 64)) + 32; return %pos; } " Code Sample 4.2.19 " function ClientCheckerBoard::initPiece(%this, %type) { // check which team the piece is if(%type == $red) { // get the count of current pieces of that color %num = %this.redPieces.getCount(); // create a new piece as a clone of our stored piece %piece = $redChecker.clone(true); %piece.setName("redPiece" @ %num); // add the piece to its color's container object %this.redPieces.add(%piece); } else if(%type == $blue) { // get the count of current pieces of that color %num = %this.bluePieces.getCount(); // create a new piece as a clone of our stored piece %piece = $blueChecker.clone(true); %piece.setName("bluePiece" @ %num); // add the piece to its color's container object %this.bluePieces.add(%piece); } // set the piece's size and return it %piece.setSize("60 60"); return %piece; } " Code Sample 4.2.20 " function ClientCheckerBoard::setClientPiece(%this, %x, %y, %type, %piece) { // if the piece isn't empty we do some extra settings if(%type != 0) { // grab the position of the piece %pos = %this.getTilePosition(%x, %y); // set the position of the piece %piece.setPosition(%pos); // set the image %this.images[%x,%y] = %piece; } // set the piece's type %this.setPiece(%x, %y, %type); } " Code Sample 4.2.21 " function ClientCheckerBoard::init(%this) { // create container object's for the team %this.redPieces = new SimSet(); %this.bluePieces = new SimSet(); // here we loop through and create the checker board and the initial checkers for(%x = 0; %x < 8;%x++) { for(%y = 0;%y < 8;%y++) { // check if the checker board spot is black if(%this.isBlack(%x, %y)) { // here we check to see if we're populating the checker's // starting area if(%y < 2) { // create a new red piece %piece = %this.initPiece($red); // set this piece to this location %this.setClientPiece(%x, %y, $red, %piece); } else if( %y > 5 ) { // create a new blue checker piece %piece = %this.initPiece($blue); // set the piece to this location %this.setClientPiece(%x, %y, $blue, %piece); } else { // set the empty space %this.setClientPiece(%x, %y, $none); } } else { %this.setClientPiece(%x, %y, $none); } } } } "