protected new bool RotateBody(BoardObject ob, Direction weaponDelta) { // check first rotation bool inRange = false; // check if tank can rotate turret. if can and in range, return false if (this.TankData.TurretRotationSpeed != 0 && RotateIfNeededInternal(ob) == false) { return(false); } //rotated once, rotate TurretRotationSpeed times. for (int i = 1; i < this.TankData.TurretRotationSpeed; ++i) { if (RotateIfNeededInternal(ob) == false) { inRange = true; break; } } // its not the end. tank can rotate with body. if (base.RotateBody(ob, turretDirectionFromTankDirection) == false) { inRange = true; } return(!inRange); }
/// <summary> /// attacks region - manage ammo type. /// </summary> /// <param name="ob"></param> protected void AttackRegion(BoardObject ob) { Position s = ob.Position; Ammo a = new Ammo(new ObjectID(this.ObjectID.PlayerID, _simulation.Players[this.ObjectID.PlayerID].GenerateObjectID()), this.Position, ob.Position, this.AmmoType, this._ammoSpeed, this._firePower, this.ammoDamageRange, _simulation); InfoLog.WriteInfo(a.ObjectID.ToString() + " for ammunition ", EPrefix.GObj); this._simulation.AddAmmo(a); this._simulation.OnShoot(a); /* * * List<BoardObject> objectsInRange = GetObjectsInRange(s); * * foreach (BoardObject boardObject in objectsInRange) { * if (boardObject.BoardObjectClass == BoardObjectClass.Building) { * _simulation.handleAttackBuilding((Building)boardObject, this); * } else { * _simulation.handleAttackUnit((Unit)boardObject, this); * } * } */ this._remainingTurnsToReload = this._reloadTime; }
protected bool FindNearestTargetInFireRange(out BoardObject nearest) { int count; Position[] viewSpiral = RangeSpiral(this.FireRange, out count); Map m = this._map; Position p = this.Position; Position spiralPos; LinkedList <Unit> units; LinkedList <Building> buildings; for (int i = 0; i < count; ++i) { spiralPos = viewSpiral[i]; if (p.X + spiralPos.X >= 0 && p.X + spiralPos.X < m.Width && p.Y + spiralPos.Y >= 0 && p.Y + spiralPos.Y < m.Height) { units = m.Units[p.X + spiralPos.X, p.Y + spiralPos.Y]; foreach (Unit unit in units) { if (unit.Equals(this)) { continue; } if (unit.ObjectID.PlayerID != this.ObjectID.PlayerID && _simulation.Players[unit.ObjectID.PlayerID].TeamID != _simulation.Players[this.ObjectID.PlayerID].TeamID) { // target //TODO RS: bresenham to check if there is a way to shoot. // else - continue -> move attackingBuilding = false; InfoLog.WriteInfo("Unit:AI: found unit in fire range < " + this.FireRange, EPrefix.SimulationInfo); nearest = unit; return(true); } } buildings = m.Buildings[p.X + spiralPos.X, p.Y + spiralPos.Y]; foreach (Building building in buildings) { // erase true;) if (building.ObjectID.PlayerID != this.ObjectID.PlayerID && _simulation.Players[building.ObjectID.PlayerID].TeamID != _simulation.Players[this.ObjectID.PlayerID].TeamID) { attackingBuilding = true; nearest = building; InfoLog.WriteInfo("Unit:AI: found building fire range < " + this.FireRange, EPrefix.SimulationInfo); return(true); } } } } nearest = null; return(false); }
/// <summary> /// checks if object is in shooting range /// </summary> /// <param name="ob"></param> /// <returns></returns> protected bool CheckRangeToShoot(BoardObject ob) { int r = (int)Math.Floor(Math.Pow(ob.Position.X - this.Position.X, 2) + Math.Pow(ob.Position.Y - this.Position.Y, 2)); int range = this.FireRange * this.FireRange; InfoLog.WriteInfo("Unit:AI: in range:" + r + " ?<= " + range, EPrefix.SimulationInfo); return(r <= range); }
protected bool FindNearestTargetInViewRange(out BoardObject ob) { int count; Position[] viewSpiral = RangeSpiral(this.ViewRange, out count); Map m = this._map; Position p = this.Position; Position spiralPos; LinkedList <Unit> units; LinkedList <Building> buildings; for (int i = 0; i < count; ++i) { spiralPos = viewSpiral[i]; if (p.X + spiralPos.X >= 0 && p.X + spiralPos.X < m.Width && p.Y + spiralPos.Y >= 0 && p.Y + spiralPos.Y < m.Height) { units = m.Units[p.X + spiralPos.X, p.Y + spiralPos.Y]; foreach (Unit unit in units) { //TODO erase true;) if (unit.Equals(this)) { continue; } if (unit.ObjectID.PlayerID != this.ObjectID.PlayerID && _simulation.Players[unit.ObjectID.PlayerID].TeamID != _simulation.Players[this.ObjectID.PlayerID].TeamID) { ob = unit; InfoLog.WriteInfo("Unit:AI: found unit in view in range < " + this.ViewRange, EPrefix.SimulationInfo); return(true); } } buildings = m.Buildings[p.X + spiralPos.X, p.Y + spiralPos.Y]; foreach (Building building in buildings) { //TODO erase true;) if (building.ObjectID.PlayerID != this.ObjectID.PlayerID && _simulation.Players[building.ObjectID.PlayerID].TeamID != _simulation.Players[this.ObjectID.PlayerID].TeamID) { attackingBuilding = true; ob = building; InfoLog.WriteInfo("Unit:AI: found building in view in range < " + this.ViewRange, EPrefix.SimulationInfo); return(true); } } } } ob = null; return(false); }
private bool BelowTarged(BoardObject attackedObject) { if (this.Position.X == attackedObject.Position.X && this.Position.Y == attackedObject.Position.Y) { return(true); } else { return(false); } }
/// <summary> /// checks what type of object to attack; manage reload, destroying units, turret rotation /// </summary> /// <param name="ob"></param> protected void TryAttack(BoardObject ob) { // rotate turret if (RotateIfNeeded(ob) == true) { return; } if (this.roundToReload == 0) { AttackRegion(ob); } }
/// <summary> /// checks what type of object to attack; manage reload, destroying units, turret rotation /// </summary> /// <param name="ob"></param> protected virtual void TryAttack(BoardObject ob) { // Direction.East - no delta if (RotateBody(ob, Direction.East) == true) { return; } if (_remainingTurnsToReload == 0) { AttackRegion(ob); } }
protected bool RotateIfNeeded(BoardObject ob) { // check first rotation if (this.BuildingData.IsTurret && RotateIfNeededInternal(ob) == false) { return(false); } //for (int i = 1; i < RotationSpeed; ++i) { // if (RotateIfNeededInternal(ob) == false) break; //} // rotated more than once. return(true); }
protected virtual bool RotateBody(BoardObject ob, Direction turretDelta) { if (RotateBodyInternal(ob, turretDelta) == false) { return(false); } for (int i = 1; i < this.RotationSpeed; ++i) { if (RotateBodyInternal(ob, turretDelta) == false) { return(false); } } return(true); }
/// <summary> /// checks if object exists on map. /// </summary> /// <param name="ob"></param> /// <returns></returns> protected bool CheckIfStillExistTarget(BoardObject ob) { if (ob == null) { return(false); } if (attackingBuilding) { Building b = (Building)ob; return(b.State != Building.BuildingState.destroyed); } else { Unit u = (Unit)ob; return(u.state != UnitState.destroyed); } }
/// <summary> /// rotate if target is out of range /// </summary> /// <param name="ob">target</param> /// <returns>if rotation was needed</returns> protected bool RotateIfNeededInternal(BoardObject ob) { int alfaTarget = GetAlfa(ob.Position.X - this.Position.X, ob.Position.Y - this.Position.Y); int tankRotation = ConvertToNumber(Direction); int turretRotationDelta = ConvertToNumber(turretDirectionFromTankDirection); int turretRotation = turretRotationDelta + tankRotation; turretRotation %= 360; int delta = turretRotation - alfaTarget; delta += 360; delta %= 360; InfoLog.WriteInfo("## turret rot " + turretRotationDelta + " tank rot: " + tankRotation + "# target: " + alfaTarget + "### " + delta, EPrefix.SimulationInfo); int turn = 0; if (delta < 360 - 23 && delta >= 180) { // rotate clockwise turn = 45; turretRotationDelta += turn; turretRotationDelta += 360; turretRotationDelta %= 360; turretDirectionFromTankDirection = ConvertToDirection(turretRotationDelta); return(true); } else if (delta > 23 && delta < 180) { // rotate counterclockwise turn = -45; turretRotationDelta += turn; turretRotationDelta += 360; turretRotationDelta %= 360; turretDirectionFromTankDirection = ConvertToDirection(turretRotationDelta); return(true); } else { return(false); } }
private bool RotateBodyInternal(BoardObject ob, Direction weaponDelta) { int alfaTarget = GetAlfa(ob.Position.X - this.Position.X, ob.Position.Y - this.Position.Y); int unitRotation = ConvertToNumber(Direction); int weaponRotationDelta = ConvertToNumber(weaponDelta); int weaponRotation = weaponRotationDelta + unitRotation; weaponRotation %= 360; int delta = weaponRotation - alfaTarget; delta += 360; delta %= 360; InfoLog.WriteInfo("## weapon rot " + weaponRotationDelta + " unit rot: " + unitRotation + "# target: " + alfaTarget + "### " + delta, EPrefix.SimulationInfo); int turn = 0; if (delta < 360 - 23 && delta >= 180) { // rotate clockwise turn = 45; unitRotation += turn; unitRotation += 360; unitRotation %= 360; Direction = ConvertToDirection(unitRotation); return(true); } else if (delta > 23 && delta < 180) { // rotate counterclockwise turn = -45; unitRotation += turn; unitRotation += 360; unitRotation %= 360; Direction = ConvertToDirection(unitRotation); return(true); } else { return(false); } }
/// <summary> /// Sets object to attack (building or unit) by this unit. /// </summary> /// <param name="objectID"></param> public void OrderAttack(BoardObject boardObject, bool isBuilding) { attackedObject = boardObject; orderedAttack = true; if (CheckRangeToShoot(boardObject) == false) { MoveTo(boardObject.Position); state = UnitState.orderedAttack; } else { // will shoot ordered. state = UnitState.stopped; StopMoving(); } this.attackingBuilding = isBuilding; InfoLog.WriteInfo("Unit:AI: attacking!!!! ", EPrefix.SimulationInfo); }
/// <summary> /// rotate if target is out of range /// </summary> /// <param name="ob">target</param> /// <returns>if rotation was needed</returns> protected bool RotateIfNeededInternal(BoardObject ob) { int alfaTarget = GetAlfa(ob.Position.X - this.Position.X, ob.Position.Y - this.Position.Y); int turretRotationDelta = ConvertToNumber(Direction); int delta = turretRotationDelta - alfaTarget; delta += 360; delta %= 360; int turn = 0; if (delta < 360 - 23 && delta >= 180) { // rotate clockwise turn = 45; turretRotationDelta += turn; turretRotationDelta += 360; turretRotationDelta %= 360; _direction = ConvertToDirection(turretRotationDelta); return(true); } else if (delta > 23 && delta < 180) { // rotate counterclockwise turn = -45; turretRotationDelta += turn; turretRotationDelta += 360; turretRotationDelta %= 360; _direction = ConvertToDirection(turretRotationDelta); return(true); } else { return(false); } }
protected bool FindNearestTargetOnSandInViewRange(out BoardObject ob) { int count; Position[] viewSpiral = RangeSpiral(this.SandwormData.__ViewRange, out count); Map m = this._map; Position p = this.Position; Position spiralPos; LinkedList <Unit> units; //LinkedList<Building> buildings; for (int i = 0; i < count; ++i) { spiralPos = viewSpiral[i]; if (p.X + spiralPos.X >= 0 && p.X + spiralPos.X < m.Width && p.Y + spiralPos.Y >= 0 && p.Y + spiralPos.Y < m.Height && _map.Tiles[p.X + spiralPos.X, p.Y + spiralPos.Y] == TileType.Sand) { units = m.Units[p.X + spiralPos.X, p.Y + spiralPos.Y]; foreach (Unit unit in units) { //TODO erase true;) if (unit.Equals(this)) { continue; } ob = unit; InfoLog.WriteInfo("Unit:AI: found unit in view in range < " + this.ViewRange, EPrefix.SimulationInfo); return(true); } } } ob = null; return(false); }