Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved key sharing functionality #2380

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions wadsrc/static/zscript/actors/inventory/inv_misc.zs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Key : Inventory
Default
{
+DONTGIB; // Don't disappear due to a crusher
+INVENTORY.ISKEYITEM;
Inventory.InterHubAmount 0;
Inventory.PickupSound "misc/k_pkup";
}
Expand All @@ -59,28 +60,6 @@ class Key : Inventory
return false;
}

override void AttachToOwner(Actor other)
{
Super.AttachToOwner(other);

if (multiplayer && !deathmatch && sv_coopsharekeys)
{
for (int i = 0; i < MAXPLAYERS; i++)
{
if (playeringame[i])
{
let pmo = players[i].mo;

if (pmo == other)
continue;

if (!pmo.FindInventory(GetClass()))
pmo.GiveInventoryType(GetClass());
}
}
}
}

override bool ShouldStay ()
{
return !!multiplayer;
Expand Down Expand Up @@ -127,6 +106,7 @@ class PuzzleItem : Inventory
{
+NOGRAVITY
+INVENTORY.INVBAR
+INVENTORY.ISKEYITEM
Inventory.DefMaxAmount;
Inventory.UseSound "PuzzleSuccess";
Inventory.PickupSound "misc/i_pkup";
Expand Down
43 changes: 43 additions & 0 deletions wadsrc/static/zscript/actors/inventory/inventory.zs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Inventory : Actor
const BLINKTHRESHOLD = (4*32);
const BONUSADD = 6;

private bool bSharingItem; // Currently being shared (avoid infinite recursions).
private bool pickedUp[MAXPLAYERS]; // If items are set to local, track who already picked it up.

deprecated("3.7") private int ItemFlags;
Expand Down Expand Up @@ -68,6 +69,7 @@ class Inventory : Actor
flagdef AlwaysPickup: ItemFlags, 23;
flagdef Unclearable: ItemFlags, 24;
flagdef NeverLocal: ItemFlags, 25;
flagdef IsKeyItem: ItemFlags, 26;

flagdef ForceRespawnInSurvival: none, 0;
flagdef PickupFlash: none, 6;
Expand Down Expand Up @@ -258,6 +260,43 @@ class Inventory : Actor
}
}

protected void ShareItemWithPlayers(Actor giver)
{
if (bSharingItem)
return;

class<Inventory> type = GetClass();
int skip = giver && giver.player ? giver.PlayerNumber() : -1;

for (int i; i < MAXPLAYERS; ++i)
{
if (!playerInGame[i] || i == skip)
continue;

let item = Inventory(Spawn(type));
if (!item)
continue;

item.bSharingItem = true;
if (!item.CallTryPickup(players[i].mo))
{
item.Destroy();
continue;
}
item.bSharingItem = false;

if (!bQuiet)
{
PlayPickupSound(players[i].mo);
if (!bNoScreenFlash && players[i].PlayerState != PST_DEAD)
players[i].BonusCount = BONUSADD;
}
}

if (!bQuiet && consoleplayer != skip)
PrintPickupMessage(true, PickupMessage());
}

//===========================================================================
//
// Inventory :: DoRespawn
Expand Down Expand Up @@ -650,6 +689,10 @@ class Inventory : Actor
}
// [AA] Let the toucher do something with the item they've just received:
toucher.HasReceived(self);

// If the item can be shared, make sure every player gets a copy.
if (multiplayer && !deathmatch && sv_coopsharekeys && bIsKeyItem)
ShareItemWithPlayers(toucher);
}
return res, toucher;
}
Expand Down
Loading