Example #1
0
        public override void OnEntitySpawn()
        {
            base.OnEntitySpawn();

            if (World.Api.Side == EnumAppSide.Server)
            {
                EntityBehaviorTaskAI taskAi = GetBehavior <EntityBehaviorTaskAI>();

                taskAi.taskManager.ShouldExecuteTask =
                    (task) => tradingWith == null || (task is AiTaskIdle || task is AiTaskSeekEntity || task is AiTaskGotoEntity);

                if (TradeProps != null)
                {
                    RefreshBuyingSellingInventory();
                    WatchedAttributes.SetDouble("lastRefreshTotalDays", World.Calendar.TotalDays - World.Rand.NextDouble() * 6);
                    Inventory.GiveToTrader((int)TradeProps.Money.nextFloat(1f, World.Rand));
                }
                else
                {
                    World.Logger.Warning("Trader TradeProps not set during trader entity spawn. Won't have any items for sale/purchase.");
                }

                Personality = Personalities.GetKeyAtIndex(World.Rand.Next(Personalities.Count));
                (AnimManager as TraderAnimationManager).Personality = this.Personality;
            }
        }
        public override void OnGameTick(float dt)
        {
            base.OnGameTick(dt);


            if (Alive && AnimManager.ActiveAnimationsByAnimCode.Count == 0)
            {
                AnimManager.StartAnimation(new AnimationMetaData()
                {
                    Code = "idle", Animation = "idle", EaseOutSpeed = 10000, EaseInSpeed = 10000
                });
            }

            if (World.Side == EnumAppSide.Client)
            {
                talkUtil.OnGameTick(dt);
            }
            else
            {
                if (tickCount++ > 200)
                {
                    double lastRefreshTotalDays = WatchedAttributes.GetDouble("lastRefreshTotalDays", World.Calendar.TotalDays - 10);
                    int    maxRefreshes         = 10;

                    while (World.Calendar.TotalDays - lastRefreshTotalDays > doubleRefreshIntervalDays && tradingWith == null && maxRefreshes-- > 0)
                    {
                        int    traderAssets = Inventory.GetTraderAssets();
                        double giveRel      = 0.07 + World.Rand.NextDouble() * 0.21;

                        float nowWealth = TradeProps.Money.nextFloat(1f, World.Rand);

                        int toGive = (int)Math.Max(-3, Math.Min(nowWealth, traderAssets + giveRel * (int)nowWealth) - traderAssets);
                        Inventory.GiveToTrader(toGive);

                        RefreshBuyingSellingInventory(0.5f);

                        lastRefreshTotalDays += doubleRefreshIntervalDays;
                        WatchedAttributes.SetDouble("lastRefreshTotalDays", lastRefreshTotalDays);

                        tickCount = 1;
                    }

                    if (maxRefreshes <= 0)
                    {
                        WatchedAttributes.SetDouble("lastRefreshTotalDays", World.Calendar.TotalDays + 1 + World.Rand.NextDouble() * 5);
                    }
                }
            }

            if (tradingWith != null && (tradingWith.Pos.SquareDistanceTo(this.Pos) > 5 || Inventory.openedByPlayerGUIds.Count == 0 || !Alive))
            {
                dlg?.TryClose();
                IPlayer tradingPlayer = tradingWith?.Player;
                if (tradingPlayer != null)
                {
                    Inventory.Close(tradingPlayer);
                }
            }
        }
Example #3
0
        public override void Initialize(EntityProperties properties, ICoreAPI api, long chunkindex3d)
        {
            controls.StopAllMovement();

            if (api.Side == EnumAppSide.Client)
            {
                talkUtil                = new EntityTalkUtil(api as ICoreClientAPI, this);
                talkUtil.soundName      = new AssetLocation("sounds/voice/altoflute");
                talkUtil.idleTalkChance = 0f;
            }

            base.Initialize(properties, api, chunkindex3d);

            if (api.Side == EnumAppSide.Server && !WatchedAttributes.attributes.ContainsKey("lastReviveTotalHours"))
            {
                double hrs = Api.World.Calendar.GetDayLightStrength(ServerPos.X, ServerPos.Z) < 0.5 ? Api.World.Calendar.TotalHours : -9999;
                WatchedAttributes.SetDouble("lastReviveTotalHours", hrs);
            }
        }
Example #4
0
        public override void OnGameTick(float dt)
        {
            base.OnGameTick(dt);


            if (Alive && AnimManager.ActiveAnimationsByAnimCode.Count == 0)
            {
                AnimManager.StartAnimation(new AnimationMetaData()
                {
                    Code = "idle", Animation = "idle", EaseOutSpeed = 10000, EaseInSpeed = 10000
                });
            }

            if (World.Side == EnumAppSide.Client)
            {
                talkUtil.OnGameTick(dt);
            }
            else
            {
                if (tickCount++ % 1000 == 0)
                {
                    double lastRefreshTotalDays = WatchedAttributes.GetDouble("lastRefreshTotalDays", 0);

                    if (World.Calendar.TotalDays - lastRefreshTotalDays > 14 && tradingWith == null)
                    {
                        RefreshBuyingSellingInventory(0.5f);
                        WatchedAttributes.SetDouble("lastRefreshTotalDays", World.Calendar.TotalDays);
                        tickCount = 1;
                    }
                }
            }

            if (tradingWith != null && (tradingWith.Pos.SquareDistanceTo(this.Pos) > 5 || Inventory.openedByPlayerGUIds.Count == 0))
            {
                dlg?.TryClose();
                tradingWith = null;
            }
        }
Example #5
0
        public override void Initialize(EntityProperties properties, ICoreAPI api, long chunkindex3d)
        {
            base.Initialize(properties, api, chunkindex3d);

            if (Itemstack == null || !Itemstack.ResolveBlockOrItem(World))
            {
                Die();
                this.Itemstack = null;
                return;
            }

            // If attribute was modified and resent to client, make sure we still have the resolved thing in memory
            WatchedAttributes.RegisterModifiedListener("itemstack", () => {
                if (Itemstack != null && Itemstack.Collectible == null)
                {
                    Itemstack.ResolveBlockOrItem(World);
                }
                Slot.Itemstack = Itemstack;
            });

            if (Itemstack.Collectible.Attributes?["gravityFactor"].Exists == true)
            {
                WatchedAttributes.SetDouble("gravityFactor", Itemstack.Collectible.Attributes["gravityFactor"].AsDouble(1));
            }
            if (Itemstack.Collectible.Attributes?["airDragFactor"].Exists == true)
            {
                WatchedAttributes.SetDouble("airDragFactor", Itemstack.Collectible.Attributes["airDragFactor"].AsDouble(1));
            }


            itemSpawnedMilliseconds = World.ElapsedMilliseconds;
            Swimming = FeetInLiquid = World.BlockAccessor.GetBlock(Pos.AsBlockPos).IsLiquid();

            tmpPos.Set(Pos.XInt, Pos.YInt, Pos.ZInt);
            windLoss = World.BlockAccessor.GetDistanceToRainFall(tmpPos) / 4f;
        }