Mod keys now attach their replacements, which are hidden. This should fix a lot of dumb scripts that use CheckInventory to get keys.

This commit is contained in:
Mari the Deer 2021-02-17 19:39:44 +01:00
commit ea74370463
5 changed files with 58 additions and 6 deletions

View file

@ -935,10 +935,31 @@ Class SWWMStatusBar : BaseStatusBar
int colc = 0;
double colh = 0;
int n = Key.GetKeyTypeCount();
Array<Key> klist;
for ( int i=0; i<n; i++ )
{
let k = Key(CPlayer.mo.FindInventory(Key.GetKeyType(i)));
if ( !k || !k.Icon.IsValid() ) continue;
klist.Push(k);
}
// because we can't call GetReplacement from ui, let's wrangle something real ugly here to clean up replaced keys
for ( int i=0; i<klist.Size(); i++ )
{
if ( !(klist[i] is 'SWWMKey') ) continue;
// remove the key this replaces
Class<Key> pc = klist[i].Species;
if ( !pc ) continue;
for ( int j=0; j<klist.Size(); j++ )
{
if ( klist[j].GetClass() != pc ) continue;
klist.Delete(j);
j--;
if ( i >= j ) i--;
}
}
for ( int i=0; i<klist.Size(); i++ )
{
let k = klist[i];
Vector2 siz = TexMan.GetScaledSize(k.Icon);
Screen.DrawTexture(k.Icon,false,keypos.x-siz.x,keypos.y,DTA_VirtualWidthF,ss.x,DTA_VirtualHeightF,ss.y,DTA_KeepRatio,true);
keypos.x -= siz.x+2;

View file

@ -2218,7 +2218,23 @@ Class DemolitionistMenu : GenericMenu
for ( int i=0; i<n; i++ )
{
let k = Key(players[consoleplayer].mo.FindInventory(Key.GetKeyType(i)));
if ( k ) invlist.Push(k);
if ( !k ) continue;
invlist.Push(k);
}
// because we can't call GetReplacement from ui, let's wrangle something real ugly here to clean up replaced keys
for ( int i=0; i<invlist.Size(); i++ )
{
if ( !(invlist[i] is 'SWWMKey') ) continue;
// remove the key this replaces
Class<Key> pc = invlist[i].Species;
if ( !pc ) continue;
for ( int j=0; j<invlist.Size(); j++ )
{
if ( invlist[j].GetClass() != pc ) continue;
invlist.Delete(j);
j--;
if ( i >= j ) i--;
}
}
// collectibles, sorted by name
Array<Inventory> cols;

View file

@ -23,6 +23,22 @@ Class SWWMKey : Key abstract
}
}
override void AttachToOwner( Actor other )
{
Super.AttachToOwner(other);
// also attach the vanilla key that we replace, mainly for script compatibility
Class<Key> pc = Species;
if ( pc )
{
let p = Inventory(Spawn(pc));
if ( Owner is 'Demolitionist' )
Demolitionist(Owner).key_reentrant = true; // avoid infinite loop
p.AttachToOwner(Owner);
if ( Owner is 'Demolitionist' )
Demolitionist(Owner).key_reentrant = false;
}
}
override bool Use( bool pickup )
{
if ( Owner.player && !propagated )

View file

@ -2004,13 +2004,12 @@ Class Demolitionist : PlayerPawn
override void AddInventory( Inventory item )
{
// hackaround for replaced keys
if ( item is 'Key' )
if ( (item is 'Key') && !key_reentrant )
{
let rep = (Class<Inventory>)(GetReplacement(item.GetClass()));
if ( rep && (item.GetClass() != rep) )
{
item.Destroy();
// add the new key instead
// also add the new key
Actor whom = player?player.mo:PlayerPawn(self);
if ( !whom.FindInventory(rep) )
{