Beispiel #1
0
        void MagnetAttachObject(Body mapObjectBody)
        {
            if (mapObjectBody.IsDisposed)
            {
                return;
            }

            if (IsMagnetBodyAttached(mapObjectBody))
            {
                Log.Fatal("Crane: MagnetAttachObject: IsMagnetBodyAttached( mapObjectBody ).");
            }

            MapObject mapObject = MapSystemWorld.GetMapObjectByBody(mapObjectBody);

            if (mapObject != null && mapObject.PhysicsModel != null)
            {
                MagnetObjectItem item = new MagnetObjectItem();
                item.mapObject = mapObject;
                item.body      = mapObjectBody;
                item.bodyIndex = Array.IndexOf <Body>(mapObject.PhysicsModel.Bodies, mapObjectBody);

                SubscribeToDeletionEvent(mapObject);

                CreateFixedJointForAttachedObject(item);

                magnetAttachedObjects.Add(item);
            }
        }
Beispiel #2
0
        void MagnetAttachObject(Body mapObjectBody)
        {
            if (mapObjectBody.IsDisposed)
            {
                return;
            }

            Trace.Assert(!IsMagnetBodyAttached(mapObjectBody));

            MapObject mapObject = MapSystemWorld.GetMapObjectByBody(mapObjectBody);

            if (mapObject == null)
            {
                return;
            }

            MagnetObjectItem item = new MagnetObjectItem();

            item.mapObject = mapObject;
            item.body      = mapObjectBody;
            item.bodyIndex = Array.IndexOf <Body>(mapObject.PhysicsModel.Bodies, mapObjectBody);

            AddRelationship(mapObject);

            CreateFixedJointForAttachedObject(item);

            magnetAttachedObjects.Add(item);
        }
Beispiel #3
0
        protected void UpdateTargetObject()
        {
            //redraw
            targetedEntity = null;

            if (EngineApp.Instance.MouseRelativeMode)
            {
                return;
            }

            Ray ray = RendererWorld.Instance.DefaultCamera.GetCameraToViewportRay(EngineApp.Instance.MousePosition);

            RayCastResult[] results = PhysicsWorld.Instance.RayCastPiercing(ray, (int)ContactGroup.CastOnlyCollision);

            foreach (RayCastResult result in results)
            {
                //terrain
                HeightmapTerrain terrain = HeightmapTerrain.GetTerrainByBody(result.Shape.Body);
                if (terrain != null)
                {
                    targetedEntity = terrain;
                    return;
                }

                //other
                targetedEntity = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);
                return;
            }
        }
Beispiel #4
0
            private bool CheckDirectVisibilityByRayCast(Vec3 from, Vec3 targetPosition, MapObject targetObject)
            {
                MechUnitAI controlledObj = Owner.ControlledObject;

                Ray ray = new Ray(from, targetPosition - from);

                RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                    ray, (int)ContactGroup.CastOnlyContact);
                foreach (RayCastResult result in piercingResult)
                {
                    MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);
                    if (obj != null)
                    {
                        //skip target object
                        if (targetObject != null && obj == targetObject)
                        {
                            continue;
                        }
                        //skip controlled object
                        if (obj == controlledObj)
                        {
                            continue;
                        }
                    }

                    //found body which breaks visibility
                    return(false);
                }

                return(true);
            }
        private Set <MapObject> GetObjectsInActiveArea()
        {
            Set <MapObject> result = new Set <MapObject>();

            float height = Type.ActiveAreaHeight;
            float radius = Type.ActiveAreaRadius;

            Bounds bounds = new Bounds(
                Position - new Vec3(radius, radius, 0),
                Position + new Vec3(radius, radius, height));

            Body[] bodies = PhysicsWorld.Instance.VolumeCast(bounds, (int)ContactGroup.CastOnlyDynamic);

            foreach (Body body in bodies)
            {
                if (!body.Static)
                {
                    MapObject obj = MapSystemWorld.GetMapObjectByBody(body);
                    if (obj != null && obj != this && IsAllowToTeleport(obj) &&
                        CheckPositionInActiveArea(obj.Position))
                    {
                        result.AddWithCheckAlreadyContained(obj);
                    }
                }
            }

            return(result);
        }
Beispiel #6
0
        void magnetBody_Collision(ref CollisionEvent collisionEvent)
        {
            Body mapObjectBody = collisionEvent.OtherShape.Body;

            MapObject mapObject = MapSystemWorld.GetMapObjectByBody(mapObjectBody);

            if (mapObject == null)
            {
                return;
            }

            if (mapObject == this)
            {
                return;
            }

            if (IsMagnetBodyAttached(mapObjectBody))
            {
                return;
            }

            int count;

            if (lastMagnetContactsCount.TryGetValue(mapObjectBody, out count))
            {
                lastMagnetContactsCount.Remove(mapObjectBody);
            }
            else
            {
                count = 0;
            }
            lastMagnetContactsCount.Add(mapObjectBody, count + 1);
        }
Beispiel #7
0
        void ToggleEntity()
        {
            if (EngineApp.Instance.MouseRelativeMode)
            {
                return;
            }

            MapEditorInterface.Instance.SetMapModified();

            Ray ray = RendererWorld.Instance.DefaultCamera.GetCameraToViewportRay(EngineApp.Instance.MousePosition);

            RayCastResult[] results = PhysicsWorld.Instance.RayCastPiercing(ray, (int)ContactGroup.CastOnlyCollision);

            foreach (RayCastResult result in results)
            {
                //heightmapterrain
                HeightmapTerrain terrain = HeightmapTerrain.GetTerrainByBody(result.Shape.Body);
                if (terrain != null)
                {
                    //deselect
                    if (RecastNavigationSystem.Instance.Geometries.Contains(terrain))
                    {
                        //Log.Info("Removed terrain " + terrain.Name + " from geometries.");
                        RecastNavigationSystem.Instance.Geometries.Remove(terrain);
                        return;
                    }

                    //select
                    if (RecastNavigationSystem.Instance.GeometryVerifier(terrain, true))
                    {
                        //Log.Info("Added terrain " + terrain.Name + " to geometries.");
                        RecastNavigationSystem.Instance.Geometries.Add(terrain);
                        return;
                    }
                }

                MapObject mapObject = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);
                if (mapObject != null)
                {
                    //deselect
                    if (RecastNavigationSystem.Instance.Geometries.Contains(mapObject))
                    {
                        RecastNavigationSystem.Instance.Geometries.Remove(mapObject);
                        return;
                    }

                    //select
                    if (RecastNavigationSystem.Instance.GeometryVerifier(mapObject, true))
                    {
                        RecastNavigationSystem.Instance.Geometries.Add(mapObject);
                        return;
                    }
                }

                Log.Error("Unknown entity with collisions, what shenanigans!");
            }

            return;
        }
        private bool IsWeaponDirectedToTarget(AttackTask attackTask)
        {
            Vec3 targetPos = CalculateTargetPosition(attackTask);

            Weapon weapon = attackTask.Weapon;

            //to check up a weapon angle
            {
                Vec3 needDirection   = (targetPos - weapon.Position).GetNormalize();
                Vec3 weaponDirection = weapon.Rotation.GetForward();

                Radian angle = Math.Abs(MathFunctions.ACos(Vec3.Dot(needDirection, weaponDirection)));
                Radian minimalDifferenceAngle = new Degree(2).InRadians();

                if (angle > minimalDifferenceAngle)
                {
                    return(false);
                }
            }

            //to check up a line of fire
            {
                Ray ray = new Ray(weapon.Position, targetPos - weapon.Position);

                RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                    ray, (int)ContactGroup.CastOnlyContact);

                foreach (RayCastResult result in piercingResult)
                {
                    Dynamic dynamic = MapSystemWorld.GetMapObjectByBody(result.Shape.Body) as Dynamic;
                    if (dynamic != null)
                    {
                        Unit parentUnit = dynamic.GetParentUnit();
                        if (parentUnit != null)
                        {
                            if (parentUnit == attackTask.TargetEntity)
                            {
                                continue;
                            }
                            if (parentUnit == ControlledObject)
                            {
                                continue;
                            }
                        }
                    }

                    return(false);
                }
            }

            return(true);
        }
        private Unit AquireNewTarget()
        {
            Unit   TempTarget = null;
            Bounds volume     = new Bounds(this.Position);

            volume.Expand(new Vec3(Type.RadarRange, Type.RadarRange, Type.RadarRange));

            Body[] result = PhysicsWorld.Instance.VolumeCast(volume,
                                                             (int)ContactGroup.CastOnlyDynamic);

            foreach (Body body in result)
            {
                MapObject obj = MapSystemWorld.GetMapObjectByBody(body);
                if (obj != null)
                {
                    Unit unit = obj as Unit;

                    if (unit != null)
                    {
                        float Angletounit = CalculateAngleTo(unit);
                        //0.8f in radian is 45 Degrees and the absoulote value whould give the missile a window of 90'
                        if (Angletounit > 0.8f)
                        {
                            continue;
                        }

                        if (TempTarget == null)
                        {
                            TempTarget = unit;
                        }
                        else
                        {
                            float PreTargetDistance = (TempTarget.Position - Position).LengthFast();
                            float newTargetDistance = (unit.Position - Position).LengthFast();

                            float DifDis    = PreTargetDistance - newTargetDistance;
                            float DiftAngle = CalculateAngleTo(TempTarget) - Angletounit;

                            if (DifDis > 0 && DiftAngle > 0)
                            {
                                TempTarget = unit;
                            }
                        }
                    }
                }
            }

            return(TempTarget);
        }
        private void attachedParentBody_Collision(ref CollisionEvent collisionEvent)
        {
            if (lastJumpTime == 0)
            {
                return;
            }

            lastJumpTime = 0;

            Dynamic objDynamic = MapSystemWorld.GetMapObjectByBody(
                collisionEvent.OtherShape.Body) as Dynamic;

            if (objDynamic == null)
            {
                return;
            }

            Character unit = (Character)AttachedMapObjectParent;

            if (unit == null || unit.Intellect == null)
            {
                return;
            }

            //Not kick allies
            Unit objUnit = objDynamic.GetParentUnitHavingIntellect();

            if (objUnit == null)
            {
                return;
            }
            if (objUnit.Intellect.Faction == unit.Intellect.Faction)
            {
                return;
            }

            objUnit.DoDamage(unit, unit.Position, collisionEvent.OtherShape,
                             Type.NormalMode.Damage, true);

            SoundPlay3D(Type.SoundBlowKick, .5f, false);

            if (EntitySystemWorld.Instance.IsServer() &&
                Type.NetworkType == EntityNetworkTypes.Synchronized)
            {
                Server_SendSoundPlayBlowKick();
            }
        }
Beispiel #11
0
        Dynamic GetObject()
        {
            Vec3 mouseMapPos = Vec3.Zero;

            //get pick information
            Ray ray = RendererWorld.Instance.DefaultCamera.GetCameraToViewportRay(EngineApp.Instance.MousePosition);

            if (!float.IsNaN(ray.Direction.X))
            {
                RayCastResult result = PhysicsWorld.Instance.RayCast(ray, (int)ContactGroup.CastOnlyContact);
                if (result.Shape != null)
                {
                    return(MapSystemWorld.GetMapObjectByBody(result.Shape.Body) as Dynamic);
                }
            }
            return(null);
        }
Beispiel #12
0
        private Set <MapObject> GetObjectsInActiveArea()
        {
            Set <MapObject> result     = new Set <MapObject>();
            Bounds          areabounds = new Bounds(Position - box.Extents, Position + box.Extents);

            Body[] bodies = PhysicsWorld.Instance.VolumeCast(areabounds, (int)ContactGroup.CastOnlyDynamic);

            foreach (Body body in bodies)
            {
                if (!body.Static)
                {
                    MapObject obj = MapSystemWorld.GetMapObjectByBody(body);
                    if (obj != null && obj != this && IsAllowToTeleport(obj) &&
                        CheckPositionInActiveArea(areabounds, obj.Position))
                    {
                        result.AddWithCheckAlreadyContained(obj);
                    }
                }
            }

            return(result);
        }
Beispiel #13
0
        public List <MapObject> ObjectsInPath(MapObject startPos, MapObject endPos)
        {
            List <MapObject> objects = new List <MapObject>();

            Ray ray = new Ray(startPos.Position, endPos.Position - startPos.Position);

            if (ray.Direction != Vec3.Zero)
            {
                RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(ray, (int)ContactGroup.CastOnlyContact);

                foreach (RayCastResult result in piercingResult)
                {
                    MapObject mapObject = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                    if (mapObject != null && mapObject != startPos && mapObject != endPos)
                    {
                        objects.Add(mapObject);
                    }
                }
            }

            return(objects);
        }
Beispiel #14
0
        /// <summary>Overridden from <see cref="Engine.EntitySystem.Entity.OnTick()"/>.</summary>
        protected override void OnTick()
        {
            base.OnTick();

            if (Type.Velocity != 0)
            {
                if (Type.Gravity != 0)
                {
                    velocity.Z -= Type.Gravity * TickDelta;
                }

                Vec3  offset   = velocity * TickDelta;
                float distance = offset.LengthFast();

                bool deleteIfNoCollisions = false;
                if (Type.MaxDistance != 0)
                {
                    if (flyDistance + distance >= Type.MaxDistance)
                    {
                        distance = Type.MaxDistance - flyDistance;
                        if (distance <= 0)
                        {
                            distance = .001f;
                        }
                        offset = offset.GetNormalizeFast() * distance;
                        deleteIfNoCollisions = true;
                    }
                }

                Vec3 startPosition = Position;

                //back check (that did not fly by through moving towards objects)
                if (!firstTick)
                {
                    startPosition -= offset * .1f;
                }

                Ray ray = new Ray(startPosition, offset);

                RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                    ray, (int)ContactGroup.CastOnlyContact);

                foreach (RayCastResult result in piercingResult)
                {
                    MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                    if (obj != null)
                    {
                        Dynamic dynamic = obj as Dynamic;
                        if (dynamic != null && sourceUnit != null &&
                            dynamic.GetParentUnitHavingIntellect() == sourceUnit)
                        {
                            continue;
                        }
                    }

                    Position = result.Position;
                    OnHit(result.Shape, result.Normal, obj);
                    CreateWaterPlaneSplash(new Ray(startPosition, Position - startPosition));
                    goto end;
                }

                Position    += offset;
                flyDistance += distance;

                //update rotation
                if (velocity != Vec3.Zero)
                {
                    Rotation = Quat.FromDirectionZAxisUp(velocity.GetNormalizeFast());
                }

                CreateWaterPlaneSplash(ray);

                if (deleteIfNoCollisions)
                {
                    SetDeleted();
                    return;
                }
            }
            else
            {
                const float checkPieceSize = 40.0f;

                Bounds checkBounds = SceneManager.Instance.GetTotalObjectsBounds();
                checkBounds.Expand(200);

                Vec3 dir = Rotation.GetForward();

                Vec3 pos = Position;
                while (checkBounds.IsContainsPoint(pos))
                {
                    Vec3  offset   = dir * checkPieceSize;
                    float distance = offset.LengthFast();

                    bool deleteIfNoCollisions = false;
                    if (Type.MaxDistance != 0)
                    {
                        if (flyDistance + distance >= Type.MaxDistance)
                        {
                            distance = Type.MaxDistance - flyDistance;
                            if (distance <= 0)
                            {
                                distance = .001f;
                            }
                            offset = offset.GetNormalizeFast() * distance;
                            deleteIfNoCollisions = true;
                        }
                    }

                    Ray ray = new Ray(pos, offset);

                    RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                        ray, (int)ContactGroup.CastOnlyContact);

                    foreach (RayCastResult result in piercingResult)
                    {
                        MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                        if (obj != null)
                        {
                            Dynamic dynamic = obj as Dynamic;
                            if (dynamic != null && sourceUnit != null &&
                                dynamic.GetParentUnitHavingIntellect() == sourceUnit)
                            {
                                continue;
                            }
                        }

                        Position = result.Position;
                        OnHit(result.Shape, result.Normal, obj);
                        CreateWaterPlaneSplash(new Ray(ray.Origin, Position - ray.Origin));
                        goto end;
                    }

                    pos         += offset;
                    flyDistance += distance;

                    CreateWaterPlaneSplash(ray);

                    if (deleteIfNoCollisions)
                    {
                        SetDeleted();
                        return;
                    }
                }
                SetDeleted();
            }

            end :;

            firstTick = false;
        }
Beispiel #15
0
        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;
            }
        }
Beispiel #16
0
        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;
            }
        }
Beispiel #17
0
        private Unit AquireNewTarget()
        {
            // NH & KEEN - reset the targetted bodypart
            targetBody  = null;
            targetShape = null;

            Unit   TempTarget = null;
            Bounds volume     = new Bounds(this.Position);

            volume.Expand(new Vec3(Type.RadarRange, Type.RadarRange, Type.RadarRange));

            Body[] result = PhysicsWorld.Instance.VolumeCast(volume,
                                                             (int)ContactGroup.CastOnlyDynamic);

            foreach (Body body in result)
            {
                MapObject obj = MapSystemWorld.GetMapObjectByBody(body);
                if (obj != null)
                {
                    Unit unit = obj as Unit;

                    if (unit != null)
                    {
                        float Angletounit = CalculateAngleToTarget(unit);
                        //0.8f in radian is 45 Degrees and the absoulote value whould give the missile a window of 90'

                        float targettingAngle = Type.HomingAngle / 2;

                        if (Angletounit > targettingAngle)
                        {
                            continue;
                        }

                        if (TempTarget == null)
                        {
                            TempTarget = unit;
                        }
                        else
                        {
                            float PreTargetDistance = (TempTarget.Position - Position).Length();
                            float newTargetDistance = (unit.Position - Position).Length();

                            float DifDis    = PreTargetDistance - newTargetDistance;
                            float DiftAngle = CalculateAngleToTarget(TempTarget) - Angletounit;

                            if (DifDis > 0 && DiftAngle > 0)
                            {
                                TempTarget = unit;
                            }
                        }
                    }
                }
            }

            // NH & KEEN - if our target is a unit, select a random bodypart to target, otherwise check if there is a physics body to follow
            if (TempTarget != null)
            {
                AKunit targetUnit = TempTarget as AKunit;

                if (targetUnit != null)
                {
                    if (targetUnit.PhysicsModel != null && targetUnit.PhysicsModel.Bodies.Length != 0)
                    {
                        string targetBodyName = targetUnit.Type.BodyParts[World.Instance.Random.Next(0, targetUnit.Type.BodyParts.Count)].PhysicsShape;

                        FindTargetShape(targetUnit, targetBodyName, out targetBody, out targetShape);
                    }
                }
                else
                {
                    if (TempTarget.PhysicsModel != null && TempTarget.PhysicsModel.Bodies.Length != 0)
                    {
                        targetBody = TempTarget.PhysicsModel.Bodies[0];
                    }
                }
            }

            return(TempTarget);
        }
Beispiel #18
0
        protected virtual void Blow()
        {
            if (currentFireTypeMode == null)
            {
                return;
            }

            Unit unit = GetParentUnitHavingIntellect();

            Sphere kickSphere = new Sphere(Position, currentFireTypeMode.KickCheckRadius);

            bool playSound = false;

            Sphere volume = new Sphere(Position, currentFireTypeMode.KickCheckRadius);

            Body[] volumeResult = PhysicsWorld.Instance.VolumeCast(volume,
                                                                   (int)ContactGroup.CastOnlyContact);
            foreach (Body body in volumeResult)
            {
                //no kick
                if (body.Shapes[0].ContactGroup == (int)ContactGroup.NoContact)
                {
                    continue;
                }

                Dynamic dynamic = MapSystemWorld.GetMapObjectByBody(body) as Dynamic;

                if (dynamic != null)
                {
                    //not kick allies
                    Unit objUnit = dynamic.GetParentUnitHavingIntellect();
                    if (objUnit != null && objUnit.Intellect != null && unit.Intellect != null &&
                        objUnit.Intellect.Faction == unit.Intellect.Faction)
                    {
                        continue;
                    }

                    //impulse
                    float impulse = currentFireTypeMode.Impulse;
                    if (impulse != 0 && dynamic.PhysicsModel != null)
                    {
                        foreach (Body b in dynamic.PhysicsModel.Bodies)
                        {
                            if (b.Shapes[0].ContactGroup != (int)ContactGroup.NoContact)
                            {
                                Vec3 dir = b.Position - unit.Position;
                                dir.Normalize();
                                body.AddForce(ForceType.GlobalAtGlobalPos, 0, dir * impulse, Position);
                            }
                        }
                    }

                    //damage
                    float damage = currentFireTypeMode.Damage;
                    if (damage != 0)
                    {
                        dynamic.DoDamage(unit, Position, null, damage, false);
                    }
                }

                playSound = true;
            }

            if (playSound)
            {
                SoundPlay3D(currentFireTypeMode.SoundKick, .5f, false);

                if (EntitySystemWorld.Instance.IsServer() &&
                    Type.NetworkType == EntityNetworkTypes.Synchronized)
                {
                    bool alternative = currentFireTypeMode == Type.AlternativeMode;
                    Server_SendSoundPlayKickToAllClients(alternative);
                }
            }
        }
Beispiel #19
0
        protected virtual void CreateBullet(Mode mode)
        {
            Bullet obj = (Bullet)Entities.Instance.Create(mode.typeMode.BulletType, Parent);

            obj.SourceUnit = GetParentUnitHavingIntellect();
            obj.Position   = GetFirePosition(mode.typeMode);

            //Correcting position at a shot in very near object (when the point of a shot inside object).
            {
                Vec3 startPos = Position;
                if (AttachedMapObjectParent != null)
                {
                    startPos = AttachedMapObjectParent.Position;
                }

                Ray ray = new Ray(startPos, obj.Position - startPos);
                if (ray.Direction != Vec3.Zero)
                {
                    RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                        ray, (int)ContactGroup.CastOnlyContact);

                    foreach (RayCastResult result in piercingResult)
                    {
                        MapObject mapObject = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                        if (mapObject != null)
                        {
                            if (mapObject == this)
                            {
                                continue;
                            }
                            if (mapObject == this.AttachedMapObjectParent)
                            {
                                continue;
                            }
                        }

                        obj.Position = result.Position - ray.Direction * .01f;
                        break;
                    }
                }
            }

            Quat   rot             = GetFireRotation(mode.typeMode);
            Radian dispersionAngle = mode.typeMode.DispersionAngle;

            if (dispersionAngle != 0)
            {
                EngineRandom random = World.Instance.Random;

                Mat3 matrix;
                matrix  = Mat3.FromRotateByX(random.NextFloat() * MathFunctions.PI * 2);
                matrix *= Mat3.FromRotateByZ(random.NextFloat() * dispersionAngle);

                rot *= matrix.ToQuat();
            }
            obj.Rotation = rot;

            obj.PostCreate();

            //set damage coefficient
            float coef = obj.DamageCoefficient;
            Unit  unit = GetParentUnitHavingIntellect();

            if (unit != null && unit.BigDamageInfluence != null)
            {
                coef *= unit.BigDamageInfluence.Type.Coefficient;
            }
            obj.DamageCoefficient = coef;
        }
Beispiel #20
0
        private bool Process(float maxDistanceForBulletWithInfiniteSpeed)
        {
            Vec3  offset;
            float distance;

            if (Type.Velocity != 0)
            {
                offset   = velocity * TickDelta;
                distance = offset.Length();
            }
            else
            {
                offset   = Rotation.GetForward() * maxDistanceForBulletWithInfiniteSpeed;
                distance = maxDistanceForBulletWithInfiniteSpeed;
            }
            bool deleteIfNoCollisions = false;

            if (Type.MaxDistance != 0 && flyDistance + distance >= Type.MaxDistance)
            {
                distance = Type.MaxDistance - flyDistance;
                if (distance <= 0)
                {
                    distance = .001f;
                }
                offset = offset.GetNormalize() * distance;
                deleteIfNoCollisions = true;
            }

            Vec3 startPosition = Position;

            //back check (that did not fly by through moving towards objects)
            if (!firstTick)
            {
                startPosition -= offset * .1f;
            }

            Ray ray = new Ray(startPosition, offset);

            //find a hit
            Shape     hitShape     = null;
            Vec3      hitPosition  = Vec3.Zero;
            Vec3      hitNormal    = Vec3.Zero;
            MapObject hitMapObject = null;//can be null (as example in case when collided with a HeightmapTerrain shape)

            {
                RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(ray, (int)ContactGroup.CastOnlyContact);
                foreach (RayCastResult result in piercingResult)
                {
                    //get associated MapObject with the parent body of the shape
                    MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                    //skip bullet creator
                    Dynamic dynamic = obj as Dynamic;
                    if (dynamic != null && sourceUnit != null && dynamic.GetParentUnitHavingIntellect() == sourceUnit)
                    {
                        continue;
                    }

                    //found!
                    hitShape     = result.Shape;
                    hitPosition  = result.Position;
                    hitNormal    = result.Normal;
                    hitMapObject = obj;
                    break;
                }
            }

            if (hitShape != null)
            {
                //process the hit

                Ray rayToHit = new Ray(ray.Origin, hitPosition - ray.Origin);

                //networking: call OnHit on clients
                if (EntitySystemWorld.Instance.IsServer() && Type.NetworkType == EntityNetworkTypes.Synchronized)
                {
                    Body hitShapeBody = null;
                    if (hitShape != null)
                    {
                        hitShapeBody = hitShape.Body;
                    }

                    Server_SendHitCallToAllClients(hitPosition,
                                                   hitShapeBody != null ? hitShapeBody.Name : "",
                                                   hitShape != null ? hitShape.Name : "",
                                                   hitNormal, hitMapObject);
                }

                Position = hitPosition;
                OnHit(hitShape, hitNormal, hitMapObject);

                CreateWaterPlaneSplash(rayToHit);

                return(true);
            }
            else
            {
                //no hit. continue the flying or delete

                Position += offset;
                //update rotation
                if (velocity != Vec3.Zero)
                {
                    Rotation = Quat.FromDirectionZAxisUp(velocity.GetNormalize());
                }
                flyDistance += distance;

                CreateWaterPlaneSplash(ray);

                if (deleteIfNoCollisions)
                {
                    SetForDeletion(false);
                }

                return(false);
            }
        }
Beispiel #21
0
        void CreateDirectionalDecal(Shape shape, Vec3 pos, Vec3 normal, int triangleID)
        {
            bool smallDecal = Type.Size < .3f;

            Body body = shape.Body;

            //static objects
            if (shape.ContactGroup == (int)ContactGroup.Collision)
            {
                ShapeTriangleID triangle = new ShapeTriangleID(shape, triangleID);

                //StaticMesh
                {
                    StaticMesh staticMesh = StaticMesh.GetStaticMeshByBody(body);
                    if (staticMesh != null)
                    {
                        if (staticMesh.AllowDecals == StaticMesh.DecalTypes.OnlySmall && smallDecal ||
                            staticMesh.AllowDecals == StaticMesh.DecalTypes.All)
                        {
                            CreateDecalForStaticObject(triangle, pos, normal, null);
                            return;
                        }
                    }
                }

                //HeightmapTerrain
                if (HeightmapTerrain.GetTerrainByBody(body) != null)
                {
                    CreateDecalForStaticObject(triangle, pos, normal, null);
                    return;
                }

                if (Type.ApplyToMapObjects)
                {
                    //MapObject: Attached mesh
                    MapObject mapObject = MapSystemWorld.GetMapObjectByBody(body);
                    if (mapObject != null)
                    {
                        bool isCollision = false;

                        foreach (MapObjectAttachedObject attachedObject in mapObject.AttachedObjects)
                        {
                            MapObjectAttachedMesh attachedMesh = attachedObject as MapObjectAttachedMesh;
                            if (attachedMesh != null && attachedMesh.CollisionBody == body)
                            {
                                isCollision = true;
                                break;
                            }
                        }

                        if (isCollision)
                        {
                            CreateDecalForStaticObject(triangle, pos, normal, mapObject);
                            return;
                        }
                    }
                }
            }

            //dynamic objects
            {
                //not implemented
            }
        }
        private void body_Collision(ref CollisionEvent collisionEvent)
        {
            Body otherBody = collisionEvent.OtherShape.Body;

            if (HeightmapTerrain.GetTerrainByBody(otherBody) != null)
            {
                return;
            }

            if (otherBody.Static && otherBody.Name.Contains("Map")) //LDASH custom map names
            {
                return;
            }

            //Note: Incin ---- Dynamic_Collision needs to be removed

            Body thisBody = collisionEvent.ThisShape.Body;

            //if (!otherBody.Static)
            //    DoForce(thisBody);

            if (otherBody == null && thisBody != null)
            {
                DoForce(null, thisBody);
            }

            if (!otherBody.Static)
            {
                DoForce(otherBody, thisBody); //collisionEvent.Position, collisionEvent.Normal);
            }
            float otherMass = otherBody.Mass;
            float impulse   = 0;

            impulse += thisBody.LastStepLinearVelocity.Length() * thisBody.Mass;

            if (otherMass != 0)
            {
                impulse += otherBody.LastStepLinearVelocity.Length() * otherMass;
            }

            float damage = impulse; // *Type.ImpulseDamageCoefficient;

            MapObject mapobj = MapSystemWorld.GetMapObjectByBody(otherBody);

            if (mapobj != null)
            {
                Dynamic obj = mapobj as Dynamic;
                if (obj != null)
                {
                    if (obj.Name.Contains("House"))
                    {
                        //damage house
                        if (obj.Type.ImpulseDamageCoefficient != 0)
                        {
                            damage = impulse * obj.Type.ImpulseDamageCoefficient;
                        }
                        else
                        {
                            float health = obj.Health / 2;
                            damage = health;
                        }
                        OnDamage(mapobj, collisionEvent.Position, collisionEvent.OtherShape, damage, true);

                        //damage player if too fast
                        if (Type.ImpulseDamageCoefficient != 0)
                        {
                            damage = impulse * Type.ImpulseDamageCoefficient;
                        }
                        else
                        {
                            damage = impulse;
                        }

                        //if minimal damage do player damage
                        ////if (damage >= Type.ImpulseMinimalDamage)
                        ////{
                        ////    //OnDamage(null, collisionEvent.Position, collisionEvent.ThisShape, damage, true);//damage the other guy here
                        ////    OnDamage(null, collisionEvent.Position, collisionEvent.ThisShape, damage, true);
                        ////}
                    }
                    else //still object type damage
                    {
                        if (obj.Type.ImpulseDamageCoefficient != 0)
                        {
                            damage = impulse * obj.Type.ImpulseDamageCoefficient;
                        }
                        else
                        {
                            damage = impulse * 0.5f;
                        }

                        OnDamage(mapobj, collisionEvent.Position, collisionEvent.OtherShape, damage, true);
                    }
                }
            }
            //else  //damage self
            //{
            //    //if (Type.ImpulseDamageCoefficient != 0)
            //    //    damage = impulse * Type.ImpulseDamageCoefficient;
            //    //else
            //    //    damage = impulse;

            //    //if (damage >= Type.ImpulseMinimalDamage)
            //    //{
            //    //    OnDamage(null, collisionEvent.Position, collisionEvent.ThisShape, damage, true);
            //    //}
            //}
        }
        bool GetTurnToPosition(out Vec3 turnToPosition)
        {
            if (Instance != this)
            {
                Log.Fatal("PlayerIntellect: GetTurnToPosition: Instance != this.");
            }

            turnToPosition = Vec3.Zero;

            if (ControlledObject == null)
            {
                return(false);
            }

            //CutSceneManager specific
            if (CutSceneManager.Instance != null && CutSceneManager.Instance.CutSceneEnable)
            {
                return(false);
            }

            Vec3 from;
            Vec3 dir;

            if (!fpsCamera)
            {
                from = ControlledObject.Position + new Vec3(0, 0, tpsCameraCenterOffset);
                dir  = lookDirection.GetVector();
            }
            else
            {
                from = ControlledObject.Position +
                       ControlledObject.Type.FPSCameraOffset * ControlledObject.Rotation;
                dir = lookDirection.GetVector();
            }

            //invalid ray
            if (dir == Vec3.Zero || float.IsNaN(from.X) || float.IsNaN(dir.X))
            {
                return(false);
            }

            float distance = 1000.0f;

            turnToPosition = from + dir * distance;

            RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                new Ray(from, dir * distance), (int)ContactGroup.CastAll);
            foreach (RayCastResult result in piercingResult)
            {
                WaterPlane waterPlane = WaterPlane.GetWaterPlaneByBody(result.Shape.Body);

                if (waterPlane == null && result.Shape.ContactGroup == (int)ContactGroup.NoContact)
                {
                    continue;
                }

                MapObject obj = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                if (obj == ControlledObject)
                {
                    continue;
                }

                if (waterPlane != null)
                {
                    //ignore water from inside
                    if (result.Shape.Body.GetGlobalBounds().IsContainsPoint(from))
                    {
                        continue;
                    }
                }

                Dynamic dynamic = obj as Dynamic;
                if (dynamic != null)
                {
                    if (dynamic.GetParentUnitHavingIntellect() == ControlledObject)
                    {
                        continue;
                    }
                }

                turnToPosition = result.Position;
                break;
            }

            return(true);
        }
Beispiel #24
0
        protected override void CreateBullet(Mode mode)
        {
            //only missiles for missilelauncher
            if (mode.typeMode.BulletType as Missile2Type == null)
            {
                return;
            }

            Missile2 obj = (Missile2)Entities.Instance.Create(mode.typeMode.BulletType, Parent);

            obj.SourceUnit = GetParentUnitHavingIntellect();
            obj.Position   = GetFirePosition(mode.typeMode);

            //Correcting position at a shot in very near object (when the point of a shot inside object).
            {
                Vec3 startPos = Position;
                if (AttachedMapObjectParent != null)
                {
                    startPos = AttachedMapObjectParent.Position;
                }

                Ray ray = new Ray(startPos, obj.Position - startPos);
                if (ray.Direction != Vec3.Zero)
                {
                    RayCastResult[] piercingResult = PhysicsWorld.Instance.RayCastPiercing(
                        ray, (int)ContactGroup.CastOnlyContact);

                    foreach (RayCastResult result in piercingResult)
                    {
                        MapObject mapObject = MapSystemWorld.GetMapObjectByBody(result.Shape.Body);

                        if (mapObject != null)
                        {
                            if (mapObject == this)
                            {
                                continue;
                            }
                            if (mapObject == this.AttachedMapObjectParent)
                            {
                                continue;
                            }
                        }

                        obj.Position = result.Position - ray.Direction * .01f;
                        break;
                    }
                }
            }

            Quat   rot             = GetFireRotation(mode.typeMode);
            Radian dispersionAngle = mode.typeMode.DispersionAngle;

            if (dispersionAngle != 0)
            {
                EngineRandom random = World.Instance.Random;

                float halfDir;

                halfDir = random.NextFloatCenter() * dispersionAngle * .5f;
                rot    *= new Quat(new Vec3(0, 0, MathFunctions.Sin(halfDir)),
                                   MathFunctions.Cos(halfDir));
                halfDir = random.NextFloatCenter() * dispersionAngle * .5f;
                rot    *= new Quat(new Vec3(0, MathFunctions.Sin(halfDir), 0),
                                   MathFunctions.Cos(halfDir));
                halfDir = random.NextFloatCenter() * dispersionAngle * .5f;
                rot    *= new Quat(new Vec3(MathFunctions.Sin(halfDir), 0, 0),
                                   MathFunctions.Cos(halfDir));
            }
            obj.Rotation = rot;

            obj.PostCreate();

            //set damage coefficient
            float coef = obj.DamageCoefficient;
            Unit  unit = GetParentUnitHavingIntellect();

            if (unit != null && unit.BigDamageInfluence != null)
            {
                coef *= unit.BigDamageInfluence.Type.Coefficient;
            }
            obj.DamageCoefficient = coef;

            foreach (MapObjectAttachedObject attachedObject in AttachedObjects)
            {
                MapObjectAttachedMesh attachedMesh = attachedObject as MapObjectAttachedMesh;
                if (attachedMesh == null)
                {
                    continue;
                }

                if (attachedMesh.Alias.Equals("Missile"))
                {
                    if (!attachedMesh.Visible)
                    {
                        continue;
                    }
                    else
                    {
                        attachedMesh.Visible = false;
                        break;
                    }
                }
            }
        }