private void OnReceivedAction(TurnAction action) { bool validAction = false; if (action.components.Count == 1 && action.components[0].type == TurnActionComponentType.Forfeit) { validAction = true; } else { int pieceIndex = board.PieceOnSpace(action.searchPiece); if (pieceIndex >= 0) { TurnOptions currentOptions = turnOptionCalculator.Send(new TurnOptionCalculatorArgs() { pieceIndex = pieceIndex, luaState = LuaTranslator.GetMoveCalcState(pieceIndex, board, db), }); foreach (var possible in currentOptions.options) { if (possible.IsEquivalent(action)) { validAction = true; break; } } } } if (!validAction) { UnityEngine.Debug.LogError("Cheating detected! Or at least a mod mismatch!"); } choiceMadeEvent.Send(action); }
private TurnOptions CalculateMoveOptions(TurnOptionCalculatorArgs args) { TurnOptions finalOptions = new TurnOptions(); finalOptions.options = new List <TurnAction>(); Piece piece = board.Pieces[args.pieceIndex]; PiecePrototype proto = db.PiecePrototypes[piece.PrototypeID]; scripts.AddGlobal("actionHelper", luaHelper); DynValue ret = scripts.CallFunction(proto.MovementFunction, args.luaState); scripts.RemoveGlobal("actionHelper"); try { Table optionList = SafeGetTable(ret, string.Format("Piece {0} action function error: Expected table to be returned", proto.LuaTag)); int numOptions = optionList.Length; for (int i = 1; i <= numOptions; i++) { Table actionList = SafeGetTable(optionList.Get(i), string.Format("Piece {0} action function error: Expected table at position {1} in returned list", proto.LuaTag, i)); TurnAction action = TranslateActionTable(args.pieceIndex, actionList, i); finalOptions.options.Add(action); } } catch (BadActionException ex) { scripts.WriteToAuthorLog(ex.Message); finalOptions.options.Clear(); } return(finalOptions); }
private void OnActionChosen(TurnAction action) { if (states.State == (int)State.ChooseAction && action != null) { states.State = (int)State.Inactive; choiceMadeEvent.Send(action); requestSendAction.Send(action); } }
public static void FilterOptionsForPosition(List <int> actionList, TurnOptions allOptions, IntVector2 boardPos) { actionList.Clear(); for (int a = 0; a < allOptions.options.Count; a++) { TurnAction action = allOptions.options[a]; bool addAction = false; // Add if final move action ends at boardPos int lastMoveIndex = -1; for (int i = action.components.Count - 1; i >= 0; i--) { if (action.components[i].type == TurnActionComponentType.MovePiece) { lastMoveIndex = i; break; } } if (lastMoveIndex >= 0 && action.components[lastMoveIndex].target == boardPos) { addAction = true; } else { // Add actions that capture or promote a piece on this position foreach (var component in action.components) { switch (component.type) { case TurnActionComponentType.CapturePiece: if (component.target == boardPos) { addAction = true; } break; case TurnActionComponentType.PromotePiece: if (component.actor == boardPos) { addAction = true; } break; } if (addAction) { break; } } } if (addAction) { actionList.Add(a); } } }
private void HandleChoiceMade(TurnAction action) { stopTurnChoosingCommand.Send(); TurnActionExecutionResult result = TurnActionExecutionResult.Success; for (int c = 0; c < action.components.Count; c++) { TurnActionComponent comp = action.components[c]; try { switch (comp.type) { case TurnActionComponentType.MovePiece: moveCommand.Send(new MoveCommand() { startPosition = comp.actor, moveTo = comp.target, }); break; case TurnActionComponentType.CapturePiece: captureCommand.Send(new CaptureCommand() { space = comp.target, }); break; case TurnActionComponentType.PromotePiece: promoteCommand.Send(new PromoteCommand() { piecePos = comp.actor, promoteTo = comp.promotionIndex, }); break; case TurnActionComponentType.Forfeit: if (board.ActivePlayer == board.LocalPlayerOrder) { result = TurnActionExecutionResult.Forfeit; } break; default: throw new TurnActionExcecutionException("Bad component type"); } } catch (TurnActionExcecutionException ex) { scripts.WriteToAuthorLog(string.Format("Turn action execution error on component {0}: {1}", c, ex.Message)); result = TurnActionExecutionResult.Error; break; } } handleEndOfTurn.Send(result); }
private ActionIndicatorPattern Compile(ActionIndicatorPatternCompileArgs args) { ActionIndicatorPattern pattern = new ActionIndicatorPattern(); pattern.indicators = new List <ActionIndicator>(); List <TurnAction> options = args.options.options; for (int o = 0; o < options.Count; o++) { if (args.showOnly == null || args.showOnly.Contains(o)) { TurnAction action = options[o]; for (int c = 0; c < action.components.Count; c++) { TurnActionComponent comp = action.components[c]; IntVector2 boardPos = GetBoardPosition(comp); ActionIndicatorType type = GetIndicatorType(comp); int decorationModel = GetDecorationModel(comp); int promotionPiece = GetPromotionPiece(comp); ActionIndicatorStrength strength; if (args.mouseOverMode) { strength = ActionIndicatorStrength.Mouseover; } else if (o == args.highlightedIndex) { strength = ActionIndicatorStrength.Selected; } else { strength = ActionIndicatorStrength.Inactive; } if (!CheckForDuplicates(pattern, boardPos, type, decorationModel, promotionPiece, strength)) { pattern.indicators.Add(new ActionIndicator() { boardPosition = boardPos, type = type, decorationModel = decorationModel, strength = strength, promotionPiece = promotionPiece, }); } } } } return(pattern); }
public bool IsEquivalent(TurnAction other) { if (searchPiece == other.searchPiece && components.Count == other.components.Count) { for (int i = 0; i < components.Count; i++) { if (!components[i].IsEquivalent(other.components[i])) { return(false); } } return(true); } else { return(false); } }
private void ForfeitGameCommand() { switch ((State)states.State) { case State.ChoosePiece: case State.PieceChosenWait: case State.ChooseAction: TurnAction action = new TurnAction(); action.components = new List <TurnActionComponent>(); action.components.Add(new TurnActionComponent() { type = TurnActionComponentType.Forfeit, }); action.searchPiece = new IntVector2(-1, -1); choiceMadeEvent.Send(action); requestSendAction.Send(action); break; } }
public TurnAction TranslateActionTable(int searchPiece, Table actionList, int optionIndex) { TurnAction action = new TurnAction(); Piece piece = board.Pieces[searchPiece]; action.searchPiece = piece.BoardPosition; string pieceName = db.PiecePrototypes[piece.PrototypeID].LuaTag; int numActions = actionList.Length; //BalugaDebug.Log(numActions); action.components = new List <TurnActionComponent>(numActions); for (int i = 1; i <= numActions; i++) { string exMsgHeader = string.Format("Piece {0} action function error at Option {1} Index {2}:", pieceName, optionIndex, i); TurnActionComponent comp = SafeGetAction(actionList.Get(i), exMsgHeader); action.components.Add(comp); } return(action); }