mirror of
https://github.com/p2r3/bareiron.git
synced 2025-10-02 07:35:08 +02:00
implement basic crafting
This commit is contained in:
84
src/crafting.c
Normal file
84
src/crafting.c
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "globals.h"
|
||||||
|
#include "registries.h"
|
||||||
|
#include "crafting.h"
|
||||||
|
|
||||||
|
void getCraftingOutput (PlayerData *player, uint8_t *count, uint16_t *item) {
|
||||||
|
|
||||||
|
uint8_t i, filled = 0, first = 10;
|
||||||
|
for (i = 0; i < 9; i ++) {
|
||||||
|
if (player->craft_items[i]) {
|
||||||
|
filled ++;
|
||||||
|
if (first == 10) first = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t first_col = first % 3, first_row = first / 3;
|
||||||
|
|
||||||
|
switch (filled) {
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
*item = 0;
|
||||||
|
*count = 0;
|
||||||
|
return;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
switch (player->craft_items[first]) {
|
||||||
|
case I_oak_log:
|
||||||
|
*item = I_oak_planks;
|
||||||
|
*count = 4;
|
||||||
|
return;
|
||||||
|
case I_oak_planks:
|
||||||
|
*item = 715; // oak_button
|
||||||
|
*count = 1;
|
||||||
|
return;
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
switch (player->craft_items[first]) {
|
||||||
|
case I_oak_planks:
|
||||||
|
if (first_col != 2 && player->craft_items[first + 1] == I_oak_planks) {
|
||||||
|
*item = 731; // oak_pressure_plate
|
||||||
|
*count = 1;
|
||||||
|
return;
|
||||||
|
} else if (first_row != 2 && player->craft_items[first + 3] == I_oak_planks) {
|
||||||
|
*item = I_stick;
|
||||||
|
*count = 4;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
switch (player->craft_items[first]) {
|
||||||
|
case I_oak_planks:
|
||||||
|
if (
|
||||||
|
first_col != 2 && first_row != 2 &&
|
||||||
|
player->craft_items[first + 1] == I_oak_planks &&
|
||||||
|
player->craft_items[first + 3] == I_oak_planks &&
|
||||||
|
player->craft_items[first + 4] == I_oak_planks
|
||||||
|
) {
|
||||||
|
*item = 320; // crafting_table
|
||||||
|
*count = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
*count = 0;
|
||||||
|
*item = 0;
|
||||||
|
|
||||||
|
}
|
10
src/crafting.h
Normal file
10
src/crafting.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef CRAFTING_H
|
||||||
|
#define CRAFTING_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
|
void getCraftingOutput (PlayerData *player, uint8_t *count, uint16_t *item);
|
||||||
|
|
||||||
|
#endif
|
@@ -2,6 +2,7 @@
|
|||||||
#define H_GLOBALS
|
#define H_GLOBALS
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define true 1
|
#define true 1
|
||||||
#define false 0
|
#define false 0
|
||||||
@@ -41,6 +42,7 @@ typedef struct {
|
|||||||
int8_t pitch;
|
int8_t pitch;
|
||||||
uint8_t hotbar;
|
uint8_t hotbar;
|
||||||
uint16_t inventory_items[41];
|
uint16_t inventory_items[41];
|
||||||
|
uint16_t craft_items[9];
|
||||||
uint8_t inventory_count[41];
|
uint8_t inventory_count[41];
|
||||||
} PlayerData;
|
} PlayerData;
|
||||||
|
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
#include "varnum.h"
|
#include "varnum.h"
|
||||||
#include "registries.h"
|
#include "registries.h"
|
||||||
#include "worldgen.h"
|
#include "worldgen.h"
|
||||||
|
#include "crafting.h"
|
||||||
|
|
||||||
// C->S Handshake
|
// C->S Handshake
|
||||||
int cs_handshake (int client_fd) {
|
int cs_handshake (int client_fd) {
|
||||||
@@ -432,6 +433,7 @@ int cs_useItemOn (int client_fd) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// C->S Click Container
|
||||||
int cs_clickContainer (int client_fd) {
|
int cs_clickContainer (int client_fd) {
|
||||||
|
|
||||||
int window_id = readVarInt(client_fd);
|
int window_id = readVarInt(client_fd);
|
||||||
@@ -446,18 +448,19 @@ int cs_clickContainer (int client_fd) {
|
|||||||
PlayerData *player;
|
PlayerData *player;
|
||||||
if (getPlayerData(client_fd, &player)) return 1;
|
if (getPlayerData(client_fd, &player)) return 1;
|
||||||
|
|
||||||
uint8_t slot, count;
|
uint8_t slot, count, craft = false;
|
||||||
uint16_t item;
|
uint16_t item;
|
||||||
int tmp;
|
int tmp;
|
||||||
|
|
||||||
for (int i = 0; i < changes_count; i ++) {
|
for (int i = 0; i < changes_count; i ++) {
|
||||||
|
|
||||||
slot = clientSlotToServerSlot(readUint16(client_fd));
|
slot = clientSlotToServerSlot(readUint16(client_fd));
|
||||||
|
if (slot > 40) craft = true;
|
||||||
|
|
||||||
if (!readByte(client_fd)) { // no item?
|
if (!readByte(client_fd)) { // no item?
|
||||||
if (slot != 255) {
|
if (slot != 255) {
|
||||||
player->inventory_items[slot] = 0;
|
player->inventory_items[slot] = 0;
|
||||||
player->inventory_count[slot] = 0;
|
if (slot <= 40) player->inventory_count[slot] = 0;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -471,13 +474,18 @@ int cs_clickContainer (int client_fd) {
|
|||||||
tmp = readVarInt(client_fd);
|
tmp = readVarInt(client_fd);
|
||||||
recv(client_fd, recv_buffer, tmp, MSG_WAITALL);
|
recv(client_fd, recv_buffer, tmp, MSG_WAITALL);
|
||||||
|
|
||||||
if (item <= 255 && count > 0) {
|
if (count > 0) {
|
||||||
player->inventory_items[slot] = item;
|
player->inventory_items[slot] = item;
|
||||||
player->inventory_count[slot] = count;
|
if (slot <= 40) player->inventory_count[slot] = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (craft) {
|
||||||
|
getCraftingOutput(player, &count, &item);
|
||||||
|
sc_setContainerSlot(client_fd, window_id, 0, count, item);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -186,6 +186,14 @@ uint8_t clientSlotToServerSlot (uint8_t slot) {
|
|||||||
if (slot >= 9 && slot <= 35) return slot;
|
if (slot >= 9 && slot <= 35) return slot;
|
||||||
if (slot == 45) return 40;
|
if (slot == 45) return 40;
|
||||||
if (slot >= 5 && slot <= 8) return 4 - (slot - 5) + 36;
|
if (slot >= 5 && slot <= 8) return 4 - (slot - 5) + 36;
|
||||||
|
|
||||||
|
// map inventory crafting slots to player data crafting grid (semi-hack)
|
||||||
|
// this abuses the fact that the buffers are adjacent in player data
|
||||||
|
if (slot == 1) return 41;
|
||||||
|
if (slot == 2) return 42;
|
||||||
|
if (slot == 3) return 44;
|
||||||
|
if (slot == 4) return 45;
|
||||||
|
|
||||||
return 255;
|
return 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user