public static WorldEditBlock CreateNew(Block pBlock, Vector3i pPosition, Vector3i?pSourcePosition)
        {
            WorldEditBlock web = new WorldEditBlock();

            web.Position = pPosition;
            web.Type     = pBlock.GetType();

            var constuctor = web.Type.GetConstructor(Type.EmptyTypes);

            if (constuctor == null)
            {
                if (pBlock is PlantBlock)
                {
                    web.Data = EcoSerializer.Serialize(PlantBlock.GetPlant(pPosition)).ToArray();
                }

                if (pBlock is WorldObjectBlock)
                {
                    WorldObjectBlock wob = (WorldObjectBlock)pBlock;

                    if (wob.WorldObjectHandle.Object.Position3i == pSourcePosition)
                    {
                        web.Data = EcoSerializer.Serialize(wob.WorldObjectHandle.Object).ToArray();
                    }
                }
            }

            return(web);
        }
Ejemplo n.º 2
0
        protected override void Execute(WorldRange selection)
        {
            selection = selection.FixXZ(Shared.Voxel.World.VoxelSize);

            void DoAction(Vector3i pos)
            {
                Block block = Eco.World.World.GetBlock(pos);

                if (block.GetType() == typeof(PlantBlock) || block.GetType() == typeof(TreeBlock))
                {
                    var pb = PlantBlock.GetPlant(pos);
                    pb.GrowthPercent = 1;
                    pb.Tended        = true;
                    pb.Tick();
                }
            }

            selection.ForEachInc(DoAction);
        }
 public override void BlockInteraction(Vector3i pos, Quaternion rot, VehicleComponent vehicle, Inventory inv = null)
 {
     foreach (var offset in area)
     {
         var    targetPos  = (rot.RotateVector(offset) + pos).XYZi;
         Result authResult = AuthManager.IsAuthorized(targetPos, vehicle.Driver.User);
         if (authResult.Success)
         {
             var plant = PlantBlock.GetPlant(targetPos);
             if (!(plant is IHarvestable))
             {
                 continue;
             }
             if (plant.Alive)
             {
                 ((IHarvestable)plant).TryHarvest(vehicle.Driver, false, inv);
             }
             else
             {
                 World.DeleteBlock(targetPos);
             }
         }
     }
 }
        public static void SetBlock(Type pType, Vector3i pPosition, UserSession pSession = null, Vector3i?pSourcePosition = null, Block pSourceBlock = null, byte[] pData = null)
        {
            if (pType == null || pType.DerivesFrom <PickupableBlock>())
            {
                pType = typeof(EmptyBlock);
            }

            WorldObjectBlock worldObjectBlock = Eco.World.World.GetBlock(pPosition) as WorldObjectBlock;

            if (worldObjectBlock != null)
            {
                if (worldObjectBlock.WorldObjectHandle.Object.Position3i == pPosition)
                {
                    worldObjectBlock.WorldObjectHandle.Object.Destroy();
                }
            }

            if (pType == typeof(EmptyBlock))
            {
                Eco.World.World.DeleteBlock(pPosition);
                return;
            }

            var constuctor = pType.GetConstructor(Type.EmptyTypes);

            if (constuctor != null)
            {
                Eco.World.World.SetBlock(pType, pPosition);
                return;
            }

            Type[] types = new Type[1];
            types[0] = typeof(WorldPosition3i);

            constuctor = pType.GetConstructor(types);

            if (constuctor != null)
            {
                object obj = null;

                if (pData != null)
                {
                    MemoryStream ms = new MemoryStream(pData);
                    obj = EcoSerializer.Deserialize(ms);
                }

                if (pType.DerivesFrom <PlantBlock>())
                {
                    PlantSpecies ps = null;
                    Plant        pb = null;

                    if (obj != null)
                    {
                        pb = (Plant)obj;
                        ps = pb.Species;
                    }
                    else
                    {
                        ps = WorldUtils.GetPlantSpecies(pType);

                        if (pSourceBlock != null)
                        {
                            pb = PlantBlock.GetPlant(pPosition);
                        }
                    }

                    Plant newplant = EcoSim.PlantSim.SpawnPlant(ps, pPosition);

                    if (pb != null)
                    {
                        newplant.YieldPercent  = pb.YieldPercent;
                        newplant.Dead          = pb.Dead;
                        newplant.DeadType      = pb.DeadType;
                        newplant.GrowthPercent = pb.GrowthPercent;
                        newplant.DeathTime     = pb.DeathTime;
                        newplant.Tended        = pb.Tended;
                    }

                    return;
                }

                AsphaltLog.WriteLine("Unknown Type: " + pType);
            }

            types[0]   = typeof(WorldObject);
            constuctor = pType.GetConstructor(types);

            if (constuctor != null)
            {
                WorldObject wObject = null;

                if (pSourceBlock != null)
                {
                    wObject = ((WorldObjectBlock)pSourceBlock).WorldObjectHandle.Object;

                    //if this is not the "main block" of an object do nothing
                    if (wObject.Position3i != pSourcePosition.Value)
                    {
                        return;
                    }
                }
                else if (pData != null)
                {
                    MemoryStream ms  = new MemoryStream(pData);
                    var          obj = EcoSerializer.Deserialize(ms);

                    if (obj is WorldObject)
                    {
                        wObject = obj as WorldObject;
                    }
                    else
                    {
                        throw new InvalidOperationException("obj is not WorldObjectBlock");
                    }
                }

                if (wObject == null)
                {
                    return;
                }


                //    WorldObject newObject = WorldObjectUtil.Spawn(wObject.GetType().Name, wObject.Creator.User, pPosition);
                //    newObject.Rotation = wObject.Rotation;


                WorldObject newObject = (WorldObject)Activator.CreateInstance(wObject.GetType(), true);
                newObject = WorldObjectManager.Add(newObject, wObject.Creator.User, pPosition, wObject.Rotation);


                {
                    StorageComponent newSC = newObject.GetComponent <StorageComponent>();

                    if (newSC != null)
                    {
                        StorageComponent oldPSC = wObject.GetComponent <StorageComponent>();
                        newSC.Inventory.AddItems(oldPSC.Inventory.Stacks);
                        //    newSC.Inventory.OnChanged.Invoke(null);
                    }
                }

                {
                    CustomTextComponent newTC = newObject.GetComponent <CustomTextComponent>();

                    if (newTC != null)
                    {
                        CustomTextComponent oldTC = wObject.GetComponent <CustomTextComponent>();

                        typeof(CustomTextComponent).GetProperty("Text", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(newTC, oldTC.Text);
                    }
                }

                return;
            }

            AsphaltLog.WriteLine("Unknown Type: " + pType);
        }