Skip to content

Commit

Permalink
Add quest plugin (#730)
Browse files Browse the repository at this point in the history
Co-authored-by: Niklas G <niegoedde@gmail.com>
  • Loading branch information
ngoedde and Niklas G authored Oct 19, 2023
1 parent a65fd15 commit 288a66f
Show file tree
Hide file tree
Showing 34 changed files with 1,862 additions and 123 deletions.
20 changes: 19 additions & 1 deletion Application/RSBot/Views/Main.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 33 additions & 15 deletions Application/RSBot/Views/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using SDUI.Controls;
using SDUI.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
Expand All @@ -34,6 +35,8 @@ public partial class Main : UIWindow
/// </summary>
private string _playerName;


private Dictionary<string, UIWindow> _pluginWindows = new(8);
#endregion Members

#region Constructor
Expand Down Expand Up @@ -112,6 +115,12 @@ private void RegisterEvents()
EventManager.SubscribeEvent("OnStopBot", OnStopBot);
EventManager.SubscribeEvent("OnAgentServerDisconnected", OnAgentServerDisconnected);
EventManager.SubscribeEvent("OnShowScriptRecorder", new Action<int, bool>(OnShowScriptRecorder));
EventManager.SubscribeEvent("OnAddSidebarElement", new Action<Control>(OnAddSidebarElement));
}

private void OnAddSidebarElement(Control obj)
{
pSidebarCustom.Controls.Add(obj);
}

private void OnShowScriptRecorder(int ownerId, bool startRecording)
Expand Down Expand Up @@ -297,25 +306,34 @@ private void PluginMenuItem_Click(object sender, EventArgs e)
var menuItem = (ToolStripMenuItem)sender;
var plugin = (IPlugin)menuItem.Tag;

var window = new UIWindow
if (!_pluginWindows.TryGetValue(plugin.InternalName, out var pluginWindow) || pluginWindow.IsDisposed)
{
Text = plugin.DisplayName,
Name = plugin.InternalName,
MaximizeBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog,
Icon = Icon,
StartPosition = FormStartPosition.CenterParent,
ShowTitle = true
};
pluginWindow = new UIWindow
{
Text = plugin.DisplayName,
Name = plugin.InternalName,
MaximizeBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog,
Icon = Icon,
StartPosition = FormStartPosition.CenterParent,
ShowTitle = true
};

var content = plugin.View;
content.Dock = DockStyle.Fill;

plugin.Translate();

var content = plugin.View;
content.Dock = DockStyle.Fill;
pluginWindow.Size = new Size(content.Size.Width + 16, content.Size.Height + 32);
pluginWindow.Controls.Add(content);

_pluginWindows[plugin.InternalName] = pluginWindow;
}

plugin.Translate();
if (!pluginWindow.Visible)
pluginWindow.Show();

window.Size = new Size(content.Size.Width + 16, content.Size.Height + 32);
window.Controls.Add(content);
window.Show();
pluginWindow.BringToFront();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Library/RSBot.Core/Client/DivisionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal static DivisionInfo Load()
Name = reader.ReadJoymaxString(),
GatewayServers = new List<string>()
};
reader.ReadByte(); //Null terminator for CharacterName
reader.ReadByte(); //Null terminator for NameStrID

var gatewayCount = reader.ReadByte();

Expand Down
34 changes: 33 additions & 1 deletion Library/RSBot.Core/Client/ReferenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class ReferenceManager
public List<RefTeleport> TeleportData { get; } = new(256);
public List<RefTeleportLink> TeleportLinks { get; } = new(256);
public Dictionary<int, RefOptionalTeleport> OptionalTeleports { get; } = new(32);
public Dictionary<uint, RefQuestReward> QuestRewards { get; } = new(1024);
public List<RefQuestRewardItem> QuestRewardItems { get; } = new(1024);
public GatewayInfo GatewayInfo { get; private set; }
public DivisionInfo DivisionInfo { get; private set; }
public VersionInfo VersionInfo { get; private set; }
Expand Down Expand Up @@ -80,9 +82,16 @@ public void Load(int languageTab)
() => LoadScrapOfPackageItemData("RefScrapOfPackageItem.txt"),
() => LoadReferenceFile("magicoption.txt", MagicOptions),
() => LoadReferenceFile("magicoptionassign.txt", MagicOptionAssignments),
() => LoadReferenceFile("refoptionalteleport.txt", OptionalTeleports)
() => LoadReferenceFile("refoptionalteleport.txt", OptionalTeleports),
() => LoadReferenceFile("refquestrewarditems.txt", QuestRewardItems),
() => LoadReferenceFile("refqusetreward.txt", QuestRewards)
);

if (Game.ClientType > GameClientType.Chinese)
LoadConditionalData("QuestData.txt", QuestData);
else
LoadReferenceFile("questdata.txt", QuestData);

if(Game.ClientType > GameClientType.Japanese_Old)
{
LoadReferenceFile("refabilitybyitemoptleveldata.txt", AbilityItemByOptLevel);
Expand Down Expand Up @@ -657,6 +666,7 @@ public RefMagicOpt GetMagicOption(string group, byte degree)
return MagicOptions?.FirstOrDefault(m => m.Group == group && m.Level == degree);
}


/// <summary>
/// Gets a list of magic options for the specified type ids
/// </summary>
Expand Down Expand Up @@ -689,5 +699,27 @@ public IEnumerable<RefExtraAbilityByEquipItemOptLevel> GetExtraAbilityItems(uint
{
return ExtraAbilityByEquipItemOptLevel.Where(p => p.ItemId == itemId && p.OptLevel == optLevel);
}

/// <summary>
/// Gets a list of reward items for the specified quest.
/// </summary>
/// <param name="questId"></param>
/// <returns></returns>
public IEnumerable<RefQuestRewardItem> GetQuestRewardItems(uint questId)
{
return QuestRewardItems.Where(r => r.QuestId == questId);
}

/// <summary>
/// Returns the reward for the specified quest
/// </summary>
/// <param name="questId"></param>
/// <returns></returns>
public RefQuestReward GetQuestReward(uint questId)
{
QuestRewards.TryGetValue(questId, out var result);

return result;
}
}
}
13 changes: 12 additions & 1 deletion Library/RSBot.Core/Client/ReferenceObjects/RefQuest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace RSBot.Core.Client.ReferenceObjects
using System.Collections.Generic;

namespace RSBot.Core.Client.ReferenceObjects
{
public class RefQuest : IReference<uint>
{
#region Properties

public RefQuestReward Reward => Game.ReferenceManager.GetQuestReward(ID);
public IEnumerable<RefQuestRewardItem> RewardItems => Game.ReferenceManager.GetQuestRewardItems(ID);

#endregion

#region Fields

public byte Service;
Expand Down Expand Up @@ -45,6 +54,8 @@ public bool Load(ReferenceParser parser)

return true;
}

public string GetTranslatedName() => Game.ReferenceManager.GetTranslation(NameString);
}
}

Expand Down
60 changes: 60 additions & 0 deletions Library/RSBot.Core/Client/ReferenceObjects/RefQuestReward.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace RSBot.Core.Client.ReferenceObjects
{
public class RefQuestReward : IReference<uint>
{
#region Fields

public uint QuestId;
public string QuestCodeName;
public bool IsView;
public bool IsBasicReward;
public bool IsItemReward;
public bool IsCheckCondition;
public bool IsCheckCountry;
public bool IsCheckClass;
public bool IsCheckGender;
public int Gold;
public int Exp;
public int SPExp;
public int SP;
public int AP;
public string APType;
public byte Hwan;
public byte InventorySlots;
public byte ItemRewardType;
public byte SelectionCount;

#endregion Fields

public uint PrimaryKey => QuestId;

public bool Load(ReferenceParser parser)
{
parser.TryParse(0, out QuestId);
parser.TryParse(1, out QuestCodeName);
parser.TryParse(2, out IsView);
parser.TryParse(3, out IsBasicReward);
parser.TryParse(4, out IsItemReward);
parser.TryParse(5, out IsCheckCondition);
parser.TryParse(6, out IsCheckCountry);
parser.TryParse(7, out IsCheckClass);
parser.TryParse(8, out IsCheckGender);

//9?!

parser.TryParse(10, out Gold);
parser.TryParse(11, out Exp);
parser.TryParse(12, out SPExp);
parser.TryParse(13, out SP);
parser.TryParse(14, out AP);
parser.TryParse(15, out APType);
parser.TryParse(16, out Hwan);
parser.TryParse(17, out InventorySlots);
parser.TryParse(18, out ItemRewardType);
parser.TryParse(19, out SelectionCount);

return true;
}
}
}

38 changes: 38 additions & 0 deletions Library/RSBot.Core/Client/ReferenceObjects/RefQuestRewardItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace RSBot.Core.Client.ReferenceObjects
{
public class RefQuestRewardItem : IReference
{
#region Fields

public uint QuestId;
public string QuestCodeName;
public byte RewardType;
public string ItemCodeName;
public string OptionalItemCode;
public int OptionalItemCount;
public int AchieveQuantity;
public string RentItemCodeName;

public RefObjItem Item => ItemCodeName == "xxx" ? null : Game.ReferenceManager.GetRefItem(ItemCodeName);

public RefObjItem OptionalItem => OptionalItemCode == "xxx" ? null : Game.ReferenceManager.GetRefItem(OptionalItemCode);
public RefObjItem RentItem => OptionalItemCode == "xxx" ? null : Game.ReferenceManager.GetRefItem(RentItemCodeName);

#endregion Fields

public bool Load(ReferenceParser parser)
{
parser.TryParse(0, out QuestId);
parser.TryParse(1, out QuestCodeName);
parser.TryParse(2, out RewardType);
parser.TryParse(3, out ItemCodeName);
parser.TryParse(4, out OptionalItemCode);
parser.TryParse(5, out OptionalItemCount);
parser.TryParse(6, out AchieveQuantity);
parser.TryParse(7, out RentItemCodeName);

return true;
}
}
}

44 changes: 44 additions & 0 deletions Library/RSBot.Core/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Text;

namespace RSBot.Core.Extensions
{
public static class StringExtension
{
#region Methods

public static string JoymaxFormat<T1>(this string unformatted, T1 valA)
{
var stringBulder = new StringBuilder(unformatted);
stringBulder = stringBulder.Replace("%d", "%s");

stringBulder = stringBulder.Replace("%s", valA.ToString(), 0, stringBulder.ToString().IndexOf("%s") + 2);

return stringBulder.ToString();
}

public static string JoymaxFormat<T1, T2>(this string unformatted, T1 valA, T2 valB)
{
var stringBulder = new StringBuilder(unformatted);
stringBulder = stringBulder.Replace("%d", "%s");

stringBulder = stringBulder.Replace("%s", valA.ToString(), 0, stringBulder.ToString().IndexOf("%s") + 2);
stringBulder = stringBulder.Replace("%s", valB.ToString(), 0, stringBulder.ToString().IndexOf("%s") + 2);

return stringBulder.ToString();
}

public static string JoymaxFormat<T1, T2, T3>(this string unformatted, T1 valA, T2 valB, T3 valC)
{
var stringBulder = new StringBuilder(unformatted);
stringBulder = stringBulder.Replace("%d", "%s");

stringBulder = stringBulder.Replace("%s", valA.ToString(), 0, stringBulder.ToString().IndexOf("%s") + 2);
stringBulder = stringBulder.Replace("%s", valB.ToString(), 0, stringBulder.ToString().IndexOf("%s") + 2);
stringBulder = stringBulder.Replace("%s", valC.ToString(), 0, stringBulder.ToString().IndexOf("%s") + 2);

return stringBulder.ToString();
}

#endregion Methods
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using RSBot.Core.Components;
using RSBot.Core.Event;
using RSBot.Core.Objects;
using RSBot.Core.Objects.Spawn;

namespace RSBot.Core.Network.Handler.Agent.Action
{
Expand Down Expand Up @@ -41,6 +40,7 @@ public void Invoke(Packet packet)
Game.Player.State.DialogState = new DialogState
{
RequestedNpcId = entityId,
TalkOption = option
};

EventManager.FireEvent("OnTalkRequest", entityId, option);
Expand Down
Loading

0 comments on commit 288a66f

Please sign in to comment.