Skip to content

Commit f5fe511

Browse files
committed
cleanup itemBase in Intersect.Client.Core
1 parent 44ce97f commit f5fe511

File tree

2 files changed

+160
-121
lines changed

2 files changed

+160
-121
lines changed

Intersect.Client.Core/Entities/Player.cs

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -544,39 +544,41 @@ public int FindHotbarItem(IHotbarInstance hotbarInstance)
544544
if (itm != null && itm.ItemId == hotbarInstance.ItemOrSpellId)
545545
{
546546
bestMatch = i;
547-
var itemBase = ItemDescriptor.Get(itm.ItemId);
548-
if (itemBase != null)
547+
var itemDescriptor = ItemDescriptor.Get(itm.ItemId);
548+
if (itemDescriptor == null)
549549
{
550-
if (itemBase.ItemType == ItemType.Bag)
550+
continue;
551+
}
552+
553+
if (itemDescriptor.ItemType == ItemType.Bag)
554+
{
555+
if (hotbarInstance.BagId == itm.BagId)
551556
{
552-
if (hotbarInstance.BagId == itm.BagId)
553-
{
554-
break;
555-
}
557+
break;
556558
}
557-
else if (itemBase.ItemType == ItemType.Equipment)
559+
}
560+
else if (itemDescriptor.ItemType == ItemType.Equipment)
561+
{
562+
if (hotbarInstance.PreferredStatBuffs != null)
558563
{
559-
if (hotbarInstance.PreferredStatBuffs != null)
564+
var statMatch = true;
565+
for (var s = 0; s < hotbarInstance.PreferredStatBuffs.Length; s++)
560566
{
561-
var statMatch = true;
562-
for (var s = 0; s < hotbarInstance.PreferredStatBuffs.Length; s++)
567+
if (itm.ItemProperties.StatModifiers[s] != hotbarInstance.PreferredStatBuffs[s])
563568
{
564-
if (itm.ItemProperties.StatModifiers[s] != hotbarInstance.PreferredStatBuffs[s])
565-
{
566-
statMatch = false;
567-
}
569+
statMatch = false;
568570
}
571+
}
569572

570-
if (statMatch)
571-
{
572-
break;
573-
}
573+
if (statMatch)
574+
{
575+
break;
574576
}
575577
}
576-
else
577-
{
578-
break;
579-
}
578+
}
579+
else
580+
{
581+
break;
580582
}
581583
}
582584
}
@@ -600,46 +602,62 @@ public bool IsEquipped(int slot)
600602

601603
public bool IsItemOnCooldown(int slot)
602604
{
603-
if (Inventory[slot] != null)
605+
if (Inventory[slot] is not {} inventorySlot)
604606
{
605-
var itm = Inventory[slot];
606-
if (itm.ItemId != Guid.Empty)
607-
{
608-
if (ItemCooldowns.TryGetValue(itm.ItemId, out var value) && value > Timing.Global.Milliseconds)
609-
{
610-
return true;
611-
}
607+
return false;
608+
}
612609

613-
if ((ItemDescriptor.TryGet(itm.ItemId, out var itemBase) && !itemBase.IgnoreGlobalCooldown) && Globals.Me?.GlobalCooldown > Timing.Global.Milliseconds)
614-
{
615-
return true;
616-
}
617-
}
610+
if (inventorySlot.ItemId == Guid.Empty)
611+
{
612+
return false;
618613
}
619614

620-
return false;
615+
if (ItemCooldowns.TryGetValue(inventorySlot.ItemId, out var cooldownEndTime) &&
616+
cooldownEndTime > Timing.Global.Milliseconds)
617+
{
618+
return true;
619+
}
620+
621+
return ItemDescriptor.TryGet(inventorySlot.ItemId, out var itemDescriptor) &&
622+
!itemDescriptor.IgnoreGlobalCooldown &&
623+
Globals.Me?.GlobalCooldown > Timing.Global.Milliseconds;
621624
}
622625

623626
public long GetItemRemainingCooldown(int slot)
624627
{
625-
if (Inventory[slot] != null)
628+
if (Inventory[slot] is not { } inventorySlot)
626629
{
627-
var itm = Inventory[slot];
628-
if (itm.ItemId != Guid.Empty)
629-
{
630-
if (ItemCooldowns.TryGetValue(itm.ItemId, out var value) && value > Timing.Global.Milliseconds)
631-
{
632-
return value - Timing.Global.Milliseconds;
633-
}
630+
return 0;
631+
}
634632

635-
if ((ItemDescriptor.TryGet(itm.ItemId, out var itemBase) && !itemBase.IgnoreGlobalCooldown) && Globals.Me?.GlobalCooldown > Timing.Global.Milliseconds)
636-
{
637-
return Globals.Me.GlobalCooldown - Timing.Global.Milliseconds;
638-
}
639-
}
633+
if (inventorySlot.ItemId == Guid.Empty)
634+
{
635+
return 0;
640636
}
641637

642-
return 0;
638+
if (ItemCooldowns.TryGetValue(inventorySlot.ItemId, out var cooldownEndTime) &&
639+
cooldownEndTime > Timing.Global.Milliseconds)
640+
{
641+
return cooldownEndTime - Timing.Global.Milliseconds;
642+
}
643+
644+
if (!ItemDescriptor.TryGet(inventorySlot.ItemId, out var itemDescriptor))
645+
{
646+
return 0;
647+
}
648+
649+
if (itemDescriptor.IgnoreGlobalCooldown)
650+
{
651+
return 0;
652+
}
653+
654+
if (Globals.Me is not { } player)
655+
{
656+
return 0;
657+
}
658+
659+
var itemRemainingCooldown = player.GlobalCooldown - Timing.Global.Milliseconds;
660+
return Math.Max(0, itemRemainingCooldown);
643661
}
644662

645663
public bool IsSpellOnCooldown(int slot)

Intersect.Client.Core/Maps/MapInstance.cs

Lines changed: 91 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -906,20 +906,28 @@ public void DrawItemsAndLights()
906906
var mapItemHeight = Options.Instance.Map.MapItemHeight;
907907

908908
// Draw map items.
909-
foreach (var (key, tileItems) in MapItems)
909+
foreach (var (tileIndex, itemInstancesOnTile) in MapItems)
910910
{
911911
// Calculate tile coordinates.
912-
var tileX = key % _width;
913-
var tileY = (int)Math.Floor(key / (float)_width);
912+
var tileX = tileIndex % _width;
913+
var tileY = (int)Math.Floor(tileIndex / (float)_width);
914914

915915
// Loop through this in reverse to match client/server display and pick-up order.
916-
for (var index = tileItems.Count - 1; index >= 0; index--)
916+
for (var index = itemInstancesOnTile.Count - 1; index >= 0; index--)
917917
{
918+
var mapItemInstance = itemInstancesOnTile[index];
919+
if (ItemDescriptor.TryGet(mapItemInstance.ItemId, out var itemDescriptor))
920+
{
921+
continue;
922+
}
923+
918924
// Set up all information we need to draw this name.
919-
var itemBase = ItemDescriptor.Get(tileItems[index].ItemId);
920-
var itemTex = Globals.ContentManager.GetTexture(Framework.Content.TextureType.Item, itemBase.Icon);
925+
var itemTexture = GameContentManager.Current.GetTexture(
926+
Framework.Content.TextureType.Item,
927+
itemDescriptor.Icon
928+
);
921929

922-
if (itemTex == null)
930+
if (itemTexture == null)
923931
{
924932
continue;
925933
}
@@ -933,10 +941,10 @@ public void DrawItemsAndLights()
933941

934942
// Draw the item texture.
935943
Graphics.DrawGameTexture(
936-
itemTex,
937-
new FloatRect(0, 0, itemTex.Width, itemTex.Height),
944+
itemTexture,
945+
new FloatRect(0, 0, itemTexture.Width, itemTexture.Height),
938946
new FloatRect(textureXPosition, textureYPosition, mapItemWidth, mapItemHeight),
939-
itemBase.Color
947+
itemDescriptor.Color
940948
);
941949
}
942950
}
@@ -961,77 +969,90 @@ public void DrawItemNames()
961969
return;
962970
}
963971

972+
if (Globals.Me is not { } player)
973+
{
974+
return;
975+
}
976+
964977
// Get where our mouse is located and convert it to a tile based location.
965978
var mousePos = Graphics.ConvertToWorldPoint(Globals.InputManager.GetMousePosition());
966-
var x = (int)(mousePos.X - (int)X) / _tileWidth;
967-
var y = (int)(mousePos.Y - (int)Y) / _tileHeight;
979+
var x = (int)(mousePos.X - X) / _tileWidth;
980+
var y = (int)(mousePos.Y - Y) / _tileHeight;
968981
var mapId = Id;
969982

970983
// Is this an actual location on this map?
971-
if (Globals.Me.TryGetRealLocation(ref x, ref y, ref mapId) && mapId == Id)
984+
if (!player.TryGetRealLocation(ref x, ref y, ref mapId) || mapId != Id)
972985
{
973-
// Apparently it is! Do we have any items to render here?
974-
var tileItems = new List<IMapItemInstance>();
975-
if (MapItems.TryGetValue(y * _width + x, out tileItems))
976-
{
977-
var baseOffset = 0;
978-
// Loop through this in reverse to match client/server display and pick-up order.
979-
for (var index = tileItems.Count - 1; index >= 0; index--)
980-
{
981-
// Set up all information we need to draw this name.
982-
var itemBase = ItemDescriptor.Get(tileItems[index].ItemId);
983-
var name = tileItems[index].Descriptor.Name;
984-
var quantity = tileItems[index].Quantity;
985-
var rarity = itemBase.Rarity;
986-
if (tileItems[index].Quantity > 1)
987-
{
988-
name = Strings.General.MapItemStackable.ToString(
989-
name,
990-
Strings.FormatQuantityAbbreviated(quantity)
991-
);
992-
}
986+
return;
987+
}
993988

994-
var color = CustomColors.Items.MapRarities.ContainsKey(rarity)
995-
? CustomColors.Items.MapRarities[rarity]
996-
: new LabelColor(Color.White, Color.Black, new Color(100, 0, 0, 0));
997-
var textSize = Graphics.Renderer.MeasureText(
998-
name,
999-
Graphics.EntityNameFont,
1000-
Graphics.EntityNameFontSize,
1001-
1
1002-
);
1003-
var offsetY = (baseOffset * textSize.Y);
1004-
var destX = X + (int)Math.Ceiling(((x * _tileWidth) + (_tileHalfWidth)) - (textSize.X / 2));
1005-
var destY = Y + (int)Math.Ceiling(((y * _tileHeight) - ((_tileHeight / 3) + textSize.Y))) - offsetY;
989+
// Apparently it is! Do we have any items to render here?
990+
if (!MapItems.TryGetValue(y * _width + x, out var tileItems))
991+
{
992+
return;
993+
}
1006994

1007-
// Do we need to draw a background?
1008-
if (color.Background != Color.Transparent)
1009-
{
1010-
Graphics.DrawGameTexture(
1011-
Graphics.Renderer.WhitePixel,
1012-
new FloatRect(0, 0, 1, 1),
1013-
new FloatRect(destX - 4, destY, textSize.X + 8, textSize.Y),
1014-
color.Background
1015-
);
1016-
}
995+
var baseOffset = 0;
996+
// Loop through this in reverse to match client/server display and pick-up order.
997+
for (var index = tileItems.Count - 1; index >= 0; index--)
998+
{
999+
var mapItemInstance = tileItems[index];
1000+
if (!ItemDescriptor.TryGet(mapItemInstance.ItemId, out var itemDescriptor)) {
1001+
continue;
1002+
}
10171003

1018-
// Finaly, draw the actual name!
1019-
Graphics.Renderer.DrawString(
1020-
name,
1021-
Graphics.EntityNameFont,
1022-
Graphics.EntityNameFontSize,
1023-
destX,
1024-
destY,
1025-
1,
1026-
color.Name,
1027-
true,
1028-
null,
1029-
color.Outline
1030-
);
1004+
// Set up all information we need to draw this name.
1005+
var name = mapItemInstance.Descriptor.Name;
1006+
var quantity = mapItemInstance.Quantity;
1007+
var rarity = itemDescriptor.Rarity;
1008+
if (mapItemInstance.Quantity > 1)
1009+
{
1010+
name = Strings.General.MapItemStackable.ToString(
1011+
name,
1012+
Strings.FormatQuantityAbbreviated(quantity)
1013+
);
1014+
}
10311015

1032-
baseOffset++;
1033-
}
1016+
var color = CustomColors.Items.MapRarities.GetValueOrDefault(
1017+
rarity,
1018+
new LabelColor(Color.White, Color.Black, new Color(100, 0, 0, 0))
1019+
);
1020+
var textSize = Graphics.Renderer.MeasureText(
1021+
name,
1022+
Graphics.EntityNameFont,
1023+
Graphics.EntityNameFontSize,
1024+
1
1025+
);
1026+
var offsetY = (baseOffset * textSize.Y);
1027+
var destX = X + (int)Math.Ceiling(((x * _tileWidth) + (_tileHalfWidth)) - (textSize.X / 2));
1028+
var destY = Y + (int)Math.Ceiling(((y * _tileHeight) - ((_tileHeight / 3f) + textSize.Y))) - offsetY;
1029+
1030+
// Do we need to draw a background?
1031+
if (color.Background != Color.Transparent)
1032+
{
1033+
Graphics.DrawGameTexture(
1034+
Graphics.Renderer.WhitePixel,
1035+
new FloatRect(0, 0, 1, 1),
1036+
new FloatRect(destX - 4, destY, textSize.X + 8, textSize.Y),
1037+
color.Background
1038+
);
10341039
}
1040+
1041+
// Finaly, draw the actual name!
1042+
Graphics.Renderer.DrawString(
1043+
name,
1044+
Graphics.EntityNameFont,
1045+
Graphics.EntityNameFontSize,
1046+
destX,
1047+
destY,
1048+
1,
1049+
color.Name,
1050+
true,
1051+
null,
1052+
color.Outline
1053+
);
1054+
1055+
baseOffset++;
10351056
}
10361057
}
10371058

0 commit comments

Comments
 (0)