-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPlayerWOFX.cs
More file actions
600 lines (546 loc) · 27.4 KB
/
PlayerWOFX.cs
File metadata and controls
600 lines (546 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.DataStructures;
using Terraria.Graphics.Shaders;
using Terraria.ModLoader.IO;
using System.IO;
//using Terraria.Graphics.Shaders;
//vs collapse all Ctrl-M-O
namespace WeaponOut//Lite
{
public class PlayerWOFX : ModPlayer
{
private const bool DEBUG_WEAPONHOLD = false;
private const bool DEBUG_BOOMERANGS = false;
private static Mod itemCustomizer;
#region Weapon Holding
public bool weaponVisual = true;
public bool WeaponVisual { get { return weaponVisual || ModConf.forceShowWeaponOut; } }
public int weaponFrame;//frame of weapon...
#endregion
public override void PostUpdate() {
manageBodyFrame();
}
private void manageBodyFrame() {
//if (WeaponOutLite.Disabled) return;
if (Main.netMode == 2) return; // Oh yeah, server calls this so don't pls
//change idle pose for player using a heavy weapon
//copypasting from drawPlayerItem
Item heldItem = player.inventory[player.selectedItem];
if (heldItem == null || heldItem.type == 0 || heldItem.holdStyle != 0 || !ModConf.showWeaponOut) return; //no item so nothing to show
Texture2D weaponTex = weaponTex = Main.itemTexture[heldItem.type];
if (weaponTex == null) return; //no texture to item so ignore too
float itemWidth = weaponTex.Width * heldItem.scale;
float itemHeight = weaponTex.Height * heldItem.scale;
if (heldItem.modItem != null) {
if (Main.itemAnimations[heldItem.type] != null) {
itemHeight /= Main.itemAnimations[heldItem.type].FrameCount;
}
}
float larger = Math.Max(itemWidth, itemHeight);
int playerBodyFrameNum = player.bodyFrame.Y / player.bodyFrame.Height;
// Is a long gun
HoldType customHold = getCustomHoldType(heldItem);
if (customHold == HoldType.LargeGun
||
( customHold == HoldType.None // only when none set
&& (heldItem.useStyle == 5
&& weaponTex.Width >= weaponTex.Height * 1.2f
&& (!heldItem.noUseGraphic || !heldItem.melee)
&& larger >= 45
&& WeaponVisual)) //toggle with accessory1 visibility, or forceshow is on
) {
if (playerBodyFrameNum == 0) player.bodyFrame.Y = 10 * player.bodyFrame.Height;
}
}
public override void OnEnterWorld(Player player) {
//if (WeaponOutLite.Disabled) return;
itemCustomizer = ModLoader.GetMod("ItemCustomizer");
// Update visuals
WeaponOut.NetUpdateWeaponVisual(mod, this);
}
#region Save and Load
public override TagCompound Save() {
//if (WeaponOutLite.Disabled) return base.Save();
return new TagCompound
{
{ "weaponVisual", weaponVisual }
};
}
public override void Load(TagCompound tag) {
//if (WeaponOutLite.Disabled) return;
weaponVisual = tag.GetBool("weaponVisual");
}
#endregion
#region Player Layers
public static readonly PlayerLayer HeldItem = new PlayerLayer("WeaponOut", "HeldItem", PlayerLayer.HeldItem, delegate (PlayerDrawInfo drawInfo) {
if (drawInfo.shadow != 0f) {
return;
}
try {
drawPlayerItem(drawInfo, false);
}
catch { }
});
public static readonly PlayerLayer HairBack = new PlayerLayer("WeaponOut", "HairBack", PlayerLayer.HairBack, delegate (PlayerDrawInfo drawInfo) {
if (drawInfo.shadow != 0f) {
return;
}
try {
drawPlayerItem(drawInfo, true);
}
catch { }
});
#endregion
#region draw
public bool hidden = false;
public override void ModifyDrawLayers(List<PlayerLayer> layers) {
//if (WeaponOutLite.Disabled) return;
if (hidden) {
hidden = false;
foreach (PlayerLayer l in layers) {
l.visible = false;
}
return;
}
#region Show fists with weapon visuals
if (WeaponVisual) {
if (player.HeldItem.useStyle == ModPlayerFists.useStyle) {
if (player.HeldItem.handOnSlot > 0) {
player.handon = player.HeldItem.handOnSlot;
player.cHandOn = 0;
}
if (player.HeldItem.handOffSlot > 0) {
player.handoff = player.HeldItem.handOffSlot;
player.cHandOff = 0;
}
if (player.itemAnimation > 0) layers.Remove(PlayerLayer.HeldItem); // hide fist when attacking
return;
}
}
#endregion
if (player.itemAnimation > 0) return; // No show when swinging
HeldItem.visible = true; // For items held in hand
HairBack.visible = true; // For items behind the player (sheathed)
//MiscEffectsFront.visible = !player.dead;
try {
int heldItemStack = layers.IndexOf(PlayerLayer.HeldItem);
int hairBackStack = layers.IndexOf(PlayerLayer.HairBack);
//int MiscEffectsFrontStack = layers.IndexOf(PlayerLayer.MiscEffectsFront);
if (heldItemStack >= 0) layers.Insert(heldItemStack, HeldItem);
if (hairBackStack >= 0) layers.Insert(hairBackStack, HairBack);
}
catch { }
//layers.Insert(MiscEffectsFrontStack, MiscEffectsFront);
}
internal static HoldType getCustomHoldType(Item item)
{
int style;
if (ModConfWeaponOutCustom.TryGetCustomHoldStyle(item.type, out style))
{
return setHoldTypeSafeWrapping(style);
}
return HoldType.None;
}
internal static HoldType setHoldTypeSafeWrapping(int style)
{
if (style > HoldTypeCount)
{ style = (int)HoldType.None; }
if (style < (int)HoldType.None)
{ style = HoldTypeCount; }
return (HoldType)style;
}
/// <summary>
/// We gonna handle all the weapon identification and calls here
/// </summary>
/// <param name="drawInfo"></param>
/// <param name="drawOnBack"></param>
private static void drawPlayerItem(PlayerDrawInfo drawInfo, bool drawOnBack) {
//don't draw when not ingame
if (Main.gameMenu || !ModConf.showWeaponOut) return;
//get player player
Player drawPlayer = drawInfo.drawPlayer;
//hide if dead, stoned etc.
if (!drawPlayer.active || drawPlayer.dead || drawPlayer.stoned) return;
try {
if (//do nothing if player is doing something
drawPlayer.itemAnimation > 0 ||
//also hide if visual is disabled
(!ModConf.forceShowWeaponOut && !drawPlayer.GetModPlayer<PlayerWOFX>().weaponVisual)) {
return;
}
}
catch { }
//player player's held item
Item heldItem = drawPlayer.inventory[drawPlayer.selectedItem];
if (heldItem == null || heldItem.type == 0 || heldItem.holdStyle != 0) return; //no item so nothing to show
//ignore projectile melee weapons
bool isYoyo = false;
// items work when checked at least once in singleplayer first...?
if (DEBUG_BOOMERANGS) Main.NewText("Shoot is " + heldItem.shoot + " (!=0)");
if (heldItem.shoot != 0) {
if (DEBUG_BOOMERANGS) Main.NewText("heldItem.melee = " + heldItem.melee);
if (DEBUG_BOOMERANGS) Main.NewText("heldItem.noMelee = " + heldItem.noMelee);
if (heldItem.melee && heldItem.noMelee) {
for (int i = 0; i < Main.projectile.Length; i++) {
if (!Main.projectile[i].active) continue;
if (Main.projectile[i].owner == drawPlayer.whoAmI &&
Main.projectile[i].melee) {
return;
}
}
}
// YOYO is aiStyle 99
Projectile p = new Projectile();
p.SetDefaults(heldItem.shoot);
if (p.aiStyle == 99) {
isYoyo = true;
}
}
//item texture
Texture2D weaponTex = weaponTex = Main.itemTexture[heldItem.type];
if (weaponTex == null) return; //no texture to item so ignore too
int gWidth = weaponTex.Width;
int gHeight = weaponTex.Height;
//does the item have an animation? No vanilla weapons do
Rectangle? sourceRect = null;
if (heldItem.modItem != null) {
if (Main.itemAnimations[heldItem.type] != null) // in the case of modded weapons with animations...
{
//get local player frame counting
PlayerWOFX p = drawPlayer.GetModPlayer<PlayerWOFX>();
int frameCount = Main.itemAnimations[heldItem.type].FrameCount;
int frameCounter = Main.itemAnimations[heldItem.type].TicksPerFrame * 2;
//add them up
if (Main.time % frameCounter == 0) {
p.weaponFrame++;
if (p.weaponFrame >= frameCount) {
p.weaponFrame = 0;
}
}
//set frame on source
gHeight /= frameCount;
sourceRect = new Rectangle(0, gHeight * p.weaponFrame, gWidth, gHeight);
}
}
//get draw location of player
int drawX = (int)(drawPlayer.MountedCenter.X - Main.screenPosition.X);
int drawY = (int)(drawPlayer.MountedCenter.Y - Main.screenPosition.Y + drawPlayer.gfxOffY) - 3;
//get the lighting on the player's tile
Color lighting = Lighting.GetColor(
(int)((drawInfo.position.X + drawPlayer.width / 2f) / 16f),
(int)((drawInfo.position.Y + drawPlayer.height / 2f) / 16f));
//get item alpha (like starfury) then player stealth and alpha (inviciblity etc.)
lighting = drawPlayer.GetImmuneAlpha(heldItem.GetAlpha(lighting) * drawPlayer.stealth, 0);
float scale = heldItem.scale;
if (isYoyo) scale *= 0.6f;
//standard items
SpriteEffects spriteEffects = SpriteEffects.None;
if (drawPlayer.direction < 0) spriteEffects = SpriteEffects.FlipHorizontally;
if (drawPlayer.gravDir < 0) {
drawY += 6;
spriteEffects = SpriteEffects.FlipVertically | spriteEffects;
}
DrawData data = new DrawData(
weaponTex,
new Vector2(drawX, drawY),
sourceRect,
lighting,
0f,
new Vector2(gWidth / 2f, gHeight / 2f),
scale,
spriteEffects,
0);
// Item customiser integration
// https://github.com/gamrguy/ItemCustomizer
if (itemCustomizer != null) {
data.shader = ItemCustomizerGetShader(itemCustomizer, heldItem);
}
//work out what type of weapon it is!
#region Weapon Algorithm
float itemWidth = gWidth * heldItem.scale;
float itemHeight = gHeight * heldItem.scale;
//not all items have width/height set the same, so use largest as "length" including weapon sizemod
float larger = Math.Max(itemWidth, itemHeight);
float lesser = Math.Min(itemWidth, itemHeight);
PickItemDrawType(drawOnBack, drawPlayer, heldItem, isYoyo, gWidth, gHeight, ref data, itemWidth, itemHeight, larger, lesser);
#endregion
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "[]: " + itemWidth + " x " + itemHeight, 100, 200, 150);
}
public enum HoldType
{ None, Hand, Waist, Back, Spear, PowerTool, Bow, SmallGun, LargeGun, Staff }
private static readonly bool[] holdTypeHideOnBack = new bool[] {
true, true, false, false, true, true, true, true, true, true
};
public static readonly int HoldTypeCount = (int)HoldType.Staff;
private static void PickItemDrawType(bool drawOnBack, Player drawPlayer, Item heldItem, bool isYoyo, int gWidth, int gHeight, ref DrawData data, float itemWidth, float itemHeight, float larger, float lesser) {
HoldType holdType = HoldType.None;
int style;
if (ModConfWeaponOutCustom.TryGetCustomHoldStyle(heldItem.type, out style))
{
holdType = getCustomHoldType(heldItem);
}
else
{
#region AutoPicker
if (heldItem.useStyle == 1 || //swing
heldItem.useStyle == 2 || //eat
heldItem.useStyle == 3) //stab
{
//| ######
//| ## ##
//| ##########
//| ## ##
//| ## ##
//| ## ##
//| ## ##
//| ##
//Items, daggers and other throwables lie below 28 and are easily held in the hand
if ((larger < 28 && !heldItem.magic) || //nonmagic weapons
(larger <= 32 && heldItem.shoot != 0) || //larger for throwing weapons
(larger <= 24 && heldItem.magic)) //only smallest magic weapons
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(hand): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Hand;
}
//| ####
//| ## ##
//| ## ##
//| ## ##
//| #### ##
//| ## ##
//| ## ####
//| ####
//Broadsword weapons are swing type weapons between 28 - 48
//They are worn on the waist, and react to falling! Except when disabled
//This also amusingly applies to ducks, axes and rockfish
//But shouldn't apply to pickaxes, except when they are also not pickaxes
else if (larger <= 48 &&
(heldItem.pick <= 0 ||
(heldItem.pick > 0 && heldItem.axe > 0)))
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(waist): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Waist;
}
//| ########
//| ## ##
//| ## ####
//| ## ## ##
//| #### ##
//| ## ####
//| ## ########
//| ######
//Great weapons are swing type weapons past 36 in size and slung on the back
else
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(back): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Back;
}
}
if (heldItem.useStyle == 4 || //hold up
heldItem.useStyle == 5) //hold out
{
bool isAStaff = Item.staff[heldItem.type];
//staves, guns and bows
if (gHeight >= gWidth * 1.2f && !isAStaff)
{
//| ######
//| ## ######
//| ## ## ##
//| ## ## ##
//| ## ## ##
//| ## ## ##
//| ## ######
//| ######
//bows
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(bow): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Bow;
}
else if (gWidth >= gHeight * 1.2f && !isAStaff)
{
if (heldItem.noUseGraphic && heldItem.melee)
{
//|
//| ####
//| ## ##########
//| #### ## ####
//| ## ## ## ####
//| ## ## ######
//| ############
//|
//drills, chainsaws
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(drill): " + itemWidth + " x " + itemHeight);
holdType = HoldType.PowerTool;
}
else
{
if (larger < 45)
{
//| #### ####
//| ## ######## ##
//| #### ##
//| ## ########
//| ## ## ##
//| ## ####
//| ######
//|
if (drawPlayer.grapCount > 0) return; // can't see while grappling
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(pistol): " + itemWidth + " x " + itemHeight);
//small aimed weapons (like handgun/aquasceptre) held halfway down, 1/3 back
holdType = HoldType.SmallGun;
}
else
{
//|
//| ##
//| ######################
//| ## ## ## ##
//| ## ############
//| #### ## ##
//| #### ##
//|
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(rifle): " + itemWidth + " x " + itemHeight);
//large guns (rifles, launchers, etc.) held with both hands
holdType = HoldType.LargeGun;
}
}
}
else
{
if (heldItem.noUseGraphic && !isAStaff)
{
if (!heldItem.autoReuse)
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(polearm): " + itemWidth + " x " + itemHeight);
if (isYoyo)
{
//sam (?why did i write sam? maybe same?)
data = WeaponDrawInfo.modDraw_HandWeapon(data, drawPlayer, larger, lesser, isYoyo);
}
else
{
//| ####
//| #### ##
//| ## ##
//| ## ##
//| ## ##
//| ##
//| ##
//| ##
//spears are held facing to the floor, maces generally held
holdType = HoldType.Spear;
}
}
else
{
//nebula blaze, flairon, solar eruption (too inconsistent)
if (larger <= 48)
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(waist safe): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Waist;
}
else
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(back safe): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Back;
}
}
}
else
{
if (larger + lesser <= 72) //only smallest magic weapons
{
//| ######
//| ## ## ##
//| ## ## ##
//| ## ######
//| ## ## ##
//| ## ## ##
//| ## ## ##
//| ######
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(hand magic): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Hand;
}
else if (lesser <= 42) //medium sized magic weapons, treated like polearms
{
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(polearm magic): " + itemWidth + " x " + itemHeight);
holdType = HoldType.Spear;
}
else
{ //largestaves are held straight up
//|
//| ####
//| ############ ##
//| ## ## ##
//| ############ ##
//| ####
//|
//|
if (DEBUG_WEAPONHOLD && drawPlayer.controlHook) Main.NewText(heldItem.useStyle + "(staff): " + itemWidth + " x " + itemHeight);
//staves
holdType = HoldType.Staff;
}
}
}
}
#endregion
// Get any custom hold if no override was set
foreach (var drawMethod in WeaponOut.mod.weaponOutCustomHoldMethods)
{
holdType = setHoldTypeSafeWrapping(drawMethod(drawPlayer, heldItem, (int)holdType));
}
}
// can't see non-backed while grappling
if (holdTypeHideOnBack[(int)holdType] && drawPlayer.grapCount > 0) return;
// Only draw back items when it is a back item hold type
if (holdTypeHideOnBack[(int)holdType] == drawOnBack) return;
switch (holdType) {
case HoldType.Hand: data = WeaponDrawInfo.modDraw_HandWeapon(data, drawPlayer, larger, lesser); break;
case HoldType.Waist: data = WeaponDrawInfo.modDraw_WaistWeapon(data, drawPlayer, larger); break;
case HoldType.Spear: data = WeaponDrawInfo.modDraw_PoleWeapon(data, drawPlayer, larger); break;
case HoldType.PowerTool: data = WeaponDrawInfo.modDraw_DrillWeapon(data, drawPlayer, larger); break;
case HoldType.Back: data = WeaponDrawInfo.modDraw_BackWeapon(data, drawPlayer, larger); break;
case HoldType.Bow: data = WeaponDrawInfo.modDraw_ForwardHoldWeapon(data, drawPlayer, lesser); break;
case HoldType.SmallGun: data = WeaponDrawInfo.modDraw_AimedWeapon(data, drawPlayer, larger); break;
case HoldType.LargeGun: data = WeaponDrawInfo.modDraw_HeavyWeapon(data, drawPlayer, lesser); break;
case HoldType.Staff: data = WeaponDrawInfo.modDraw_MagicWeapon(data, drawPlayer, larger); break;
default: return;
}
// Run custom draws
bool allowDraw = true;
foreach (var drawMethod in WeaponOut.mod.weaponOutCustomPreDrawMethods)
{
if (!drawMethod(drawPlayer, heldItem, data))
{ allowDraw = false; }
}
// Attempt standard draw if allowed
if (allowDraw)
{
//Add the weapon to the draw layers
Main.playerDrawData.Add(data);
WeaponDrawInfo.drawGlowLayer(data, drawPlayer, heldItem);
}
}
private static int ItemCustomizerGetShader(Mod mod, Item item) {
if (!Main.dedServ) {
try {
GlobalItem cii = item.GetGlobalItem(mod, "CustomizerItem");
// The field we're looking for
var shaderIDInfo = cii.GetType().GetField("shaderID");
// Check this field on this class
int shaderID = (int)shaderIDInfo.GetValue(cii);
// We got this
return shaderID;
}
catch { }
}
return 0;
}
#endregion
}
}