summaryrefslogtreecommitdiff
path: root/src/map
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-07-16 02:09:49 +0300
committerAndrei Karas <akaras@inbox.ru>2016-07-17 16:18:53 +0300
commit454070587645bf840be610ec31f9783b5d0dbce0 (patch)
tree331146f1ce4197ae0c8556dad52dc09d6cc8c1fa /src/map
parentd797f0177b067612353a426ad4aa11b3b4845685 (diff)
downloadhercules-454070587645bf840be610ec31f9783b5d0dbce0.tar.gz
hercules-454070587645bf840be610ec31f9783b5d0dbce0.tar.bz2
hercules-454070587645bf840be610ec31f9783b5d0dbce0.tar.xz
hercules-454070587645bf840be610ec31f9783b5d0dbce0.zip
Improve a bit performance in bl_getall_area.
Diffstat (limited to 'src/map')
-rw-r--r--src/map/map.c100
1 files changed, 73 insertions, 27 deletions
diff --git a/src/map/map.c b/src/map/map.c
index c5ea7c1f3..d6425b94e 100644
--- a/src/map/map.c
+++ b/src/map/map.c
@@ -683,49 +683,95 @@ static int bl_getall_area(int type, int m, int x0, int y0, int x1, int y1, int (
x1 = min(x1, map->list[m].xs - 1);
y1 = min(y1, map->list[m].ys - 1);
- for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) {
- for (bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++) {
- if (type&~BL_MOB) {
- for (bl = map->list[m].block[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) {
- if (bl->type&type && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1) {
- if( map->bl_list_count >= map->bl_list_size )
- map_bl_list_expand();
- if (func) {
- va_start(args, func);
- if (func(bl, args)) {
+ {
+ const int x0b = x0 / BLOCK_SIZE;
+ const int x1b = x1 / BLOCK_SIZE;
+ const int y0b = y0 / BLOCK_SIZE;
+ const int y1b = y1 / BLOCK_SIZE;
+ const struct map_data *const listm = &map->list[m];
+ const int bxs0 = listm->bxs;
+
+ // duplication for better performance
+ if (func != NULL) {
+ if (type & ~BL_MOB) {
+ for (by = y0b; by <= y1b; by++) {
+ const int bxs = by * bxs0;
+ for (bx = x0b; bx <= x1b; bx++) {
+ for (bl = listm->block[bx + bxs]; bl != NULL; bl = bl->next) {
+ const int x = bl->x;
+ const int y = bl->y;
+ if (bl->type & type && x >= x0 && x <= x1 && y >= y0 && y <= y1) {
+ va_start(args, func);
+ if (func(bl, args)) {
+ if (map->bl_list_count >= map->bl_list_size)
+ map_bl_list_expand();
+ map->bl_list[map->bl_list_count++] = bl;
+ found++;
+ }
+ va_end(args);
+ }
+ }
+ }
+ }
+ }
+ if (type & BL_MOB) {
+ for (by = y0b; by <= y1b; by++) {
+ const int bxs = by * bxs0;
+ for (bx = x0b; bx <= x1b; bx++) {
+ for (bl = listm->block_mob[bx + bxs]; bl != NULL; bl = bl->next) {
+ const int x = bl->x;
+ const int y = bl->y;
+ if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
+ va_start(args, func);
+ if (func(bl, args)) {
+ if (map->bl_list_count >= map->bl_list_size)
+ map_bl_list_expand();
+ map->bl_list[map->bl_list_count++] = bl;
+ found++;
+ }
+ va_end(args);
+ }
+ }
+ }
+ }
+ }
+ } else { // func != NULL
+ if (type & ~BL_MOB) {
+ for (by = y0b; by <= y1b; by++) {
+ const int bxs = by * bxs0;
+ for (bx = x0b; bx <= x1b; bx++) {
+ for (bl = listm->block[bx + bxs]; bl != NULL; bl = bl->next) {
+ const int x = bl->x;
+ const int y = bl->y;
+ if (bl->type & type && x >= x0 && x <= x1 && y >= y0 && y <= y1) {
+ if (map->bl_list_count >= map->bl_list_size)
+ map_bl_list_expand();
map->bl_list[map->bl_list_count++] = bl;
found++;
}
- va_end(args);
- } else {
- map->bl_list[map->bl_list_count++] = bl;
- found++;
}
}
}
}
- if (type&BL_MOB) { // TODO: fix this code duplication
- for (bl = map->list[m].block_mob[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) {
- if (bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1) {
- if( map->bl_list_count >= map->bl_list_size )
- map_bl_list_expand();
- if (func) {
- va_start(args, func);
- if (func(bl, args)) {
+ if (type & BL_MOB) {
+ for (by = y0b; by <= y1b; by++) {
+ const int bxs = by * bxs0;
+ for (bx = x0b; bx <= x1b; bx++) {
+ for (bl = listm->block_mob[bx + bxs]; bl != NULL; bl = bl->next) {
+ const int x = bl->x;
+ const int y = bl->y;
+ if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
+ if (map->bl_list_count >= map->bl_list_size)
+ map_bl_list_expand();
map->bl_list[map->bl_list_count++] = bl;
found++;
}
- va_end(args);
- } else {
- map->bl_list[map->bl_list_count++] = bl;
- found++;
}
}
}
}
}
}
-
return found;
}