public bool IsTriggered(EncounterState state) { if (this.TriggerType == OrderTriggerType.UNIT_HAS_STANDING_ORDER) { foreach (var watchedUnitId in this.WatchedUnitIds) { if (this.AwaitedStandingOrders.Contains(state.GetUnit(watchedUnitId).StandingOrder)) { return(true); } } return(false); } else if (this.TriggerType == OrderTriggerType.UNIT_BELOW_STRENGTH_PERCENT) { foreach (var watchedUnitId in this.WatchedUnitIds) { var unit = state.GetUnit(watchedUnitId); if ((float)unit.NumInFormation / (float)unit.OriginalUnitStrength < this.BelowStrengthPercent) { return(true); } } return(false); } else if (this.TriggerType == OrderTriggerType.ALL_UNITS_OF_FACTION_ROUTED) { foreach (var unit in state.GetUnitsOfFaction(this.TriggerFaction)) { if (unit.StandingOrder != UnitOrder.ROUT) { return(false); } } return(true); } else if (this.TriggerType == OrderTriggerType.LANE_CLEAR_OF_UNITS_FROM_FACTION) { if (this.TriggerFaction == FactionName.NEUTRAL || this.WatchedUnitIds.Count != 1) { throw new ArgumentException("didn't set faction or proper watched unit ID, Fs in chat"); } Lane closestLane = null; double closestDistance = 9999.0; Unit unit = state.GetUnit(this.WatchedUnitIds[0]); foreach (var lane in state.DeploymentInfo.Lanes) { var d = lane.LaneCenter.DistanceTo(unit.AveragePosition); if (d < closestDistance) { closestDistance = d; closestLane = lane; } } /* if (closestLane == null) { * GD.PrintErr("ClosestLane null somehow!? wtf."); * GD.PrintErr(unit.AveragePosition); * GD.PrintErr(unit.UnitFaction, " lol ", unit.NumInFormation); * return false; * } */ return(closestLane.UnitsForFaction(this.TriggerFaction) .Select((unitAtLanePosition) => state.GetUnit(unitAtLanePosition.UnitId).StandingOrder) .All((order) => order == UnitOrder.ROUT)); } else if (this.TriggerType == OrderTriggerType.ACTIVATE_ON_OR_AFTER_TURN) { if (this.ActivateOnTurn == -1) { throw new ArgumentException("didn't set timer"); } if (state.CurrentTurn >= this.ActivateOnTurn) { GD.Print(String.Format("Activating trigger on turn {0}, timed for turn {1}!", state.CurrentTurn, this.ActivateOnTurn)); } return(state.CurrentTurn >= this.ActivateOnTurn); } else { throw new NotImplementedException(); } }