From 6bbe790cbfba5bb83a8aa1a67907860972294aaf Mon Sep 17 00:00:00 2001 From: mekolat Date: Mon, 25 Apr 2016 14:28:44 -0400 Subject: use map_id_is_XXX in script-fun where appropriate --- src/map/script-fun.cpp | 82 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 16 deletions(-) (limited to 'src/map/script-fun.cpp') diff --git a/src/map/script-fun.cpp b/src/map/script-fun.cpp index c8d1af4..637f893 100644 --- a/src/map/script-fun.cpp +++ b/src/map/script-fun.cpp @@ -693,6 +693,12 @@ void builtin_target(ScriptState *st) int flag = conv_num(st, &AARG(2)); int val = 0; + if (!source || !target) + { + push_int(st->stack, val); + return; + } + if (flag & 0x01) { int x0 = source->bl_x - AREA_SIZE; @@ -1109,9 +1115,11 @@ void builtin_destroy(ScriptState *st) else id = st->oid; - dumb_ptr nd = map_id2bl(id)->is_npc()->is_script(); - if(!nd) + dumb_ptr nd = map_id_is_npc(id); + if(!nd || nd->npc_subtype != NpcSubtype::SCRIPT) return; + + nd = nd->is_script(); //assert(nd->disposable == true); we don't care about it anymore npc_free(nd); if (!HARG(0)) @@ -1276,7 +1284,7 @@ void builtin_set(ScriptState *st) } else - bl = script_rid2sd(st)->is_player(); + bl = script_rid2sd(st); if (bl == nullptr) return; @@ -1342,10 +1350,10 @@ void builtin_set(ScriptState *st) set_scope_reg(st, reg, &AARG(1)); return; } - bl = map_id2bl(st->oid)->is_npc(); + bl = map_id_is_npc(st->oid); } else - bl = map_id2bl(st->rid)->is_player(); + bl = map_id_is_player(st->rid); } if (bl == nullptr) return; @@ -1433,7 +1441,7 @@ void builtin_setarray(ScriptState *st) { ZString tn = conv_str(st, sdata); if (tn == "this"_s || tn == "oid"_s) - bl = map_id2bl(st->oid)->is_npc(); + bl = map_id_is_npc(st->oid); else { NpcName name_ = stringish(tn); @@ -1444,9 +1452,9 @@ void builtin_setarray(ScriptState *st) { int tid = conv_num(st, sdata); if (tid == 0) - bl = map_id2bl(st->oid)->is_npc(); + bl = map_id_is_npc(st->oid); else - bl = map_id2bl(wrap(tid))->is_npc(); + bl = map_id_is_npc(wrap(tid)); } if (!bl) { @@ -1457,7 +1465,13 @@ void builtin_setarray(ScriptState *st) j = getarraysize2(reg, bl); } else if (prefix != '$' && !name.startswith(".@"_s)) - bl = map_id2bl(st->rid)->is_player(); + bl = map_id_is_player(st->rid); + + if (!bl) + { + PRINTF("builtin_setarray: rid not attached\n"_fmt); + return; + } for (; i < st->end - st->start - 2 && j < 256; i++, j++) { @@ -1490,9 +1504,15 @@ void builtin_cleararray(ScriptState *st) return; } if (prefix == '.' && !name.startswith(".@"_s)) - bl = map_id2bl(st->oid)->is_npc(); + bl = map_id_is_npc(st->oid); else if (prefix != '$' && !name.startswith(".@"_s)) - bl = map_id2bl(st->rid)->is_player(); + bl = map_id_is_player(st->rid); + + if (!bl) + { + PRINTF("builtin_cleararray: no block list\n"_fmt); + return; + } for (int i = 0; i < sz; i++) { @@ -1969,7 +1989,7 @@ void builtin_getnpcid(ScriptState *st) if (HARG(0)) nd = npc_name2id(stringish(ZString(conv_str(st, &AARG(0))))); else - nd = map_id2bl(st->oid)->is_npc(); + nd = map_id_is_npc(st->oid); if (nd == nullptr) { push_int(st->stack, -1); @@ -2002,10 +2022,16 @@ void builtin_strcharinfo(ScriptState *st) int num; if (HARG(1)) //指定したキャラを状態異常にする - sd = map_id2bl(wrap(conv_num(st, &AARG(1))))->is_player(); + sd = map_id_is_player(wrap(conv_num(st, &AARG(1)))); else sd = script_rid2sd(st); + if (!sd) + { + PRINTF("builtin_strcharinfo: player not found\n"_fmt); + return; + } + num = conv_num(st, &AARG(0)); if (num == 0) { @@ -2434,6 +2460,12 @@ void builtin_summon(ScriptState *st) if (HARG(7)) extract(ZString(conv_str(st, &AARG(7))), &event); + if (!owner_e) + { + PRINTF("builtin_summon: bad owner\n"_fmt); + return; + } + if (monster_attitude == MonsterAttitude::SERVANT && owner_e->bl_type == BL::PC) owner = owner_e->is_player(); // XXX in the future this should also work with mobs as owner @@ -2801,6 +2833,8 @@ void builtin_setnpcdirection(ScriptState *st) else nd_ = map_id_is_npc(st->oid); + assert (nd_); + if (bool(conv_num(st, &AARG(1)))) action = DamageType::SIT; else @@ -3355,10 +3389,16 @@ void builtin_getpvpflag(ScriptState *st) { dumb_ptr sd; if (HARG(1)) //指定したキャラを状態異常にする - sd = map_id2bl(wrap(conv_num(st, &AARG(1))))->is_player(); + sd = map_id_is_player(wrap(conv_num(st, &AARG(1)))); else sd = script_rid2sd(st); + if (!sd) + { + PRINTF("builtin_getpvpflag: player not found\n"_fmt); + return; + } + int num = conv_num(st, &AARG(0)); int flag = 0; @@ -3552,6 +3592,8 @@ void builtin_explode(ScriptState *st) else if (prefix != '$' && prefix != '.') bl = map_id2bl(st->rid)->is_player(); + assert (bl); + for (int j = 0; j < 256; j++) { auto find = std::find(str.begin(), str.end(), separator); @@ -3915,7 +3957,7 @@ void builtin_get(ScriptState *st) else var = pc_readaccountreg(bl->is_player(), stringish(name_)); } - else + else if (bl) var = pc_readreg(bl, reg); push_int(st->stack, var); @@ -4317,6 +4359,8 @@ void builtin_getsavepoint(ScriptState *st) static void builtin_areatimer_sub(dumb_ptr bl, interval_t tick, NpcEvent event) { + if (!bl) + return; if (bl->bl_type == BL::PC) { dumb_ptr sd = map_id_is_player(bl->bl_id); @@ -4540,7 +4584,7 @@ void builtin_strnpcinfo(ScriptState *st) else { BlockId id = wrap(conv_num(st, sdata)); - nd = map_id2bl(id)->is_npc(); + nd = map_id_is_npc(id); } if (!nd) @@ -4552,6 +4596,8 @@ void builtin_strnpcinfo(ScriptState *st) nd = map_id_is_npc(st->oid); } + assert (nd); + switch(num) { case 0: @@ -4592,6 +4638,8 @@ void builtin_getnpcx(ScriptState *st) nd = map_id_is_npc(st->oid); } + assert (nd); + push_int(st->stack, nd->bl_x); } @@ -4616,6 +4664,8 @@ void builtin_getnpcy(ScriptState *st) nd = map_id_is_npc(st->oid); } + assert (nd); + push_int(st->stack, nd->bl_y); } -- cgit v1.2.3-60-g2f50