move a bit of code out of inputhandler and into inventory

This commit is contained in:
UnknownShadow200 2019-08-26 21:32:20 +10:00
parent 363f28f52b
commit e1b2aa32ea
5 changed files with 37 additions and 29 deletions

View File

@ -60,15 +60,15 @@ typedef unsigned __INTPTR_TYPE__ cc_uintptr;
#endif
#elif __MWERKS__
typedef signed char cc_int8;
typedef signed short cc_int16;
typedef signed int cc_int32;
typedef signed short int cc_int16;
typedef signed long int cc_int32;
typedef signed long long cc_int64;
typedef unsigned char cc_uint8;
typedef unsigned short cc_uint16;
typedef unsigned int cc_uint32;
typedef unsigned short int cc_uint16;
typedef unsigned long int cc_uint32;
typedef unsigned long long cc_uint64;
typedef unsigned int cc_uintptr;
typedef unsigned long int cc_uintptr;
/* TODO: Is there actual attribute support for these somewhere? */
#define CC_INLINE inline
#define CC_NOINLINE

View File

@ -366,28 +366,7 @@ void InputHandler_PickBlocks(bool cooldown, bool left, bool middle, bool right)
cur = World_GetBlock(pos.X, pos.Y, pos.Z);
if (Blocks.Draw[cur] == DRAW_GAS) return;
if (!(Blocks.CanPlace[cur] || Blocks.CanDelete[cur])) return;
if (!Inventory_CheckChangeSelected() || Inventory_SelectedBlock == cur) return;
/* Is the currently selected block an empty slot? */
if (Inventory_SelectedBlock == BLOCK_AIR) {
Inventory_SetSelectedBlock(cur); return;
}
/* Try to replace same block */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != cur) continue;
Inventory_SetSelectedIndex(i); return;
}
/* Try to replace empty slots */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != BLOCK_AIR) continue;
Inventory_Set(i, cur);
Inventory_SetSelectedIndex(i); return;
}
/* Finally, replace the currently selected block */
Inventory_SetSelectedBlock(cur);
Inventory_PickBlock(cur);
}
}

View File

@ -43,6 +43,32 @@ void Inventory_SetSelectedBlock(BlockID block) {
Event_RaiseVoid(&UserEvents.HeldBlockChanged);
}
void Inventory_PickBlock(BlockID block) {
int i;
if (!Inventory_CheckChangeSelected() || Inventory_SelectedBlock == block) return;
/* Is the currently selected block an empty slot? */
if (Inventory_SelectedBlock == BLOCK_AIR) {
Inventory_SetSelectedBlock(block); return;
}
/* Try to replace same block */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != block) continue;
Inventory_SetSelectedIndex(i); return;
}
/* Try to replace empty slots */
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) {
if (Inventory_Get(i) != BLOCK_AIR) continue;
Inventory_Set(i, block);
Inventory_SetSelectedIndex(i); return;
}
/* Finally, replace the currently selected block */
Inventory_SetSelectedBlock(block);
}
static const cc_uint8 classicInventory[42] = {
BLOCK_STONE, BLOCK_COBBLE, BLOCK_BRICK, BLOCK_DIRT, BLOCK_WOOD, BLOCK_LOG, BLOCK_LEAVES, BLOCK_GLASS, BLOCK_SLAB,
BLOCK_MOSSY_ROCKS, BLOCK_SAPLING, BLOCK_DANDELION, BLOCK_ROSE, BLOCK_BROWN_SHROOM, BLOCK_RED_SHROOM, BLOCK_SAND, BLOCK_GRAVEL, BLOCK_SPONGE,

View File

@ -44,6 +44,9 @@ void Inventory_SetHotbarIndex(int index);
/* Attempts to set the block for the selected index in the current hotbar. */
/* NOTE: If another slot is already this block, the selected index is instead changed. */
void Inventory_SetSelectedBlock(BlockID block);
/* Attempts to set the selected block in a user-friendly manner. */
/* e.g. this method tries to replace empty slots before other blocks */
void Inventory_PickBlock(BlockID block);
/* Sets all slots to contain their default associated block. */
/* NOTE: The order of default blocks may not be in order of ID. */
void Inventory_ApplyDefaultMapping(void);

View File

@ -106,9 +106,9 @@ static int Program_Run(int argc, char** argv) {
int argsCount = Platform_GetCommandLineArgs(argc, argv, args);
#ifdef _MSC_VER
/* NOTE: Make sure to comment this out before pushing a commit */
String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565");
//String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565");
//String rawArgs = String_FromConst("UnknownShadow200");
argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4);
//argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4);
#endif
if (argsCount == 0) {