Example #1
0
        private static void Heal(ConnectionType type, int index, byte[] data)
        {
            ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer();
            buffer.WriteBytes(data);
            ReadHeader(ref buffer);
            if (DateTime.Now > World.instance.players[Network.instance.Clients[index].Character_ID].HealCooldownTime)
            {
                int Healed = 0;
                if (World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP + World.HealAmount > World.instance.players[Network.instance.Clients[index].Character_ID].Max_HP)
                {
                    int Current_HP = World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP;
                    int Max_HP     = World.instance.players[Network.instance.Clients[index].Character_ID].Max_HP;
                    Healed = Max_HP - Current_HP;
                    World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP = World.instance.players[Network.instance.Clients[index].Character_ID].Max_HP;
                }
                else
                {
                    World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP += World.HealAmount;
                    Healed = World.HealAmount;
                }

                SendData.Heal(index, World.instance.players[Network.instance.Clients[index].Character_ID].Current_HP, Healed);
                World.instance.players[Network.instance.Clients[index].Character_ID].HealCooldownTime = DateTime.Now.AddSeconds(World.SpellCooldown);
            }
        }
Example #2
0
        public void Start()
        {
            Log.log("World Loop Thread starting..", Log.LogType.SYSTEM);
            // Spawn all of the NPC's and Collectables
            int i_npc = 0;

            NPCsInWorld.Clear();
            int i_col = 0;

            collectablesInWorld = new ConcurrentBag <Collectable>();
            // Spawn all NPC's
            foreach (KeyValuePair <int, Spawn> spawn in spawns)
            {
                if (!spawn.Value.InUse)
                {
                    if (spawn.Value.NPC_ID > -1)
                    {
                        NPCsInWorld.Add
                        (
                            new NPC
                            (
                                NPCs[spawn.Value.NPC_ID].NPC_ID,
                                NPCs[spawn.Value.NPC_ID].Status,
                                NPCs[spawn.Value.NPC_ID].Name,
                                NPCs[spawn.Value.NPC_ID].Respawn_Time,
                                NPCs[spawn.Value.NPC_ID].Level,
                                NPCs[spawn.Value.NPC_ID].gender,
                                NPCs[spawn.Value.NPC_ID].Max_HP,
                                NPCs[spawn.Value.NPC_ID].Strength,
                                NPCs[spawn.Value.NPC_ID].Agility,
                                NPCs[spawn.Value.NPC_ID].Experience
                            )
                        );
                        NPCsInWorld[i_npc].position.x = spawn.Value.Pos_X;
                        NPCsInWorld[i_npc].position.y = spawn.Value.Pos_Y;
                        NPCsInWorld[i_npc].position.z = spawn.Value.Pos_Z;
                        NPCsInWorld[i_npc].r          = spawn.Value.Rotation_Y;
                        NPCsInWorld[i_npc].Spawn_ID   = spawn.Key;
                        ++i_npc;
                    }
                    else if (spawn.Value.Collectable_ID > -1)
                    {
                        collectablesInWorld.Add
                        (
                            new Collectable
                            (
                                collectables[spawn.Value.Collectable_ID].Collectable_ID,
                                collectables[spawn.Value.Collectable_ID].Name,
                                collectables[spawn.Value.Collectable_ID].Respawn_Time,
                                spawn.Value.Pos_X,
                                spawn.Value.Pos_Y,
                                spawn.Value.Pos_Z,
                                spawn.Value.Rotation_Y
                            )
                        );
                        collectablesInWorld.Last().Spawn_ID = spawn.Key;
                        ++i_col;
                    }
                }
            }
            Running = true;
            // Main loop
            NextTick = DateTime.Now.AddSeconds(5);
            while (Running)
            {
                float dt = MathF.deltaTime;
                try
                {
                    foreach (NPC npc in NPCsInWorld)
                    {
                        if (npc.Active && npc.Current_HP <= 0)
                        {
                            if (npc.TargetID > -1)
                            {
                                players[npc.TargetID].InCombat = false;
                            }
                            npc.InCombat = false;
                            npc.TargetID = -1;
                            foreach (int id in npc.PlayerCredit)
                            {
                                if (players.ContainsKey(id))
                                {
                                    players[id].experience += npc.Experience;
                                }
                            }
                            npc.PlayerCredit = new ConcurrentBag <int>();
                            npc.r            = spawns[npc.Spawn_ID].Rotation_Y;
                            npc.TargetType   = EntityType.NONE;
                            npc.Active       = false;
                        }
                        else if (npc.TargetID > -1 && npc.Current_HP > 0 && npc.Active)
                        {
                            if (npc.InCombat && players[npc.TargetID].InWorld && MathF.Distance(npc, players[npc.TargetID]) <= ChaseDistance && MathF.Distance(npc, npc.spawn) <= ResetDistance)
                            {
                                if (DateTime.Now >= npc.NextAttack && npc.TargetID > 0 &&
                                    players[npc.TargetID].Current_HP > 0)
                                {
                                    bool Crit   = false;
                                    int  Damage = MathF.Damage(npc.Strength, npc.Agility, npc.BloodMultiplier, out Crit);
                                    players[npc.TargetID].Current_HP -= Damage;
                                    SendData.Attacked(Network.instance.GetIndex(npc.TargetID), players[npc.TargetID].Current_HP, Damage);
                                    npc.NextAttack = DateTime.Now.AddSeconds(GlobalAttackSpeed);
                                    MathF.LookAt(npc, players[npc.TargetID]);
                                }
                            }
                            else if (npc.InCombat && players[npc.TargetID].InWorld && MathF.Distance(npc, players[npc.TargetID]) > ChaseDistance && MathF.Distance(npc, npc.spawn) <= ResetDistance)
                            {
                                float step = MovementSpeed * dt;
                                MathF.MoveTowards(npc, players[npc.TargetID], step);
                            }
                            else if (!players[npc.TargetID].InWorld && npc.InCombat && (MathF.Distance(npc, players[npc.TargetID]) > ResetDistance || MathF.Distance(npc, npc.spawn) > ResetDistance))
                            {
                                npc.PlayerCredit = new ConcurrentBag <int>();
                                players[npc.TargetID].InCombat = false;
                                npc.InCombat   = false;
                                npc.r          = spawns[npc.Spawn_ID].Rotation_Y;
                                npc.TargetID   = -1;
                                npc.TargetType = EntityType.NONE;
                                float step = MovementSpeed * dt;
                                MathF.MoveTowards(npc, npc.spawn, step);
                            }
                            else
                            {
                                npc.PlayerCredit = new ConcurrentBag <int>();
                                players[npc.TargetID].InCombat = false;
                                npc.InCombat   = false;
                                npc.r          = spawns[npc.Spawn_ID].Rotation_Y;
                                npc.TargetID   = -1;
                                npc.TargetType = EntityType.NONE;
                                float step = MovementSpeed * dt;
                                MathF.MoveTowards(npc, npc.spawn, step);
                            }
                        }
                        else if (npc.TargetID == -1 && npc.Active)
                        {
                            npc.PlayerCredit = new ConcurrentBag <int>();
                            npc.InCombat     = false;
                            npc.r            = spawns[npc.Spawn_ID].Rotation_Y;
                            npc.TargetID     = -1;
                            npc.TargetType   = EntityType.NONE;
                            float step = MovementSpeed * dt;
                            MathF.MoveTowards(npc, npc.spawn, step);
                        }
                    }
                    if (DateTime.Now >= NextTick)
                    {
                        foreach (NPC npc in NPCsInWorld)
                        {
                            if (npc.Active && npc.Current_HP > 0)
                            {
                                if (npc.Current_HP + HealthRegenPer5 > npc.Max_HP)
                                {
                                    npc.Current_HP = npc.Max_HP;
                                }
                                else
                                {
                                    npc.Current_HP += HealthRegenPer5;
                                }
                            }
                        }
                        foreach (Player player in playersInWorld)
                        {
                            if (player.Current_HP + HealthRegenPer5 > player.Max_HP)
                            {
                                player.Current_HP = player.Max_HP;
                            }
                            else
                            {
                                player.Current_HP += HealthRegenPer5;
                                SendData.Heal(Network.instance.GetIndex(player.Character_ID), player.Current_HP, HealAmount);
                            }
                            if (player.InCombat && MathF.Distance(player, GetNPCByEntityID(player.TargetID)) > InteractionDistance)
                            {
                                player.InCombat            = false;
                                player.AnimState.Attacking = false;
                            }
                        }
                        NextTick = DateTime.Now.AddSeconds(5);
                    }
                    foreach (Player p in playersInWorld)
                    {
                        if (DateTime.Now > p.BloodExpire)
                        {
                            p.BloodMultiplier = 1;
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.log("An error was caught during the world loop: " + e.Message);
                }
                Thread.Sleep(TickRate);
            }
            UpdateThread.Join();
        }