diff --git a/source/duke3d/src/gamedef.cpp b/source/duke3d/src/gamedef.cpp index 5c93f1e12..93bb0ba72 100644 --- a/source/duke3d/src/gamedef.cpp +++ b/source/duke3d/src/gamedef.cpp @@ -181,6 +181,9 @@ static tokenmap_t const vm_keywords[] = { "displayrandvar", CON_DISPLAYRANDVAR }, { "displayrandvarvar", CON_DISPLAYRANDVARVAR }, { "dist", CON_DIST }, + { "divr", CON_DIVR }, + { "divrd", CON_DIVVARVAR }, // div round toward zero -- alias until proven otherwise + { "divru", CON_DIVRU }, { "divscale", CON_DIVSCALE }, { "divvar", CON_DIVVAR }, { "divvarvar", CON_DIVVARVAR }, @@ -4080,6 +4083,8 @@ DO_DEFSTATE: case CON_PREVSPRITESECT: case CON_NEXTSPRITESECT: case CON_SECTOROFWALL: + case CON_DIVR: + case CON_DIVRU: C_GetNextVarType(GAMEVAR_READONLY); fallthrough__; case CON_ADDLOGVAR: diff --git a/source/duke3d/src/gamedef.h b/source/duke3d/src/gamedef.h index 669b4d84b..86516ee38 100644 --- a/source/duke3d/src/gamedef.h +++ b/source/duke3d/src/gamedef.h @@ -1169,6 +1169,8 @@ enum ScriptKeywords_t CON_STARTSCREEN, // 409 CON_SCREENPAL, // 410 CON_QSTRCMP, // 411 + CON_DIVR, // 412 + CON_DIVRU, // 413 CON_END }; // KEEPINSYNC with the keyword list in lunatic/con_lang.lua diff --git a/source/duke3d/src/gameexec.cpp b/source/duke3d/src/gameexec.cpp index 730567ad5..feaa5d808 100644 --- a/source/duke3d/src/gameexec.cpp +++ b/source/duke3d/src/gameexec.cpp @@ -4886,6 +4886,42 @@ finish_qsprintf: continue; } + case CON_DIVR: // div round to nearest + insptr++; + { + tw = *insptr++; + + int const dividend = Gv_GetVarX(tw); + int const divisor = Gv_GetVarX(*insptr++); + + if (EDUKE32_PREDICT_FALSE(!divisor)) + { + CON_CRITICALERRPRINTF("divide by zero!\n"); + continue; + } + + Gv_SetVarX(tw, tabledivide32((dividend - ksgn(dividend) * klabs(divisor / 2)), divisor)); + continue; + } + + case CON_DIVRU: // div round away from zero + insptr++; + { + tw = *insptr++; + + int const dividend = Gv_GetVarX(tw); + int const divisor = Gv_GetVarX(*insptr++); + + if (EDUKE32_PREDICT_FALSE(!divisor)) + { + CON_CRITICALERRPRINTF("divide by zero!\n"); + continue; + } + + Gv_SetVarX(tw, tabledivide32((dividend - ksgn(dividend) * klabs(divisor) + 1), divisor)); + continue; + } + case CON_MODVARVAR: insptr++; {