IEnumerator MoveDelay() //0.031초마다 플레이어의 위치, 회전을 상대 유저에게 보냄 { yield return(new WaitForSeconds(0.31f)); enemyObj = spawnInfo.nowEnemy; enemyAtk = enemyObj.GetComponent <AttackMgr>(); enemyAtk.playerWeapon = weaponNum; while (true) { Vector3 pos = transform.position; Quaternion rot = transform.rotation; sMove move = new sMove(pos.x, pos.y, pos.z, rot.x, rot.y, rot.z, rot.w); SocketServer.SingleTonServ().SendMsg(move); yield return(new WaitForSeconds(0.031f)); } }
private static void ReceiveCallBack(IAsyncResult ar) //서버에게 신호를 받았을 때 { RecvData recvData = (RecvData)ar.AsyncState; //리시브된 데이터 int receive = recvData.socket.EndReceive(ar); //리시브된 정보가 있는지 체크 if (receive <= 0) //서버 종료 { recvData.socket.Close(); //소켓을 닫음 Application.Quit(); return; } IntPtr buff = Marshal.AllocHGlobal(recvData.buffer.Length); //받은 byte 데이터를 struct로 변환 Marshal.Copy(recvData.buffer, 0, buff, recvData.buffer.Length); Type type = typeof(sGameRoom); room = (sGameRoom)Marshal.PtrToStructure(buff, type); if (room.flag == (int)eMSG.em_LOGIN) { Type m_type = typeof(sLogin); sLogin loginInfo = (sLogin)Marshal.PtrToStructure(buff, m_type); userScript.LoginResult(loginInfo.nick, loginInfo.loginSucc); } else if (room.flag == (int)eMSG.em_LOGINCHECK) { Type m_type = typeof(sLoginCheck); sLoginCheck loginChk = (sLoginCheck)Marshal.PtrToStructure(buff, m_type); fScript.FriendAccCheck(loginChk.nick, loginChk.loginChk); } else if (room.flag == (int)eMSG.em_MATCHREQUEST) { Type m_type = typeof(sMatchReq); sMatchReq matchReq = (sMatchReq)Marshal.PtrToStructure(buff, m_type); fScript.MatchReqResult(matchReq.sendUserNick, matchReq.matchSucc); } else if (room.flag == (int)eMSG.em_ENTER) //매칭 버튼을 눌렀다는 정보 { if (room.userNum == 0) //상대가 없음 { gScript.Matching(); } else //상대가 접속 { enterNum = room.userNum; gScript.MatchSucc(); } } else if (room.flag == (int)eMSG.em_CHARINFO) //아이템 선택이 끝났다는 정보 { Type m_type = typeof(sCharInfo); sCharInfo charInfo = (sCharInfo)Marshal.PtrToStructure(buff, m_type); if (enterNum == 1) //내가 먼저 입장 { sScript.SpawnInfo("Spawn1", "Spawn2", charInfo); } else if (enterNum == 2) //상대가 먼저 입장 { sScript.SpawnInfo("Spawn2", "Spawn1", charInfo); } } else if (room.flag == (int)eMSG.em_READY) //두 유저 모두 준비되었다는 정보 { sScript.SpawnReady(); } else if (eScript != null && room.flag == (int)eMSG.em_MOVE) //적의 좌표, 회전 정보 { Type m_type = typeof(sMove); sMove move = (sMove)Marshal.PtrToStructure(buff, m_type); Vector3 enemyPos = new Vector3(move.x, move.y, move.z); Quaternion enemyRot = new Quaternion(move.rotX, move.rotY, move.rotZ, move.rotW); eScript.EnemyMove(enemyPos, enemyRot); } else if (room.flag == (int)eMSG.em_ATK) //적이 공격했다는 정보 { Type m_type = typeof(sAtk); sAtk atk = (sAtk)Marshal.PtrToStructure(buff, m_type); eScript.EnemyAtk(atk.atkAni); } else if (room.flag == (int)eMSG.em_HIT) //내가 공격을 성공함 { Type m_type = typeof(sHit); sHit hit = (sHit)Marshal.PtrToStructure(buff, m_type); pScript.ChangePlayerHp(hit.hp); pScript.PlayerDamage(hit); } else if (room.flag == (int)eMSG.em_INFO) //적의 hp정보가 변한경우 { Type m_type = typeof(sChangeInfo); sChangeInfo hpInfo = (sChangeInfo)Marshal.PtrToStructure(buff, m_type); eScript.ChangeEnemyHp(hpInfo.hp); } else if (room.flag == (int)eMSG.em_ITEMSPAWN) { Type m_type = typeof(sItemSpawn); sItemSpawn itemSpawn = (sItemSpawn)Marshal.PtrToStructure(buff, m_type); pScript.passOnItemSpawnInfo(itemSpawn.itemKind); pScript.GetAtkMgrFromServer(true); } else if (room.flag == (int)eMSG.em_USEITEM) //아이템 사용 { Type m_type = typeof(sUseItem); sUseItem useItem = (sUseItem)Marshal.PtrToStructure(buff, m_type); pScript.ChangeItemImg(useItem.itemNum, true); pScript.ChangePlayerHp(useItem.hp); pScript.ChangePlayerSpeed(useItem.speed); } else if (room.flag == (int)eMSG.em_ENDITEM) //아이템 시간 끝 { Type m_type = typeof(sEndItem); sEndItem endItem = (sEndItem)Marshal.PtrToStructure(buff, m_type); pScript.ChangeItemImg(endItem.itemNum, false); pScript.ChangePlayerSpeed(endItem.speed); } else if (room.flag == (int)eMSG.em_GETOBJ) //던질 물건 잡기 { Type m_type = typeof(sGetObj); sGetObj getObj = (sGetObj)Marshal.PtrToStructure(buff, m_type); Debug.Log("from server get succ"); eScript.GetThrowObj(getObj.itemNum); } else if (room.flag == (int)eMSG.em_THROWOBJ) //물건 던지기 { eScript.ThrowObj(); Debug.Log("from server throw succ"); } else if (room.flag == (int)eMSG.em_END) //게임이 종료되었을 경우 { Type m_type = typeof(sEnd); sEnd esc = (sEnd)Marshal.PtrToStructure(buff, m_type); gameResult = esc.result; enemyNick = esc.enemyNick; if (pScript == null) { sScript.GameEndTrue(); } else { pScript.GameEndTrue(); } } else if (room.flag == (int)eMSG.em_CHAT) //채팅 { Type m_type = typeof(sChat); sChat chat = (sChat)Marshal.PtrToStructure(buff, m_type); string nowChat = new string(chat.chat); chatScript.ChatRecv(nowChat); } }
public void Set( sMove move ) { this.moveType = move.moveType; this.trgSquare = move.trgSquare; this.srcSquare = move.srcSquare; this.capturedSquare = move.capturedSquare; }
public static void BitBoardToMoveList( ulong ulBitboard, MoveType moveType, ChessBoard board, ChessBoardSquare srcSquare, List<sMove> listRetBoardPos ) { for( int i=0; i<ChessData.nNumBoardSquare; i++ ) { ulong ulCurrMask = (ulong)1 << i; ulong ulCurrPosition = ulBitboard & ulCurrMask; if( ulCurrPosition > 0 ) { int nCurrRank, nCurrFile; nCurrRank = i % ChessData.nNumRank; nCurrFile = i / ChessData.nNumPile; ChessBoardSquare trgSquare = board.aBoardSquare[nCurrFile, nCurrRank]; ChessBoardSquare capturedSquare = null; sMove move = new sMove(); // check capture move if( ChessMover.IsCaptureMove( moveType ) ) { if( ChessMover.IsEnpassantMove( moveType ) ) { int nCapturedPawnRank, nCapturedPawnFile; nCapturedPawnRank = nCurrRank; nCapturedPawnFile = srcSquare.piece.playerSide == PlayerSide.e_White ? nCurrFile - ChessData.nNumPile : nCurrFile + ChessData.nNumPile; capturedSquare = board.aBoardSquare[nCapturedPawnFile, nCapturedPawnRank]; } else capturedSquare = trgSquare; } // check pawn promote move if( ChessMover.IsPawnMove( moveType ) ) { // promote move check if( srcSquare.piece.playerSide == PlayerSide.e_White ) { if( (ulCurrMask & ChessBitBoard.firstRank) > 0 ) move.moveType |= MoveType.ePromote_Move; } else { if( (ulCurrMask & ChessBitBoard.lastRank) > 0 ) move.moveType |= MoveType.ePromote_Move; } } // normal move move.moveType = moveType; move.srcSquare = srcSquare; move.trgSquare = trgSquare; move.capturedSquare = capturedSquare; // check mate state // virtually move board.bitBoardVirtual.CopyFrom( board.bitBoard ); board.bitBoardVirtual.MoveUpdate( move ); bool bWillCheckMateState = false; if( srcSquare.piece.playerSide == PlayerSide.e_White ) { if( board.bitBoardVirtual.IsWhiteKingInCheck() ) { bWillCheckMateState = true; } } else if( srcSquare.piece.playerSide == PlayerSide.e_Black ) { if( board.bitBoardVirtual.IsBlackKingInCheck() ) { bWillCheckMateState = true; } } // unmove virtually // skip this move if( bWillCheckMateState ) continue; // for debug foreach( sMove aMove in listRetBoardPos ) { if( aMove.trgSquare == move.trgSquare ) { UnityEngine.Debug.Log( "!!!!!!!!!!!!!!!!!!!!!!!!ChessMover::BitBoardToMoveList() - move collision Aleady exist!!!! " + "file : " + move.trgSquare.position.nPile + " Rank : " + move.trgSquare.position.nRank ); string strOccupied = string.Format( "occupied : {0:X}", board.bitBoard.occupiedBB ); UnityEngine.Debug.LogError( "!!!!!!!!!!!!!!!!!!!!!!!!ChessMover::BitBoardToMoveList() - " + strOccupied ); string strEmpty = string.Format( "empty : {0:X}", board.bitBoard.emptyBB ); UnityEngine.Debug.LogError( "!!!!!!!!!!!!!!!!!!!!!!!!ChessMover::BitBoardToMoveList() - " + strEmpty ); } } listRetBoardPos.Add( move ); } } }
public static bool GetKingMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // all(radial) direction one move int nTempRank, nTempPile; for( int nMovePile=-1; nMovePile<=1; nMovePile++ ) { for( int nMoveRank=-1; nMoveRank<=1; nMoveRank++ ) { nTempRank = nMoveRank; nTempPile = nMovePile; movePos.SetPosition( srcPos ); bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // normal move if( trgSquare.IsBlank() ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eNormal_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } // capture move else if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eCapture_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } } } // castling move // king side castling movePos.SetPosition( srcPos ); nTempRank = 2; nTempPile = 0; bool bValidCastlingMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsBlank() ) { // position check castling bool bCalstling = false; if( srcPlayerSide == PlayerSide.e_White ) { if( board.currCastlingState.CastlingWKSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } else { if( board.currCastlingState.CastlingBKSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } if( bCalstling ) { // check look square blank nTempRank = -1; nTempPile = 0; ChessPosition moveLookPos = new ChessPosition(movePos.pos); bValidCastlingMove = moveLookPos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare lookTrgSquare = board.aBoardSquare[moveLookPos.nPile, moveLookPos.nRank]; if( lookTrgSquare.IsBlank() ) { sMove move = new sMove(); MoveType castlingSideType = srcPlayerSide == PlayerSide.e_White ? MoveType.eCastling_White_KingSide_Move : MoveType.eCastling_Black_KingSide_Move; move.moveType = MoveType.eCastling_Move | castlingSideType; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } } } } // queen side castling movePos.SetPosition( srcPos ); nTempRank = -2; nTempPile = 0; bValidCastlingMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsBlank() ) { // position check castling bool bCalstling = false; if( srcPlayerSide == PlayerSide.e_White ) { if( board.currCastlingState.CastlingWQSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } else { if( board.currCastlingState.CastlingBQSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } if( bCalstling ) { // check look square blank nTempRank = 1; nTempPile = 0; ChessPosition moveLookPos = new ChessPosition(movePos.pos); bValidCastlingMove = moveLookPos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare lookTrgSquare = board.aBoardSquare[moveLookPos.nPile, moveLookPos.nRank]; if( lookTrgSquare.IsBlank() ) { sMove move = new sMove(); MoveType castlingSideType = srcPlayerSide == PlayerSide.e_White ? MoveType.eCastling_White_QueenSide_Move : MoveType.eCastling_Black_QueenSide_Move; move.moveType = MoveType.eCastling_Move | castlingSideType; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } } } } return true; }
// stright line move public static bool GetStraightMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos, MoveDirectionType moveDirection ) { ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // all(radial) direction one move int nTempRank, nTempPile; int nIterCount; nIterCount = GetNumDirectionIterCount( movePos.nRank, movePos.nPile, moveDirection ); //UnityEngine.Debug.LogError( "GetStraightMoveList() - nIterCount = " + nIterCount + " movePos.nRank, movePos.nPile " + movePos.nRank + " " + movePos.nPile ); for( int nCurrIter=1; nCurrIter<=nIterCount; nCurrIter++ ) { nTempRank = 0; nTempPile = 0; GetNextDirectionRankPile( ref nTempRank, ref nTempPile, moveDirection, nCurrIter ); //UnityEngine.Debug.LogError( "GetStraightMoveList() - nTempRank, nTempPile " + nTempRank + " " + nTempPile ); movePos.SetPosition( srcPos ); bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { UnityEngine.Debug.LogError( "GetStraightMoveList() - bValidMove - nTempRank, nTempPile " + nTempRank + " " + nTempPile ); ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // normal move if( trgSquare.IsBlank() ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eNormal_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } // capture move else if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eCapture_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); return true; } // our piece else { if( nCurrIter > 1 ) return true; return false; } } } return false; }
public static bool GetPawnMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { //UnityEngine.Debug.LogError( "GetPawnMoveList - start" + " " + piece.position + " " + piece.playerSide ); ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // pure move // pure move - one pile move int nTempRank, nTempPile; nTempRank = 0; nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1; bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // check already existing piece if( trgSquare.IsBlank() ) { // normal move sMove move = new sMove(); move.moveType = MoveType.eNormal_Move | MoveType.ePawn_Move; // promote move if( srcPlayerSide == PlayerSide.e_White ) { if( movePos.IsTopBoundary() ) move.moveType |= MoveType.ePromote_Move; } else { if( movePos.IsBottomBoundary() ) move.moveType |= MoveType.ePromote_Move; } move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } // pure move - two pile move if( ( srcPos.nPile == 1 && srcPlayerSide == PlayerSide.e_White ) || ( srcPos.nPile == 6 && srcPlayerSide == PlayerSide.e_Black ) ) { movePos.SetPosition( srcPos ); nTempRank = 0; nTempPile = srcPlayerSide == PlayerSide.e_White ? 2 : -2; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsBlank() ) { sMove move = new sMove(); move.moveType = MoveType.eNormal_Move | MoveType.ePawn_Move | MoveType.ePawn_Two_Move ; move.srcSquare = selSquare; move.trgSquare = trgSquare; // en passant target move check move.enPassantTargetSquare.Rank = movePos.nRank; move.enPassantTargetSquare.Pile = movePos.nPile; move.enPassantTargetSquare.Available = true; listRetBoardPos.Add( move ); } } } // capture move // left diagonal capture // check left boundary movePos.SetPosition( srcPos ); nTempRank = -1; nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eCapture_Move | MoveType.ePawn_Move; // promote move if( srcPlayerSide == PlayerSide.e_White ) { if( movePos.IsTopBoundary() ) move.moveType |= MoveType.ePromote_Move; } else { if( movePos.IsBottomBoundary() ) move.moveType |= MoveType.ePromote_Move; } move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } // right diagonal capture // check right boundary movePos.SetPosition( srcPos ); nTempRank = 1; nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eCapture_Move | MoveType.ePawn_Move; // promote move if( srcPlayerSide == PlayerSide.e_White ) { if( movePos.IsTopBoundary() ) move.moveType |= MoveType.ePromote_Move; } else { if( movePos.IsBottomBoundary() ) move.moveType |= MoveType.ePromote_Move; } move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } // en-passant move // left en passant move check movePos.SetPosition( srcPos ); nTempRank = -1; nTempPile = 0; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) && trgSquare.piece.bEnPassantCapture ) { if( srcPlayerSide == PlayerSide.e_White ) bValidMove = movePos.MovePosition( 0, 1 ); else bValidMove = movePos.MovePosition( 0, -1 ); if( bValidMove ) { ChessBoardSquare finalTrgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( finalTrgSquare.IsBlank() ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move; move.srcSquare = selSquare; move.trgSquare = finalTrgSquare; listRetBoardPos.Add( move ); } } } } // right en passant move check movePos.SetPosition( srcPos ); nTempRank = 1; nTempPile = 0; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) && trgSquare.piece.bEnPassantCapture ) { if( srcPlayerSide == PlayerSide.e_White ) bValidMove = movePos.MovePosition( 0, 1 ); else bValidMove = movePos.MovePosition( 0, -1 ); if( bValidMove ) { ChessBoardSquare finalTrgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( finalTrgSquare.IsBlank() ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move; move.srcSquare = selSquare; move.trgSquare = finalTrgSquare; listRetBoardPos.Add( move ); } } } } return true; }
// leap move public static bool GetLeapMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos, MoveDirectionType moveDirection ) { ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // all(radial) direction one move int nTempRank = 0, nTempPile = 0; GetNextDirectionRankPile( ref nTempRank, ref nTempPile, moveDirection, 0 ); movePos.SetPosition( srcPos ); bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // normal move if( trgSquare.IsBlank() ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eNormal_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); return true; } // capture move else if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eCapture_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); return true; } // our piece else { return false; } } return false; }
public void Set( sMove move ) { this.moveType = move.moveType; this.trgSquare = move.trgSquare; this.srcSquare = move.srcSquare; this.enPassantTargetSquare = move.enPassantTargetSquare; }