diff options
author | Haru <haru@dotalux.com> | 2018-11-13 17:38:21 +0100 |
---|---|---|
committer | Haru <haru@dotalux.com> | 2018-11-13 17:38:21 +0100 |
commit | 35ee16dd3908c021752e439a4fe4ea9e83af9ec0 (patch) | |
tree | d0de667fae7b066a61d793fa8b1fa421718f70cc | |
parent | 1e5df06b8a23ea13c3fc1b07339c261a77600bae (diff) | |
download | hercules-35ee16dd3908c021752e439a4fe4ea9e83af9ec0.tar.gz hercules-35ee16dd3908c021752e439a4fe4ea9e83af9ec0.tar.bz2 hercules-35ee16dd3908c021752e439a4fe4ea9e83af9ec0.tar.xz hercules-35ee16dd3908c021752e439a4fe4ea9e83af9ec0.zip |
Replace sd->channels with a VECTOR
This removes some shady array compaction code and prepares the ground
for some further fixes.
Signed-off-by: Haru <haru@dotalux.com>
-rw-r--r-- | src/map/atcommand.c | 40 | ||||
-rw-r--r-- | src/map/channel.c | 52 | ||||
-rw-r--r-- | src/map/clif.c | 4 | ||||
-rw-r--r-- | src/map/pc.c | 1 | ||||
-rw-r--r-- | src/map/pc.h | 3 | ||||
-rw-r--r-- | src/map/unit.c | 1 |
6 files changed, 36 insertions, 65 deletions
diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 236975b32..adf617ccd 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -9019,7 +9019,6 @@ ACMD(channel) { struct channel_data *chan; char subcmd[HCS_NAME_LENGTH], sub1[HCS_NAME_LENGTH], sub2[HCS_NAME_LENGTH], sub3[HCS_NAME_LENGTH]; - unsigned char k = 0; sub1[0] = sub2[0] = sub3[0] = '\0'; if (!*message || sscanf(message, "%19s %19s %19s %19s", subcmd, sub1, sub2, sub3) < 1) { @@ -9056,7 +9055,7 @@ ACMD(channel) } else if (strcmpi(subcmd,"list") == 0) { // sub1 = list type; sub2 = unused; sub3 = unused if (sub1[0] != '\0' && strcmpi(sub1,"colors") == 0) { - for (k = 0; k < channel->config->colors_count; k++) { + for (int k = 0; k < channel->config->colors_count; k++) { safesnprintf(atcmd_output, sizeof(atcmd_output), "[ %s list colors ] : %s", command, channel->config->colors_name[k]); clif->messagecolor_self(fd, channel->config->colors[k], atcmd_output); @@ -9085,6 +9084,7 @@ ACMD(channel) } } else if (strcmpi(subcmd,"setcolor") == 0) { // sub1 = channel name; sub2 = color; sub3 = unused + int k; if (sub1[0] != '#') { clif->message(fd, msg_fd(fd,1405));// Channel name must start with a '#' return false; @@ -9102,10 +9102,7 @@ ACMD(channel) return false; } - for (k = 0; k < channel->config->colors_count; k++) { - if (strcmpi(sub2, channel->config->colors_name[k]) == 0) - break; - } + ARR_FIND(0, channel->config->colors_count, k, strcmpi(sub2, channel->config->colors_name[k]) == 0); if (k == channel->config->colors_count) { safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1411), sub2);// Unknown color '%s' clif->message(fd, atcmd_output); @@ -9116,51 +9113,47 @@ ACMD(channel) clif->message(fd, atcmd_output); } else if (strcmpi(subcmd,"leave") == 0) { // sub1 = channel name; sub2 = unused; sub3 = unused + int k; if (sub1[0] != '#') { clif->message(fd, msg_fd(fd,1405));// Channel name must start with a '#' return false; } - for (k = 0; k < sd->channel_count; k++) { - if (strcmpi(sub1+1,sd->channels[k]->name) == 0) - break; - } - if (k == sd->channel_count) { + ARR_FIND(0, VECTOR_LENGTH(sd->channels), k, strcmpi(sub1 + 1, VECTOR_INDEX(sd->channels, k)->name) == 0); + if (k == VECTOR_LENGTH(sd->channels)) { safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1425),sub1);// You're not part of the '%s' channel clif->message(fd, atcmd_output); return false; } - if (sd->channels[k]->type == HCS_TYPE_ALLY) { + if (VECTOR_INDEX(sd->channels, k)->type == HCS_TYPE_ALLY) { do { - for (k = 0; k < sd->channel_count; k++) { - if (sd->channels[k]->type == HCS_TYPE_ALLY) { - channel->leave(sd->channels[k],sd); + for (k = 0; k < VECTOR_LENGTH(sd->channels); k++) { + if (VECTOR_INDEX(sd->channels, k)->type == HCS_TYPE_ALLY) { + channel->leave(VECTOR_INDEX(sd->channels, k), sd); break; } } - } while (k != sd->channel_count); + } while (k != VECTOR_LENGTH(sd->channels)); // FIXME } else { - channel->leave(sd->channels[k],sd); + channel->leave(VECTOR_INDEX(sd->channels, k), sd); } safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1426),sub1); // You've left the '%s' channel clif->message(fd, atcmd_output); } else if (strcmpi(subcmd,"bindto") == 0) { // sub1 = channel name; sub2 = unused; sub3 = unused + int k; if (sub1[0] != '#') { clif->message(fd, msg_fd(fd,1405));// Channel name must start with a '#' return false; } - for (k = 0; k < sd->channel_count; k++) { - if (strcmpi(sub1+1,sd->channels[k]->name) == 0) - break; - } - if (k == sd->channel_count) { + ARR_FIND(0, VECTOR_LENGTH(sd->channels), k, strcmpi(sub1 + 1, VECTOR_INDEX(sd->channels, k)->name) == 0); + if (k == VECTOR_LENGTH(sd->channels)) { safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1425),sub1);// You're not part of the '%s' channel clif->message(fd, atcmd_output); return false; } - sd->gcbind = sd->channels[k]; + sd->gcbind = VECTOR_INDEX(sd->channels, k); safesnprintf(atcmd_output, sizeof(atcmd_output), msg_fd(fd,1431),sub1); // Your global chat is now bound to the '%s' channel clif->message(fd, atcmd_output); } else if (strcmpi(subcmd,"unbind") == 0) { @@ -9332,6 +9325,7 @@ ACMD(channel) dbi_destroy(iter); } else if (strcmpi(subcmd,"setopt") == 0) { // sub1 = channel name; sub2 = option name; sub3 = value + int k; const char* opt_str[3] = { "None", "JoinAnnounce", diff --git a/src/map/channel.c b/src/map/channel.c index 3ba56b100..86ca44ea2 100644 --- a/src/map/channel.c +++ b/src/map/channel.c @@ -319,8 +319,8 @@ static void channel_join_sub(struct channel_data *chan, struct map_session_data if (idb_put(chan->users, sd->status.char_id, sd)) return; - RECREATE(sd->channels, struct channel_data *, ++sd->channel_count); - sd->channels[sd->channel_count - 1] = chan; + VECTOR_ENSURE(sd->channels, 1, 1); + VECTOR_PUSH(sd->channels, chan); if (!stealth && (chan->options&HCS_OPT_ANNOUNCE_JOIN)) { char message[60]; @@ -329,7 +329,7 @@ static void channel_join_sub(struct channel_data *chan, struct map_session_data } /* someone is cheating, we kindly disconnect the bastard */ - if (sd->channel_count > 200) { + if (VECTOR_LENGTH(sd->channels) > 200) { sockt->eof(sd->fd); } @@ -411,32 +411,16 @@ static enum channel_operation_status channel_join(struct channel_data *chan, str */ static void channel_leave_sub(struct channel_data *chan, struct map_session_data *sd) { - unsigned char i; + int i; nullpo_retv(chan); nullpo_retv(sd); - for (i = 0; i < sd->channel_count; i++) { - if (sd->channels[i] == chan) { - sd->channels[i] = NULL; - break; - } - } - if (i < sd->channel_count) { - unsigned char cursor = 0; - for (i = 0; i < sd->channel_count; i++) { - if (sd->channels[i] == NULL) - continue; - if (cursor != i) { - sd->channels[cursor] = sd->channels[i]; - } - cursor++; - } - if (!(sd->channel_count = cursor)) { - aFree(sd->channels); - sd->channels = NULL; - } + ARR_FIND(0, VECTOR_LENGTH(sd->channels), i, VECTOR_INDEX(sd->channels, i) == chan); + if (i < VECTOR_LENGTH(sd->channels)) { + VECTOR_ERASE(sd->channels, i); } } + /** * Leaves a channel. * @@ -475,14 +459,9 @@ static void channel_leave(struct channel_data *chan, struct map_session_data *sd static void channel_quit(struct map_session_data *sd) { nullpo_retv(sd); - while (sd->channel_count > 0) { + while (VECTOR_LENGTH(sd->channels) > 0) { // Loop downward to avoid unnecessary array compactions by channel_leave - struct channel_data *chan = sd->channels[sd->channel_count-1]; - - if (chan == NULL) { - sd->channel_count--; - continue; - } + struct channel_data *chan = VECTOR_LAST(sd->channels); channel->leave(chan, sd); } @@ -585,13 +564,11 @@ static void channel_guild_leave_alliance(const struct guild *g_source, const str */ static void channel_quit_guild(struct map_session_data *sd) { - unsigned char i; - nullpo_retv(sd); - for (i = 0; i < sd->channel_count; i++) { - struct channel_data *chan = sd->channels[i]; + for (int i = 0; i < VECTOR_LENGTH(sd->channels); i++) { // FIXME + struct channel_data *chan = VECTOR_INDEX(sd->channels, i); - if (chan == NULL || chan->type != HCS_TYPE_ALLY) + if (chan->type != HCS_TYPE_ALLY) continue; channel->leave(chan, sd); @@ -855,7 +832,6 @@ static void do_final_channel(void) { struct DBIterator *iter = db_iterator(channel->db); struct channel_data *chan; - unsigned char i; for( chan = dbi_first(iter); dbi_exists(iter); chan = dbi_next(iter) ) { channel->delete(chan); @@ -863,7 +839,7 @@ static void do_final_channel(void) dbi_destroy(iter); - for(i = 0; i < channel->config->colors_count; i++) { + for (int i = 0; i < channel->config->colors_count; i++) { aFree(channel->config->colors_name[i]); } diff --git a/src/map/clif.c b/src/map/clif.c index 44cefcd7e..800b13833 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -11083,8 +11083,8 @@ static void clif_parse_WisMessage(int fd, struct map_session_data *sd) if (chan) { int k; - ARR_FIND(0, sd->channel_count, k, sd->channels[k] == chan); - if (k < sd->channel_count || channel->join(chan, sd, "", true) == HCS_STATUS_OK) { + ARR_FIND(0, VECTOR_LENGTH(sd->channels), k, VECTOR_INDEX(sd->channels, k) == chan); + if (k < VECTOR_LENGTH(sd->channels) || channel->join(chan, sd, "", true) == HCS_STATUS_OK) { channel->send(chan,sd,message); } else { clif->message(fd, msg_fd(fd,1402)); //You're not in that channel, type '@join <#channel_name>' diff --git a/src/map/pc.c b/src/map/pc.c index b8a6912ee..282eb7112 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -1330,6 +1330,7 @@ static bool pc_authok(struct map_session_data *sd, int login_id2, time_t expirat sd->bg_queue.client_has_bg_data = 0; sd->bg_queue.type = 0; + VECTOR_INIT(sd->channels); VECTOR_INIT(sd->script_queues); VECTOR_INIT(sd->achievement); // Achievements [Smokexyz/Hercules] VECTOR_INIT(sd->storage.item); // initialize storage item vector. diff --git a/src/map/pc.h b/src/map/pc.h index 4ee5f560b..8302279b1 100644 --- a/src/map/pc.h +++ b/src/map/pc.h @@ -557,8 +557,7 @@ END_ZEROED_BLOCK; int shadowform_id; /* [Ind/Hercules] */ - struct channel_data **channels; - unsigned char channel_count; + VECTOR_DECL(struct channel_data *) channels; struct channel_data *gcbind; unsigned char fontcolor; int fontcolor_tid; diff --git a/src/map/unit.c b/src/map/unit.c index 9174bdccd..3c2574111 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -2764,6 +2764,7 @@ static int unit_free(struct block_list *bl, clr_type clrtype) aFree(sd->instance); sd->instance = NULL; } + VECTOR_CLEAR(sd->channels); VECTOR_CLEAR(sd->script_queues); VECTOR_CLEAR(sd->achievement); // Achievement [Smokexyz/Hercules] VECTOR_CLEAR(sd->storage.item); |