aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/item_inventory/sv_item_inventory.gnut
blob: a768b3f3506d94e513cb58e6816fd0677f895d01 (plain)
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
global function Sv_ItemInventory_Init
global function SvPlayerInventory_ItemCount
global function PlayerInventory_CountTurrets
global function PlayerInventory_RefreshEquippedState
global function PlayerInventory_StartCriticalSection
global function PlayerInventory_EndCriticalSectionForWeaponOnEndFrame

global function PlayerInventory_PushInventoryItem
global function PlayerInventory_PushInventoryItemByBurnRef
global function PlayerInventory_PopInventoryItem
global function PlayerInventory_CountBurnRef

struct
{
	table<entity, array<InventoryItem> > playerInventoryStacks
} file

void function Sv_ItemInventory_Init()
{
	AddCallback_OnClientConnected( Sv_ItemInventory_OnClientConnected )
	AddCallback_OnPlayerRespawned( Sv_ItemInventory_OnPlayerRespawned )
}

void function Sv_ItemInventory_OnClientConnected( entity player )
{
	file.playerInventoryStacks[ player ] <- []
}

void function Sv_ItemInventory_OnPlayerRespawned( entity player )
{
	array<InventoryItem> playerInventoryStack = file.playerInventoryStacks[ player ]

	if (playerInventoryStack.len() > 0) {
		InventoryItem topInventoryItem = playerInventoryStack[playerInventoryStack.len() - 1]
		PlayerInventory_GiveInventoryItem(player, topInventoryItem)
	}

	return
}


int function SvPlayerInventory_ItemCount( entity player )
{
	return file.playerInventoryStacks[ player ].len()
}

int function PlayerInventory_CountTurrets( entity player )
{
	int turretCount = 0

	foreach ( inventoryItem in file.playerInventoryStacks[ player ] )
		if ( inventoryItem.weaponRef == "mp_ability_turretweapon" )
			turretCount += 1

	return turretCount
}

int function PlayerInventory_CountBurnRef( entity player, string burnRef )
{
	int count = 0

	foreach ( inventoryItem in file.playerInventoryStacks[ player ] )
		if ( inventoryItem.itemType == eInventoryItemType.burnmeter )
			if ( inventoryItem.burnReward.ref == burnRef )
				count += 1

	return count
}

void function PlayerInventory_TakeInventoryItem( entity player )
{
	entity preexistingWeapon = player.GetOffhandWeapon( OFFHAND_INVENTORY )
	if ( IsValid( preexistingWeapon ) )
		player.TakeWeaponNow( preexistingWeapon.GetWeaponClassName() )
}

void function PlayerInventory_GiveInventoryItem( entity player, InventoryItem inventoryItem )
{
	array<string> mods = []

	if ( inventoryItem.itemType == eInventoryItemType.burnmeter ) {
		mods.append( "burn_card_weapon_mod" )
		if ( inventoryItem.burnReward.extraWeaponMod != "" )
			mods.append( inventoryItem.burnReward.extraWeaponMod )
	}

	// ensure inventory slot isn't full to avoid crash
	PlayerInventory_TakeInventoryItem( player )

	player.GiveOffhandWeapon( inventoryItem.weaponRef, OFFHAND_INVENTORY, mods )
}

void function PlayerInventory_PushInventoryItem( entity player, InventoryItem inventoryItem )
{
	file.playerInventoryStacks[ player ].append(inventoryItem)
	player.SetPlayerNetInt( "itemInventoryCount", file.playerInventoryStacks[ player ].len() )

	PlayerInventory_GiveInventoryItem(player, inventoryItem)
}

void function PlayerInventory_PushInventoryItemByBurnRef( entity player, string burnRef )
{
	InventoryItem inventoryItem
	inventoryItem.itemType = eInventoryItemType.burnmeter
	inventoryItem.burnReward = BurnReward_GetByRef( burnRef )
	inventoryItem.weaponRef = inventoryItem.burnReward.weaponName

	PlayerInventory_PushInventoryItem(player, inventoryItem)
}

void function PlayerInventory_PopInventoryItem( entity player )
{
	array<InventoryItem> playerInventoryStack = file.playerInventoryStacks[ player ]

	if (playerInventoryStack.len() > 0) {
		InventoryItem topInventoryItem = playerInventoryStack.pop()
		player.SetPlayerNetInt( "itemInventoryCount", playerInventoryStack.len() )

		if (playerInventoryStack.len() > 0) {
			InventoryItem nextInventoryItem = playerInventoryStack[playerInventoryStack.len() - 1]
			PlayerInventory_GiveInventoryItem(player, nextInventoryItem)
		} else {
			PlayerInventory_TakeInventoryItem( player )
		}
	}

	return
}

void function PlayerInventory_RefreshEquippedState( entity player )
{

}

void function PlayerInventory_StartCriticalSection( entity player )
{

}

void function PlayerInventory_EndCriticalSectionForWeaponOnEndFrame( entity weapon )
{

}