Skip to content

Commit 17383db

Browse files
committed
Fixed set_unique_id script function
* it unassigned unique ID even when the object doesn't have one. * it didn't sync the attached script's owner id after unassignment. Added a safeguard to prevent item_identical function returning false when comparing a unique_id item to itself. (ref. fallout2-ce/fallout2-ce#466)
1 parent d74ec5f commit 17383db

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

sfall/Modules/Objects.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bool Objects::IsUniqueID(long id) {
3535
return (id > UniqueID::Start || (id >= fo::PLAYER_ID && id < 83536)); // 65535 maximum possible number of prototypes
3636
}
3737

38-
static void SetScriptObjectID(fo::GameObject* obj) {
38+
void Objects::SetScriptObjectID(fo::GameObject* obj) {
3939
fo::ScriptInstance* script;
4040
if (fo::func::scr_ptr(obj->scriptId, &script) != -1) {
4141
script->ownerObjectId = obj->id;
@@ -74,8 +74,11 @@ void Objects::SetNewEngineID(fo::GameObject* obj) {
7474
}
7575

7676
static __declspec(naked) void item_identical_hack() {
77+
static const DWORD item_identical_End = 0x477AD5;
7778
using namespace fo::Fields;
7879
__asm {
80+
cmp esi, edi; // inv_item == place_item? (safeguard)
81+
je identical;
7982
mov ecx, [edi]; // place_item id
8083
cmp ecx, Start; // start unique ID
8184
jg notIdentical;
@@ -87,6 +90,9 @@ static __declspec(naked) void item_identical_hack() {
8790
cmp eax, ebx;
8891
notIdentical:
8992
retn; // if ZF == 0 then item is not identical
93+
identical:
94+
add esp, 4;
95+
jmp item_identical_End;
9096
}
9197
}
9298

@@ -111,7 +117,7 @@ static void map_fix_critter_id() {
111117
if (obj->IsCritter()) {
112118
if (obj->id < fo::PLAYER_ID) {
113119
obj->id = npcStartID++;
114-
SetScriptObjectID(obj);
120+
Objects::SetScriptObjectID(obj);
115121
}
116122
Stats::UpdateHPStat(obj);
117123
}

sfall/Modules/Objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Objects : public Module {
3636
static long uniqueID;
3737

3838
static bool IsUniqueID(long id);
39+
static void SetScriptObjectID(fo::GameObject* obj);
3940

4041
static long __fastcall SetObjectUniqueID(fo::GameObject* obj);
4142
static long __fastcall SetSpecialID(fo::GameObject* obj);

sfall/Modules/Scripting/Handlers/Objects.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -584,14 +584,16 @@ void mf_set_drugs_data(OpcodeContext& ctx) {
584584

585585
void mf_set_unique_id(OpcodeContext& ctx) {
586586
fo::GameObject* obj = ctx.arg(0).object();
587-
long id;
588-
if (ctx.arg(1).rawValue() == -1) {
589-
id = fo::func::new_obj_id();
590-
obj->id = id;
591-
} else {
592-
id = Objects::SetObjectUniqueID(obj);
587+
if (ctx.numArgs() > 1 && ctx.arg(1).rawValue() == -1) {
588+
// unassign unique ID only if the object currently has one
589+
if (obj->id > UniqueID::Start) {
590+
obj->id = fo::func::new_obj_id();
591+
Objects::SetScriptObjectID(obj);
592+
}
593+
ctx.setReturn(obj->id);
594+
return;
593595
}
594-
ctx.setReturn(id);
596+
ctx.setReturn(Objects::SetObjectUniqueID(obj));
595597
}
596598

597599
void mf_objects_in_radius(OpcodeContext& ctx) {

0 commit comments

Comments
 (0)