private void ReadBattleStateSerializationFile() { FinalGambit.BattleState = new BattleState(); FileStream f = File.OpenRead(FileNames.battleState); StreamReader sr = new StreamReader(f); bool isReadingForParty = true; PartyCharacter lastPC = null; LegalActionAndRequirments lastAction = null; // Console.WriteLine("starting reading "); string data; while ((data = sr.ReadLine()) != null) { //Console.WriteLine("reading " + data); string[] processing = data.Split(","); int identifier = Int32.Parse(processing[0]); if (identifier == CoreStats) { lastPC = new PartyCharacter(Int32.Parse(processing[1]), Int32.Parse(processing[2]), Int32.Parse(processing[3]), Int32.Parse(processing[4]), Int32.Parse(processing[5]), float.Parse(processing[6]), float.Parse(processing[7]), Int32.Parse(processing[8]), Int32.Parse(processing[9]), Int32.Parse(processing[10]), Int32.Parse(processing[11])); if (isReadingForParty) { FinalGambit.BattleState.partyCharacters.AddLast(lastPC); } else { FinalGambit.BattleState.foeCharacters.AddLast(lastPC); } } else if (identifier == FoePartyInfoStart) { isReadingForParty = false; } else if (identifier == Perk) { lastPC.perkIDs.AddLast(Int32.Parse(processing[1])); } else if (identifier == StatusEffect) { lastPC.statusEffects.AddLast(new StatusEffect(Int32.Parse(processing[1]), Int32.Parse(processing[2]))); } else if (identifier == PassiveAbility) { lastPC.passiveAbilities.AddLast(Int32.Parse(processing[1])); } else if (identifier == Ability) { lastPC.battleActions.AddLast(Int32.Parse(processing[1])); } else if (identifier == PartyInventoryItem) { if (isReadingForParty) { FinalGambit.BattleState.partyInventory.AddLast(new InventoryItem(Int32.Parse(processing[1]), Int32.Parse(processing[2]))); } else { FinalGambit.BattleState.foeInventory.AddLast(new InventoryItem(Int32.Parse(processing[1]), Int32.Parse(processing[2]))); } } else if (identifier == CharacterWithInitiative) { FinalGambit.BattleState.characterWithInitiative = FinalGambit.GetCharacterFromCharacterIndex(Int32.Parse(processing[1])); } else if (identifier == PossibleAction) { lastAction = new LegalActionAndRequirments(Int32.Parse(processing[1])); FinalGambit.BattleState.legalActions.AddLast(lastAction); } else if (identifier == ActionLegality) { //Console.WriteLine("Adding legality " + Int32.Parse(processing[1]) + " to " + FinalGambit.BattleActionID.lookUp[lastAction.actionID]); lastAction.legalities.AddLast(Int32.Parse(processing[1])); } else if (identifier == RecordOfBattleAction) { FinalGambit.recordOfBattleActions.AddLast(new RecordOfBattleAction(Int32.Parse(processing[1]), Int32.Parse(processing[2]), Int32.Parse(processing[3]), Int32.Parse(processing[4]))); FileStream f2; if (!File.Exists(FileNames.battleReport)) { //Console.WriteLine("report create"); f2 = File.Create(FileNames.battleReport); } else { //Console.WriteLine("report append"); f2 = File.Open(FileNames.battleReport, FileMode.Append); } StreamWriter sw = new StreamWriter(f2); //Console.WriteLine("report write = " + data); sw.WriteLine("" + data); sw.Close(); f2.Close(); } else if (identifier == IsFistStateSentForNewBattle) { FinalGambit.recordOfBattleActions = new LinkedList <RecordOfBattleAction>(); File.Delete(FileNames.battleReport); } } sr.Close(); f.Close(); }
static public bool AreBattleActionAndTargetLegal(int battleActionID, PartyCharacter target, bool writeErrorToConsole) { bool isLegal = true; LegalActionAndRequirments actionAndLegalReqs = null; foreach (LegalActionAndRequirments legalAction in FinalGambit.BattleState.legalActions) { if (legalAction.actionID == battleActionID) { actionAndLegalReqs = legalAction; break; } } if (actionAndLegalReqs == null) { isLegal = false; if (writeErrorToConsole) { Console.WriteLine(BattleActionID.lookUp[battleActionID] + " is not in the list of possible actions!"); } } if (actionAndLegalReqs != null) { if (target == null && !actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.NoTargetRequired)) { if (writeErrorToConsole) { Console.WriteLine(BattleActionID.lookUp[battleActionID] + " requires a target!"); } isLegal = false; } if (target != null) { if (target.hp > 0) { if (actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.TargetSlain)) { if (writeErrorToConsole) { Console.WriteLine(BattleActionID.lookUp[battleActionID] + " requires a slain target!"); } isLegal = false; } } else { if (!actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.TargetSlain)) { if (writeErrorToConsole) { Console.WriteLine(BattleActionID.lookUp[battleActionID] + " cannot target a slain character!"); } isLegal = false; } } int ind = GetCharacterIndexFromCharacter(target); //Console.WriteLine("ind = " + ind + " " + actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.TargetFoe) + " " + actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.TargetAlly)); if (ind > 0) { if (actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.TargetFoe)) { if (writeErrorToConsole) { Console.WriteLine(BattleActionID.lookUp[battleActionID] + " cannot target an ally, must target a foe!"); } isLegal = false; } } else if (ind < 0) { if (actionAndLegalReqs.legalities.Contains(LegalTargetIdentifiers.TargetAlly)) { if (writeErrorToConsole) { Console.WriteLine(BattleActionID.lookUp[battleActionID] + " cannot target a foe, must target an ally!"); } isLegal = false; } } } } return(isLegal); }