From 07c18f248b9e2db1ecc31f7deaa9e5ee34e4d28a Mon Sep 17 00:00:00 2001 From: HAO YAN Date: Fri, 21 Oct 2022 00:25:49 +0800 Subject: [PATCH] Add getrandmobid script command (#7309) * Used to generate random monster ID in NPC scripts. --- doc/script_commands.txt | 26 ++++++++++++++++++++ src/map/script.cpp | 47 ++++++++++++++++++++++++++++++++++++ src/map/script_constants.hpp | 9 +++++++ 3 files changed, 82 insertions(+) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 44034a896d..bf442fc718 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -3583,6 +3583,32 @@ This command does not count skills which are set as flag 4 (permament granted) ( --------------------------------------- +*getrandmobid({,{,}}) + +This command returns a random monster ID from the random monster group. +With you can apply certain restrictions which monsters of the group can be returned. +Returns 0 if one of the parameters is invalid or no monster could be found with the given parameters. + +Valid are: + MOBG_BRANCH_OF_DEAD_TREE + MOBG_PORING_BOX + MOBG_BLOODY_DEAD_BRANCH + MOBG_RED_POUCH_OF_SURPRISE + MOBG_CLASSCHANGE + MOBG_TAEKWON_MISSION + +Valid are: + RMF_NONE = 0x00 - Apply no flags + RMF_DB_RATE = 0x01 - Apply the summon success chance found in the list (otherwise get any monster from the db) + RMF_CHECK_MOB_LV = 0x02 - Apply a monster level check + RMF_MOB_NOT_BOSS = 0x04 - Selected monster should not be a Boss type (default) + - (except those from MOBG_BLOODY_DEAD_BRANCH) + RMF_MOB_NOT_SPAWN = 0x08 - Selected monster must have normal spawn + RMF_MOB_NOT_PLANT = 0x10 - Selected monster should not be a Plant type + RMF_ALL = 0xFF - Apply all flags + +--------------------------------------- + *getmonsterinfo(,) *getmonsterinfo(,) diff --git a/src/map/script.cpp b/src/map/script.cpp index 6f1a88aa9a..687567b595 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -18226,6 +18226,52 @@ BUILDIN_FUNC(delmonsterdrop) } + +/*========================================== + * Returns a random mob_id + * type: Where to fetch from (see enum e_random_monster) + * flag: Type of checks to apply (see enum e_random_monster_flags) + * lv: Mob level to check against + *------------------------------------------*/ +BUILDIN_FUNC(getrandmobid) +{ + int type = script_getnum(st, 2); + + if (type < MOBG_BRANCH_OF_DEAD_TREE || type >= MOBG_MAX) { + ShowWarning("buildin_getrandmobid: Invalid type %d.\n", type); + script_pushint(st, 0); + return SCRIPT_CMD_FAILURE; + } + + int flag = script_hasdata(st, 3) ? script_getnum(st, 3) : RMF_MOB_NOT_BOSS; + if (flag < RMF_NONE || flag > RMF_ALL) { + ShowWarning("buildin_getrandmobid: Invalid flag %d.\n", flag); + script_pushint(st, 0); + return SCRIPT_CMD_FAILURE; + } + + int lv; + if ( script_hasdata(st, 4) ) { + lv = script_getnum(st, 4); + + if (lv <= 0) { + ShowWarning("buildin_getrandmobid: Invalid level %d.\n", lv); + script_pushint(st, 0); + return SCRIPT_CMD_FAILURE; + } + + // If a level is provided, make sure it is respected + flag |= RMF_CHECK_MOB_LV; + } else { + lv = MAX_LEVEL; + } + + script_pushint(st, mob_get_random_id(type, (enum e_random_monster_flags)flag, lv)); + + return SCRIPT_CMD_SUCCESS; +} + + /*========================================== * Returns some values of a monster [Lupus] * Name, Level, race, size, etc... @@ -27093,6 +27139,7 @@ struct script_function buildin_func[] = { BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus + BUILDIN_DEF(getrandmobid, "i??"), BUILDIN_DEF(getmonsterinfo,"vi"), //Lupus BUILDIN_DEF(addmonsterdrop,"vii??"), //Akinari [Lupus] BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus] diff --git a/src/map/script_constants.hpp b/src/map/script_constants.hpp index 83bf56eaad..7f7b7c3e12 100644 --- a/src/map/script_constants.hpp +++ b/src/map/script_constants.hpp @@ -4944,6 +4944,15 @@ export_constant(MOBG_CLASSCHANGE); export_constant(MOBG_TAEKWON_MISSION); + /* mob random groups flags */ + export_constant(RMF_NONE); + export_constant(RMF_DB_RATE); + export_constant(RMF_CHECK_MOB_LV); + export_constant(RMF_MOB_NOT_BOSS); + export_constant(RMF_MOB_NOT_SPAWN); + export_constant(RMF_MOB_NOT_PLANT); + export_constant(RMF_ALL); + /* random option attributes */ export_constant(ROA_ID); export_constant(ROA_VALUE);