viewing paste topic/4570- setbgid_20151107.c | 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
//===== Hercules Plugin ======================================
//= Battleground System without waitingroom
//===== By: ==================================================
//= AnnieRuru
//===== Current Version: =====================================
//= 1.5
//===== Compatible With: ===================================== 
//= Hercules 2015-11-07
//===== Description: =========================================
//= Introduce 3 new Script Commands
//= *createbgid = creates a battleground team
//= *setbgid = join a battleground team
//= *getbgusers = return an array $@arenamembers of battleground team
//===== Topic ================================================
//= http://hercules.ws/board/topic/4570-
//===== Additional Comments: =================================  
//= beter update this before someone say its not working ...
//============================================================
 
#include "common/hercules.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "map/script.h"
#include "map/pc.h"
#include "map/mapreg.h"
#include "common/HPMDataCheck.h"
 
HPExport struct hplugin_info pinfo = {
    "setbgid",      // Plugin name
    SERVER_TYPE_MAP,// Which server types this plugin works with?
    "1.5",          // Plugin version
    HPM_VERSION,    // HPM Version (don't change, macro is automatically updated)
};
 
//  createbgid <respawn map>, <respawn x>, <respawn y>, <On Quit event>, <On Death event>;
BUILDIN(createbgid) {
    unsigned int bg_id;
    if ( ( bg_id = bg->create( mapindex->name2id( script_getstr(st,2) ), script_getnum(st,3), script_getnum(st,4), script_getstr(st,5), script_getstr(st,6) ) ) > 0 )
        script_pushint( st, bg_id );
    else
        script_pushint( st, 0 );
    return true;
}
 
//  setbgid <battleground ID> {, <player name> };
//  setbgid <battleground ID> {, <player account ID> };
BUILDIN(setbgid) {
    unsigned int bg_id = script_getnum(st,2);
    struct battleground_data *bgd = bg->team_search( bg_id );
    struct map_session_data *sd;
    if ( script_hasdata( st, 3 ) ) {
        if ( data_isstring( script_getdata(st,3) ) )
            sd = map->nick2sd( script_getstr(st,3) );
        else
            sd = map->id2sd( script_getnum(st,3) );
    } else
        sd = script->rid2sd(st);
    if ( !sd ) {
        script_pushint( st, -3 ); // player no attach
        return true;
    }
    if ( !bgd && bg_id > 0 ) {
        script_pushint( st, -1 ); // battleground team haven't created
        return true;
    }
    if ( bg_id && sd->bg_id == bg_id ) {
        script_pushint( st, -5 ); // the player has already join this battleground team
        return true;
    }
    if ( sd->bg_id )
        bg->team_leave( sd, 0 );
    if ( bg_id == 0 ) {
        script_pushint( st, 0 );
        return true;
    }
    if ( !bg->team_join( bg_id, sd ) )
        script_pushint( st, -2 ); // cannot join anymore, because has reached MAX_BG_MEMBERS
    else
        script_pushint( st, bg_id );
    return true;
}
 
//  getbgusers <battleground ID>;
BUILDIN(getbgusers) {
    struct battleground_data *bgd = bg->team_search( script_getnum(st,2) );
    int i, j = 0;
    if ( !bgd ) {
        script_pushint( st, -1 );
        return true;
    }
    for ( i = 0; i < MAX_BG_MEMBERS; i++ ) {
        if ( bgd->members[i].sd ) {
            mapreg->setreg( reference_uid( script->add_str("$@arenamembers"), j ), bgd->members[i].sd->bl.id );
            j++;
        }
    }
    mapreg->setreg( script->add_str("$@arenamembersnum"), j );
    script_pushint( st, j );
    return true;
}
 
HPExport void plugin_init (void) {
    addScriptCommand( "createbgid", "siiss", createbgid );
    addScriptCommand( "setbgid", "i?", setbgid );
    addScriptCommand( "getbgusers", "i", getbgusers );
}
Viewed 1622 times, submitted by AnnieRuru.