From 3b951b85786725285776099b110a655a62a78143 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Thu, 22 Dec 2022 20:36:31 +0300 Subject: [PATCH] Add opSetWeaponAmmoPid --- src/sfall_opcodes.cc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/sfall_opcodes.cc b/src/sfall_opcodes.cc index 50e7d59..d746f93 100644 --- a/src/sfall_opcodes.cc +++ b/src/sfall_opcodes.cc @@ -163,6 +163,30 @@ static void opGetWeaponAmmoPid(Program* program) programStackPushInteger(program, pid); } +// There are two problems with this function. +// +// 1. Sfall's implementation changes ammo PID of misc items, which is impossible +// since it's stored in proto, not in the object. +// 2. Changing weapon's ammo PID is done without checking for ammo +// quantity/capacity which can probably lead to bad things. +// +// set_weapon_ammo_pid +static void opSetWeaponAmmoPid(Program* program) +{ + int ammoTypePid = programStackPopInteger(program); + Object* obj = static_cast(programStackPopPointer(program)); + + if (obj != nullptr) { + if (PID_TYPE(obj->pid) == OBJ_TYPE_ITEM) { + switch (itemGetType(obj)) { + case ITEM_TYPE_WEAPON: + obj->data.item.weapon.ammoTypePid = ammoTypePid; + break; + } + } + } +} + // get_weapon_ammo_count static void opGetWeaponAmmoCount(Program* program) { @@ -297,6 +321,7 @@ void sfallOpcodesInit() interpreterRegisterOpcode(0x8211, opGetVersionMinor); interpreterRegisterOpcode(0x8212, opGetVersionPatch); interpreterRegisterOpcode(0x8217, opGetWeaponAmmoPid); + interpreterRegisterOpcode(0x8218, opSetWeaponAmmoPid); interpreterRegisterOpcode(0x8219, opGetWeaponAmmoCount); interpreterRegisterOpcode(0x821A, opSetWeaponAmmoCount); interpreterRegisterOpcode(0x821C, opGetMouseX);