viewing paste topic/11580- setunitdir_0.2 | C

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
//===== Hercules Plugin ======================================
//= setunitdir
//===== By: ==================================================
//= AnnieRuru
//===== Current Version: =====================================
//= 0.2
//===== Compatible With: ===================================== 
//= Hercules 2015-12-28
//===== Description: =========================================
//= modify and retrieve the value of unit's direction
//===== Topic ================================================
//= http://herc.ws/board/topic/11580-about-skid-trap-effect/
//===== Additional Comments: =================================  
//= it seems all others use unit->ud.dir, but npc uses nd->dir
//= it needs some clean up before this go into our repo
//============================================================
 
#include "common/hercules.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "map/pc.h"
#include "map/npc.h"
#include "map/unit.h"
#include "map/script.h"
#include "common/HPMDataCheck.h"
 
HPExport struct hplugin_info pinfo = {
    "setunitdir",
    SERVER_TYPE_MAP,
    "0,2",
    HPM_VERSION,
};
 
BUILDIN(setunitdir) {
    int unit_id = script_getnum(st,2);
    struct block_list *bl = map->id2bl( unit_id );
    uint8 udir = script_getnum(st,3);
    if ( !bl ) {
        ShowWarning( "buildin_setunitdir: no such GID existed '%d'\n", unit_id );
        return false;
    }
    if ( bl->type == BL_PC ) {
        TBL_PC *sd = map->id2sd( unit_id );
        sd->ud.dir = udir % 8;
        if ( script_hasdata(st,4) )
            sd->head_dir = script_getnum(st,4) % 3;
    }
    else if ( bl->type == BL_NPC )
        ((TBL_NPC*)bl)->dir = udir % 8;
    else
        unit->bl2ud(bl)->dir = udir % 8;
    clif->changed_dir(bl, AREA);
    return true;
}
 
BUILDIN(getunitdir) {
    int unit_id = script_getnum(st,2);
    struct block_list *bl = map->id2bl( unit_id );
    uint8 udir = 0;
    if ( !bl ) {
        ShowWarning( "buildin_getunitdir: no such GID existed '%d'\n", unit_id );
        script_pushint(st, -1);
        return false;
    }
    if ( script_hasdata(st,3) ) {
        if ( bl->type != BL_PC ) {
            ShowWarning( "buildin_getunitdir: attempt to get head direction for non PC object '%d'\n", unit_id );
            script_pushint(st, -1);
            return false;
        }
        else {
            TBL_PC *sd = map->id2sd( unit_id );
            udir = sd->head_dir;
        }
    }
    else if ( bl->type == BL_NPC )
        udir = ((TBL_NPC*)bl)->dir;
    else
        udir = unit->bl2ud(bl)->dir;
    script_pushint(st,udir);
    return true;
}
 
HPExport void plugin_init (void) {
    script->set_constant("DIR_HEAD_FRONT", 0, false);
    script->set_constant("DIR_HEAD_LEFT", 1, false);
    script->set_constant("DIR_HEAD_RIGHT", 2, false);
    
    addScriptCommand( "getunitdir", "i?", getunitdir );
    addScriptCommand( "setunitdir", "ii?", setunitdir );
}
Viewed 1397 times, submitted by AnnieRuru.