private bool TryDamageBranch(INetObject damager, float amount, InteractionContext context)
    {
        int        branchID = context.Parameters["branch"];
        TreeBranch branch   = this.branches[branchID];

        if (context.Parameters.ContainsKey("leaf"))
        {
            int leafID = context.Parameters["leaf"];

            // damage leaf
            LeafBunch leaf = branch.Leaves[leafID];

            if (leaf.Health > 0)
            {
                List <IAtomicAction> actions = new List <IAtomicAction>();
                if (damager is Player)
                {
                    var action = PlayerActions.HarvestLeaves.CreateAtomicAction(((Player)damager).User, this);
                    actions.Add(action);

                    if (!PlayerActions.HarvestLeaves.CreateAtomicAction(((Player)damager).User, this).CanApply().Notify((Player)damager))
                    {
                        // We only want to dispose the action if it is invalid.  Othewise we want to keep it around to possibly apply later.
                        action.Dispose();
                        return(false);
                    }
                }

                leaf.Health = Mathf.Max(0, leaf.Health - amount);

                if (leaf.Health <= 0)
                {
                    if (!new MultiAtomicAction(actions).TryApply().Success)
                    {
                        throw new Exception("Removing this stump was verified to be legal a moment ago, but is not anymore.");
                    }

                    leaf.Health = 0;
                    this.RPC("DestroyLeaves", branchID, leafID);
                }
                else
                {
                    new MultiAtomicAction(actions).Dispose();
                }
            }

            this.Save();
            return(true);
        }
        else
        {
            return(this.TryDamageBranch(branch, branchID, amount));
        }
    }
    void DestroyLeaf(int branchID, int leafID)
    {
        TreeBranch branch = this.branches[branchID];
        LeafBunch  leaf   = branch.Leaves[leafID];

        if (leaf.Health > 0)
        {
            // replicate to all clients
            leaf.Health = 0;
            this.RPC("DestroyLeaves", branchID, leafID);
        }
    }
Пример #3
0
    private bool TryDamageBranch(INetObject damager, float amount, InteractionContext context)
    {
        int        branchID = context.Parameters["branch"];
        TreeBranch branch   = this.branches[branchID];

        if (context.Parameters.ContainsKey("leaf"))
        {
            int leafID = context.Parameters["leaf"];

            // damage leaf
            LeafBunch leaf = branch.Leaves[leafID];

            if (leaf.Health > 0)
            {
                leaf.Health = Mathf.Max(0, leaf.Health - amount);

                if (leaf.Health <= 0)
                {
                    leaf.Health = 0;

                    if (RandomUtil.Value < this.Species.SeedDropChance)
                    {
                        var    numSeeds      = (int)this.Species.SeedRange.Max;
                        int    numBonusSeeds = 0;
                        Item[] newSeeds      = new Item[] { };
                        if (numSeeds > 0 && this.Species.SeedItem != null)
                        {
                            var yield = ItemAttribute.Get <YieldAttribute>(this.Species.SeedItem.Type);
                            numBonusSeeds = yield != null?yield.Yield.GetCurrentValueInt(context.Player.User) : 0;

                            context.Player.User.Inventory.TryAddItems(this.Species.SeedItem.Type, numSeeds + numBonusSeeds);
                        }
                    }
                    this.RPC("DestroyLeaves", branchID, leafID);
                }
            }

            this.Save();
            return(true);
        }
        else
        {
            return(this.TryDamageBranch(branch, branchID, amount));
        }
    }
    private bool TryDamageBranch(INetObject damager, float amount, InteractionContext context)
    {
        int        branchID = context.Parameters["branch"];
        TreeBranch branch   = this.branches[branchID];

        if (context.Parameters.ContainsKey("leaf"))
        {
            int leafID = context.Parameters["leaf"];

            // damage leaf
            LeafBunch leaf = branch.Leaves[leafID];

            if (leaf.Health > 0)
            {
                List <IAtomicAction> actions = new List <IAtomicAction>();
                if (damager is Player)
                {
                    var action = PlayerActions.HarvestLeaves.CreateAtomicAction((Player)damager, this);
                    actions.Add(action);

                    if (!PlayerActions.HarvestLeaves.CreateAtomicAction((Player)damager, this).CanApply().Notify((Player)damager))
                    {
                        // We only want to dispose the action if it is invalid.  Othewise we want to keep it around to possibly apply later.
                        action.Dispose();
                        return(false);
                    }
                }

                leaf.Health = Mathf.Max(0, leaf.Health - amount);

                if (leaf.Health <= 0)
                {
                    if (!new MultiAtomicAction(actions).TryApply().Success)
                    {
                        throw new Exception("Removing this stump was verified to be legal a moment ago, but is not anymore.");
                    }

                    leaf.Health = 0;

                    if (RandomUtil.Value < this.Species.SeedDropChance)
                    {
                        var    numSeeds      = (int)this.Species.SeedRange.Max;
                        int    numBonusSeeds = 0;
                        Item[] newSeeds      = new Item[] { };
                        if (numSeeds > 0 && this.Species.SeedItem.Type != null)
                        {
                            var yield = ItemAttribute.Get <YieldAttribute>(this.Species.SeedItem.Type);
                            numBonusSeeds = yield != null?yield.Yield.GetCurrentValueInt(context.Player.User) : 0;

                            context.Player.User.Inventory.TryAddItems(this.Species.SeedItem.Type, numSeeds + numBonusSeeds);
                        }
                    }
                    this.RPC("DestroyLeaves", branchID, leafID);
                }
                else
                {
                    new MultiAtomicAction(actions).Dispose();
                }
            }

            this.Save();
            return(true);
        }
        else
        {
            return(this.TryDamageBranch(branch, branchID, amount));
        }
    }