Version 4, last updated by lord2800 at April 20, 2011 21:38 UTC
Adding Custom D2bs Function
Goal : to add packet based NPC moving to D2bs
problem: coverting packet info like npc.gid -> packet(1) isnt a JS friendly task.
Solution: make a d2bs function and do the conversions in c++
Adding the Function
In JSGame.cpp
JSAPI_FUNC(my_moveNPC)
{ // moveNPC(npc,x,y)
if(!WaitForGameReady())
THROW_WARNING(cx, "Game not ready");
if(argc < 2)
THROW_ERROR(cx, "Not enough parameters were passed to moveNPC!");
*rval = JSVAL_FALSE;
myUnit* pNpc = (myUnit*)JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
if(!pNpc || pNpc->dwType !=1)
THROW_ERROR(cx, "Invalid NPC passed to moveNPC!");
DWORD dwX = JSVAL_TO_INT(argv[1]);
DWORD dwY = JSVAL_TO_INT(argv[2]);
if(!WaitForGameReady())
THROW_WARNING(cx, "Game not ready");
//59 17 Make entity move 59 [DWORD entity kind] [DWORD entity id] [WORD x] 00 00 [WORD y] 00 00
// from http://www.blizzhackers.cc/viewtopic.php?f=182&t=457480
// build packet setting up the Packet i learned from reading old d2bs shopping function rev 200 ish
BYTE aPacket[17];
aPacket[0] = 0x59;
*(DWORD*)&aPacket[1] = pNpc->dwType; // npc entity kind = 1
*(DWORD*)&aPacket[5] = pNpc->dwUnitId;
*(DWORD*)&aPacket[9] = dwX;
*(DWORD*)&aPacket[13] = dwY;
D2NET_SendPacket(sizeof(aPacket), 1, aPacket);
*rval = JSVAL_TRUE;
return JS_TRUE;
}
In JSGame.h
JSAPI_FUNC(my_moveNPC); |
In JSGlobalFuncs.h to make it accessable to Scripts
{"moveNPC", my_moveNPC, 0}, |
This wasent exactly difficult finding the 3 places to edit was as simple as ctrl +f on any my_func, packet info is on EON, Filling in the packet info could be a bit tricky but I mostly copy pasted from the old shopping function that used packets.
Results:
