Skip to content

Commit 0e18ff7

Browse files
Merge pull request #2430 from Emistry/scriptcommand_npcspeed
Fix map-crash when floating npc use *npcspeed script command
2 parents 059d89f + 1259ea8 commit 0e18ff7

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

src/map/script.c

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15937,37 +15937,41 @@ static BUILDIN(npctalk)
1593715937
// change npc walkspeed [Valaris]
1593815938
static BUILDIN(npcspeed)
1593915939
{
15940-
struct npc_data* nd;
15941-
int speed;
15942-
15943-
speed = script_getnum(st,2);
15944-
nd = map->id2nd(st->oid);
15940+
struct npc_data *nd = map->id2nd(st->oid);
15941+
int speed = script_getnum(st, 2);
1594515942

1594615943
if (nd != NULL) {
1594715944
unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit
15945+
if (nd->ud == NULL) {
15946+
ShowWarning("buildin_npcspeed: floating NPC don't have unit data.\n");
15947+
return false;
15948+
}
1594815949
nd->speed = speed;
1594915950
nd->ud->state.speed_changed = 1;
1595015951
}
1595115952

1595215953
return true;
1595315954
}
15955+
1595415956
// make an npc walk to a position [Valaris]
1595515957
static BUILDIN(npcwalkto)
1595615958
{
1595715959
struct npc_data *nd = map->id2nd(st->oid);
15958-
int x=0,y=0;
15959-
15960-
x=script_getnum(st,2);
15961-
y=script_getnum(st,3);
15960+
int x = script_getnum(st, 2);
15961+
int y = script_getnum(st, 3);
1596215962

1596315963
if (nd != NULL) {
1596415964
unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit
15965+
if (nd->ud == NULL) {
15966+
ShowWarning("buildin_npcwalkto: floating NPC don't have unit data.\n");
15967+
return false;
15968+
}
1596515969
if (!nd->status.hp) {
1596615970
status_calc_npc(nd, SCO_FIRST);
1596715971
} else {
1596815972
status_calc_npc(nd, SCO_NONE);
1596915973
}
15970-
unit->walktoxy(&nd->bl,x,y,0);
15974+
unit->walktoxy(&nd->bl, x, y, 0);
1597115975
}
1597215976

1597315977
return true;
@@ -15979,6 +15983,10 @@ static BUILDIN(npcstop)
1597915983

1598015984
if (nd != NULL) {
1598115985
unit->bl2ud2(&nd->bl); // ensure nd->ud is safe to edit
15986+
if (nd->ud == NULL) {
15987+
ShowWarning("buildin_npcstop: floating NPC don't have unit data.\n");
15988+
return false;
15989+
}
1598215990
unit->stop_walking(&nd->bl, STOPWALKING_FLAG_FIXPOS|STOPWALKING_FLAG_NEXTCELL);
1598315991
}
1598415992

@@ -20473,24 +20481,28 @@ static BUILDIN(getunittitle)
2047320481
/// unitwalk(<unit_id>,<target_id>) -> <bool>
2047420482
static BUILDIN(unitwalk)
2047520483
{
20476-
struct block_list* bl;
20484+
struct block_list *bl = map->id2bl(script_getnum(st, 2));
2047720485

20478-
bl = map->id2bl(script_getnum(st,2));
20479-
if( bl == NULL ) {
20486+
if (bl == NULL) {
2048020487
script_pushint(st, 0);
2048120488
return true;
2048220489
}
2048320490

20484-
if( bl->type == BL_NPC ) {
20485-
unit->bl2ud2(bl); // ensure the ((struct npc_data*)bl)->ud is safe to edit
20491+
if (bl->type == BL_NPC) {
20492+
struct unit_data *ud = unit->bl2ud2(bl); // ensure the ((struct npc_data*)bl)->ud is safe to edit
20493+
if (ud == NULL) {
20494+
ShowWarning("buildin_unitwalk: floating NPC don't have unit data.\n");
20495+
return false;
20496+
}
2048620497
}
20487-
if( script_hasdata(st,4) ) {
20488-
int x = script_getnum(st,3);
20489-
int y = script_getnum(st,4);
20490-
script_pushint(st, unit->walktoxy(bl,x,y,0));// We'll use harder calculations.
20491-
} else {
20492-
int target_id = script_getnum(st,3);
20493-
script_pushint(st, unit->walktobl(bl,map->id2bl(target_id),1,1));
20498+
if (script_hasdata(st, 4)) {
20499+
int x = script_getnum(st, 3);
20500+
int y = script_getnum(st, 4);
20501+
script_pushint(st, unit->walktoxy(bl, x, y, 0));// We'll use harder calculations.
20502+
}
20503+
else {
20504+
int target_id = script_getnum(st, 3);
20505+
script_pushint(st, unit->walktobl(bl, map->id2bl(target_id), 1, 1));
2049420506
}
2049520507

2049620508
return true;
@@ -20514,32 +20526,34 @@ static BUILDIN(unitkill)
2051420526
/// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
2051520527
static BUILDIN(unitwarp)
2051620528
{
20517-
int unit_id;
20529+
int unit_id = script_getnum(st, 2);
20530+
const char *mapname = script_getstr(st, 3);
20531+
short x = (short)script_getnum(st, 4);
20532+
short y = (short)script_getnum(st, 5);
2051820533
int mapid;
20519-
short x;
20520-
short y;
20521-
struct block_list* bl;
20522-
const char *mapname;
20523-
20524-
unit_id = script_getnum(st,2);
20525-
mapname = script_getstr(st, 3);
20526-
x = (short)script_getnum(st,4);
20527-
y = (short)script_getnum(st,5);
20534+
struct block_list *bl;
2052820535

2052920536
if (!unit_id) //Warp the script's runner
2053020537
bl = map->id2bl(st->rid);
2053120538
else
2053220539
bl = map->id2bl(unit_id);
2053320540

20534-
if( strcmp(mapname,"this") == 0 )
20535-
mapid = bl?bl->m:-1;
20541+
if (strcmp(mapname, "this") == 0)
20542+
mapid = bl ? bl->m : -1;
2053620543
else
2053720544
mapid = map->mapname2mapid(mapname);
2053820545

20539-
if( mapid >= 0 && bl != NULL ) {
20540-
unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit
20541-
script_pushint(st, unit->warp(bl,mapid,x,y,CLR_OUTSIGHT));
20542-
} else {
20546+
if (mapid >= 0 && bl != NULL) {
20547+
struct unit_data *ud = unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit
20548+
if (bl->type == BL_NPC) {
20549+
if (ud == NULL) {
20550+
ShowWarning("buildin_unitwarp: floating NPC don't have unit data.\n");
20551+
return false;
20552+
}
20553+
}
20554+
script_pushint(st, unit->warp(bl, mapid, x, y, CLR_OUTSIGHT));
20555+
}
20556+
else {
2054320557
script_pushint(st, 0);
2054420558
}
2054520559

@@ -20610,17 +20624,19 @@ static BUILDIN(unitattack)
2061020624
/// unitstop <unit_id>;
2061120625
static BUILDIN(unitstop)
2061220626
{
20613-
int unit_id;
20614-
struct block_list* bl;
20615-
20616-
unit_id = script_getnum(st,2);
20627+
struct block_list *bl = map->id2bl(script_getnum(st, 2));
2061720628

20618-
bl = map->id2bl(unit_id);
20619-
if( bl != NULL ) {
20620-
unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit
20629+
if (bl != NULL) {
20630+
struct unit_data *ud = unit->bl2ud2(bl); // ensure ((struct npc_data *)bl)->ud is safe to edit
20631+
if (bl->type == BL_NPC) {
20632+
if (ud == NULL) {
20633+
ShowWarning("buildin_unitstop: floating NPC don't have unit data.\n");
20634+
return false;
20635+
}
20636+
}
2062120637
unit->stop_attack(bl);
2062220638
unit->stop_walking(bl, STOPWALKING_FLAG_NEXTCELL);
20623-
if( bl->type == BL_MOB )
20639+
if (bl->type == BL_MOB)
2062420640
BL_UCAST(BL_MOB, bl)->target_id = 0;
2062520641
}
2062620642

0 commit comments

Comments
 (0)