From cee7567a9e5a91037c44190cfa31d617efaaca7b Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 17:24:35 +0300 Subject: [PATCH 1/8] Fixed missing semi-colons and #include. --- src/entity_impl.c | 11 +++++------ src/game.c | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/entity_impl.c b/src/entity_impl.c index 0fda1e2..126d023 100644 --- a/src/entity_impl.c +++ b/src/entity_impl.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -135,7 +136,7 @@ void tick_arrow(struct world* world, struct entity* entity) { } } } - END_HASHMAP_ITERATION(world->entities) + END_HASHMAP_ITERATION(world->entities); if (ehit != NULL) { float speed = sqrtf(entity->motX * entity->motX + entity->motY * entity->motY + entity->motZ * entity->motZ); int damage = ceil(speed * entity->data.arrow.damage); @@ -237,7 +238,7 @@ void tick_itemstack(struct world* world, struct entity* entity) { //for (int32_t icz = chunk_z - 1; icz <= chunk_z + 1; icz++) { //struct chunk* ch = world_get_chunk(entity->world, icx, icz); //if (ch != NULL) { - BEGIN_HASHMAP_ITERATION(entity->world->entities) + BEGIN_HASHMAP_ITERATION(entity->world->entities); struct entity* oe = (struct entity*) value; if (oe == entity || entity_distsq(entity, oe) > 16. * 16.) continue; if (oe->type == ENT_PLAYER && oe->health > 0.) { @@ -259,7 +260,6 @@ void tick_itemstack(struct world* world, struct entity* entity) { END_BROADCAST(entity->world->players) world_despawn_entity(world, entity); freeEntity(entity); - return 1; } else { BEGIN_BROADCAST_DIST(entity, 128.) struct packet* pkt = xmalloc(sizeof(struct packet)); @@ -268,9 +268,8 @@ void tick_itemstack(struct world* world, struct entity* entity) { writeMetadata(entity, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size); add_queue(bc_player->outgoing_packets, pkt); END_BROADCAST(entity->world->players) - BREAK_HASHMAP_ITERATION(entity->world->entities) + BREAK_HASHMAP_ITERATION(entity->world->entities); } - break; } } else if (oe->type == ENT_ITEM) { if (oe->data.itemstack.slot->item == entity->data.itemstack.slot->item && oe->data.itemstack.slot->damage == entity->data.itemstack.slot->damage && oe->data.itemstack.slot->count + entity->data.itemstack.slot->count <= slot_max_size(entity->data.itemstack.slot)) { @@ -295,6 +294,6 @@ void tick_itemstack(struct world* world, struct entity* entity) { } } } - END_HASHMAP_ITERATION(entity->world->entities) + END_HASHMAP_ITERATION(entity->world->entities); return 0; } diff --git a/src/game.c b/src/game.c index 63caffb..2890abc 100644 --- a/src/game.c +++ b/src/game.c @@ -325,7 +325,7 @@ void broadcast(char* text, char* color) { char* rs = xmalloc(s); snprintf(rs, s, "{\"text\": \"%s\", \"color\": \"%s\"}", replace(replace(rsx, "\\", "\\\\"), "\"", "\\\""), color); printf(" %s\n", text); - BEGIN_BROADCAST (players) + BEGIN_BROADCAST (players); struct packet* pkt = xmalloc(sizeof(struct packet)); pkt->id = PKT_PLAY_CLIENT_CHATMESSAGE; pkt->data.play_client.chatmessage.position = 0; From 4de52395e42ac8c55de736721b4c21f5effb7ad9 Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 17:29:40 +0300 Subject: [PATCH 2/8] Fixed overwhelming amount of parameters and one more leftover semi-colon. --- src/entity_impl.c | 2 +- src/item.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/entity_impl.c b/src/entity_impl.c index 126d023..cecba30 100644 --- a/src/entity_impl.c +++ b/src/entity_impl.c @@ -109,7 +109,7 @@ void tick_arrow(struct world* world, struct entity* entity) { struct entity* ehit = NULL; struct entity* shooter = world_get_entity(world, entity->objectData - 1); double bd = 999.; - BEGIN_HASHMAP_ITERATION(world->entities) + BEGIN_HASHMAP_ITERATION(world->entities); struct entity* e2 = value; double rd = entity_distsq_block(e2, entity->x + entity->motX, entity->y + entity->motY, entity->z + entity->motZ); if (rd > 4) continue; diff --git a/src/item.c b/src/item.c index 40deff5..61e8a29 100644 --- a/src/item.c +++ b/src/item.c @@ -166,7 +166,7 @@ int onItemInteract_painting(struct world* world, struct player* player, uint8_t else if (p == 24) ent->data.painting.title = "Skeleton"; else if (p == 25) ent->data.painting.title = "DonkeyKong"; world_spawn_entity(world, ent); - if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1, 1); + if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1); } return 0; @@ -185,7 +185,7 @@ int onItemInteract_minecart(struct world* world, struct player* player, uint8_t else if (slot->item == ITM_MINECARTCOMMANDBLOCK) et = ENT_MINECARTCOMMANDBLOCK; struct entity* ent = entity_new(nextEntityID++, (double) x + .5, (double) y + dy, (double) z + .5, et, 0., 0.); world_spawn_entity(world, ent); - if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1, 1); + if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1); return 0; } @@ -544,7 +544,7 @@ void onItemUse_armor(struct world* world, struct player* player, uint8_t slot_in else if (ii->armorType == ARMOR_BOOTS) sli = 8; if (inventory_get(player, player->inventory, sli) != NULL) return; inventory_set_slot(player, player->inventory, sli, slot, 1); - inventory_set_slot(player, player->inventory, slot_index, NULL, 1, 0); + inventory_set_slot(player, player->inventory, slot_index, NULL, 1); } float onEntityHitWhileWearing_armor(struct world* world, struct player* player, uint8_t slot_index, struct slot* slot, float damage) { @@ -629,7 +629,7 @@ void onItemUse_bow(struct world* world, struct player* player, uint8_t slot_inde if (--ammo->count <= 0) { ammo = NULL; } - inventory_set_slot(player, player->inventory, bs, ammo, 1, 1); + inventory_set_slot(player, player->inventory, bs, ammo, 1); } world_spawn_entity(player->world, arrow); } From 403858bec4b88dd146f1da1714e6dff92c38ae99 Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 17:47:04 +0300 Subject: [PATCH 3/8] More fixes. --- include/basin/game.h | 2 +- src/accept.c | 2 +- src/entity_impl.c | 87 +++++++++++++++++++++++--------------------- src/game.c | 33 ++++++++++------- src/player.c | 2 +- 5 files changed, 69 insertions(+), 57 deletions(-) diff --git a/include/basin/game.h b/include/basin/game.h index d96e1c0..b5fffab 100644 --- a/include/basin/game.h +++ b/include/basin/game.h @@ -51,6 +51,6 @@ void broadcastf(char* color, char* fmt, ...); #define BEGIN_BROADCAST_EXCEPT(players, except) if(players->mc) pthread_rwlock_rdlock(&players->data_mutex); for (size_t i = 0; i < players->size; i++) {struct player* bc_player = (struct player*) players->data[i];if (bc_player != NULL && bc_player != except) { #define END_BROADCAST_MT(players) }} if(players->mc) pthread_rwlock_unlock(&players->data_mutex); #define END_BROADCAST }} - */ +*/ #endif /* BASIN_GAME_H_ */ diff --git a/src/accept.c b/src/accept.c index f390e06..f92dfb1 100644 --- a/src/accept.c +++ b/src/accept.c @@ -28,7 +28,7 @@ void run_accept(struct server* server) { spfd.fd = server->fd; while (1) { struct mempool* pool = mempool_new(); - struct connection* conn = pcalloc(pool, sizeof(struct connection)); + struct connection* conn = pcalloc(mempool_new(), sizeof(struct connection)); conn->pool = pool; conn->addrlen = sizeof(struct sockaddr_in6); conn->managed_conn = pcalloc(conn->pool, sizeof(struct netmgr_connection)); diff --git a/src/entity_impl.c b/src/entity_impl.c index cecba30..713e7cb 100644 --- a/src/entity_impl.c +++ b/src/entity_impl.c @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -22,7 +23,7 @@ void onSpawned_minecart(struct world* world, struct entity* entity) { void onTick_tnt(struct world* world, struct entity* entity) { if (entity->data.tnt.fuse-- <= 0) { world_despawn_entity(world, entity); - world_explode(world, NULL, entity->x, entity->y + .5, entity->z, 4f); + world_explode(world, NULL, entity->x, entity->y + .5, entity->z, .4f); } } @@ -112,8 +113,9 @@ void tick_arrow(struct world* world, struct entity* entity) { BEGIN_HASHMAP_ITERATION(world->entities); struct entity* e2 = value; double rd = entity_distsq_block(e2, entity->x + entity->motX, entity->y + entity->motY, entity->z + entity->motZ); - if (rd > 4) continue; - //printf("4d %f\n", rd); + if (rd > 4) { + //printf("4d %f\n", rd); + } if (e2 != entity && e2 != shooter && hasFlag(getEntityInfo(e2->type), "livingbase")) { //todo: ticksInAir >= 5? struct boundingbox eb; getEntityCollision(e2, &eb); @@ -232,43 +234,47 @@ void tick_itemstack(struct world* world, struct entity* entity) { cebb.minZ -= .625; cebb.maxZ += .625; struct boundingbox oebb; -//int32_t chunk_x = ((int32_t) entity->x) >> 4; -//int32_t chunk_z = ((int32_t) entity->z) >> 4; -//for (int32_t icx = chunk_x - 1; icx <= chunk_x + 1; icx++) -//for (int32_t icz = chunk_z - 1; icz <= chunk_z + 1; icz++) { -//struct chunk* ch = world_get_chunk(entity->world, icx, icz); -//if (ch != NULL) { - BEGIN_HASHMAP_ITERATION(entity->world->entities); - struct entity* oe = (struct entity*) value; - if (oe == entity || entity_distsq(entity, oe) > 16. * 16.) continue; - if (oe->type == ENT_PLAYER && oe->health > 0.) { - getEntityCollision(oe, &oebb); - //printf("%f, %f, %f vs %f, %f, %f\n", entity->x, entity->y, entity->z, oe->x, oe->y, oe->z); - if (boundingbox_intersects(&oebb, &cebb)) { - int os = entity->data.itemstack.slot->count; - pthread_mutex_lock(&oe->data.player.player->inventory->mut); - int r = inventory_add_player(oe->data.player.player, oe->data.player.player->inventory, entity->data.itemstack.slot, 1); - pthread_mutex_unlock(&oe->data.player.player->inventory->mut); - if (r <= 0) { - BEGIN_BROADCAST_DIST(entity, 32.) - struct packet* pkt = xmalloc(sizeof(struct packet)); - pkt->id = PKT_PLAY_CLIENT_COLLECTITEM; - pkt->data.play_client.collectitem.collected_entity_id = entity->id; - pkt->data.play_client.collectitem.collector_entity_id = oe->id; - pkt->data.play_client.collectitem.pickup_item_count = os - r; - add_queue(bc_player->outgoing_packets, pkt); - END_BROADCAST(entity->world->players) - world_despawn_entity(world, entity); - freeEntity(entity); + /* + int32_t chunk_x = ((int32_t) entity->x) >> 4; + int32_t chunk_z = ((int32_t) entity->z) >> 4; + for (int32_t icx = chunk_x - 1; icx <= chunk_x + 1; icx++) + for (int32_t icz = chunk_z - 1; icz <= chunk_z + 1; icz++) { + struct chunk* ch = world_get_chunk(entity->world, icx, icz); + */ + if (ch != NULL) { + BEGIN_HASHMAP_ITERATION(entity->world->entities); + struct entity* oe = (struct entity*) value; + if (oe == entity || entity_distsq(entity, oe) > 16. * 16.) { + + } + if (oe->type == ENT_PLAYER && oe->health > 0.) { + getEntityCollision(oe, &oebb); + //printf("%f, %f, %f vs %f, %f, %f\n", entity->x, entity->y, entity->z, oe->x, oe->y, oe->z); + if (boundingbox_intersects(&oebb, &cebb)) { + int os = entity->data.itemstack.slot->count; + pthread_mutex_lock(&oe->data.player.player->inventory->mut); + int r = inventory_add_player(oe->data.player.player, oe->data.player.player->inventory, entity->data.itemstack.slot, 1); + pthread_mutex_unlock(&oe->data.player.player->inventory->mut); + if (r <= 0) { + BEGIN_BROADCAST_DIST(entity, 32.); + struct packet* pkt = xmalloc(sizeof(struct packet)); + pkt->id = PKT_PLAY_CLIENT_COLLECTITEM; + pkt->data.play_client.collectitem.collected_entity_id = entity->id; + pkt->data.play_client.collectitem.collector_entity_id = oe->id; + pkt->data.play_client.collectitem.pickup_item_count = os - r; + add_queue(bc_player->outgoing_packets, pkt); + END_BROADCAST(entity->world->players); + world_despawn_entity(world, entity); + freeEntity(entity); } else { - BEGIN_BROADCAST_DIST(entity, 128.) - struct packet* pkt = xmalloc(sizeof(struct packet)); - pkt->id = PKT_PLAY_CLIENT_ENTITYMETADATA; - pkt->data.play_client.entitymetadata.entity_id = entity->id; - writeMetadata(entity, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size); - add_queue(bc_player->outgoing_packets, pkt); - END_BROADCAST(entity->world->players) - BREAK_HASHMAP_ITERATION(entity->world->entities); + BEGIN_BROADCAST_DIST(entity, 128.); + struct packet* pkt = xmalloc(sizeof(struct packet)); + pkt->id = PKT_PLAY_CLIENT_ENTITYMETADATA; + pkt->data.play_client.entitymetadata.entity_id = entity->id; + writeMetadata(entity, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size); + add_queue(bc_player->outgoing_packets, pkt); + END_BROADCAST(entity->world->players); + BREAK_HASHMAP_ITERATION(entity->world->entities); } } } else if (oe->type == ENT_ITEM) { @@ -283,14 +289,13 @@ void tick_itemstack(struct world* world, struct entity* entity) { world_despawn_entity(world, entity); oe->data.itemstack.slot->count += entity->data.itemstack.slot->count; freeEntity(entity); - BEGIN_BROADCAST_DIST(oe, 128.) + BEGIN_BROADCAST_DIST(oe, 128.); struct packet* pkt = xmalloc(sizeof(struct packet)); pkt->id = PKT_PLAY_CLIENT_ENTITYMETADATA; pkt->data.play_client.entitymetadata.entity_id = oe->id; writeMetadata(oe, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size); add_queue(bc_player->outgoing_packets, pkt); END_BROADCAST(oe->world->players) - return 1; } } } diff --git a/src/game.c b/src/game.c index 2890abc..d03873b 100644 --- a/src/game.c +++ b/src/game.c @@ -37,7 +37,8 @@ void game_load_player(struct player* to, struct player* from) { packet->data.play_client.spawnplayer.z = from->entity->z; packet->data.play_client.spawnplayer.yaw = (uint8_t) ((from->entity->yaw / 360.) * 256.); packet->data.play_client.spawnplayer.pitch = (uint8_t) ((from->entity->pitch / 360.) * 256.); - entitymeta_write(from->entity, &packet->data.play_client.spawnplayer.metadata.metadata, &packet->data.play_client.spawnplayer.metadata.metadata_size); + struct mempool* pool = mempool_new(); + entitymeta_write(from->entity, &packet->data.play_client.spawnplayer.metadata.metadata, &packet->data.play_client.spawnplayer.metadata.metadata_size, pool); queue_push(to->outgoing_packets, packet); game_entity_equipment(to, from->entity, 0, from->inventory->slots[from->currentItem + 36]); game_entity_equipment(to, from->entity, 5, from->inventory->slots[5]); @@ -74,7 +75,8 @@ void game_load_entity(struct player* to, struct entity* from) { queue_push(to->outgoing_packets, packet); packet = packet_new(mempool_new(), PKT_PLAY_CLIENT_ENTITYMETADATA); packet->data.play_client.entitymetadata.entity_id = from->id; - entitymeta_write(from, &packet->data.play_client.entitymetadata.metadata.metadata, &packet->data.play_client.entitymetadata.metadata.metadata_size); + struct mempool* pool = mempool_new(); + entitymeta_write(from, &packet->data.play_client.entitymetadata.metadata.metadata, &packet->data.play_client.entitymetadata.metadata.metadata_size, pool); queue_push(to->outgoing_packets, packet); } else if (from->type == ENT_PLAYER) { return; @@ -116,7 +118,8 @@ void game_load_entity(struct player* to, struct entity* from) { packet->data.play_client.spawnmob.velocity_x = (int16_t)(from->motX * 8000.); packet->data.play_client.spawnmob.velocity_y = (int16_t)(from->motY * 8000.); packet->data.play_client.spawnmob.velocity_z = (int16_t)(from->motZ * 8000.); - entitymeta_write(from, &packet->data.play_client.spawnmob.metadata.metadata, &packet->data.play_client.spawnmob.metadata.metadata_size); + struct mempool* pool = mempool_new(); + entitymeta_write(from, &packet->data.play_client.spawnmob.metadata.metadata, &packet->data.play_client.spawnmob.metadata.metadata_size, pool); queue_push(to->outgoing_packets, packet); } hashmap_putint(to->loaded_entities, (uint64_t) from->id, from); @@ -177,10 +180,11 @@ void game_drop_block(struct world* world, struct slot* slot, int32_t x, int32_t item->motY = .2; item->motZ = game_rand_float() * .2 - .1; item->data.itemstack.delayBeforeCanPickup = 0; - slot_duplicate(slot, item->data.itemstack.slot); + struct mempool* pool = mempool_new(); + slot_duplicate(pool, slot, item->data.itemstack.slot); world_spawn_entity(world, item); - BEGIN_BROADCAST_DIST(item, 128.) - game_load_entity(bc_player, item); + BEGIN_BROADCAST_DIST(item, 128.); + game_load_entity(bc_player, item); END_BROADCAST(item->world->players) } @@ -197,10 +201,11 @@ void dropPlayerItem(struct player* player, struct slot* drop) { item->motY += (game_rand_float() - game_rand_float()) * .1; item->motZ += sin(nos) * mag; item->data.itemstack.delayBeforeCanPickup = 20; - slot_duplicate(drop, item->data.itemstack.slot); + struct mempool* pool = mempool_new(); + slot_duplicate(pool, drop, item->data.itemstack.slot); world_spawn_entity(player->world, item); - BEGIN_BROADCAST_DIST(player->entity, 128.) - game_load_entity(bc_player, item); + BEGIN_BROADCAST_DIST(player->entity, 128.); + game_load_entity(bc_player, item); END_BROADCAST(player->world->players) } @@ -229,10 +234,11 @@ void dropEntityItem_explode(struct entity* entity, struct slot* drop) { item->motZ = cos(f2) * f1; item->motY = .2; item->data.itemstack.delayBeforeCanPickup = 20; - slot_duplicate(drop, item->data.itemstack.slot); + struct mempool* pool = mempool_new(); + slot_duplicate(pool, drop, item->data.itemstack.slot); world_spawn_entity(entity->world, item); - BEGIN_BROADCAST_DIST(entity, 128.) - game_load_entity(bc_player, item); + BEGIN_BROADCAST_DIST(entity, 128.); + game_load_entity(bc_player, item); END_BROADCAST(entity->world->players) } @@ -284,7 +290,8 @@ void player_openInventory(struct player* player, struct inventory* inv) { pkt->data.play_client.windowitems.count = inv->slot_count; pkt->data.play_client.windowitems.slot_data = xmalloc(sizeof(struct slot) * inv->slot_count); for (size_t i = 0; i < inv->slot_count; i++) { - slot_duplicate(inv->slots[i], &pkt->data.play_client.windowitems.slot_data[i]); + struct mempool* pool = mempool_new(); + slot_duplicate(pool, inv->slots[i], &pkt->data.play_client.windowitems.slot_data[i]); } add_queue(player->outgoing_packets, pkt); } diff --git a/src/player.c b/src/player.c index 899612c..245d311 100644 --- a/src/player.c +++ b/src/player.c @@ -537,7 +537,7 @@ void player_closeWindow(struct player* player, uint16_t windowID) { inventory = NULL; } else if (inventory->type == INVTYPE_CHEST) { if (inventory->tile != NULL) { - BEGIN_BROADCAST_DIST(player->entity, 128.){ + BEGIN_BROADCAST_DIST(player->entity, 128.) { struct packet* pkt = packet_new(mempool_new(), PKT_PLAY_CLIENT_BLOCKACTION); pkt->data.play_client.blockaction.location.x = inventory->tile->x; pkt->data.play_client.blockaction.location.y = inventory->tile->y; From d86f2848dda0831bdbe15ed2605821b78e81b8b1 Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 17:55:00 +0300 Subject: [PATCH 4/8] Fixed struct and moved header files. --- include/basin/accept.h | 13 +++++++++++++ {src => include/basin}/light.h | 0 {src => include/basin}/login_stage_handler.h | 0 {src => include/basin}/wake_thread.h | 0 {src => include/basin}/work.h | 0 src/accept.c | 4 ++-- src/accept.h | 8 -------- src/light.c | 2 +- src/login_stage_handler.c | 2 +- src/main.c | 2 +- src/wake_thread.c | 2 +- src/work.c | 8 ++++---- 12 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 include/basin/accept.h rename {src => include/basin}/light.h (100%) rename {src => include/basin}/login_stage_handler.h (100%) rename {src => include/basin}/wake_thread.h (100%) rename {src => include/basin}/work.h (100%) delete mode 100644 src/accept.h diff --git a/include/basin/accept.h b/include/basin/accept.h new file mode 100644 index 0000000..48ddfe9 --- /dev/null +++ b/include/basin/accept.h @@ -0,0 +1,13 @@ +#ifndef BASIN_ACCEPT_H_ +#define BASIN_ACCEPT_H_ + +#include + +void run_accept(struct server* param); + +struct timeval { + int32_t tv_sec; + int32_t tv_usec; +}; + +#endif /* BASIN_ACCEPT_H_ */ diff --git a/src/light.h b/include/basin/light.h similarity index 100% rename from src/light.h rename to include/basin/light.h diff --git a/src/login_stage_handler.h b/include/basin/login_stage_handler.h similarity index 100% rename from src/login_stage_handler.h rename to include/basin/login_stage_handler.h diff --git a/src/wake_thread.h b/include/basin/wake_thread.h similarity index 100% rename from src/wake_thread.h rename to include/basin/wake_thread.h diff --git a/src/work.h b/include/basin/work.h similarity index 100% rename from src/work.h rename to include/basin/work.h diff --git a/src/accept.c b/src/accept.c index f92dfb1..54fd3bc 100644 --- a/src/accept.c +++ b/src/accept.c @@ -1,5 +1,5 @@ -#include "accept.h" -#include "work.h" +#include +#include #include #include #include diff --git a/src/accept.h b/src/accept.h deleted file mode 100644 index a67630d..0000000 --- a/src/accept.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef ACCEPT_H_ -#define ACCEPT_H_ - -#include - -void run_accept(struct server* param); - -#endif /* ACCEPT_H_ */ diff --git a/src/light.c b/src/light.c index 3198a5e..ebc636f 100644 --- a/src/light.c +++ b/src/light.c @@ -1,5 +1,5 @@ #include -#include "light.h" +#include /* void light_proc(struct world* world, struct chunk* chunk, int32_t x, int32_t y, int32_t z, uint8_t light) { diff --git a/src/login_stage_handler.c b/src/login_stage_handler.c index 348b77c..1c98769 100644 --- a/src/login_stage_handler.c +++ b/src/login_stage_handler.c @@ -1,4 +1,4 @@ -#include "login_stage_handler.h" +#include #include #include #include diff --git a/src/main.c b/src/main.c index 5f6fd50..35ffe9b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ #include "accept.h" -#include "work.h" +#include #include #include #include diff --git a/src/wake_thread.c b/src/wake_thread.c index 3bf7391..01d0dc7 100644 --- a/src/wake_thread.c +++ b/src/wake_thread.c @@ -1,4 +1,4 @@ -#include "wake_thread.h" +#include #include #include #include diff --git a/src/work.c b/src/work.c index b9f3032..0cdaf4e 100644 --- a/src/work.c +++ b/src/work.c @@ -1,7 +1,7 @@ -#include "work.h" -#include "accept.h" -#include "basin/packet.h" -#include "login_stage_handler.h" +#include +#include +#include +#include #include #include #include From 5274ab01e04cb70b081a8f76724973cd83975aed Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 18:09:02 +0300 Subject: [PATCH 5/8] Removed several continues, made some fixes, added TO-DO. --- src/ai.c | 8 ++++++-- src/entity_impl.c | 15 +++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/ai.c b/src/ai.c index a82a64e..ee7f1c8 100644 --- a/src/ai.c +++ b/src/ai.c @@ -601,11 +601,15 @@ int ai_shouldnearestattackabletarget(struct world* world, struct entity* entity, struct entity* ce = NULL; BEGIN_HASHMAP_ITERATION(world->entities); struct entity* ie = value; - if (!entity_has_flag(entity_get_info(ie->type), "livingbase") || ie == entity) continue; + if (!entity_has_flag(entity_get_info(ie->type), "livingbase") || ie == entity) { + + } double dsq = entity_distsq(entity, value); if (ie->type == ENT_PLAYER) { struct player* pl = ie->data.player.player; - if (pl->gamemode == 1 || pl->gamemode == 3 || pl->invulnerable) continue; + if (pl->gamemode == 1 || pl->gamemode == 3 || pl->invulnerable) { + + } int sk = entity_has_flag(entity_get_info(entity->type), "skeleton"); int zo = entity_has_flag(entity_get_info(entity->type), "zombie"); int cr = entity_has_flag(entity_get_info(entity->type), "creeper"); diff --git a/src/entity_impl.c b/src/entity_impl.c index 713e7cb..3e53945 100644 --- a/src/entity_impl.c +++ b/src/entity_impl.c @@ -1,7 +1,6 @@ - +#include #include #include -#include #include #include #include @@ -218,14 +217,18 @@ void tick_arrow(struct world* world, struct entity* entity) { void tick_itemstack(struct world* world, struct entity* entity) { if (entity->data.itemstack.delayBeforeCanPickup > 0) { entity->data.itemstack.delayBeforeCanPickup--; - return 0; } if (entity->age >= 6000) { - world_despawn_entity(world, entity); + world_despawn_entity(world, entity); + /* + TODO: Make this a configurable option and improve the mechanics of despawning. + All nametagged entities (as well as wolves) must be excluded. + */ freeEntity(entity); - return 1; } - if (tick_counter % 10 != 0) return 0; + if (world->tick_counter % 10 != 0) { + + } struct boundingbox cebb; getEntityCollision(entity, &cebb); cebb.minX -= .625; From e89badc722b21f579b85b82d94f89ec0be700265 Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 21:05:51 +0300 Subject: [PATCH 6/8] Small tweaks. --- src/accept.c | 2 +- src/login_stage_handler.c | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/accept.c b/src/accept.c index 54fd3bc..c09c3a7 100644 --- a/src/accept.c +++ b/src/accept.c @@ -28,7 +28,7 @@ void run_accept(struct server* server) { spfd.fd = server->fd; while (1) { struct mempool* pool = mempool_new(); - struct connection* conn = pcalloc(mempool_new(), sizeof(struct connection)); + struct connection* conn = pcalloc(pool, sizeof(struct connection)); conn->pool = pool; conn->addrlen = sizeof(struct sockaddr_in6); conn->managed_conn = pcalloc(conn->pool, sizeof(struct netmgr_connection)); diff --git a/src/login_stage_handler.c b/src/login_stage_handler.c index 1c98769..b1bf125 100644 --- a/src/login_stage_handler.c +++ b/src/login_stage_handler.c @@ -293,7 +293,12 @@ int handle_encryption_response(struct connection* conn, struct packet* packet) { SSL_set_fd(ssl, session_tls_fd); if (SSL_connect(ssl) != 1) goto ssl_error; char write_buf[4096]; - int write_length = snprintf(write_buf, 1024, "GET /session/minecraft/hasJoined?username=%s&serverId=%s HTTP/1.1\r\nHost: sessionserver.mojang.com\r\nUser-Agent: Basin " VERSION "\r\nConnection: close\r\n\r\n", conn->online_username, hex_hash_signed); + int write_length = snprintf(write_buf, 1024, "\ + GET /session/minecraft/hasJoined?username=%s&serverId=%s \ + HTTP/1.1\r\nHost: sessionserver.mojang.com\r\n\ + User-Agent: Basin " VERSION "\r\n\ + Connection: close\r\n\r\n\ + ", conn->online_username, hex_hash_signed); int written = 0; while (written < write_length) { int r = SSL_write(ssl, write_buf, write_length); From 600eb1a8822cd2e1e4920b3a3ad10e09822c1e72 Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 21:13:15 +0300 Subject: [PATCH 7/8] Revert "Fixed overwhelming amount of parameters and one more leftover semi-colon." This reverts commit 4de52395e42ac8c55de736721b4c21f5effb7ad9. --- src/entity_impl.c | 2 +- src/item.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/entity_impl.c b/src/entity_impl.c index 3e53945..3d481c3 100644 --- a/src/entity_impl.c +++ b/src/entity_impl.c @@ -109,7 +109,7 @@ void tick_arrow(struct world* world, struct entity* entity) { struct entity* ehit = NULL; struct entity* shooter = world_get_entity(world, entity->objectData - 1); double bd = 999.; - BEGIN_HASHMAP_ITERATION(world->entities); + BEGIN_HASHMAP_ITERATION(world->entities) struct entity* e2 = value; double rd = entity_distsq_block(e2, entity->x + entity->motX, entity->y + entity->motY, entity->z + entity->motZ); if (rd > 4) { diff --git a/src/item.c b/src/item.c index 61e8a29..40deff5 100644 --- a/src/item.c +++ b/src/item.c @@ -166,7 +166,7 @@ int onItemInteract_painting(struct world* world, struct player* player, uint8_t else if (p == 24) ent->data.painting.title = "Skeleton"; else if (p == 25) ent->data.painting.title = "DonkeyKong"; world_spawn_entity(world, ent); - if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1); + if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1, 1); } return 0; @@ -185,7 +185,7 @@ int onItemInteract_minecart(struct world* world, struct player* player, uint8_t else if (slot->item == ITM_MINECARTCOMMANDBLOCK) et = ENT_MINECARTCOMMANDBLOCK; struct entity* ent = entity_new(nextEntityID++, (double) x + .5, (double) y + dy, (double) z + .5, et, 0., 0.); world_spawn_entity(world, ent); - if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1); + if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1, 1); return 0; } @@ -544,7 +544,7 @@ void onItemUse_armor(struct world* world, struct player* player, uint8_t slot_in else if (ii->armorType == ARMOR_BOOTS) sli = 8; if (inventory_get(player, player->inventory, sli) != NULL) return; inventory_set_slot(player, player->inventory, sli, slot, 1); - inventory_set_slot(player, player->inventory, slot_index, NULL, 1); + inventory_set_slot(player, player->inventory, slot_index, NULL, 1, 0); } float onEntityHitWhileWearing_armor(struct world* world, struct player* player, uint8_t slot_index, struct slot* slot, float damage) { @@ -629,7 +629,7 @@ void onItemUse_bow(struct world* world, struct player* player, uint8_t slot_inde if (--ammo->count <= 0) { ammo = NULL; } - inventory_set_slot(player, player->inventory, bs, ammo, 1); + inventory_set_slot(player, player->inventory, bs, ammo, 1, 1); } world_spawn_entity(player->world, arrow); } From 3614ca243021bceec6aabfd0e81998f364cc4605 Mon Sep 17 00:00:00 2001 From: Panagiotis Vasilopoulos Date: Sun, 5 May 2019 21:40:13 +0300 Subject: [PATCH 8/8] Re-did some changes. --- src/accept.c | 1 - src/block.c | 4 +++- src/command.c | 4 +++- src/crafting.c | 27 +++++++++++++++++++-------- src/entity.c | 45 +++++++++++++++++++++++++++++++++------------ src/entity_impl.c | 10 +++++----- src/game.c | 3 +-- src/item.c | 6 +++--- 8 files changed, 67 insertions(+), 33 deletions(-) diff --git a/src/accept.c b/src/accept.c index c09c3a7..98e82ea 100644 --- a/src/accept.c +++ b/src/accept.c @@ -46,7 +46,6 @@ void run_accept(struct server* server) { if ((spfd.revents ^ POLLIN) != 0) { printf("Error after polling server: %i (poll revents)!\n", spfd.revents); pfree(pool); - break; } spfd.revents = 0; int fd = accept(server->fd, (struct sockaddr*) &conn->addr, &conn->addrlen); diff --git a/src/block.c b/src/block.c index 1821645..ef35ee7 100644 --- a/src/block.c +++ b/src/block.c @@ -448,10 +448,12 @@ void onBlockInteract_workbench(struct world* world, block blk, int32_t x, int32_ block onBlockPlaced_chest(struct world* world, block blk, int32_t x, int32_t y, int32_t z, block replaced) { int16_t meta = blk & 0x0f; if (meta < 2 || meta > 5) meta = 2; + struct mempool* pool = mempool_new(); struct tile_entity* te = tile_new("minecraft:chest", x, y, z); te->data.chest.lock = NULL; te->data.chest.inv = xmalloc(sizeof(struct inventory)); - inventory_new(te->data.chest.inv, INVTYPE_CHEST, 2, 27); + struct mempool* pool = mempool_new(); + inventory_new(pool, te->data.chest.inv, INVTYPE_CHEST, 2, 27); te->data.chest.inv->tile = te; te->data.chest.inv->title = xstrdup("{\"text\": \"Chest\"}", 0); world_set_tile(world, x, y, z, te); diff --git a/src/command.c b/src/command.c index 4d7e4eb..b43d1cd 100644 --- a/src/command.c +++ b/src/command.c @@ -234,7 +234,9 @@ void callCommand(struct player* player, struct mempool* pool, char* command) { } for (size_t i3 = 0; i3 < registered_commands->size; i3++) { struct command* com = (struct command*) registered_commands->data[i3]; - if (com == NULL) continue; + if (com == NULL) { + + } if (streq_nocase(com->command, rc)) { (*com->callback)(player, args, arg_count); return; diff --git a/src/crafting.c b/src/crafting.c index b9e75f1..5996433 100644 --- a/src/crafting.c +++ b/src/crafting.c @@ -82,8 +82,7 @@ void crafting_init() { ITER_LLIST_END(); } list_append(crafting_recipies, recipe); - continue; - crafting_format_error:; + crafting_format_error: ; printf("[WARNING] Error Loading Crafting Recipe \"%s\"! Skipped.\n", child_json->name); ITER_LLIST_END(); } @@ -94,7 +93,9 @@ void crafting_once(struct player* player, struct inventory* inv) { int cap = inv->type == INVTYPE_PLAYERINVENTORY ? 4 : 9; for (int i = 1; i <= cap; i++) { struct slot* slot = inventory_get(player, inv, i); - if (slot == NULL) continue; + if (slot == NULL) { + + } if (--slot->count <= 0) { if (slot->nbt != NULL) { pfree(slot->nbt->pool); @@ -111,12 +112,16 @@ int crafting_all(struct player* player, struct inventory* inv) { uint8_t count = 64; for (int i = 1; i <= cap; i++) { struct slot* slot = inventory_get(player, inv, i); - if (slot == NULL) continue; + if (slot == NULL) { + + } if (slot->count < count) count = slot->count; } for (int i = 1; i <= cap; i++) { struct slot* slot = inventory_get(player, inv, i); - if (slot == NULL) continue; + if (slot == NULL) { + + } slot->count -= count; if (slot->count <= 0) { if (slot->nbt != NULL) { @@ -177,12 +182,16 @@ struct slot* crafting_result(struct mempool* pool, struct slot** slots, size_t s for (size_t i = 0; i < crafting_recipies->size; i++) { struct crafting_recipe* recipe = (struct crafting_recipe*) crafting_recipies->data[i]; - if (recipe == NULL || (recipe->width > 2 && slot_count <= 4) || (slot_count == 4 && (recipe->slot[6] != NULL || recipe->slot[7] != NULL || recipe->slot[8] != NULL))) continue; + if (recipe == NULL || (recipe->width > 2 && slot_count <= 4) || (slot_count == 4 && (recipe->slot[6] != NULL || recipe->slot[7] != NULL || recipe->slot[8] != NULL))) { + + } if (recipe->shapeless) { // TODO: optimize int matching = 1; for (int slot_index = 0; slot_index <= slot_count; slot_index++) { struct slot* slot = slots[slot_index]; - if (slot == NULL) continue; + if (slot == NULL) { + + } int local_matched = 0; for (int ri = 0; ri < 9; ri++) { struct slot* local_slot = recipe->slot[ri]; @@ -199,7 +208,9 @@ struct slot* crafting_result(struct mempool* pool, struct slot** slots, size_t s if (matching) { for (int slot_index_2 = 0; slot_index_2 < 9; slot_index_2++) { struct slot* slot = recipe->slot[slot_index_2]; - if (slot == NULL) continue; + if (slot == NULL) { + + } int local_matched = 0; for (int ls = 1; ls <= 4; ls++) { struct slot* local_slot = slots[ls]; diff --git a/src/entity.c b/src/entity.c index ae57d3a..9c0048f 100644 --- a/src/entity.c +++ b/src/entity.c @@ -133,7 +133,6 @@ void init_entities() { info->dataname = str_dup(tmp->data.string, 0, entities_pool); list_set(entity_infos, id, info); hashmap_put(entity_infos_by_name, str_tolower(str_dup(info->dataname, 0, entities_pool)), info); - continue; entity_error: ; printf("[WARNING] Error Loading Entity \"%s\"! Skipped.\n", child_json->name); ITER_LLIST_END(); @@ -539,10 +538,14 @@ int entity_move(struct entity* entity, double* motionX, double* motionY, double* for (int32_t z = (int32_t) floor(obb.minZ); z < floor(obb.maxZ + 1.); z++) { for (int32_t y = (int32_t) floor(obb.minY); y < floor(obb.maxY + 1.); y++) { block b = world_get_block_guess(entity->world, ch, x, y, z); - if (b == 0) continue; + if (b == 0) { + + } b = entity_adjustCollision(entity->world, entity, ch, b, x, y, z); struct block_info* bi = getBlockInfo(b); - if (bi == NULL) continue; + if (bi == NULL) { + + } for (size_t i = 0; i < bi->boundingBox_count; i++) { struct boundingbox* bbx = &bi->boundingBoxes[i]; struct boundingbox bbd; @@ -578,10 +581,14 @@ int entity_move(struct entity* entity, double* motionX, double* motionY, double* for (int32_t z = (int32_t) floor(obb.minZ); z < floor(obb.maxZ + 1.); z++) { for (int32_t y = (int32_t) floor(obb.minY); y < floor(obb.maxY + 1.); y++) { block b = world_get_block_guess(entity->world, ch, x, y, z); - if (b == 0) continue; + if (b == 0) { + + } b = entity_adjustCollision(entity->world, entity, ch, b, x, y, z); struct block_info* bi = getBlockInfo(b); - if (bi == NULL) continue; + if (bi == NULL) { + + } for (size_t i = 0; i < bi->boundingBox_count; i++) { struct boundingbox* bbx = &bi->boundingBoxes[i]; struct boundingbox bbd; @@ -617,10 +624,14 @@ int entity_move(struct entity* entity, double* motionX, double* motionY, double* for (int32_t z = (int32_t) floor(obb.minZ); z < floor(obb.maxZ + 1.); z++) { for (int32_t y = (int32_t) floor(obb.minY); y < floor(obb.maxY + 1.); y++) { block b = world_get_block_guess(entity->world, ch, x, y, z); - if (b == 0) continue; + if (b == 0) { + + } b = entity_adjustCollision(entity->world, entity, ch, b, x, y, z); struct block_info* bi = getBlockInfo(b); - if (bi == NULL) continue; + if (bi == NULL) { + + } for (size_t i = 0; i < bi->boundingBox_count; i++) { struct boundingbox* bbx = &bi->boundingBoxes[i]; struct boundingbox bbd; @@ -835,7 +846,9 @@ int damageEntity(struct entity* attacked, float damage, int armorable) { for (size_t i = 0; i < info->loot_count; i++) { struct entity_loot* el = &info->loots[i]; int amt = el->amountMax == el->amountMin ? el->amountMax : (rand() % (el->amountMax - el->amountMin) + el->amountMin); - if (amt <= 0) continue; + if (amt <= 0) { + + } struct slot it; it.item = el->id; it.count = (unsigned char) amt; @@ -888,9 +901,13 @@ int entity_inFluid(struct entity* entity, uint16_t blk, float ydown, int meta_ch for (int32_t z = (int32_t) floor(pbb.minZ); z < floor(pbb.maxZ + 1.); z++) { for (int32_t y = (int32_t) floor(pbb.minY); y < floor(pbb.maxY + 1.); y++) { block b = world_get_block(entity->world, x, y, z); - if (meta_check ? (b != blk) : ((b >> 4) != (blk >> 4))) continue; + if (meta_check ? (b != blk) : ((b >> 4) != (blk >> 4))) { + + } struct block_info* bi = getBlockInfo(b); - if (bi == NULL) continue; + if (bi == NULL) { + + } struct boundingbox bb2; bb2.minX = 0. + (double) x; bb2.maxX = 1. + (double) x; @@ -921,9 +938,13 @@ int entity_inBlock(struct entity* ent, block blk) { // blk = 0 for any block for (int32_t z = floor(obb.minZ); z < floor(obb.maxZ + 1.); z++) { for (int32_t y = floor(obb.minY); y < floor(obb.maxY + 1.); y++) { block b = world_get_block(ent->world, x, y, z); - if (b == 0 || (blk != 0 && blk != b)) continue; + if (b == 0 || (blk != 0 && blk != b)) { + + } struct block_info* bi = getBlockInfo(b); - if (bi == NULL) continue; + if (bi == NULL) { + + } for (size_t i = 0; i < bi->boundingBox_count; i++) { struct boundingbox* bb = &bi->boundingBoxes[i]; struct boundingbox nbb; diff --git a/src/entity_impl.c b/src/entity_impl.c index 3d481c3..b42f2e2 100644 --- a/src/entity_impl.c +++ b/src/entity_impl.c @@ -109,7 +109,7 @@ void tick_arrow(struct world* world, struct entity* entity) { struct entity* ehit = NULL; struct entity* shooter = world_get_entity(world, entity->objectData - 1); double bd = 999.; - BEGIN_HASHMAP_ITERATION(world->entities) + BEGIN_HASHMAP_ITERATION(world->entities); struct entity* e2 = value; double rd = entity_distsq_block(e2, entity->x + entity->motX, entity->y + entity->motY, entity->z + entity->motZ); if (rd > 4) { @@ -154,7 +154,7 @@ void tick_arrow(struct world* world, struct entity* entity) { if (ehit->type != ENT_ENDERMAN) { world_despawn_entity(world, entity); freeEntity(entity); - return 1; + return; } } else if (hf >= 0) { entity->x = hx; @@ -187,7 +187,7 @@ void tick_arrow(struct world* world, struct entity* entity) { if (entity->data.arrow.ticksInGround == 1200) { world_despawn_entity(world, entity); freeEntity(entity); - return 1; + return; } } @@ -211,7 +211,7 @@ void tick_arrow(struct world* world, struct entity* entity) { entity->yaw = entity->last_yaw + (entity->yaw - entity->last_yaw) * .2; } } - return 0; + return; } void tick_itemstack(struct world* world, struct entity* entity) { @@ -303,5 +303,5 @@ void tick_itemstack(struct world* world, struct entity* entity) { } } END_HASHMAP_ITERATION(entity->world->entities); - return 0; + return; } diff --git a/src/game.c b/src/game.c index d03873b..4b849cd 100644 --- a/src/game.c +++ b/src/game.c @@ -290,8 +290,7 @@ void player_openInventory(struct player* player, struct inventory* inv) { pkt->data.play_client.windowitems.count = inv->slot_count; pkt->data.play_client.windowitems.slot_data = xmalloc(sizeof(struct slot) * inv->slot_count); for (size_t i = 0; i < inv->slot_count; i++) { - struct mempool* pool = mempool_new(); - slot_duplicate(pool, inv->slots[i], &pkt->data.play_client.windowitems.slot_data[i]); + slot_duplicate(pkt->pool, inv->slots[i], &pkt->data.play_client.windowitems.slot_data[i]); } add_queue(player->outgoing_packets, pkt); } diff --git a/src/item.c b/src/item.c index 40deff5..3a2961a 100644 --- a/src/item.c +++ b/src/item.c @@ -166,7 +166,7 @@ int onItemInteract_painting(struct world* world, struct player* player, uint8_t else if (p == 24) ent->data.painting.title = "Skeleton"; else if (p == 25) ent->data.painting.title = "DonkeyKong"; world_spawn_entity(world, ent); - if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1, 1); + if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1); } return 0; @@ -185,7 +185,7 @@ int onItemInteract_minecart(struct world* world, struct player* player, uint8_t else if (slot->item == ITM_MINECARTCOMMANDBLOCK) et = ENT_MINECARTCOMMANDBLOCK; struct entity* ent = entity_new(nextEntityID++, (double) x + .5, (double) y + dy, (double) z + .5, et, 0., 0.); world_spawn_entity(world, ent); - if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1, 1); + if (player->gamemode != 1) inventory_set_slot(player, player->inventory, 36 + player->currentItem, NULL, 1); return 0; } @@ -629,7 +629,7 @@ void onItemUse_bow(struct world* world, struct player* player, uint8_t slot_inde if (--ammo->count <= 0) { ammo = NULL; } - inventory_set_slot(player, player->inventory, bs, ammo, 1, 1); + inventory_set_slot(player, player->inventory, bs, ammo, 1); } world_spawn_entity(player->world, arrow); }