diff options
Diffstat (limited to 'src/map/script.c')
-rw-r--r-- | src/map/script.c | 267 |
1 files changed, 230 insertions, 37 deletions
diff --git a/src/map/script.c b/src/map/script.c index 069b98eef..743a1779a 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -1240,6 +1240,80 @@ static const char *parse_variable(const char *p) return p; } +/** + * Converts a number expression literal to an actual integer. + * Number separators are skipped. + * + * expects these formats: + * 1337 + * 0x1337 + * 0b1001 + * 0o1337 + * + * example with separating nibbles of a binary literal: + * 0b1101_0111_1001_1111 + * + * @param p - a pointer to the first char of the number literal + * @param lli - a pointer to the resulting long long integer + * @returns a pointer to the first char after the parsed number +*/ +static const char *parse_number(const char *p, long long *lli) { + nullpo_retr(NULL, p); + + const bool unary_plus = (*p == '+'); + const bool unary_minus = (*p == '-'); + + if (unary_plus || unary_minus) { + p++; + } + + if (ISNSEPARATOR(*p)) { + disp_error_message("parse_number: number literals cannot begin with a separator", p); + } + +#define PARSENUMBER(skip, func, radix) \ + for (p += skip; func(*p) || (ISNSEPARATOR(*p) && (func(p[1]) || ISNSEPARATOR(p[1]))); ++p) { \ + if (func(*p)) { \ + *lli *= radix; \ + *lli += (*p < 'A') ? (*p & 0xF) : (9 + (*p & 0x7)); \ + } else if (ISNSEPARATOR(p[1])) { \ + disp_error_message("parse_number: number literals cannot contain two separators in a row", p + 1); \ + } \ + } + + if (*p == '0' && p[1] == 'x') { + PARSENUMBER(2, ISXDIGIT, 16); + } else if (*p == '0' && p[1] == 'o') { + PARSENUMBER(2, ISODIGIT, 8); + } else if (*p == '0' && p[1] == 'b') { + PARSENUMBER(2, ISBDIGIT, 2); + } else { + PARSENUMBER(0, ISDIGIT, 10); + } + +#undef PARSENUMBER + + if (ISNSEPARATOR(*p)) { + disp_error_message("parse_number: number literals cannot end with a separator", p); + } + + if (unary_minus) { + // reverse the sign + *lli = -(*lli); + } + + // make sure we can't underflow/overflow + if (*lli < INT_MIN) { + *lli = INT_MIN; + script->disp_warning_message("parse_number: underflow detected, capping value to INT_MIN", p); + } else if (*lli > INT_MAX) { + *lli = INT_MAX; + script->disp_warning_message("parse_number: overflow detected, capping value to INT_MAX", p); + } + + return p; +} + /* * Checks whether the gives string is a number literal * @@ -1256,24 +1330,44 @@ static const char *parse_variable(const char *p) static bool is_number(const char *p) { const char *np; - if (!p) - return false; - if (*p == '-' || *p == '+') + nullpo_retr(false, p); + + if (*p == '-' || *p == '+') { p++; + } + np = p; + if (*p == '0' && p[1] == 'x') { - p+=2; - np = p; - // Hexadecimal - while (ISXDIGIT(*np)) + // Hexadecimal: 0xFFFF + np = (p += 2); + while (ISXDIGIT(*np) || ISNSEPARATOR(*np)) { np++; - } else { - // Decimal - while (ISDIGIT(*np)) + } + } else if (*p == '0' && p[1] == 'b') { + // Binary: 0b0001 + np = (p += 2); + while (ISBDIGIT(*np) || ISNSEPARATOR(*np)) { np++; + } + } else if (*p == '0' && p[1] == 'o') { + // Octal: 0o1500 + np = (p += 2); + while (ISODIGIT(*np) || ISNSEPARATOR(*np)) { + np++; + } + } else if (ISDIGIT(*p)) { + // Decimal: 1234 + while (ISDIGIT(*np) || ISNSEPARATOR(*np)) { + np++; + } } - if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _ + + if (p != np && *np != '_' && !ISALPHA(*np)) { + // At least one digit, and next isn't a letter or _ return true; + } + return false; } @@ -1367,21 +1461,9 @@ static const char *parse_simpleexpr_paren(const char *p) static const char *parse_simpleexpr_number(const char *p) { - char *np = NULL; - long long lli; - - nullpo_retr(NULL, p); - while (*p == '0' && ISDIGIT(p[1])) - p++; // Skip leading zeros, we don't support octal literals + long long lli = 0; + const char *np = parse_number(p, &lli); - lli = strtoll(p, &np, 0); - if (lli < INT_MIN) { - lli = INT_MIN; - script->disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN", p); - } else if (lli > INT_MAX) { - lli = INT_MAX; - script->disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX", p); - } script->addi((int)lli); // Cast is safe, as it's already been checked for overflows return np; @@ -3610,8 +3692,8 @@ static int set_reg(struct script_state *st, struct map_session_data *sd, int64 n const char *str = (const char*)value; if (script->is_permanent_variable(name) && strlen(str) > SCRIPT_STRING_VAR_LENGTH) { - ShowError("script:set_reg: Value of variable %s is too long: %lu! Maximum is %d. Skipping...\n", - name, strlen(str), SCRIPT_STRING_VAR_LENGTH); + ShowError("script:set_reg: Value of variable %s is too long: %d! Maximum is %d. Skipping...\n", + name, (int)strlen(str), SCRIPT_STRING_VAR_LENGTH); if (st != NULL) { script->reportsrc(st); @@ -9053,22 +9135,71 @@ static BUILDIN(delitemidx) return true; } -/*========================================== - * Enables/Disables use of items while in an NPC [Skotlex] - *------------------------------------------*/ +/** + * Enable item actions while interacting with NPC. + * + * @code{.herc} + * enableitemuse({<flag>}); + * enable_items({<flag>}); + * @endcode + * + **/ static BUILDIN(enableitemuse) { + int flag = battle_config.item_enabled_npc; + + if (script_hasdata(st, 2)) { + if (!script_isinttype(st, 2)) + return true; + + flag = script_getnum(st, 2); + } + + if (flag < 0) + return true; + struct map_session_data *sd = script->rid2sd(st); - if (sd != NULL) - st->npc_item_flag = sd->npc_item_flag = 1; + + if (sd == NULL) + return true; + + st->npc_item_flag |= flag; + sd->npc_item_flag |= flag; + return true; } +/** + * Disable item actions while interacting with NPC. + * + * @code{.herc} + * disableitemuse({<flag>}); + * disable_items({<flag>}); + * @endcode + * + **/ static BUILDIN(disableitemuse) { + int flag = battle_config.item_enabled_npc; + + if (script_hasdata(st, 2)) { + if (!script_isinttype(st, 2)) + return true; + + flag = script_getnum(st, 2); + } + + if (flag < 0) + return true; + struct map_session_data *sd = script->rid2sd(st); - if (sd != NULL) - st->npc_item_flag = sd->npc_item_flag = 0; + + if (sd == NULL) + return true; + + st->npc_item_flag &= ~flag; + sd->npc_item_flag &= ~flag; + return true; } @@ -12402,6 +12533,50 @@ static BUILDIN(mobattached) return true; } +/** + * Announces a colored text in '<char_name> Shouts : <message>' format. + * Default color is white ("FFFFFF"). + * + * This is a special use case of packet 0x009a where the message's first 34 bytes + * are reserved for string "micc" (4B) which identifies the broadcast as megaphone shout, + * the character's name (24B) and the text color (6B). + * + * 009a <packet len>.W <micc>.4B <char name>.24B <color>.6B <message>.?B + * + * @code{.herc} + * loudhailer("<message>"{, "<color>"}); + * @endcode + * + **/ +static BUILDIN(loudhailer) +{ + const char *mes = script_getstr(st, 2); + size_t len_mes = strlen(mes); + + Assert_retr(false, len_mes + 33 < CHAT_SIZE_MAX); // +33 because of the '<char_name> Shouts : ' message prefix. + + const char *color = script_hasdata(st, 3) ? script_getstr(st, 3) : "FFFFFF"; + + Assert_retr(false, strlen(color) == 6); + + struct map_session_data *sd = script->rid2sd(st); + + if (sd == NULL) + return false; + + char mes_formatted[CHAT_SIZE_MAX + 30] = ""; + + strcpy(mes_formatted, sd->status.name); + strcpy(mes_formatted + 24, color); + safesnprintf(mes_formatted + 30, CHAT_SIZE_MAX, "%s Shouts : %s", sd->status.name, mes); + + size_t len_formatted = 30 + strlen(sd->status.name) + 10 + len_mes + 1; + + clif->broadcast(&sd->bl, mes_formatted, (int)len_formatted, BC_MEGAPHONE, ALL_CLIENT); + + return true; +} + /*========================================== *------------------------------------------*/ static BUILDIN(announce) @@ -19152,7 +19327,14 @@ static BUILDIN(npcshopdelitem) size--; } - RECREATE(nd->u.shop.shop_item, struct npc_item_list, size); + int alloc_size = size; + if (size < 0) { + size = 0; + alloc_size = 1; + } else if (size < 1) { + alloc_size = 1; + } + RECREATE(nd->u.shop.shop_item, struct npc_item_list, alloc_size); nd->u.shop.count = size; script_pushint(st,1); @@ -27108,8 +27290,8 @@ static void script_parse_builtin(void) BUILDIN_DEF(delitem,"vi?"), BUILDIN_DEF(delitem2,"viiiiiiii?"), BUILDIN_DEF(delitemidx, "i??"), - BUILDIN_DEF2(enableitemuse,"enable_items",""), - BUILDIN_DEF2(disableitemuse,"disable_items",""), + BUILDIN_DEF2(enableitemuse, "enable_items", "?"), + BUILDIN_DEF2(disableitemuse, "disable_items", "?"), BUILDIN_DEF(cutin,"si"), BUILDIN_DEF(viewpoint,"iiiii"), BUILDIN_DEF(heal,"ii"), @@ -27214,6 +27396,7 @@ static void script_parse_builtin(void) BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest] BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex] BUILDIN_DEF(mobattached, ""), + BUILDIN_DEF(loudhailer, "s?"), BUILDIN_DEF(announce,"si?????"), BUILDIN_DEF(mapannounce,"ssi?????"), BUILDIN_DEF(areaannounce,"siiiisi?????"), @@ -28158,6 +28341,16 @@ static void script_hardcoded_constants(void) script->set_constant("PCBLOCK_COMMANDS", PCBLOCK_COMMANDS, false, false); script->set_constant("PCBLOCK_NPC", PCBLOCK_NPC, false, false); + script->constdb_comment("NPC item action constants"); + script->set_constant("ITEMENABLEDNPC_NONE", ITEMENABLEDNPC_NONE, false, false); + script->set_constant("ITEMENABLEDNPC_EQUIP", ITEMENABLEDNPC_EQUIP, false, false); + script->set_constant("ITEMENABLEDNPC_CONSUME", ITEMENABLEDNPC_CONSUME, false, false); + + script->constdb_comment("NPC allowed skill use constants"); + script->set_constant("SKILLENABLEDNPC_NONE", SKILLENABLEDNPC_NONE, false, false); + script->set_constant("SKILLENABLEDNPC_SELF", SKILLENABLEDNPC_SELF, false, false); + script->set_constant("SKILLENABLEDNPC_ALL", SKILLENABLEDNPC_ALL, false, false); + script->constdb_comment("private airship responds"); script->set_constant("P_AIRSHIP_NONE", P_AIRSHIP_NONE, false, false); script->set_constant("P_AIRSHIP_RETRY", P_AIRSHIP_RETRY, false, false); |