Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define GAMEMODE 0

// Max render distance, determines how many chunks to send
#define VIEW_DISTANCE 2
#define VIEW_DISTANCE 15

// Time between server ticks in microseconds (default = 1s)
#define TIME_BETWEEN_TICKS 1000000
Expand Down
1 change: 1 addition & 0 deletions include/procedures.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void hurtEntity (int entity_id, int attacker_id, uint8_t damage_type, uint8_t da
void handleServerTick (int64_t time_since_last_tick);

void broadcastChestUpdate (int origin_fd, uint8_t *storage_ptr, uint16_t item, uint8_t count, uint8_t slot);
void activateNetherReactor(short x, short y, short z);

ssize_t writeEntityData (int client_fd, EntityData *data);

Expand Down
16 changes: 16 additions & 0 deletions src/crafting.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,22 @@ void getCraftingOutput (PlayerData *player, uint8_t *count, uint16_t *item) {
case I_copper_ingot: *item = I_copper_block; *count = 1; return;
default: break;
}
// Nether Reactor Core recipe
if (
player->craft_items[0] == I_iron_ingot &&
player->craft_items[1] == I_diamond &&
player->craft_items[2] == I_iron_ingot &&
player->craft_items[3] == I_iron_ingot &&
player->craft_items[4] == I_diamond &&
player->craft_items[5] == I_iron_ingot &&
player->craft_items[6] == I_iron_ingot &&
player->craft_items[7] == I_diamond &&
player->craft_items[8] == I_iron_ingot
) {
*item = I_jigsaw;
*count = 1;
return;
}
break;

default: break;
Expand Down
63 changes: 60 additions & 3 deletions src/procedures.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ void failBlockChange (short x, uint8_t y, short z, uint8_t block) {
}

uint8_t makeBlockChange (short x, uint8_t y, short z, uint8_t block) {
printf("makeBlockChange called with x: %d, y: %d, z: %d, block: %d\n", x, y, z, block);

// Transmit block update to all in-game clients
for (int i = 0; i < MAX_PLAYERS; i ++) {
Expand Down Expand Up @@ -1144,7 +1145,7 @@ void playPickupAnimation (PlayerData *player, uint16_t item, double x, double y,
#endif

void handlePlayerAction (PlayerData *player, int action, short x, short y, short z) {

printf("[DEBUG] Player action %d at x: %d, y: %d, z: %d\n", action, x, y, z);
// Re-sync slot when player drops an item
if (action == 3 || action == 4) {
sc_setContainerSlot(
Expand Down Expand Up @@ -1218,7 +1219,7 @@ void handlePlayerAction (PlayerData *player, int action, short x, short y, short
}

void handlePlayerUseItem (PlayerData *player, short x, short y, short z, uint8_t face) {

printf("[DEBUG] Player use item at x: %d, y: %d, z: %d, face: %d\n", x, y, z, face);
// Get targeted block (if coordinates are provided)
uint8_t target = face == 255 ? 0 : getBlockAt(x, y, z);
// Get held item properties
Expand Down Expand Up @@ -1328,10 +1329,15 @@ void handlePlayerUseItem (PlayerData *player, short x, short y, short z, uint8_t
// Don't proceed with block placement if no coordinates were provided
if (face == 255) return;

printf("[DEBUG] Proceeding with block placement checks.\n");
printf("[DEBUG] item in hand is %d (count: %d).\n", *item, *count);

// If the selected item doesn't correspond to a block, exit
uint8_t block = I_to_B(*item);
uint16_t block = I_to_B(*item);
if (block == 0) return;

printf("[DEBUG] Selected item corresponds to block %d.\n", block);

switch (face) {
case 0: y -= 1; break;
case 1: y += 1; break;
Expand All @@ -1342,6 +1348,13 @@ void handlePlayerUseItem (PlayerData *player, short x, short y, short z, uint8_t
default: break;
}

printf("[DEBUG] Checking all placement conditions: passable: %d, replaceable: %d, column: %d, below: %d.\n",
isPassableBlock(block),
isReplaceableBlock(getBlockAt(x, y, z)),
isColumnBlock(block),
getBlockAt(x, y - 1, z) != B_air
);

// Check if the block's placement conditions are met
if (
!( // Is player in the way?
Expand All @@ -1353,10 +1366,12 @@ void handlePlayerUseItem (PlayerData *player, short x, short y, short z, uint8_t
isReplaceableBlock(getBlockAt(x, y, z)) &&
(!isColumnBlock(block) || getBlockAt(x, y - 1, z) != B_air)
) {
printf("[DEBUG] Block placement conditions met, placing block %d at x: %d, y: %d, z: %d.\n", block, x, y, z);
// Apply server-side block change
if (makeBlockChange(x, y, z, block)) return;
// Decrease item amount in selected slot
*count -= 1;
printf("[DEBUG] Decreased item count, new count is %d.\n", *count);
// Clear item id in slot if amount is zero
if (*count == 0) *item = 0;
// Calculate fluid flow
Expand All @@ -1369,9 +1384,19 @@ void handlePlayerUseItem (PlayerData *player, short x, short y, short z, uint8_t
#endif
}

printf("[DEBUG] Exiting block placement checks.\n");

// Sync hotbar contents to player
sc_setContainerSlot(player->client_fd, 0, serverSlotToClientSlot(0, player->hotbar), *count, *item);

// Trigger Nether Reactor activation when the block is used
printf("[DEBUG] Checking for Nether Reactor activation.\n");
printf("[DEBUG] Held item is %d, Nether Reactor ID is %d.\n", *item, I_jigsaw);
if (*item == I_jigsaw) {
printf("Activating Nether Reactor at x: %d, y: %d, z: %d\n", x, y, z);
activateNetherReactor(x, y, z);
return;
}
}

void spawnMob (uint8_t type, short x, uint8_t y, short z, uint8_t health) {
Expand Down Expand Up @@ -1972,3 +1997,35 @@ int sizeEntityMetadata (EntityData *metadata, size_t length) {
}
return total_size;
}

// Function to activate the Nether Reactor
void activateNetherReactor(short x, short y, short z) {
// Define the radius of terrain conversion
const int radius = 5;
printf("Nether Reactor activated at (%d, %d, %d)\n", x, y, z);

// Iterate through the area around the reactor
for (short dx = -radius; dx <= radius; dx++) {
for (short dz = -radius; dz <= radius; dz++) {
for (short dy = -radius; dy <= radius; dy++) {
printf("[DEBUG] Checking block at offset (%d, %d, %d)\n", dx, dy, dz);
short nx = x + dx;
short nz = z + dz;
short ny = y + dy;

// Get the current block at the position
uint8_t block = getBlockAt(nx, ny, nz);
printf("[DEBUG] Block at (%d, %d, %d) is %d\n", nx, ny, nz, block);
// Replace grass with netherrack and water with lava
if (block == B_grass_block) {
makeBlockChange(nx, ny, nz, B_netherrack);
} else if (block == B_water) {
makeBlockChange(nx, ny, nz, B_lava);
}
}
}
}

// Spawn Nether mobs around the reactor
// TODO
}