Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CommonCode.cs
Go to the documentation of this file.
2using Terraria.ID;
4
6
7public static class CommonCode
8{
9 public static void DropItemFromNPC(NPC npc, int itemId, int stack, bool scattered = false)
10 {
11 if (itemId > 0 && itemId < ItemID.Count)
12 {
13 int x = (int)npc.position.X + npc.width / 2;
14 int y = (int)npc.position.Y + npc.height / 2;
15 if (scattered)
16 {
17 x = (int)npc.position.X + Main.rand.Next(npc.width + 1);
18 y = (int)npc.position.Y + Main.rand.Next(npc.height + 1);
19 }
20 int itemIndex = Item.NewItem(npc.GetItemSource_Loot(), x, y, 0, 0, itemId, stack, noBroadcast: false, -1);
21 ModifyItemDropFromNPC(npc, itemIndex);
22 }
23 }
24
25 public static void DropItemLocalPerClientAndSetNPCMoneyTo0(NPC npc, int itemId, int stack, bool interactionRequired = true)
26 {
27 if (itemId <= 0 || itemId >= ItemID.Count)
28 {
29 return;
30 }
31 if (Main.netMode == 2)
32 {
33 int num = Item.NewItem(npc.GetItemSource_Loot(), (int)npc.position.X, (int)npc.position.Y, npc.width, npc.height, itemId, stack, noBroadcast: true, -1);
35 for (int i = 0; i < 255; i++)
36 {
37 if (Main.player[i].active && (npc.playerInteraction[i] || !interactionRequired))
38 {
39 NetMessage.SendData(90, i, -1, null, num);
40 }
41 }
42 Main.item[num].active = false;
43 }
44 else
45 {
46 DropItemFromNPC(npc, itemId, stack);
47 }
48 npc.value = 0f;
49 }
50
51 public static void DropItemForEachInteractingPlayerOnThePlayer(NPC npc, int itemId, UnifiedRandom rng, int chanceNumerator, int chanceDenominator, int stack = 1, bool interactionRequired = true)
52 {
53 if (itemId <= 0 || itemId >= ItemID.Count)
54 {
55 return;
56 }
57 if (Main.netMode == 2)
58 {
59 for (int i = 0; i < 255; i++)
60 {
61 Player player = Main.player[i];
62 if (player.active && (npc.playerInteraction[i] || !interactionRequired) && rng.Next(chanceDenominator) < chanceNumerator)
63 {
64 int itemIndex = Item.NewItem(npc.GetItemSource_Loot(), player.position, player.Size, itemId, stack, noBroadcast: false, -1);
65 ModifyItemDropFromNPC(npc, itemIndex);
66 }
67 }
68 }
69 else if (rng.Next(chanceDenominator) < chanceNumerator)
70 {
71 DropItemFromNPC(npc, itemId, stack);
72 }
73 npc.value = 0f;
74 }
75
76 public static void ModifyItemDropFromNPC(NPC npc, int itemIndex)
77 {
78 Item item = Main.item[itemIndex];
79 switch (item.type)
80 {
81 case 23:
82 if (npc.type == 1 && npc.netID != -1 && npc.netID != -2 && npc.netID != -5 && npc.netID != -6)
83 {
84 item.color = npc.color;
85 NetMessage.SendData(88, -1, -1, null, itemIndex, 1f);
86 }
87 if (Main.remixWorld && npc.type == 59)
88 {
89 item.color = new Color(255, 127, 0);
90 NetMessage.SendData(88, -1, -1, null, itemIndex, 1f);
91 }
92 break;
93 case 319:
94 switch (npc.netID)
95 {
96 case 542:
97 item.color = new Color(189, 148, 96, 255);
98 NetMessage.SendData(88, -1, -1, null, itemIndex, 1f);
99 break;
100 case 543:
101 item.color = new Color(112, 85, 89, 255);
102 NetMessage.SendData(88, -1, -1, null, itemIndex, 1f);
103 break;
104 case 544:
105 item.color = new Color(145, 27, 40, 255);
106 NetMessage.SendData(88, -1, -1, null, itemIndex, 1f);
107 break;
108 case 545:
109 item.color = new Color(158, 113, 164, 255);
110 NetMessage.SendData(88, -1, -1, null, itemIndex, 1f);
111 break;
112 }
113 break;
114 }
115 }
116}
Vector2 Size
Definition Entity.cs:151
Vector2 position
Definition Entity.cs:14
static void DropItemForEachInteractingPlayerOnThePlayer(NPC npc, int itemId, UnifiedRandom rng, int chanceNumerator, int chanceDenominator, int stack=1, bool interactionRequired=true)
Definition CommonCode.cs:51
static void DropItemFromNPC(NPC npc, int itemId, int stack, bool scattered=false)
Definition CommonCode.cs:9
static void DropItemLocalPerClientAndSetNPCMoneyTo0(NPC npc, int itemId, int stack, bool interactionRequired=true)
Definition CommonCode.cs:25
static void ModifyItemDropFromNPC(NPC npc, int itemIndex)
Definition CommonCode.cs:76
static readonly short Count
Definition ItemID.cs:12138
static int NewItem(IEntitySource source, Vector2 pos, Vector2 randomBox, int Type, int Stack=1, bool noBroadcast=false, int prefixGiven=0, bool noGrabDelay=false, bool reverseLookup=false)
Definition Item.cs:49697
static Item[] item
Definition Main.cs:1681
static int netMode
Definition Main.cs:2095
static UnifiedRandom rand
Definition Main.cs:1387
static int[] timeItemSlotCannotBeReusedFor
Definition Main.cs:1683
static Player[] player
Definition Main.cs:1803
static bool remixWorld
Definition Main.cs:349
int type
Definition NPC.cs:445
Color color
Definition NPC.cs:487
bool[] playerInteraction
Definition NPC.cs:113
IEntitySource GetItemSource_Loot()
Definition NPC.cs:87551
int netID
Definition NPC.cs:531
static void SendData(int msgType, int remoteClient=-1, int ignoreClient=-1, NetworkText text=null, int number=0, float number2=0f, float number3=0f, float number4=0f, int number5=0, int number6=0, int number7=0)
Definition NetMessage.cs:88