bool InactiveFindTask() { if (initialWeapons.Count == 0) { return(false); } RTSUnit controlledObj = ControlledObject; if (controlledObj == null) { return(false); } Dynamic newTaskAttack = null; float attackObjectPriority = 0; Vec3 controlledObjPos = controlledObj.Position; float radius = controlledObj./*Type.*/ ViewRadius; Map.Instance.GetObjects(new Sphere(controlledObjPos, radius), MapObjectSceneGraphGroups.UnitGroupMask, delegate(MapObject mapObject) { Unit obj = (Unit)mapObject; Vec3 objPos = obj.Position; //check distance Vec3 diff = objPos - controlledObjPos; float objDistance = diff.Length(); if (objDistance > radius) { return; } float priority = GetAttackObjectPriority(obj); if (priority != 0 && priority > attackObjectPriority) { attackObjectPriority = priority; newTaskAttack = obj; } }); if (newTaskAttack != null) { //RTSConstructor specific if (ControlledObject.Type.Name == "RTSConstructor") { DoTask(new Task(Task.Types.BreakableRepair, newTaskAttack), false); } else { DoTask(new Task(Task.Types.BreakableAttack, newTaskAttack), false); } return(true); } return(false); }
public void UpdateInitialWeapons() { RTSUnit controlledObj = ControlledObject; initialWeapons = new List <Weapon>(); foreach (MapObjectAttachedObject attachedObject in controlledObj.AttachedObjects) { MapObjectAttachedMapObject attachedMapObject = attachedObject as MapObjectAttachedMapObject; if (attachedMapObject != null) { Weapon weapon = attachedMapObject.MapObject as Weapon; if (weapon != null) { initialWeapons.Add(weapon); } } } }
private void CreateProductedUnit() { RTSUnit unit = (RTSUnit)Entities.Instance.Create(productUnitType, Map.Instance); RTSCharacter character = unit as RTSCharacter; if (character == null) { Log.Fatal("RTSBuilding: CreateProductedUnit: character == null"); } GridBasedNavigationSystem navigationSystem = GridBasedNavigationSystem.Instances[0]; Vec2 p = navigationSystem.GetNearestFreePosition(Position.ToVec2(), character.Type.Radius * 2); unit.Position = new Vec3(p.X, p.Y, navigationSystem.GetMotionMapHeight(p) + character.Type.Height * .5f); if (Intellect != null) { unit.InitialFaction = Intellect.Faction; } unit.PostCreate(); }
protected virtual void TickTasks() { RTSUnit controlledObj = ControlledObject; if (controlledObj == null) { return; } switch (currentTask.Type) { //Stop case Task.Types.Stop: controlledObj.Stop(); break; //Move case Task.Types.Move: case Task.Types.BreakableMove: if (currentTask.Entity != null) { controlledObj.Move(currentTask.Entity.Position); } else { Vec3 pos = currentTask.Position; if ((controlledObj.Position.ToVec2() - pos.ToVec2()).Length() < 1.5f && Math.Abs(controlledObj.Position.Z - pos.Z) < 3.0f) { //get to DoNextTask(); } else { controlledObj.Move(pos); } } break; //Reload case Task.Types.Reload: VBWeaponItem wpn = (controlledObj as VBCharacter).ActiveHeldItem as VBWeaponItem; if (wpn != null) { wpn.TryReload(); DoNextTask(); } break; //Pickup case Task.Types.PickUp: if (InTaskRange()) { if (currentTask.Entity as VBItem != null) { (controlledObj as VBCharacter).TakeItem(currentTask.Entity as VBItem); } DoNextTask(); } break; //Use case Task.Types.Use: if (InTaskRange()) { InteractableObject itm = currentTask.Entity as InteractableObject; if (itm != null) { itm.Interact(controlledObj); } DoNextTask(); } break; //Talk case Task.Types.Talk: if (InTaskRange()) { EngineConsole.Instance.ExecuteString("createWindow chat"); DoNextTask(); } break; //preuse case Task.Types.PreUse: break; //Attack, Repair case Task.Types.Attack: case Task.Types.BreakableAttack: //case Task.Types.Repair: //case Task.Types.BreakableRepair: { /*/healed * if( ( currentTask.Type == Task.Types.Repair || * currentTask.Type == Task.Types.BreakableRepair ) * && currentTask.Entity != null ) * { * if( currentTask.Entity.Health == currentTask.Entity.Type.HealthMax ) * { * DoNextTask(); * break; * } * }*/ float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum; MultipleActionItem itm = (controlledObj as VBCharacter).ActiveHeldItem as MultipleActionItem; if (itm != null) { needDistance = itm.GetCurActionMode().UseDistanceRange.Maximum; } Vec3 targetPos; if (currentTask.Entity != null) { targetPos = currentTask.Entity.Position; } else { targetPos = currentTask.Position; } float distance = (controlledObj.Position - targetPos).Length(); if (distance != 0) { bool lineVisibility = false; { if (distance < needDistance) { lineVisibility = true; //direct line visibility check Vec3 start = ControlledObject.Position; Ray ray = new Ray(start, targetPos - start); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); foreach (RayCastResult result in piercingResult) { MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body); if (obj != null && obj == currentTask.Entity) { break; } if (obj != controlledObj) { lineVisibility = false; break; } } } } //movement control if (lineVisibility) { //stop controlledObj.Stop(); RTSCharacter character = controlledObj as RTSCharacter; if (character != null) { character.SetLookDirection(targetPos); } } else { //move to target controlledObj.Move(targetPos); } //weapons control if (lineVisibility) { (ControlledObject as VBCharacter).BaseAttack(currentTask.Entity, null); DoNextTask(); /* * foreach( Weapon weapon in initialWeapons ) * { * Vec3 pos = targetPos; * Gun gun = weapon as Gun; * if( gun != null && currentTask.Entity != null ) * gun.GetAdvanceAttackTargetPosition( false, currentTask.Entity, false, out pos ); * weapon.SetForceFireRotationLookTo( pos ); * * if( weapon.Ready ) * { * Range range; * * range = weapon.Type.WeaponNormalMode.UseDistanceRange; * if( distance >= range.Minimum && distance <= range.Maximum ) * weapon.TryFire( false ); * * range = weapon.Type.WeaponAlternativeMode.UseDistanceRange; * if( distance >= range.Minimum && distance <= range.Maximum ) * weapon.TryFire( true ); * } * }*/ } } } break; } }
protected virtual void TickTasks() { RTSUnit controlledObj = ControlledObject; if (controlledObj == null) { return; } switch (currentTask.Type) { //Stop case Task.Types.Stop: controlledObj.Stop(); break; //Move case Task.Types.Move: case Task.Types.BreakableMove: if (currentTask.Entity != null) { controlledObj.Move(currentTask.Entity.Position); } else { Vec3 pos = currentTask.Position; if ((controlledObj.Position.ToVec2() - pos.ToVec2()).Length() < 1.5f && Math.Abs(controlledObj.Position.Z - pos.Z) < 3.0f) { //get to DoNextTask(); } else { controlledObj.Move(pos); } } break; //Attack, Repair case Task.Types.Attack: case Task.Types.BreakableAttack: case Task.Types.Repair: case Task.Types.BreakableRepair: { //healed if ((currentTask.Type == Task.Types.Repair || currentTask.Type == Task.Types.BreakableRepair) && currentTask.Entity != null) { if (currentTask.Entity.Health == currentTask.Entity.Type.HealthMax) { DoNextTask(); break; } } float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum; Vec3 targetPos; if (currentTask.Entity != null) { targetPos = currentTask.Entity.Position; } else { targetPos = currentTask.Position; } float distance = (controlledObj.Position - targetPos).Length(); if (distance != 0) { bool lineVisibility = false; { if (distance < needDistance) { lineVisibility = true; //direct line visibility check Vec3 start = initialWeapons[0].Position; Ray ray = new Ray(start, targetPos - start); RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing( ray, (int)ContactGroup.CastOnlyContact); foreach (RayCastResult result in piercingResult) { MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body); if (obj != null && obj == currentTask.Entity) { break; } if (obj != controlledObj) { lineVisibility = false; break; } } } } //movement control if (lineVisibility) { //stop controlledObj.Stop(); RTSCharacter character = controlledObj as RTSCharacter; if (character != null) { character.SetLookDirection(targetPos); } } else { //move to target controlledObj.Move(targetPos); } //weapons control if (lineVisibility) { foreach (Weapon weapon in initialWeapons) { Vec3 pos = targetPos; Gun gun = weapon as Gun; if (gun != null && currentTask.Entity != null) { gun.GetAdvanceAttackTargetPosition(false, currentTask.Entity, false, out pos); } weapon.SetForceFireRotationLookTo(pos); if (weapon.Ready) { Range range; range = weapon.Type.WeaponNormalMode.UseDistanceRange; if (distance >= range.Minimum && distance <= range.Maximum) { weapon.TryFire(false); } range = weapon.Type.WeaponAlternativeMode.UseDistanceRange; if (distance >= range.Minimum && distance <= range.Maximum) { weapon.TryFire(true); } } } } } } break; //BuildBuilding case Task.Types.BuildBuilding: { float needDistance = controlledObj.Type.OptimalAttackDistanceRange.Maximum; Vec3 targetPos = currentTask.Position; float distance = (controlledObj.Position - targetPos).Length(); if (distance < needDistance) { controlledObj.Stop(); //get to //check free area for build bool free; { Bounds bounds; { PhysicsModel physicsModel = PhysicsWorld.Instance.LoadPhysicsModel( currentTask.EntityType.GetPhysicsModelFullPath()); if (physicsModel == null) { Log.Fatal(string.Format("No physics model for \"{0}\"", currentTask.EntityType.ToString())); } bounds = physicsModel.GetGlobalBounds(); bounds += targetPos; } Rect rect = new Rect(bounds.Minimum.ToVec2(), bounds.Maximum.ToVec2()); free = GridBasedNavigationSystem.Instances[0].IsFreeInMapMotion(rect); } if (!free) { //not free DoNextTask(); break; } //check cost RTSFactionManager.FactionItem factionItem = RTSFactionManager.Instance.GetFactionItemByType(Faction); if (factionItem != null) { float cost = ((RTSBuildingType)currentTask.EntityType).BuildCost; if (factionItem.Money - cost < 0) { //No money DoNextTask(); break; } factionItem.Money -= cost; } RTSBuilding building = (RTSBuilding)Entities.Instance.Create(currentTask.EntityType, Map.Instance); building.Position = currentTask.Position; building.InitialFaction = Faction; building.PostCreate(); building.BuildedProgress = 0; building.Health = 1; //Repair DoTaskInternal(new Task(Task.Types.Repair, building)); } else { controlledObj.Move(targetPos); } } break; } }