// when item is used on a creature // run it ONLY on legal actions public static ActionResult ResolveUse(Card item, CreatureOnBoard target) { CreatureOnBoard targetAfter = new CreatureOnBoard(target); if (item.type == Card.Type.ITEM_GREEN) // add keywords { targetAfter.keywords.hasCharge = target.keywords.hasCharge || item.keywords.hasCharge; if (item.keywords.hasCharge) { targetAfter.canAttack = !targetAfter.hasAttacked; // No Swift Strike hack } targetAfter.keywords.hasBreakthrough = target.keywords.hasBreakthrough || item.keywords.hasBreakthrough; targetAfter.keywords.hasDrain = target.keywords.hasDrain || item.keywords.hasDrain; targetAfter.keywords.hasGuard = target.keywords.hasGuard || item.keywords.hasGuard; targetAfter.keywords.hasLethal = target.keywords.hasLethal || item.keywords.hasLethal; //targetAfter.keywords.hasRegenerate = target.keywords.hasRegenerate || item.keywords.hasRegenerate; targetAfter.keywords.hasWard = target.keywords.hasWard || item.keywords.hasWard; } else // Assumming ITEM_BLUE or ITEM_RED - remove keywords { targetAfter.keywords.hasCharge = target.keywords.hasCharge && !item.keywords.hasCharge; targetAfter.keywords.hasBreakthrough = target.keywords.hasBreakthrough && !item.keywords.hasBreakthrough; targetAfter.keywords.hasDrain = target.keywords.hasDrain && !item.keywords.hasDrain; targetAfter.keywords.hasGuard = target.keywords.hasGuard && !item.keywords.hasGuard; targetAfter.keywords.hasLethal = target.keywords.hasLethal && !item.keywords.hasLethal; //targetAfter.keywords.hasRegenerate = target.keywords.hasRegenerate && !item.keywords.hasRegenerate; targetAfter.keywords.hasWard = target.keywords.hasWard && !item.keywords.hasWard; } targetAfter.attack = Math.Max(0, target.attack + item.attack); if (targetAfter.keywords.hasWard && item.defense < 0) { targetAfter.keywords.hasWard = false; } else { targetAfter.defense += item.defense; } if (targetAfter.defense <= 0) { targetAfter = null; } int itemgiverHealthChange = item.myHealthChange; int targetHealthChange = item.oppHealthChange; ActionResult result = new ActionResult(new CreatureOnBoard(item), targetAfter == null ? target : targetAfter, false, targetAfter == null, itemgiverHealthChange, targetHealthChange); result.defenderAttackChange = item.attack; result.defenderDefenseChange = item.defense; return(result); }
public CreatureOnBoard(CreatureOnBoard creature) { this.id = creature.id; this.baseId = creature.baseId; this.cost = creature.cost; this.attack = creature.attack; this.defense = creature.defense; this.keywords = new Keywords(creature.keywords); this.lastTurnDefense = creature.lastTurnDefense; baseCard = creature.baseCard; this.canAttack = creature.canAttack; this.hasAttacked = creature.hasAttacked; }
public string toTooltipText(CreatureOnBoard creatureOnBoard) { StringBuilder sb = new StringBuilder(); sb.Append(this.name + " (#" + baseId + ")").Append("\n\n"); if (id >= 0) { sb.Append("instanceId: ").Append(this.id).Append("\n"); } sb.Append("cost: ").Append(this.cost).Append("\n"); sb.Append("\n"); sb.Append(toTooltipInnerText(creatureOnBoard).Replace("\\n", "\n")); return(sb.ToString()); }
// when creature attacks player // run it ONLY on legal actions public static ActionResult ResolveAttack(CreatureOnBoard attacker) { if (!attacker.canAttack) { return(new ActionResult(false)); } CreatureOnBoard attackerAfter = new CreatureOnBoard(attacker); attackerAfter.canAttack = false; attackerAfter.hasAttacked = true; int healthGain = attacker.keywords.hasDrain ? attacker.attack : 0; int healthTaken = -attacker.attack; ActionResult result = new ActionResult(attackerAfter, null, false, false, healthGain, healthTaken); result.defenderDefenseChange = healthTaken; return(result); }
// when creature attacks creatures // run it ONLY on legal actions public static ActionResult ResolveAttack(CreatureOnBoard attacker, CreatureOnBoard defender) { if (!attacker.canAttack) { return(new ActionResult(false)); } CreatureOnBoard attackerAfter = new CreatureOnBoard(attacker); CreatureOnBoard defenderAfter = new CreatureOnBoard(defender); attackerAfter.canAttack = false; attackerAfter.hasAttacked = true; if (defender.keywords.hasWard) { defenderAfter.keywords.hasWard = attacker.attack == 0; } if (attacker.keywords.hasWard) { attackerAfter.keywords.hasWard = defender.attack == 0; } int damageGiven = defender.keywords.hasWard ? 0 : attacker.attack; int damageTaken = attacker.keywords.hasWard ? 0 : defender.attack; int healthGain = 0; int healthTaken = 0; // attacking if (damageGiven >= defender.defense) { defenderAfter = null; } if (attacker.keywords.hasBreakthrough && defenderAfter == null) { healthTaken = defender.defense - damageGiven; } if (attacker.keywords.hasLethal && damageGiven > 0) { defenderAfter = null; } if (attacker.keywords.hasDrain && damageGiven > 0) { healthGain = attacker.attack; } if (defenderAfter != null) { defenderAfter.defense -= damageGiven; } // defending if (damageTaken >= attacker.defense) { attackerAfter = null; } if (defender.keywords.hasLethal && damageTaken > 0) { attackerAfter = null; } if (attackerAfter != null) { attackerAfter.defense -= damageTaken; } ActionResult result = new ActionResult(attackerAfter == null ? attacker : attackerAfter, defenderAfter == null ? defender : defenderAfter, attackerAfter == null, defenderAfter == null, healthGain, healthTaken); result.attackerDefenseChange = -damageTaken; result.defenderDefenseChange = -damageGiven; return(result); }
public void AdvanceState(Action action) // ASSUMING THE ACTION IS LEGAL ! { if (action.type == Action.Type.SUMMON) // SUMMON [id] { Card c = cardIdMap[action.arg1]; players[currentPlayer].hand.Remove(c); players[currentPlayer].currentMana -= c.cost; CreatureOnBoard creature = new CreatureOnBoard(c); players[currentPlayer].board.Add(creature); players[currentPlayer].ModifyHealth(c.myHealthChange); players[1 - currentPlayer].ModifyHealth(c.oppHealthChange); players[currentPlayer].nextTurnDraw += c.cardDraw; action.result = new ActionResult(creature, null, false, false, c.myHealthChange, c.oppHealthChange); } else if (action.type == Action.Type.ATTACK) // ATTACK [id1] [id2] { int indexatt = -1; for (int i = 0; i < players[currentPlayer].board.Count; i++) { if (players[currentPlayer].board[i].id == action.arg1) { indexatt = i; } } CreatureOnBoard att = players[currentPlayer].board[indexatt]; int indexdef = -1; CreatureOnBoard def; ActionResult result = null; if (action.arg2 == -1) // attacking player { result = ResolveAttack(att); } else { for (int i = 0; i < players[1 - currentPlayer].board.Count; i++) { if (players[1 - currentPlayer].board[i].id == action.arg2) { indexdef = i; } } def = players[1 - currentPlayer].board[indexdef]; result = ResolveAttack(att, def); if (result.defenderDied) { players[1 - currentPlayer].removeFromBoard(indexdef); } else { players[1 - currentPlayer].board[indexdef] = result.defender; } } if (result.attackerDied) { players[currentPlayer].removeFromBoard(indexatt); } else { players[currentPlayer].board[indexatt] = result.attacker; } players[currentPlayer].ModifyHealth(result.attackerHealthChange); players[1 - currentPlayer].ModifyHealth(result.defenderHealthChange); action.result = result; } else if (action.type == Action.Type.USE) // USE [id1] [id2] { Card item = cardIdMap[action.arg1]; players[currentPlayer].hand.Remove(item); players[currentPlayer].currentMana -= item.cost; if (item.type == Card.Type.ITEM_GREEN) // here we assume that green cards never remove friendly creatures! { int indextarg = -1; for (int i = 0; i < players[currentPlayer].board.Count; i++) { if (players[currentPlayer].board[i].id == action.arg2) { indextarg = i; } } CreatureOnBoard targ = players[currentPlayer].board[indextarg]; ActionResult result = ResolveUse(item, targ); players[currentPlayer].board[indextarg] = result.defender; players[currentPlayer].ModifyHealth(result.attackerHealthChange); players[1 - currentPlayer].ModifyHealth(result.defenderHealthChange); players[currentPlayer].nextTurnDraw += item.cardDraw; action.result = result; } else // red and blue cards { int indextarg = -1; ActionResult result = null; if (action.arg2 == -1) // using on player { result = ResolveUse(item); } else // using on creature { for (int i = 0; i < players[1 - currentPlayer].board.Count; i++) { if (players[1 - currentPlayer].board[i].id == action.arg2) { indextarg = i; } } CreatureOnBoard targ = players[1 - currentPlayer].board[indextarg]; result = ResolveUse(item, targ); if (result.defenderDied) { players[1 - currentPlayer].removeFromBoard(indextarg); } else { players[1 - currentPlayer].board[indextarg] = result.defender; } } players[currentPlayer].ModifyHealth(result.attackerHealthChange); players[1 - currentPlayer].ModifyHealth(result.defenderHealthChange); players[currentPlayer].nextTurnDraw += item.cardDraw; action.result = result; } } players[currentPlayer].performedActions.Add(action); CheckWinCondition(); }
private string toTooltipInnerText(CreatureOnBoard creatureOnBoard) { StringBuilder sb = new StringBuilder(); if (type == Type.CREATURE) { Keywords keywords; if (creatureOnBoard != null) { keywords = creatureOnBoard.keywords; int aDiff = creatureOnBoard.attack - attack; int dDiff = creatureOnBoard.defense - defense; if (aDiff != 0 || dDiff != 0) { sb.Append(' '); } sb.Append(attack).Append(" / ").Append(defense).Append(" Creature"); if (aDiff != 0 || dDiff != 0) { sb.Append("\n"); if (aDiff > 0) { sb.Append("+"); } else if (aDiff == 0) { sb.Append(" "); } sb.Append(aDiff == 0 ? " " : aDiff.ToString()).Append(" "); if (dDiff > 0) { sb.Append("+"); } else if (dDiff == 0) { sb.Append(" "); } sb.Append(dDiff == 0 ? " " : dDiff.ToString()).Append(" "); } } else { keywords = this.keywords; sb.Append(attack).Append(" / ").Append(defense).Append(" Creature"); } List <string> keywordsList = keywords.getListOfKeywords(); if (keywordsList.Count != 0) { sb.Append("\\n").Append(string.Join(", ", keywordsList)); } return(sb.ToString()); } if (type == Type.ITEM_GREEN) { sb.Append("Green Item\\n"); } if (type == Type.ITEM_RED) { sb.Append("Red Item\\n"); } if (type == Type.ITEM_BLUE) { sb.Append("Blue Item\\n"); } return(sb.ToString()); }