Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NPC behavior] reduce NPC_BSFlee random range #1129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

arabusov
Copy link

@arabusov arabusov commented Nov 9, 2022

Irand fails when the requested random range is not below QRAND_MAX, which is 2^15 as it is right now. NPC_BSFlee calls irand (via Q_irand) by requesting the random range within [10000, 50000] which is of course below 2^15 and the game crashes.

Below is my stack trace in gdb:

    4  0x00007ffff784a486 in __assert_fail () from /usr/lib/libc.so.6
    5  0x00007fffd18e5b6c in irand (min=10000, max=50000) at /opt/games/git-openjk/shared/qcommon/q_math.c:252
    6  0x00007fffd18e5bcc in Q_irand (value1=10000, value2=50000) at /opt/games/git-openjk/shared/qcommon/q_math.c:264
    7  0x00007fffd177c34b in NPC_BSFlee () at /opt/games/git-openjk/code/game/NPC_behavior.cpp:1748
    8  0x00007fffd1776525 in NPC_RunBehavior (team=2, bState=10) at /opt/games/git-openjk/code/game/NPC.cpp:2055
    9  0x00007fffd1776837 in NPC_ExecuteBState (self=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/NPC.cpp:2185
    10 0x00007fffd1777472 in NPC_Think (self=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/NPC.cpp:2494
    11 0x00007fffd16d83ce in GEntity_ThinkFunc (self=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/g_functions.cpp:67
    12 0x00007fffd16e5ac4 in G_RunThink (ent=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/g_main.cpp:1075
    13 0x00007fffd16e84fa in G_RunFrame (levelTime=578050) at /opt/games/git-openjk/code/game/g_main.cpp:2055
    14 0x00005555555d9d91 in SV_Frame (msec=11, fractionMsec=0) at /opt/games/git-openjk/code/server/sv_main.cpp:513
    15 0x00005555555b0740 in Com_Frame () at /opt/games/git-openjk/code/qcommon/common.cpp:1418
    16 0x000055555562a897 in main (argc=1, argv=0x7fffffffe2c8) at /opt/games/git-openjk/shared/sys/sys_main.cpp:812
    (gdb) frame 5
    5  0x00007fffd18e5b6c in irand (min=10000, max=50000) at /opt/games/git-openjk/shared/qcommon/q_math.c:252
    252             assert((max - min) < QRAND_MAX);

Irand fails when the requested random range is not below QRAND_MAX,
which is 2**15 as it is right now. NPC_BSFlee calls irand (via Q_irand)
by requesting the random range within [10000, 50000] which is of course
below 2**15 and the game crashes.

Below is my stack trace in gdb:

4  0x00007ffff784a486 in __assert_fail () from /usr/lib/libc.so.6
5  0x00007fffd18e5b6c in irand (min=10000, max=50000) at /opt/games/git-openjk/shared/qcommon/q_math.c:252
6  0x00007fffd18e5bcc in Q_irand (value1=10000, value2=50000) at /opt/games/git-openjk/shared/qcommon/q_math.c:264
7  0x00007fffd177c34b in NPC_BSFlee () at /opt/games/git-openjk/code/game/NPC_behavior.cpp:1748
8  0x00007fffd1776525 in NPC_RunBehavior (team=2, bState=10) at /opt/games/git-openjk/code/game/NPC.cpp:2055
9  0x00007fffd1776837 in NPC_ExecuteBState (self=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/NPC.cpp:2185
10 0x00007fffd1777472 in NPC_Think (self=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/NPC.cpp:2494
11 0x00007fffd16d83ce in GEntity_ThinkFunc (self=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/g_functions.cpp:67
12 0x00007fffd16e5ac4 in G_RunThink (ent=0x7fffd1a94ac8 <g_entities+94248>) at /opt/games/git-openjk/code/game/g_main.cpp:1075
13 0x00007fffd16e84fa in G_RunFrame (levelTime=578050) at /opt/games/git-openjk/code/game/g_main.cpp:2055
14 0x00005555555d9d91 in SV_Frame (msec=11, fractionMsec=0) at /opt/games/git-openjk/code/server/sv_main.cpp:513
15 0x00005555555b0740 in Com_Frame () at /opt/games/git-openjk/code/qcommon/common.cpp:1418
16 0x000055555562a897 in main (argc=1, argv=0x7fffffffe2c8) at /opt/games/git-openjk/shared/sys/sys_main.cpp:812
(gdb) frame 5
5  0x00007fffd18e5b6c in irand (min=10000, max=50000) at /opt/games/git-openjk/shared/qcommon/q_math.c:252
252		assert((max - min) < QRAND_MAX);
code/game/NPC_behavior.cpp Outdated Show resolved Hide resolved
@Razish Razish requested a review from a team March 3, 2024 10:08
@arabusov
Copy link
Author

arabusov commented Apr 2, 2024

@Razish my memory fades, but as far as I remember, this change was causing unarmed siths to grab E11 rifles from time to time in the single player mode. I kept it because otherwise the game was just suddenly crashing, but maybe there's a smarter way of fixing the issue.

Copy link
Contributor

@Daggolin Daggolin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to Q_irand(1000, 5000) * 10 roughly matches the original intention of the code (with a variation of 10 msec missing), but not necessarily the original release build behavior (without the assert). Seems fine to me anyway.

@arabusov
Copy link
Author

arabusov commented Apr 9, 2024

@Daggolin I'm happy with the @Razish improvement. Additionally, one could call the Q_irand function the second time in the [0, 9] range and add the result to the previous value, but it may slow down the game (probably it is negligible).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants