Skip to content

Commit

Permalink
Major cleanup?!?!?!?!??!!!!!!!!?!?!?!!!!!!!!!!?!
Browse files Browse the repository at this point in the history
Changed PBEAction struct into the PBETurnAction class
Changed the switch-ins value tuple into the PBESwitchIn class
Renamed PBEAIManager to PBEAI
Renamed PBETarget to PBETurnTarget
Removed the Teams array from PBEBattle and made the PBETeams class
Added "OpposingTeam" into PBETeam
Using IEnumerable<T> way less because it wasn't good
Changed 3-line <summary> comments into 1-line comments
Cleaned up most switch statements to throw ArgumentOutOfRangeException
Added victim to PBEMoveCritPacket
Changed pp into an int (partial #200)
Changed moves, pp, and maxpps in PBEPokemon into the PBEBattleMoveset class which also adds KnownPP
Added IDisposable to classes so they can unsubscribe from events
Added ArgumentNullException checks to public methods
Renamed PBEMovesetBuilder to PBEMoveset
Renamed PBEEffortValueCollection to PBEEffortValues
Renamed PBEIndividualValueCollection to PBEIndividualValues
For the above three, setting values is simplified to properties (no longer done with methods)
Changed PBETeam.Party into a PBEList and added a Remove() method
PBESetPartyPacket was renamed to PBETeamPacket and is now sent by the battle itself (and so the replays do not need to store the party explicitly, the packet within the replay will load the party)
Ids are now based on the team id, so that way team 2 does not know how many Pokémon team 1 has
PropertyChanged and CollectionChanged events will now only fire if they ACTUALLY changed
PBEPokemonShell can now be created publicly (but not added to a PBETeamShell yet)
Packets now are created internally with methods to make the info hidden (special packets are still public, such as sending your team)
Packets also now properly store the Buffer property when being received/read
Packet buffers are now read only collections of bytes and less overhead goes into creating them
Time was removed from the PBETurnBeganPacket because it wasn't necessary and didn't really make sense
All project files now define the entry point
Silenced the "missing xml comment" warning to allow for better focus on actual warnings and errors

[CLIENT]
BattleClient now properly responds to the PBEMatchCancelledPacket and the packet timer was removed in place of a thread that only starts once all packets are received
BattleClient also now includes the player's PBETeam for easy access
Changed public setters to private/internal ones

[ISSUES]
Closes #142
Closes #203
Fixes #208
Closes #209
Fixes #211
  • Loading branch information
Kermalis committed Sep 16, 2019
1 parent 858f914 commit db14794
Show file tree
Hide file tree
Showing 122 changed files with 5,947 additions and 5,460 deletions.
121 changes: 49 additions & 72 deletions PokemonBattleEngine/AI/AIDecisions.cs

Large diffs are not rendered by default.

104 changes: 52 additions & 52 deletions PokemonBattleEngine/AI/AITargets.cs

Large diffs are not rendered by default.

250 changes: 162 additions & 88 deletions PokemonBattleEngine/Battle/Battle.cs

Large diffs are not rendered by default.

346 changes: 223 additions & 123 deletions PokemonBattleEngine/Battle/BattleActions.cs

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions PokemonBattleEngine/Battle/BattleDamage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ namespace Kermalis.PokemonBattleEngine.Battle
{
public sealed partial class PBEBattle
{
/// <summary>
/// Gets the influence a stat change has on a stat.
/// </summary>
/// <summary>Gets the influence a stat change has on a stat.</summary>
/// <param name="change">The stat change.</param>
/// <param name="forMissing">True if the stat is <see cref="PBEStat.Accuracy"/> or <see cref="PBEStat.Evasion"/>.</param>
public static double GetStatChangeModifier(sbyte change, bool forMissing)
Expand All @@ -19,9 +17,7 @@ public static double GetStatChangeModifier(sbyte change, bool forMissing)
return numerator / denominator;
}

/// <summary>
/// Deals damage to <paramref name="victim"/> and broadcasts the HP changing and substitute damage.
/// </summary>
/// <summary>Deals damage to <paramref name="victim"/> and broadcasts the HP changing and substitute damage.</summary>
/// <param name="culprit">The Pokémon responsible for the damage.</param>
/// <param name="victim">The Pokémon receiving the damage.</param>
/// <param name="hp">The amount of HP <paramref name="victim"/> will try to lose.</param>
Expand Down Expand Up @@ -81,9 +77,7 @@ private ushort DealDamage(PBEPokemon culprit, PBEPokemon victim, ushort hp, bool
return (ushort)(oldHP - victim.HP);
}
}
/// <summary>
/// Restores HP to <paramref name="pkmn"/> and broadcasts the HP changing if it changes.
/// </summary>
/// <summary>Restores HP to <paramref name="pkmn"/> and broadcasts the HP changing if it changes.</summary>
/// <param name="pkmn">The Pokémon receiving the HP.</param>
/// <param name="hp">The amount of HP <paramref name="pkmn"/> will try to gain.</param>
/// <returns>The amount of HP restored.</returns>
Expand Down Expand Up @@ -502,6 +496,10 @@ private double CalculateBasePower(PBEPokemon user, PBEPokemon[] targets, PBEMove
break;
}
break;
case PBEType.None:
{
break;
}
case PBEType.Normal:
switch (user.Item)
{
Expand Down Expand Up @@ -613,6 +611,7 @@ private double CalculateBasePower(PBEPokemon user, PBEPokemon[] targets, PBEMove
break;
}
break;
default: throw new ArgumentOutOfRangeException(nameof(moveType));
}

// Move-specific power boosts
Expand Down
125 changes: 66 additions & 59 deletions PokemonBattleEngine/Battle/BattleEffects.cs

Large diffs are not rendered by default.

66 changes: 43 additions & 23 deletions PokemonBattleEngine/Battle/BattleEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Kermalis.PokemonBattleEngine.Data;
using Kermalis.PokemonBattleEngine.Packets;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Kermalis.PokemonBattleEngine.Battle
Expand Down Expand Up @@ -50,9 +49,9 @@ private void BroadcastItem(PBEPokemon itemHolder, PBEPokemon pokemon2, PBEItem i
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastMoveCrit()
private void BroadcastMoveCrit(PBEPokemon victim)
{
var p = new PBEMoveCritPacket();
var p = new PBEMoveCritPacket(victim);
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
Expand All @@ -68,7 +67,7 @@ private void BroadcastMoveFailed(PBEPokemon moveUser, PBEPokemon pokemon2, PBEFa
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastMoveLock(PBEPokemon moveUser, PBEMove lockedMove, PBETarget lockedTargets, PBEMoveLockType moveLockType)
private void BroadcastMoveLock(PBEPokemon moveUser, PBEMove lockedMove, PBETurnTarget lockedTargets, PBEMoveLockType moveLockType)
{
var p = new PBEMoveLockPacket(moveUser, lockedMove, lockedTargets, moveLockType);
Events.Add(p);
Expand All @@ -80,18 +79,18 @@ private void BroadcastMoveMissed(PBEPokemon moveUser, PBEPokemon pokemon2)
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastMovePPChanged(PBEPokemon moveUser, PBEMove move, byte oldValue, byte newValue)
private void BroadcastMovePPChanged(PBEPokemon moveUser, PBEMove move, int amountReduced)
{
var p = new PBEMovePPChangedPacket(moveUser.FieldPosition, moveUser.Team, move, oldValue, newValue);
var p = new PBEMovePPChangedPacket(moveUser.FieldPosition, moveUser.Team, move, amountReduced);
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastMoveUsed(PBEPokemon moveUser, PBEMove move)
{
bool reveal;
if (!calledFromOtherMove && moveUser.Moves.Contains(move) && !moveUser.KnownMoves.Contains(move))
if (!_calledFromOtherMove && moveUser.Moves.Contains(move) && !moveUser.KnownMoves.Contains(move))
{
moveUser.KnownMoves[Array.IndexOf(moveUser.KnownMoves, PBEMove.MAX)] = move;
moveUser.KnownMoves[PBEMove.MAX].Move = move;
reveal = true;
}
else
Expand Down Expand Up @@ -136,7 +135,7 @@ private void BroadcastPkmnStatChanged(PBEPokemon pokemon, PBEStat stat, sbyte ol
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastPkmnSwitchIn(PBETeam team, IEnumerable<PBEPkmnSwitchInPacket.PBESwitchInInfo> switchIns, bool forced)
private void BroadcastPkmnSwitchIn(PBETeam team, PBEPkmnSwitchInPacket.PBESwitchInInfo[] switchIns, bool forced)
{
var p = new PBEPkmnSwitchInPacket(team, switchIns, forced);
Events.Add(p);
Expand Down Expand Up @@ -246,6 +245,12 @@ private void BroadcastAutoCenter(byte pokemon1Id, PBEFieldPosition pokemon1Posit
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastTeam(PBETeam team)
{
var p = new PBETeamPacket(team);
Events.Add(p);
OnNewEvent?.Invoke(this, p);
}
private void BroadcastSwitchInRequest(PBETeam team)
{
var p = new PBESwitchInRequestPacket(team);
Expand All @@ -266,13 +271,25 @@ private void BroadcastWinner(PBETeam winningTeam)
}


/// <summary>
/// Writes battle events to <see cref="Console.Out"/> in English.
/// </summary>
/// <summary>Writes battle events to <see cref="Console.Out"/> in English.</summary>
/// <param name="battle">The battle that <paramref name="packet"/> belongs to.</param>
/// <param name="packet">The battle event packet.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="battle"/> or <paramref name="packet"/> are null.</exception>
public static void ConsoleBattleEventHandler(PBEBattle battle, INetPacket packet)
{
if (battle == null)
{
throw new ArgumentNullException(nameof(battle));
}
if (packet == null)
{
throw new ArgumentNullException(nameof(packet));
}
if (battle.IsDisposed)
{
throw new ObjectDisposedException(nameof(battle));
}

string NameForTrainer(PBEPokemon pkmn)
{
return pkmn == null ? string.Empty : $"{pkmn.Team.TrainerName}'s {pkmn.KnownNickname}";
Expand Down Expand Up @@ -590,9 +607,10 @@ string NameForTrainer(PBEPokemon pkmn)
Console.WriteLine(message, NameForTrainer(itemHolder), NameForTrainer(pokemon2), PBELocalizedString.GetItemName(ip.Item).English);
break;
}
case PBEMoveCritPacket _:
case PBEMoveCritPacket mcp:
{
Console.WriteLine("A critical hit!");
PBEPokemon victim = mcp.VictimTeam.TryGetPokemon(mcp.Victim);
Console.WriteLine("A critical hit on {0}!", NameForTrainer(victim));
break;
}
case PBEMoveEffectivenessPacket mep:
Expand All @@ -602,12 +620,15 @@ string NameForTrainer(PBEPokemon pkmn)
switch (mep.Effectiveness)
{
case PBEEffectiveness.Ineffective: message = "It doesn't affect {0}..."; break;
case PBEEffectiveness.NotVeryEffective: message = "It's not very effective..."; break;
case PBEEffectiveness.Normal: message = "It's normally effective."; break;
case PBEEffectiveness.SuperEffective: message = "It's super effective!"; break;
case PBEEffectiveness.NotVeryEffective: message = "It's not very effective on {0}..."; break;
case PBEEffectiveness.Normal: message = null; break;
case PBEEffectiveness.SuperEffective: message = "It's super effective on {0}!"; break;
default: throw new ArgumentOutOfRangeException(nameof(mep.Effectiveness));
}
Console.WriteLine(message, NameForTrainer(victim));
if (message != null)
{
Console.WriteLine(message, NameForTrainer(victim));
}
break;
}
case PBEMoveFailedPacket mfp:
Expand Down Expand Up @@ -642,8 +663,7 @@ string NameForTrainer(PBEPokemon pkmn)
case PBEMovePPChangedPacket mpcp:
{
PBEPokemon moveUser = mpcp.MoveUserTeam.TryGetPokemon(mpcp.MoveUser);
int change = mpcp.NewValue - mpcp.OldValue;
Console.WriteLine("{0}'s {1} {3} {2} PP!", NameForTrainer(moveUser), PBELocalizedString.GetMoveName(mpcp.Move).English, Math.Abs(change), change <= 0 ? "lost" : "gained");
Console.WriteLine("{0}'s {1} {3} {2} PP!", NameForTrainer(moveUser), PBELocalizedString.GetMoveName(mpcp.Move).English, Math.Abs(mpcp.AmountReduced), mpcp.AmountReduced >= 0 ? "lost" : "gained");
break;
}
case PBEMoveUsedPacket mup:
Expand Down Expand Up @@ -743,7 +763,7 @@ string NameForTrainer(PBEPokemon pkmn)
{
if (!psip.Forced)
{
Console.WriteLine("{1} sent out {0}!", psip.SwitchIns.Select(s => s.Nickname).Andify(), psip.Team.TrainerName);
Console.WriteLine("{1} sent out {0}!", psip.SwitchIns.Select(s => s.Nickname).ToArray().Andify(), psip.Team.TrainerName);
}
break;
}
Expand Down Expand Up @@ -1196,12 +1216,12 @@ string NameForTrainer(PBEPokemon pkmn)
}
case PBETurnBeganPacket tbp:
{
Console.WriteLine("Turn {0} is starting. ({1})", tbp.TurnNumber, tbp.Time);
Console.WriteLine("Turn {0} is starting.", tbp.TurnNumber);
break;
}
case PBEWinnerPacket win:
{
Console.WriteLine("{0} defeated {1}!", win.WinningTeam.TrainerName, (win.WinningTeam == battle.Teams[0] ? battle.Teams[1] : battle.Teams[0]).TrainerName);
Console.WriteLine("{0} defeated {1}!", win.WinningTeam.TrainerName, win.WinningTeam.OpposingTeam.TrainerName);
break;
}
}
Expand Down
Loading

0 comments on commit db14794

Please sign in to comment.