コード例 #1
0
        public List <PositionComponent> CollectEntity_SurroundingRing(Vector3FP position, FixPoint outer_radius, FixPoint inner_radius, int exclude_id)
        {
            m_collection.Clear();
            PositionComponent cmp;

            for (int i = 0; i < m_entities.Count; ++i)
            {
                cmp = m_entities[i];
                if (cmp.GetOwnerEntityID() == exclude_id)
                {
                    continue;
                }
                Vector3FP offset   = position - cmp.CurrentPosition;
                FixPoint  distance = FixPoint.FastDistance(offset.x, offset.z);
                if (distance >= (outer_radius + cmp.Radius))
                {
                    continue;
                }
                if (inner_radius > FixPoint.Zero && distance <= (inner_radius - cmp.Radius))
                {
                    continue;
                }
                m_collection.Add(cmp);
            }
            return(m_collection);
        }
コード例 #2
0
        public PositionComponent GetNearestEnemy(Entity source_entity)
        {
            Player                   source_player       = source_entity.GetOwnerPlayer();
            PositionComponent        source_position_cmp = source_entity.GetComponent(PositionComponent.ID) as PositionComponent;
            Vector3FP                source_position     = source_position_cmp.CurrentPosition;
            List <PositionComponent> list = source_position_cmp.GetSpacePartition().CollectEntity_All();

            PositionComponent potential_enemy = null;
            FixPoint          min_distance    = FixPoint.MaxValue;

            for (int i = 0; i < list.Count; ++i)
            {
                PositionComponent target_position_component = list[i];
                Entity            entity = target_position_component.GetOwnerEntity();
                if (entity.GetComponent(DamagableComponent.ID) == null)
                {
                    continue;
                }
                if (!FactionRelation.IsFactionSatisfied(source_player.GetFaction(entity.GetOwnerPlayerID()), FactionRelation.Enemy))
                {
                    continue;
                }
                Vector3FP offset   = source_position - target_position_component.CurrentPosition;
                FixPoint  distance = FixPoint.FastDistance(offset.x, offset.z);
                if (distance < min_distance)
                {
                    potential_enemy = target_position_component;
                    min_distance    = distance;
                }
            }
            return(potential_enemy);
        }
コード例 #3
0
ファイル: FixPointTester.cs プロジェクト: suzuke/FixPoint
    public void TestSqrt()
    {
        m_max_precision_times = 0m;
        m_max_delta_percent   = 0m;
        int max_int = (int)(Math.Sqrt((double)(int.MaxValue)));

        for (int i = 0; i < max_int; ++i)
        {
            int      value   = i * i;
            FixPoint fp      = new FixPoint(value);
            FixPoint sqrt_fp = FixPoint.Sqrt(fp);
            Assert.Equal((int)sqrt_fp, i);
        }

        System.Random ran = new System.Random();
        for (int i = 0; i < 10000; ++i)
        {
            double   term         = (double)ran.NextDouble() * int.MaxValue;
            double   sqrt_term    = Math.Sqrt(term);
            FixPoint fp_sqrt_term = (FixPoint)sqrt_term;
            FixPoint fp           = (FixPoint)term;
            FixPoint sqrt_fp      = FixPoint.Sqrt(fp);
            Assert.Equal((double)sqrt_fp, sqrt_term);
            Assert.Equal(sqrt_fp, fp_sqrt_term);

            //decimal delta = (decimal)Math.Abs(sqrt_term - (double)sqrt_fp);
            //decimal precision_times = delta / FixPoint.Precision;
            //decimal delta_percent = delta * 100.0m / (decimal)term;
            //if (precision_times > m_max_precision_times)
            //    m_max_precision_times = precision_times;
            //if (delta_percent > m_max_delta_percent)
            //    m_max_delta_percent = delta_percent;
        }

        Console.WriteLine("TestSqrt() m_max_precision_times = " + m_max_precision_times + ", m_max_delta_percent = " + m_max_delta_percent);

        m_max_precision_times = 0m;
        m_max_delta_percent   = 0m;
        max_int = (int)Math.Sqrt((double)(int.MaxValue / 2));
        for (int i = 0; i < 10000; ++i)
        {
            double   term1       = (double)ran.NextDouble() * max_int;
            double   term2       = (double)ran.NextDouble() * max_int;
            double   distance    = Math.Sqrt(term1 * term1 + term2 * term2);
            FixPoint fpterm1     = (FixPoint)term1;
            FixPoint fpterm2     = (FixPoint)term2;
            FixPoint fp_distance = FixPoint.FastDistance(fpterm1, fpterm2);

            decimal delta         = (decimal)Math.Abs(distance - (double)fp_distance);
            decimal delta_percent = delta * 100.0m / (decimal)distance;
            if (delta_percent > m_max_delta_percent)
            {
                m_max_delta_percent = delta_percent;
            }
        }

        Console.WriteLine("FastDistance() m_max_delta_percent = " + m_max_delta_percent);
    }
コード例 #4
0
        public static bool FastInsideFov2D(FixPoint source_d1, FixPoint source_d2, FixPoint facing_d1, FixPoint facing_d2, FixPoint fov_degree, FixPoint target_d1, FixPoint target_d2)
        {
            FixPoint to_target_d1 = target_d1 - source_d1;
            FixPoint to_target_d2 = target_d2 - source_d2;
            FixPoint distance     = FixPoint.FastDistance(to_target_d1, to_target_d2);

            to_target_d1 /= distance;
            to_target_d2 /= distance;
            return(to_target_d1 * facing_d1 + to_target_d2 * facing_d2 >= FixPoint.Cos(FixPoint.Degree2Radian(fov_degree / FixPoint.Two)));
        }
コード例 #5
0
        public List <PositionComponent> CollectEntity_SurroundingRing(Vector3FP position, FixPoint outer_radius, FixPoint inner_radius, int exclude_id)
        {
            m_collection.Clear();
            Vector2FP start_position = new Vector2FP(position.x - outer_radius, position.z - outer_radius);
            Vector2FP end_position   = new Vector2FP(position.x + outer_radius, position.z + outer_radius);

            ComputeAreaXZ(start_position, end_position);
            Cell cell;
            PositionComponent cmp;

            for (int x = m_min_x; x <= m_max_x; ++x)
            {
                for (int z = m_min_z; z <= m_max_z; ++z)
                {
                    cell = m_cells[x, z];
                    for (int i = 0; i < cell.m_entities.Count; ++i)
                    {
                        cmp = cell.m_entities[i];
                        if (cmp.GetOwnerEntityID() == exclude_id)
                        {
                            continue;
                        }
                        Vector3FP offset   = position - cmp.CurrentPosition;
                        FixPoint  distance = FixPoint.FastDistance(offset.x, offset.z);
                        if (distance >= (outer_radius + cmp.Radius))
                        {
                            continue;
                        }
                        if (inner_radius > FixPoint.Zero && distance <= (inner_radius - cmp.Radius))
                        {
                            continue;
                        }
                        m_collection.Add(cmp);
                    }
                }
            }
            return(m_collection);
        }
コード例 #6
0
 public FixPoint FastDistance(Vector3FP v3fp)
 {
     return(FixPoint.FastDistance(FixPoint.FastDistance(v3fp.x - x, v3fp.y - y), v3fp.z - z));
 }
コード例 #7
0
 public FixPoint FastLength()
 {
     return(FixPoint.FastDistance(FixPoint.FastDistance(x, y), z));
 }
コード例 #8
0
 public FixPoint FastDistance(ref Vector2FP v2fp)
 {
     return(FixPoint.FastDistance(v2fp.x - x, v2fp.z - z));
 }