Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved code , stability #78

Merged
merged 1 commit into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Application/RSBot/Views/Controls/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ private void SubscribeEvents()
/// </summary>
private void OnSelectEntity(SpawnedBionic entity)
{

var percent = 100;
lblType.Text = string.Empty;

if (entity is SpawnedPlayer player)
lblEntityName.Text = player.Name;
else
lblEntityName.Text = entity.Record.GetRealName();

var percent = 0;
lblType.Text = string.Empty;

if (entity is SpawnedMonster monster)
{
percent = (monster.Health * 100) / monster.MaxHealth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public void Invoke(Packet packet)

Core.Game.SelectedEntity = entity;

EventManager.FireEvent("OnSelectEntity", entity);

if (entity is SpawnedMonster)
{
var hasHealth = packet.ReadBool();
Expand All @@ -68,6 +66,8 @@ public void Invoke(Packet packet)
entity.Talk.Deserialize(packet);
}
*/

EventManager.FireEvent("OnSelectEntity", entity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void Invoke(Packet packet)
character.StatPoints = packet.ReadUShort();
character.BerzerkPoints = packet.ReadByte();
character.ExperienceChunk = packet.ReadUInt();
character.Health = packet.ReadUInt();
character.Mana = packet.ReadUInt();
character.Health = packet.ReadInt();
character.Mana = packet.ReadInt();
character.AutoInverstExperience = (AutoInverstType)packet.ReadByte();

if (Core.Game.ClientType == GameClientType.Chinese)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ internal class CharacterDataResponse : IPacketHandler
/// <param name="packet">The packet.</param>
public void Invoke(Packet packet)
{
if (Core.Game.CharacterPacket == null) return; //Something went wrong between start and stop!
if (Core.Game.CharacterPacket == null)
return;

Core.Game.CharacterPacket.WriteByteArray(packet.GetBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void Invoke(Packet packet)
}
else if (objChar.TypeID2 == 2 && objChar.TypeID3 == 3 && objChar.TypeID4 == 1)
{
Core.Game.Player.Vehicle = Vehicle.FromPacket(packet, uniqueId, objectId);
var health = packet.ReadInt();

Core.Game.Player.Vehicle = new Vehicle(uniqueId, objectId, health);
EventManager.FireEvent("OnSummonVehicle");
Log.Debug("Mount vehicle");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ private static void UpdatePlayerStatus(Packet packet, EntityUpdateStatusFlag upd
{
if ((updateFlag & EntityUpdateStatusFlag.HP) == EntityUpdateStatusFlag.HP)
{
Core.Game.Player.Health = packet.ReadUInt();
Core.Game.Player.Health = packet.ReadInt();
EventManager.FireEvent("OnUpdateHP");
}

if ((updateFlag & EntityUpdateStatusFlag.MP) == EntityUpdateStatusFlag.MP)
{
Core.Game.Player.Mana = packet.ReadUInt();
Core.Game.Player.Mana = packet.ReadInt();
EventManager.FireEvent("OnUpdateMP");
}

Expand Down Expand Up @@ -98,7 +98,7 @@ private static void UpdatePlayerStatus(Packet packet, EntityUpdateStatusFlag upd
private static void UpdatePetStatus(Packet packet, EntityUpdateStatusFlag updateFlag)
{
if ((updateFlag & EntityUpdateStatusFlag.HP) == EntityUpdateStatusFlag.HP)
Core.Game.Player.AttackPet.Health = packet.ReadUInt();
Core.Game.Player.AttackPet.Health = packet.ReadInt();

if ((updateFlag & EntityUpdateStatusFlag.MP) == EntityUpdateStatusFlag.MP)
packet.ReadUInt();
Expand Down Expand Up @@ -144,7 +144,7 @@ private static void UpdatePetStatus(Packet packet, EntityUpdateStatusFlag update
private static void UpdateVehicleStatus(Packet packet, EntityUpdateStatusFlag updateFlag)
{
if ((updateFlag & EntityUpdateStatusFlag.HP) == EntityUpdateStatusFlag.HP)
Core.Game.Player.Vehicle.Health = packet.ReadUInt();
Core.Game.Player.Vehicle.Health = packet.ReadInt();

if ((updateFlag & EntityUpdateStatusFlag.MP) == EntityUpdateStatusFlag.MP)
packet.ReadUInt();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
using RSBot.Core.Client.ReferenceObjects;
using RSBot.Core.Network;
using RSBot.Core.Objects.Item;
using RSBot.Core.Network;
using RSBot.Core.Objects.Spawn;
using System.Collections.Generic;
using System.Linq;

namespace RSBot.Core.Objects
{
public class AbilityPet
public class AbilityPet : SpawnedEntity
{
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
public uint UniqueId { get; set; }

/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public uint Id { get; set; }

/// <summary>
/// Gets the record.
/// </summary>
/// <value>
/// The record.
/// </value>
public RefObjChar Record => Game.ReferenceManager.GetRefObjChar(Id);

/// <summary>
/// Gets or sets the name.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,19 @@
using RSBot.Core.Event;
using RSBot.Core.Network;
using RSBot.Core.Objects.Spawn;
using System;
using System.Linq;
using System.Timers;

namespace RSBot.Core.Objects
{
public class AttackPet
public class AttackPet : SpawnedBionic
{
#region Fields

private Timer _hungerTimer;

#endregion Fields

/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
public uint UniqueId { get; set; }

/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public uint Id { get; set; }

/// <summary>
/// Gets the record.
/// </summary>
/// <value>
/// The record.
/// </value>
public RefObjChar Record => Game.ReferenceManager.GetRefObjChar(Id);

/// <summary>
/// Gets or sets the current hp.
/// <inheritdoc/>
/// </summary>
/// <value>
/// The current hp.
/// </value>
public uint Health { get; set; }
/// <param name="objId"></param>
public AttackPet(uint objId) : base(objId) {}

/// <summary>
/// Gets or sets the experience.
Expand Down Expand Up @@ -102,7 +71,7 @@ public class AttackPet
/// <value>
/// The maximum health.
/// </value>
public uint MaxHealth { get; set; }
public int MaxHealth { get; set; }

/// <summary>
/// Gets or sets the bionic.
Expand All @@ -113,12 +82,9 @@ public class AttackPet
public SpawnedBionic Bionic => SpawnManager.GetEntity<SpawnedBionic>(UniqueId);

/// <summary>
/// Gets or sets the bad effect.
/// Hunger last tick
/// </summary>
/// <value>
/// The bad effect.
/// </value>
public BadEffect BadEffect { get; set; }
private int _lastHungerTick;

/// <summary>
/// Froms the packet.
Expand All @@ -129,14 +95,14 @@ public class AttackPet
/// <returns></returns>
internal static AttackPet FromPacket(Packet packet, uint uniqueId, uint id)
{
var result = new AttackPet
var result = new AttackPet(id)
{
UniqueId = uniqueId,
Id = id,
Health = packet.ReadUInt()
Health = packet.ReadInt()
};

var maxHealth = packet.ReadUInt(); //UNKNOWN
var maxHealth = packet.ReadInt(); //UNKNOWN
result.MaxHealth = maxHealth != 0 ? maxHealth : result.Health;

result.Experience = packet.ReadULong();
Expand All @@ -145,7 +111,6 @@ internal static AttackPet FromPacket(Packet packet, uint uniqueId, uint id)
packet.ReadInt(); //UNKNOWN (maybe control?)
result.Name = packet.ReadString();

result.StartHungerTimer();
return result;
}

Expand All @@ -161,27 +126,6 @@ public void Terminate()
PacketManager.SendPacket(packet, PacketDestination.Server);
}

/// <summary>
/// Starts the hunger timer.
/// </summary>
private void StartHungerTimer()
{
_hungerTimer = new Timer(3000);
_hungerTimer.Elapsed += HungerTimer_Elapsed;
_hungerTimer.Start();
}

/// <summary>
/// Handles the Elapsed event of the HungerTimer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
private void HungerTimer_Elapsed(object sender, ElapsedEventArgs e)
{
CurrentHungerPoints--;
EventManager.FireEvent("OnAttackPetHungerUpdate");
}

/// <summary>
/// Uses the health potion.
/// </summary>
Expand Down Expand Up @@ -247,5 +191,19 @@ public bool UseHungerPotion()

return true;
}

/// <summary>
/// Update the entity
/// </summary>
public override bool Update()
{
if(Environment.TickCount - _lastHungerTick > 3000)
{
CurrentHungerPoints--;
EventManager.FireEvent("OnAttackPetHungerUpdate");
}

return base.Update();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
using RSBot.Core.Client.ReferenceObjects;
using RSBot.Core.Network;
using RSBot.Core.Network;
using RSBot.Core.Objects.Spawn;
using System;
using System.Linq;
using System.Threading;

namespace RSBot.Core.Objects
{
public class Vehicle : SpawnedEntity
public class Vehicle : SpawnedBionic
{
/// <summary>
/// Gets or sets the current health.
/// <inheritdoc/>
/// </summary>
/// <value>
/// The current health.
/// </value>
public uint Health { get; set; }

/// <summary>
/// Gets or sets the bad effect.
/// </summary>
/// <value>
/// The bad effect.
/// </value>
public BadEffect BadEffect { get; set; }

/// <summary>
/// Froms the packet.
/// </summary>
/// <param name="packet">The packet.</param>
/// <param name="uniqueId">The unique identifier.</param>
/// <param name="id">The identifier.</param>
/// <returns></returns>
internal static Vehicle FromPacket(Packet packet, uint uniqueId, uint id)
/// <param name="objId">The entity model id</param>
/// <param name="uniqueId">The entity unique id</param>
/// <param name="health">The current health</param>
public Vehicle(uint objId, uint uniqueId, int health)
: base(objId)
{
return new Vehicle
{
UniqueId = uniqueId,
Id = id,
Health = packet.ReadUInt()
};
UniqueId = uniqueId;
Health = health;
}

/// <summary>
Expand Down
18 changes: 1 addition & 17 deletions Library/RSBot.Core/Objects/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,13 @@ public class Player : SpawnedBionic
/// </value>
public uint ExperienceChunk { get; set; }

/// <summary>
/// Gets or sets the health.
/// </summary>
/// <value>
/// The health.
/// </value>
public uint Health { get; set; }

/// <summary>
/// Gets or sets the mana.
/// </summary>
/// <value>
/// The mana.
/// </value>
public uint Mana { get; set; }
public int Mana { get; set; }

/// <summary>
/// Gets or sets the automatic inverst experience.
Expand Down Expand Up @@ -420,14 +412,6 @@ public class Player : SpawnedBionic
/// </value>
public bool InAction { get; set; }

/// <summary>
/// Gets or sets the bad effect.
/// </summary>
/// <value>
/// The bad effect.
/// </value>
public BadEffect BadEffect { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this <see cref="Player"/> is untouchable.
/// Will be set automatically after telportation.
Expand Down
Loading