From 06e65c769bbb66045e0d51da6a321a4c4ef1ff42 Mon Sep 17 00:00:00 2001 From: gumi Date: Sun, 22 Jul 2018 23:37:10 -0400 Subject: allow local NPC functions to be public or private --- src/map/npc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/map/npc.c') diff --git a/src/map/npc.c b/src/map/npc.c index 40ec380ee..570305ba1 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -389,7 +389,8 @@ static int npc_event_export(struct npc_data *nd, int i) Assert_ret(i >= 0 && i < nd->u.scr.label_list_num); lname = nd->u.scr.label_list[i].name; pos = nd->u.scr.label_list[i].pos; - if ((lname[0] == 'O' || lname[0] == 'o') && (lname[1] == 'N' || lname[1] == 'n')) { + + if ((nd->u.scr.label_list[i].flags & LABEL_IS_EXTERN) != 0 && (nd->u.scr.label_list[i].flags & LABEL_IS_USERFUNC) == 0) { struct event_data *ev; struct linkdb_node **label_linkdb = NULL; char buf[EVENT_NAME_LENGTH]; @@ -3054,11 +3055,11 @@ static int npc_unload(struct npc_data *nd, bool single, bool unload_mobs) aFree(nd->u.shop.shop_item); /// src check for duplicate shops. [Orcao] } else if (nd->subtype == SCRIPT) { char evname[EVENT_NAME_LENGTH]; - + snprintf(evname, ARRAYLENGTH(evname), "%s::OnNPCUnload", nd->exname); struct event_data *ev = strdb_get(npc->ev_db, evname); - + if (ev != NULL) script->run_npc(nd->u.scr.script, ev->pos, 0, nd->bl.id); /// Run OnNPCUnload. @@ -3665,6 +3666,7 @@ static void npc_convertlabel_db(struct npc_label_list *label_list, const char *f for( i = 0; i < script->label_count; i++ ) { const char* lname = script->get_str(script->labels[i].key); int lpos = script->labels[i].pos; + enum script_label_flags flags = script->labels[i].flags; struct npc_label_list* label; const char *p; size_t len; @@ -3686,6 +3688,7 @@ static void npc_convertlabel_db(struct npc_label_list *label_list, const char *f safestrncpy(label->name, lname, sizeof(label->name)); label->pos = lpos; + label->flags = flags; } } @@ -5606,7 +5609,7 @@ static int npc_reload(void) npc->npc_last_npd = NULL; npc->npc_last_path = NULL; npc->npc_last_ref = NULL; - + const int npc_new_min = npc->npc_id; struct s_mapiterator *iter = mapit_geteachiddb(); -- cgit v1.2.3-70-g09d2 From b12f1ff8be37346ec70ab50447c8f03bd9a990e1 Mon Sep 17 00:00:00 2001 From: gumi Date: Sat, 2 May 2020 09:35:01 -0400 Subject: add a config flag to allow to local functions to be event handlers --- conf/map/script.conf | 5 +++++ src/map/npc.c | 4 +++- src/map/script.c | 2 ++ src/map/script.h | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src/map/npc.c') diff --git a/conf/map/script.conf b/conf/map/script.conf index cda25d546..4eb84edf4 100644 --- a/conf/map/script.conf +++ b/conf/map/script.conf @@ -64,6 +64,11 @@ script_configuration: { // "public" keyword should be treated as "private" by default. // Default: true functions_private_by_default: true + + // Specifies whether public functions can be invoked as NPC events. This + // allows, for example, to use a `public function OnDeath { ... }` instead + // of a `OnDeath:` label for mob death events. + functions_as_events: false } import: "conf/import/script.conf" diff --git a/src/map/npc.c b/src/map/npc.c index 570305ba1..2f03623e4 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -390,7 +390,9 @@ static int npc_event_export(struct npc_data *nd, int i) lname = nd->u.scr.label_list[i].name; pos = nd->u.scr.label_list[i].pos; - if ((nd->u.scr.label_list[i].flags & LABEL_IS_EXTERN) != 0 && (nd->u.scr.label_list[i].flags & LABEL_IS_USERFUNC) == 0) { + if ((nd->u.scr.label_list[i].flags & LABEL_IS_EXTERN) != 0 + && ((nd->u.scr.label_list[i].flags & LABEL_IS_USERFUNC) == 0 + || script->config.functions_as_events)) { struct event_data *ev; struct linkdb_node **label_linkdb = NULL; char buf[EVENT_NAME_LENGTH]; diff --git a/src/map/script.c b/src/map/script.c index 6f21f2a82..bf9348645 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -4973,6 +4973,7 @@ static bool script_config_read(const char *filename, bool imported) libconfig->setting_lookup_bool_real(setting, "warn_func_mismatch_paramnum", &script->config.warn_func_mismatch_paramnum); libconfig->setting_lookup_bool_real(setting, "warn_func_mismatch_argtypes", &script->config.warn_func_mismatch_argtypes); libconfig->setting_lookup_bool_real(setting, "functions_private_by_default", &script->config.functions_private_by_default); + libconfig->setting_lookup_bool_real(setting, "functions_as_events", &script->config.functions_as_events); libconfig->setting_lookup_int(setting, "check_cmdcount", &script->config.check_cmdcount); libconfig->setting_lookup_int(setting, "check_gotocount", &script->config.check_gotocount); libconfig->setting_lookup_int(setting, "input_min_value", &script->config.input_min_value); @@ -28576,6 +28577,7 @@ void script_defaults(void) script->config.ontouch2_name = "OnTouch"; //ontouch2_name (run whenever a char walks into the OnTouch area) script->config.onuntouch_name = "OnUnTouch"; //onuntouch_name (run whenever a char walks from the OnTouch area) script->config.functions_private_by_default = true; + script->config.functions_as_events = false; // for ENABLE_CASE_CHECK script->calc_hash_ci = calc_hash_ci; diff --git a/src/map/script.h b/src/map/script.h index 0a6a21f1b..5a031e9f5 100644 --- a/src/map/script.h +++ b/src/map/script.h @@ -585,6 +585,7 @@ struct Script_Config { bool warn_func_mismatch_argtypes; bool warn_func_mismatch_paramnum; bool functions_private_by_default; + bool functions_as_events; int check_cmdcount; int check_gotocount; int input_min_value; -- cgit v1.2.3-70-g09d2