Feature Expose equipment's stat requirements

xKiv

Active member
A few days ago I hit 200 muscle and wanted to see what items I could now newly equip. But I couldn't do it without parsing equipment.txt, but that would take me too much time (I was already spending too many night hours just trying to play over a 11kbps connection, which is another can of worms entirely).
Now that I returned home I tried looking through the source code, but all I found is a weapon_type() function, which apparently returns the requirement stat (weapon_type() uses getWeaponStat(), not getWeaponType() ...) for a piece of equipment (not just weapons) (as a "stat" type value).
There doesn't seem to be any accompanying weapon_required_stat() function, so I am requesting it, please.
 
I believe you can load the packed-by-default version of equipment.txt using file_to_map(). No need to download the text file again, just specify the file name without a path and KoLmafia will load the file for you in a map.

PHP:
string [item, int] equipment_req_data;
file_to_map("equipment.txt", equipment_req_data);
foreach it, power, req_str in equipment_req_data
{
    if ( req_str.contains_text( "Muscle" ) || req_str == "none" )
        print( "I can equip " + it );
}

Alternatively, the function you requested:
PHP:
//Add the two lines below BEFORE calling the function.
string [item, int] equipment_req_data;
file_to_map("equipment.txt", equipment_req_data);

//Returns 0 if the item is not an equipment or has no stat requirement.
int weapon_required_stat( item i )
{
	if ( equipment_req_data contains i )
	{
		foreach power, req_str in equipment_req_data[ i ]
		{
			matcher m = create_matcher( "\\d+" , req_str );
			if ( !m.find() ) return 0;
			return m.group().to_int();
		}
	}
	return 0;
}
 
Last edited:
I couldn't do it without parsing equipment.txt, but that would take me too much time (I was already spending too many night hours just trying to play over a 11kbps connection, which is another can of worms entirely).
That's the beauty of Mafia: you have a model state of the game, including all your equipment. You don't even need to be connected to parse equipment.txt and go through the items that have a 200 muscle requirement.

Aqualectrix's SmashLib already does most of the work for you. I adapted the relevant part of SmashLib to also recognize the requirement stat.

This snippet populates the equipment map with all the items in equipment.txt, as well as the required stat.
PHP:
stat string_to_stat( string it ) {
	switch( it ) {
	case "Mus": return $stat[Muscle];
	case "Mys": return $stat[Mysticality];
	case "Mox": return $stat[Moxie];
	} return $stat[none];
}
// Load equipment information from equipment.txt
record file_equip_data
{int power; string complete_requirement; string type;};
file_equip_data [item] file_equipment;
if (!file_to_map("equipment.txt",file_equipment))
{ abort("Failed to load equipment.txt"); }
// turn string requirement into int requirement with requirement stat
record equip_data
{int power; stat req_stat; int requirement;};
equip_data [item] equipment;
int index = -1;
foreach e in file_equipment
{
	equipment[e].power = file_equipment[e].power;
	
	index = index_of(file_equipment[e].complete_requirement, ":");
	if (index > 0) {
		equipment[e].req_stat = string_to_stat(substring(file_equipment[e].complete_requirement, 0,3));
		equipment[e].requirement = to_int(substring(file_equipment[e].complete_requirement, 5));
	}
	else
		equipment[e].requirement = 0;
}

EDIT: oh well, ninja'd by Phil, with RegEx.
 
The thing is, this data is already loaded in mafia's persistence database *and* there already is code to parse out the stat requirement (in EquipmentManager.canEquip()).
...
oh right, getWeaponStat *does* check the item for being a weapon after all, so (assuming that weapon_stat() shouldn't be extended to all equipment)
...
all it needs is adding something like
Code:
		params = new Type[] { DataTypes.ITEM_TYPE };
		functions.add( new LibraryFunction( "equipment_required_stat", DataTypes.INT_TYPE, params ) );

                params = new Type[] { DataTypes.ITEM_TYPE };
                functions.add( new LibraryFunction( "equipment_type", DataTypes.STAT_TYPE, params ) );
and
Code:
    public static Value equipment_required_stat( final Value item )
    {
		return new Value( EquipmentDatabase.getEquipmentRequiredStat( item.intValue() ) );
    }

    public static Value equipment_type( final Value item )
    {
		int stat = EquipmentDatabase.getEquipmentStat( item.intValue() );
		return stat == KoLConstants.MUSCLE ? DataTypes.MUSCLE_VALUE : stat == KoLConstants.MYSTICALITY ? DataTypes.MYSTICALITY_VALUE :
			stat == KoLConstants.MOXIE ? DataTypes.MOXIE_VALUE : DataTypes.STAT_INIT;
    }
in RuntimeLibrary
and
Code:
    public static final int getEquipmentRequiredStat( final int itemId )
    {
        String req = EquipmentDatabase.getEquipRequirement( itemId );

        if ( req.startsWith( "Mox:" ) )
        {
            return StringUtilities.parseInt( req.substring( 5 ) );
        }

        if ( req.startsWith( "Mys:" ) )
        {
            return StringUtilities.parseInt( req.substring( 5 ) );
        }

        if ( req.startsWith( "Mus:" ) )
        {
            return StringUtilities.parseInt( req.substring( 5 ) );
        }
        
        return KoLConstants.NONE;
    }

    public static final int getEquipmentStat( final int itemId )
    {
        String req = EquipmentDatabase.getEquipRequirement( itemId );

        if ( req.startsWith( "Mox:" ) )
        {
            return KoLConstants.MOXIE;
        }

        if ( req.startsWith( "Mys:" ) )
        {
            return KoLConstants.MYSTICALITY;
        }

        if ( req.startsWith( "Mus:" ) )
        {
            return KoLConstants.MUSCLE;
        }
        
        return KoLConstants.NONE;
    }
in EquipmentDatabase
(not tested, but at least it appears to compile, unless I copypasted something wrong)
 
Instead of the above, could this type of information be added to the item proxy record while the other items from Bale's list(s) is/are added?
- Required $stat, if any
- Required base amount of that $stat, if any
- Required class, if any

thanks :)
~ Nifft
 
Back
Top