Death usecode

NOTICE: This forum is archived as read only.
Please use the Github Discussions at https://github.com/exult/exult/discussions
Forum rules
NOTICE: This forum is archived as read only.
Please use the Github Discussions at https://github.com/exult/exult/discussions
Locked
Donfrow
Posts: 308
Joined: Thu May 14, 2020 1:34 pm

Death usecode

Post by Donfrow »

So, as reported in the other thread about Glimmerscape I seem to be encountering some issues with death in my mod. I'm rather stumped about what I can do to fix this, though I have some suspicions as to the cause (in a high level sense).

When the Avatar dies, nearly every time it almost seems like some other "mode" of the game is getting stuck? I don't really know how to best describe it. The biggest thing that shows something is wrong is that hot keys don't work. I also noticed if I try and bring up Exult Studio (Ctrl-Alt-M) and double click the Avatar, it brings up the save/load screen rather than the Avatar's properties.

The movement of the Avatar becomes somewhat strange as well, where if you use right click to move before death you have a "free" movement where you can adjust the Avatar's direction but after death the Avatar has to walk someone to the selected destination before you can change the direction. It's a bit hard to explain so I hope that makes sense.

I'm not sure why this would be occurring. As a test I gutted my entire death usecode down to its most basic form to try to eliminate as much of my code as possible:

void Avatar_Death object#(0x400)()
{
UI_move_object(PARTY, [1745, 1975, 0], TRUE);
var max_health = UI_get_npc_prop(AVATAR, STRENGTH);
var current_health = UI_get_npc_prop(AVATAR, HEALTH);
UI_set_npc_prop(AVATAR, HEALTH, max_health-current_health-1);
UI_clear_item_flag(AVATAR, ASLEEP);
}

This issue also seems to cause issues with saved games that were initiated with the original usecode but subsequently replaced. If you save the game and either Journey Onward or restart, you get a Microsoft Visual C++ Runtime Library error:

Assertion failed!
Program C:\Program Files (x86)\Exult\Exult.exe
File gamemap.h
Line: 130

Expression: (cx >= 0) && (cx = 0) && (cy < c_num_chunks)

When trying to recreate this if I update the usecode on one of the erroring versions of death from my original base code on death, to the testing code (as outlined above) the issue doesn't seem to arise, which to me points to something in my "normal" death code but this only happens if you start an entirely new game with the new usecode. If I replace the normal death usecode after a death with my testing death usecode and a death has already the issue arises. It seems like my code is allowing me "one free death" before it bugs out and it starts on the second death. The first death black fade in/fade out is actually different on the first death, and doesn't appear on subsequent deaths either (same code), which further leads me to believe I'm doing something that the engine doesn't like.

I do use static variables in my regular death usecode which is what I think may be the root cause somehow. This is used in scripting blocks so I can move the Avatar since I never managed to pass variables to function calls within a script block, so I function that includes a move object such as:

UI_move_object(PARTY, [avatar_dead_x, avatar_dead_y, avatar_dead_z], TRUE);

where avatar_dead_x, avatar_dead_u and avatar_dead_z are static variables I set based on where the Avatar should appear after dying, based on where the Avatar died, for example:

avatar_dead_x = 1749;
avatar_dead_y = 1957;
avatar_dead_z = 0;

which I declare in my base usecode file

static var avatar_dead_x;
static var avatar_dead_y;
static var avatar_dead_z;

Sorry for the long winded post, but if anyone has any thoughts as to what could possibly be causing this it would be appreciated. The full code of the death usecode is in my mod source in avatar_death.uc if anyone has thoughts or is interested in taking a look.

Thanks, as usual!
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: Death usecode

Post by Knight Captain »

I believe you need to clear the Avatar's NPC DEAD flag, and possibly the CANT_MOVE flag as well. If I remember right, if the Avatar tries to move when DEAD it will crash the engine. There is also a global flag for the Avatar's death as well.

It's been a while since I've looked at this bit of code, but I moved the Avatar's death into its own function to help troubleshoot it (and to make the code cleaner).

One thing for me that is a huge help was writing to stdout.txt via Usecode with the UI_error_message command.
Donfrow
Posts: 308
Joined: Thu May 14, 2020 1:34 pm

Re: Death usecode

Post by Donfrow »

Well, after some initial testing of setting the flag for DEAD, then un-setting it at the end of my sequence, my initial tests are showing positive results in that I haven't have the strange behaviour. As usual KC, you seem to have caught the issue.

The strange thing is if I didn't use static variables and simply hard coded the death location I was never able to reproduce the issue but when I used my static variables it was happening each time. With modifying that flag so far it is working with the static variables. Not sure I understand why the change but so far it seems to be working.

Another odd thing is in my death usecode I never set the DEAD flag to begin with and I was under the impression 0x400 was the Avatar death usecode which I replaced with my own. If I only cleared the flag it would cause issues with death sequence and seemingly lock it up completely (would never call my unfade function). If I set it this flag earlier and then cleared it later in the death sequence it seems like it's working.

Hopefully after some more testing my main issue doesn't come back up and it was completely because of that simple flag but this entire situation has somewhat baffled me. As long as it works I suppose!

Thanks for the tip on the UI_error_message.
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: Death usecode

Post by Knight Captain »

My guess is the engine (as in the original) sets the DEAD flag on the Avatar, so it happens without being explicitly called via Usecode.

Glad to help!
Dale
Posts: 176
Joined: Thu May 14, 2020 1:34 pm

Re: Death usecode

Post by Dale »

You are both great!
Locked