Skip to content

Commit

Permalink
Stonetrap rework and various issues (#817)
Browse files Browse the repository at this point in the history
* Added delay of 100ms for socket error in connection.  This gave time for Disconnect to gracefully dispose of TcpClient and therefore unbind socket.
Added MaxAttempts int (default 20)
When MaxAttempts reached, message box will appear and no more attempts will be made.

* blink and slashingburst attack lock fix

* Stonetrap now taunts enemies which matches official.
Only one Stonetrap pet allowed (chat message will appear to tell you this).  Stonetrap will not teleport in recast.
Duration time fixed to magic level * 5 + 10  in seconds.
MapObject now supports Taunter. This check is done in Process() and sets target.  Note: Taunt swap is not instant. Slight delay due to delayed actions.

* Remove taunt feature
Logic handled in ProcessAI for StoneTrap and FindTarget for Monster

* Remove all traces of player bag weight check.
Hero weight checks still in place to prevent bag abuse.

* Fixed issue with looping music not working.
  • Loading branch information
grimchamp authored May 17, 2023
1 parent 6c4327a commit 72aa7fd
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 277 deletions.
35 changes: 0 additions & 35 deletions Client/MirControls/MirItemCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,13 +1031,6 @@ private void MoveItem()
}
}

if (GameScene.SelectedCell.Item.Weight + MapObject.User.CurrentBagWeight > MapObject.User.Stats[Stat.BagWeight])
{
GameScene.Scene.ChatDialog.ReceiveChat("Too heavy to get back.", ChatType.System);
GameScene.SelectedCell = null;
return;
}

if (Item != null)
{
if (GameScene.SelectedCell.Item.Info == Item.Info && Item.Count < Item.Info.StackSize)
Expand Down Expand Up @@ -1109,13 +1102,6 @@ private void MoveItem()
}
}

if (GameScene.SelectedCell.Item.Weight + MapObject.User.CurrentBagWeight > MapObject.User.Stats[Stat.BagWeight])
{
GameScene.Scene.ChatDialog.ReceiveChat("Too heavy to get back.", ChatType.System);
GameScene.SelectedCell = null;
return;
}

if (Item != null)
{
if (GameScene.SelectedCell.Item.Info == Item.Info && Item.Count < Item.Info.StackSize)
Expand Down Expand Up @@ -1181,13 +1167,6 @@ private void MoveItem()
}
}

if (GameScene.SelectedCell.Item.Weight + MapObject.User.CurrentBagWeight > MapObject.User.Stats[Stat.BagWeight])
{
GameScene.Scene.ChatDialog.ReceiveChat("Too heavy to get back.", ChatType.System);
GameScene.SelectedCell = null;
return;
}

if (Item != null)
{
if (GameScene.SelectedCell.Item.Info == Item.Info && Item.Count < Item.Info.StackSize)
Expand Down Expand Up @@ -1236,13 +1215,6 @@ private void MoveItem()
return;
}

if (GameScene.SelectedCell.Item.Weight + MapObject.User.CurrentBagWeight > MapObject.User.Stats[Stat.BagWeight])
{
GameScene.Scene.ChatDialog.ReceiveChat("Too heavy to get back.", ChatType.System);
GameScene.SelectedCell = null;
return;
}

if (Item == null)
{
Network.Enqueue(new C.RetrieveRentalItem { From = GameScene.SelectedCell.ItemSlot, To = ItemSlot });
Expand Down Expand Up @@ -1272,13 +1244,6 @@ private void MoveItem()
}
}

if (GameScene.SelectedCell.Item.Weight + MapObject.User.CurrentBagWeight > MapObject.User.Stats[Stat.BagWeight])
{
GameScene.Scene.ChatDialog.ReceiveChat("Too heavy to transfer.", ChatType.System);
GameScene.SelectedCell = null;
return;
}

if (Item != null)
{
if (GameScene.SelectedCell.Item.Info == Item.Info && Item.Count < Item.Info.StackSize)
Expand Down
43 changes: 35 additions & 8 deletions Client/MirNetwork/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ static class Network
{
private static TcpClient _client;
public static int ConnectAttempt = 0;
public static int MaxAttempts = 20;
public static bool ErrorShown;
public static bool Connected;
public static long TimeOutTime, TimeConnected, RetryTime = CMain.Time + 5000;

Expand All @@ -24,20 +26,46 @@ public static void Connect()
if (_client != null)
Disconnect();

ConnectAttempt++;
if (ConnectAttempt >= MaxAttempts)
{
if (ErrorShown)
{
return;
}

_client = new TcpClient {NoDelay = true};
_client.BeginConnect(Settings.IPAddress, Settings.Port, Connection, null);
ErrorShown = true;

MirMessageBox errorBox = new("Error Connecting to Server", MirMessageBoxButtons.Cancel);
errorBox.CancelButton.Click += (o, e) => Program.Form.Close();
errorBox.Label.Text = $"Maximum Connection Attempts Reached: {MaxAttempts}" +
$"{Environment.NewLine}Please try again later or check your connection settings.";
errorBox.Show();
return;
}

ConnectAttempt++;

try
{
_client = new TcpClient { NoDelay = true };
_client?.BeginConnect(Settings.IPAddress, Settings.Port, Connection, null);
}
catch (ObjectDisposedException ex)
{
if (Settings.LogErrors) CMain.SaveError(ex.ToString());
Disconnect();
}
}

private static void Connection(IAsyncResult result)
{
try
{
_client.EndConnect(result);
_client?.EndConnect(result);

if (!_client.Connected)
if ((_client != null &&
!_client.Connected) ||
_client == null)
{
Connect();
return;
Expand All @@ -50,11 +78,11 @@ private static void Connection(IAsyncResult result)
TimeOutTime = CMain.Time + Settings.TimeOut;
TimeConnected = CMain.Time;


BeginReceive();
}
catch (SocketException)
{
Thread.Sleep(100);
Connect();
}
catch (Exception ex)
Expand Down Expand Up @@ -142,12 +170,11 @@ private static void SendData(IAsyncResult result)
{ }
}


public static void Disconnect()
{
if (_client == null) return;

_client.Close();
_client?.Close();

TimeConnected = 0;
Connected = false;
Expand Down
16 changes: 12 additions & 4 deletions Client/MirObjects/MapObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,19 @@ private void RestoreTargetStates()
if (MagicObjectID == ObjectID)
MagicObject = this;

/*if (TargetObject == null)
if (!this.Dead &&
TargetObject == null &&
LastTargetObjectId == ObjectID)
{
if (lastTargetObjectId == ObjectID)
TargetObject = this;
}*/
switch (Race)
{
case ObjectType.Player:
case ObjectType.Monster:
case ObjectType.Hero:
TargetObject = this;
break;
}
}
}

public void AddBuffEffect(BuffType type)
Expand Down
23 changes: 6 additions & 17 deletions Client/MirObjects/UserObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,16 +716,9 @@ public ClientMagic GetMagic(Spell spell)

public void GetMaxGain(UserItem item)
{
if (CurrentBagWeight + item.Weight <= Stats[Stat.BagWeight] && FreeSpace(Inventory) > 0) return;

ushort min = 0;
ushort max = item.Count;

if (CurrentBagWeight >= Stats[Stat.BagWeight])
{

}

if (item.Info.Type == ItemType.Amulet)
{
for (int i = 0; i < Inventory.Length; i++)
Expand All @@ -748,9 +741,9 @@ public void GetMaxGain(UserItem item)
}
}

if (min == 0)
if (min == 0 && FreeSpace(Inventory) == 0)
{
GameScene.Scene.ChatDialog.ReceiveChat(FreeSpace(Inventory) == 0 ? GameLanguage.NoBagSpace : "You do not have enough weight.", ChatType.System);
GameScene.Scene.ChatDialog.ReceiveChat(GameLanguage.NoBagSpace, ChatType.System);

item.Count = 0;
return;
Expand All @@ -760,15 +753,11 @@ public void GetMaxGain(UserItem item)
return;
}

if (CurrentBagWeight + item.Weight > Stats[Stat.BagWeight])
if (FreeSpace(Inventory) == 0)
{
item.Count = (ushort)(Math.Max((Stats[Stat.BagWeight] - CurrentBagWeight), ushort.MinValue) / item.Info.Weight);
max = item.Count;
if (item.Count == 0)
{
GameScene.Scene.ChatDialog.ReceiveChat("You do not have enough weight.", ChatType.System);
return;
}
GameScene.Scene.ChatDialog.ReceiveChat(GameLanguage.NoBagSpace, ChatType.System);
item.Count = 0;
return;
}

if (item.Info.StackSize > 1)
Expand Down
12 changes: 0 additions & 12 deletions Client/MirScenes/Dialogs/NPCDialogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,6 @@ private void BuyItem()
return;
}

if (SelectedItem.Weight > (MapObject.User.Stats[Stat.BagWeight] - MapObject.User.CurrentBagWeight))
{
GameScene.Scene.ChatDialog.ReceiveChat("You do not have enough weight.", ChatType.System);
return;
}

for (int i = 0; i < MapObject.User.Inventory.Length; i++)
{
if (MapObject.User.Inventory[i] == null) break;
Expand Down Expand Up @@ -1979,12 +1973,6 @@ private void CraftItem()
//TODO - Check Max slots spare against slots to be used (stacksize/quantity)
//TODO - GetMaxItemGain

if (RecipeItem.Weight > (MapObject.User.Stats[Stat.BagWeight] - MapObject.User.CurrentBagWeight))
{
GameScene.Scene.ChatDialog.ReceiveChat("You do not have enough weight.", ChatType.System);
return;
}

if (max == 1)
{
if (Recipe.Gold > GameScene.Gold)
Expand Down
6 changes: 1 addition & 5 deletions Client/MirScenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10403,11 +10403,7 @@ public void LoadMap()
{
if (SetMusic != Music)
{
if (SoundManager.Music != null)
{
SoundManager.Music.Dispose();
}

SoundManager.Music?.Dispose();
SoundManager.PlayMusic(Music, true);
}
}
Expand Down
5 changes: 4 additions & 1 deletion Client/MirSounds/Libraries/NAudioLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class NAudioLibrary : ISoundLibrary, IDisposable
private WaveOutEvent outputDevice;
private AudioFileReader audioFile;

private int _unscaledVolume;
private string _fileName;
private bool _loop;
private bool _isDisposing;
Expand Down Expand Up @@ -110,7 +111,7 @@ private void OutputDevice_PlaybackStopped()
if (_loop &&
!_isDisposing)
{
outputDevice.Play();
Play(_unscaledVolume);
}
}

Expand All @@ -124,6 +125,8 @@ public void Dispose()

private float ScaleVolume(int volume)
{
_unscaledVolume = volume;

float scaled = 0.0f + (float)(volume - 0) / (100 - 0) * (1.0f - 0.0f);
return scaled;
}
Expand Down
Loading

0 comments on commit 72aa7fd

Please sign in to comment.