viewing paste Diff cmd script | C

Posted on the
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
/**
 * Full Commands bindatcmd && unbindatcmd && useatcmd for Scripts
 * Diff by Envolvents (Wellington Ferraz)
 * Date: 16/12/2013 21:50
 * @commands (script based)
 **/
 
BUILDIN(bindatcmd) {
        const char* atcmd;
        const char* eventName;
        int i, group_lv = 0, group_lv_char = 99;
        bool log = false;
        bool create = false;
        
        atcmd = script_getstr(st,2);
        eventName = script_getstr(st,3);
        
        if( *atcmd == atcommand->at_symbol || *atcmd == atcommand->char_symbol )
                atcmd++;
        
        if( script_hasdata(st,4) ) group_lv = script_getnum(st,4);
        if( script_hasdata(st,5) ) group_lv_char = script_getnum(st,5);
        if( script_hasdata(st,6) ) log = script_getnum(st,6) ? true : false;
 
        
        if( atcommand->binding_count == 0 ) {
                CREATE(atcommand->binding,struct atcmd_binding_data*,1);
                
                create = true;
        } else {
                ARR_FIND(0, atcommand->binding_count, i, strcmp(atcommand->binding[i]->command,atcmd) == 0);
                if( i < atcommand->binding_count ) {/* update existent entry */
                        safestrncpy(atcommand->binding[i]->npc_event, eventName, ATCOMMAND_LENGTH);
                        atcommand->binding[i]->group_lv = group_lv;
                        atcommand->binding[i]->group_lv_char = group_lv_char;
                        atcommand->binding[i]->log = log;
                } else
                        create = true;
        }
        
        if( create ) {
                i = atcommand->binding_count;
                
                if( atcommand->binding_count++ != 0 )
                        RECREATE(atcommand->binding,struct atcmd_binding_data*,atcommand->binding_count);
                
                CREATE(atcommand->binding[i],struct atcmd_binding_data,1);
                
                safestrncpy(atcommand->binding[i]->command, atcmd, 50);
                safestrncpy(atcommand->binding[i]->npc_event, eventName, 50);
                atcommand->binding[i]->group_lv = group_lv;
                atcommand->binding[i]->group_lv_char = group_lv_char;
                atcommand->binding[i]->log = log;
        }
        
        return true;
}
 
BUILDIN(unbindatcmd) {
        const char* atcmd;
        int i =  0;
        
        atcmd = script_getstr(st, 2);
        
        if( *atcmd == atcommand->at_symbol || *atcmd == atcommand->char_symbol )
                atcmd++;
        
        if( atcommand->binding_count == 0 ) {
                script_pushint(st, 0);
                return true;
        }
        
        ARR_FIND(0, atcommand->binding_count, i, strcmp(atcommand->binding[i]->command, atcmd) == 0);
        if( i < atcommand->binding_count ) {
                int cursor = 0;
                aFree(atcommand->binding[i]);
                atcommand->binding[i] = NULL;
                /* compact the list now that we freed a slot somewhere */
                for( i = 0, cursor = 0; i < atcommand->binding_count; i++ ) {
                        if( atcommand->binding[i] == NULL )
                                continue;
                        
                        if( cursor != i ) {
                                memmove(&atcommand->binding[cursor], &atcommand->binding[i], sizeof(struct atcmd_binding_data*));
                        }
                        
                        cursor++;
                }
                
                if( (atcommand->binding_count = cursor) == 0 )
                        aFree(atcommand->binding);
                
                script_pushint(st, 1);
        } else
                script_pushint(st, 0);/* not found */
        
        return true;
}
 
BUILDIN(useatcmd) {
        TBL_PC *sd, *dummy_sd = NULL;
        int fd;
        const char* cmd;
        
        cmd = script_getstr(st,2);
        
        if( st->rid ) {
                sd = script->rid2sd(st);
                fd = sd->fd;
        } else {
                // Use a dummy character.
                sd = dummy_sd = pc->get_dummy_sd();
                fd = 0;
                
                if( st->oid ) {
                        struct block_list* bl = map->id2bl(st->oid);
                        memcpy(&sd->bl, bl, sizeof(struct block_list));
                        if( bl->type == BL_NPC )
                                safestrncpy(sd->status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
                }
        }
        
        // compatibility with previous implementation (deprecated!)
        if( cmd[0] != atcommand->at_symbol ) {
                cmd += strlen(sd->status.name);
                while( *cmd != atcommand->at_symbol && *cmd != 0 )
                        cmd++;
        }
        
        atcommand->parse(fd, sd, cmd, 1);
        if (dummy_sd) aFree(dummy_sd);
        return true;
}
Viewed 1955 times, submitted by Envolvents.