Skip to content

Commit 403858b

Browse files
committed
More fixes.
1 parent 4de5239 commit 403858b

5 files changed

Lines changed: 69 additions & 57 deletions

File tree

include/basin/game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ void broadcastf(char* color, char* fmt, ...);
5151
#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) {
5252
#define END_BROADCAST_MT(players) }} if(players->mc) pthread_rwlock_unlock(&players->data_mutex);
5353
#define END_BROADCAST }}
54-
*/
54+
*/
5555

5656
#endif /* BASIN_GAME_H_ */

src/accept.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void run_accept(struct server* server) {
2828
spfd.fd = server->fd;
2929
while (1) {
3030
struct mempool* pool = mempool_new();
31-
struct connection* conn = pcalloc(pool, sizeof(struct connection));
31+
struct connection* conn = pcalloc(mempool_new(), sizeof(struct connection));
3232
conn->pool = pool;
3333
conn->addrlen = sizeof(struct sockaddr_in6);
3434
conn->managed_conn = pcalloc(conn->pool, sizeof(struct netmgr_connection));

src/entity_impl.c

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <basin/entity_impl.h>
33
#include <basin/entity.h>
4+
#include <basin/server.h>
45
#include <basin/game.h>
56
#include <basin/packet.h>
67
#include <avuna/pmem.h>
@@ -22,7 +23,7 @@ void onSpawned_minecart(struct world* world, struct entity* entity) {
2223
void onTick_tnt(struct world* world, struct entity* entity) {
2324
if (entity->data.tnt.fuse-- <= 0) {
2425
world_despawn_entity(world, entity);
25-
world_explode(world, NULL, entity->x, entity->y + .5, entity->z, 4f);
26+
world_explode(world, NULL, entity->x, entity->y + .5, entity->z, .4f);
2627
}
2728
}
2829

@@ -112,8 +113,9 @@ void tick_arrow(struct world* world, struct entity* entity) {
112113
BEGIN_HASHMAP_ITERATION(world->entities);
113114
struct entity* e2 = value;
114115
double rd = entity_distsq_block(e2, entity->x + entity->motX, entity->y + entity->motY, entity->z + entity->motZ);
115-
if (rd > 4) continue;
116-
//printf("4d %f\n", rd);
116+
if (rd > 4) {
117+
//printf("4d %f\n", rd);
118+
}
117119
if (e2 != entity && e2 != shooter && hasFlag(getEntityInfo(e2->type), "livingbase")) { //todo: ticksInAir >= 5?
118120
struct boundingbox eb;
119121
getEntityCollision(e2, &eb);
@@ -232,43 +234,47 @@ void tick_itemstack(struct world* world, struct entity* entity) {
232234
cebb.minZ -= .625;
233235
cebb.maxZ += .625;
234236
struct boundingbox oebb;
235-
//int32_t chunk_x = ((int32_t) entity->x) >> 4;
236-
//int32_t chunk_z = ((int32_t) entity->z) >> 4;
237-
//for (int32_t icx = chunk_x - 1; icx <= chunk_x + 1; icx++)
238-
//for (int32_t icz = chunk_z - 1; icz <= chunk_z + 1; icz++) {
239-
//struct chunk* ch = world_get_chunk(entity->world, icx, icz);
240-
//if (ch != NULL) {
241-
BEGIN_HASHMAP_ITERATION(entity->world->entities);
242-
struct entity* oe = (struct entity*) value;
243-
if (oe == entity || entity_distsq(entity, oe) > 16. * 16.) continue;
244-
if (oe->type == ENT_PLAYER && oe->health > 0.) {
245-
getEntityCollision(oe, &oebb);
246-
//printf("%f, %f, %f vs %f, %f, %f\n", entity->x, entity->y, entity->z, oe->x, oe->y, oe->z);
247-
if (boundingbox_intersects(&oebb, &cebb)) {
248-
int os = entity->data.itemstack.slot->count;
249-
pthread_mutex_lock(&oe->data.player.player->inventory->mut);
250-
int r = inventory_add_player(oe->data.player.player, oe->data.player.player->inventory, entity->data.itemstack.slot, 1);
251-
pthread_mutex_unlock(&oe->data.player.player->inventory->mut);
252-
if (r <= 0) {
253-
BEGIN_BROADCAST_DIST(entity, 32.)
254-
struct packet* pkt = xmalloc(sizeof(struct packet));
255-
pkt->id = PKT_PLAY_CLIENT_COLLECTITEM;
256-
pkt->data.play_client.collectitem.collected_entity_id = entity->id;
257-
pkt->data.play_client.collectitem.collector_entity_id = oe->id;
258-
pkt->data.play_client.collectitem.pickup_item_count = os - r;
259-
add_queue(bc_player->outgoing_packets, pkt);
260-
END_BROADCAST(entity->world->players)
261-
world_despawn_entity(world, entity);
262-
freeEntity(entity);
237+
/*
238+
int32_t chunk_x = ((int32_t) entity->x) >> 4;
239+
int32_t chunk_z = ((int32_t) entity->z) >> 4;
240+
for (int32_t icx = chunk_x - 1; icx <= chunk_x + 1; icx++)
241+
for (int32_t icz = chunk_z - 1; icz <= chunk_z + 1; icz++) {
242+
struct chunk* ch = world_get_chunk(entity->world, icx, icz);
243+
*/
244+
if (ch != NULL) {
245+
BEGIN_HASHMAP_ITERATION(entity->world->entities);
246+
struct entity* oe = (struct entity*) value;
247+
if (oe == entity || entity_distsq(entity, oe) > 16. * 16.) {
248+
249+
}
250+
if (oe->type == ENT_PLAYER && oe->health > 0.) {
251+
getEntityCollision(oe, &oebb);
252+
//printf("%f, %f, %f vs %f, %f, %f\n", entity->x, entity->y, entity->z, oe->x, oe->y, oe->z);
253+
if (boundingbox_intersects(&oebb, &cebb)) {
254+
int os = entity->data.itemstack.slot->count;
255+
pthread_mutex_lock(&oe->data.player.player->inventory->mut);
256+
int r = inventory_add_player(oe->data.player.player, oe->data.player.player->inventory, entity->data.itemstack.slot, 1);
257+
pthread_mutex_unlock(&oe->data.player.player->inventory->mut);
258+
if (r <= 0) {
259+
BEGIN_BROADCAST_DIST(entity, 32.);
260+
struct packet* pkt = xmalloc(sizeof(struct packet));
261+
pkt->id = PKT_PLAY_CLIENT_COLLECTITEM;
262+
pkt->data.play_client.collectitem.collected_entity_id = entity->id;
263+
pkt->data.play_client.collectitem.collector_entity_id = oe->id;
264+
pkt->data.play_client.collectitem.pickup_item_count = os - r;
265+
add_queue(bc_player->outgoing_packets, pkt);
266+
END_BROADCAST(entity->world->players);
267+
world_despawn_entity(world, entity);
268+
freeEntity(entity);
263269
} else {
264-
BEGIN_BROADCAST_DIST(entity, 128.)
265-
struct packet* pkt = xmalloc(sizeof(struct packet));
266-
pkt->id = PKT_PLAY_CLIENT_ENTITYMETADATA;
267-
pkt->data.play_client.entitymetadata.entity_id = entity->id;
268-
writeMetadata(entity, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size);
269-
add_queue(bc_player->outgoing_packets, pkt);
270-
END_BROADCAST(entity->world->players)
271-
BREAK_HASHMAP_ITERATION(entity->world->entities);
270+
BEGIN_BROADCAST_DIST(entity, 128.);
271+
struct packet* pkt = xmalloc(sizeof(struct packet));
272+
pkt->id = PKT_PLAY_CLIENT_ENTITYMETADATA;
273+
pkt->data.play_client.entitymetadata.entity_id = entity->id;
274+
writeMetadata(entity, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size);
275+
add_queue(bc_player->outgoing_packets, pkt);
276+
END_BROADCAST(entity->world->players);
277+
BREAK_HASHMAP_ITERATION(entity->world->entities);
272278
}
273279
}
274280
} else if (oe->type == ENT_ITEM) {
@@ -283,14 +289,13 @@ void tick_itemstack(struct world* world, struct entity* entity) {
283289
world_despawn_entity(world, entity);
284290
oe->data.itemstack.slot->count += entity->data.itemstack.slot->count;
285291
freeEntity(entity);
286-
BEGIN_BROADCAST_DIST(oe, 128.)
292+
BEGIN_BROADCAST_DIST(oe, 128.);
287293
struct packet* pkt = xmalloc(sizeof(struct packet));
288294
pkt->id = PKT_PLAY_CLIENT_ENTITYMETADATA;
289295
pkt->data.play_client.entitymetadata.entity_id = oe->id;
290296
writeMetadata(oe, &pkt->data.play_client.entitymetadata.metadata.metadata, &pkt->data.play_client.entitymetadata.metadata.metadata_size);
291297
add_queue(bc_player->outgoing_packets, pkt);
292298
END_BROADCAST(oe->world->players)
293-
return 1;
294299
}
295300
}
296301
}

src/game.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ void game_load_player(struct player* to, struct player* from) {
3737
packet->data.play_client.spawnplayer.z = from->entity->z;
3838
packet->data.play_client.spawnplayer.yaw = (uint8_t) ((from->entity->yaw / 360.) * 256.);
3939
packet->data.play_client.spawnplayer.pitch = (uint8_t) ((from->entity->pitch / 360.) * 256.);
40-
entitymeta_write(from->entity, &packet->data.play_client.spawnplayer.metadata.metadata, &packet->data.play_client.spawnplayer.metadata.metadata_size);
40+
struct mempool* pool = mempool_new();
41+
entitymeta_write(from->entity, &packet->data.play_client.spawnplayer.metadata.metadata, &packet->data.play_client.spawnplayer.metadata.metadata_size, pool);
4142
queue_push(to->outgoing_packets, packet);
4243
game_entity_equipment(to, from->entity, 0, from->inventory->slots[from->currentItem + 36]);
4344
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) {
7475
queue_push(to->outgoing_packets, packet);
7576
packet = packet_new(mempool_new(), PKT_PLAY_CLIENT_ENTITYMETADATA);
7677
packet->data.play_client.entitymetadata.entity_id = from->id;
77-
entitymeta_write(from, &packet->data.play_client.entitymetadata.metadata.metadata, &packet->data.play_client.entitymetadata.metadata.metadata_size);
78+
struct mempool* pool = mempool_new();
79+
entitymeta_write(from, &packet->data.play_client.entitymetadata.metadata.metadata, &packet->data.play_client.entitymetadata.metadata.metadata_size, pool);
7880
queue_push(to->outgoing_packets, packet);
7981
} else if (from->type == ENT_PLAYER) {
8082
return;
@@ -116,7 +118,8 @@ void game_load_entity(struct player* to, struct entity* from) {
116118
packet->data.play_client.spawnmob.velocity_x = (int16_t)(from->motX * 8000.);
117119
packet->data.play_client.spawnmob.velocity_y = (int16_t)(from->motY * 8000.);
118120
packet->data.play_client.spawnmob.velocity_z = (int16_t)(from->motZ * 8000.);
119-
entitymeta_write(from, &packet->data.play_client.spawnmob.metadata.metadata, &packet->data.play_client.spawnmob.metadata.metadata_size);
121+
struct mempool* pool = mempool_new();
122+
entitymeta_write(from, &packet->data.play_client.spawnmob.metadata.metadata, &packet->data.play_client.spawnmob.metadata.metadata_size, pool);
120123
queue_push(to->outgoing_packets, packet);
121124
}
122125
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
177180
item->motY = .2;
178181
item->motZ = game_rand_float() * .2 - .1;
179182
item->data.itemstack.delayBeforeCanPickup = 0;
180-
slot_duplicate(slot, item->data.itemstack.slot);
183+
struct mempool* pool = mempool_new();
184+
slot_duplicate(pool, slot, item->data.itemstack.slot);
181185
world_spawn_entity(world, item);
182-
BEGIN_BROADCAST_DIST(item, 128.)
183-
game_load_entity(bc_player, item);
186+
BEGIN_BROADCAST_DIST(item, 128.);
187+
game_load_entity(bc_player, item);
184188
END_BROADCAST(item->world->players)
185189
}
186190

@@ -197,10 +201,11 @@ void dropPlayerItem(struct player* player, struct slot* drop) {
197201
item->motY += (game_rand_float() - game_rand_float()) * .1;
198202
item->motZ += sin(nos) * mag;
199203
item->data.itemstack.delayBeforeCanPickup = 20;
200-
slot_duplicate(drop, item->data.itemstack.slot);
204+
struct mempool* pool = mempool_new();
205+
slot_duplicate(pool, drop, item->data.itemstack.slot);
201206
world_spawn_entity(player->world, item);
202-
BEGIN_BROADCAST_DIST(player->entity, 128.)
203-
game_load_entity(bc_player, item);
207+
BEGIN_BROADCAST_DIST(player->entity, 128.);
208+
game_load_entity(bc_player, item);
204209
END_BROADCAST(player->world->players)
205210
}
206211

@@ -229,10 +234,11 @@ void dropEntityItem_explode(struct entity* entity, struct slot* drop) {
229234
item->motZ = cos(f2) * f1;
230235
item->motY = .2;
231236
item->data.itemstack.delayBeforeCanPickup = 20;
232-
slot_duplicate(drop, item->data.itemstack.slot);
237+
struct mempool* pool = mempool_new();
238+
slot_duplicate(pool, drop, item->data.itemstack.slot);
233239
world_spawn_entity(entity->world, item);
234-
BEGIN_BROADCAST_DIST(entity, 128.)
235-
game_load_entity(bc_player, item);
240+
BEGIN_BROADCAST_DIST(entity, 128.);
241+
game_load_entity(bc_player, item);
236242
END_BROADCAST(entity->world->players)
237243
}
238244

@@ -284,7 +290,8 @@ void player_openInventory(struct player* player, struct inventory* inv) {
284290
pkt->data.play_client.windowitems.count = inv->slot_count;
285291
pkt->data.play_client.windowitems.slot_data = xmalloc(sizeof(struct slot) * inv->slot_count);
286292
for (size_t i = 0; i < inv->slot_count; i++) {
287-
slot_duplicate(inv->slots[i], &pkt->data.play_client.windowitems.slot_data[i]);
293+
struct mempool* pool = mempool_new();
294+
slot_duplicate(pool, inv->slots[i], &pkt->data.play_client.windowitems.slot_data[i]);
288295
}
289296
add_queue(player->outgoing_packets, pkt);
290297
}

src/player.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ void player_closeWindow(struct player* player, uint16_t windowID) {
537537
inventory = NULL;
538538
} else if (inventory->type == INVTYPE_CHEST) {
539539
if (inventory->tile != NULL) {
540-
BEGIN_BROADCAST_DIST(player->entity, 128.){
540+
BEGIN_BROADCAST_DIST(player->entity, 128.) {
541541
struct packet* pkt = packet_new(mempool_new(), PKT_PLAY_CLIENT_BLOCKACTION);
542542
pkt->data.play_client.blockaction.location.x = inventory->tile->x;
543543
pkt->data.play_client.blockaction.location.y = inventory->tile->y;

0 commit comments

Comments
 (0)