static void testItemAdd (void) { inventory_t inv; const objDef_t *od; item_t item; const invDef_t *container; ResetInventoryList(); OBJZERO(inv); od = INVSH_GetItemByIDSilent("assault"); CU_ASSERT_PTR_NOT_NULL(od); container = INVSH_GetInventoryDefinitionByID("right"); CU_ASSERT_PTR_NOT_NULL(container); item.t = od; item.m = NULL; item.a = 0; CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == false); CU_ASSERT_PTR_NOT_NULL(i.AddToInventory(&i, &inv, &item, container, NONE, NONE, 1)); CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == true); }
static void testItemToHeadgear (void) { inventory_t inv; const objDef_t *od; const invDef_t *container; item_t item; ResetInventoryList(); OBJZERO(inv); od = INVSH_GetItemByIDSilent("irgoggles"); CU_ASSERT_PTR_NOT_NULL_FATAL(od); container = INVSH_GetInventoryDefinitionByID("headgear"); CU_ASSERT_PTR_NOT_NULL_FATAL(container); item.t = od; item.m = NULL; item.a = 0; CU_ASSERT_FALSE(INVSH_ExistsInInventory(&inv, container, &item)); CU_ASSERT_PTR_NOT_NULL(i.AddToInventory(&i, &inv, &item, container, NONE, NONE, 1)); CU_ASSERT_TRUE(INVSH_ExistsInInventory(&inv, container, &item)); CU_ASSERT_PTR_NULL(i.AddToInventory(&i, &inv, &item, container, NONE, NONE, 1)); }
static void testItemMove (void) { inventory_t inv; const objDef_t *od; item_t item; const invDef_t *container, *containerTo; invList_t *addedItem; ResetInventoryList(); OBJZERO(inv); OBJZERO(item); od = INVSH_GetItemByIDSilent("assault"); CU_ASSERT_PTR_NOT_NULL(od); container = INVSH_GetInventoryDefinitionByID("right"); CU_ASSERT_PTR_NOT_NULL(container); item.t = od; item.m = NULL; item.a = 0; CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == false); addedItem = i.AddToInventory(&i, &inv, &item, container, NONE, NONE, 1); CU_ASSERT_PTR_NOT_NULL(addedItem); CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == true); containerTo = INVSH_GetInventoryDefinitionByID("backpack"); CU_ASSERT_PTR_NOT_NULL(containerTo); CU_ASSERT_EQUAL(IA_MOVE, i.MoveInInventory(&i, &inv, container, addedItem, containerTo, NONE, NONE, NULL, NULL)); CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == false); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerTo, &item) == true); }
static void testItemReload (void) { inventory_t inv; const objDef_t *od, *ad; item_t item, ammo, ammoFrom; const invDef_t *container, *containerFrom; invList_t *addedItem; ResetInventoryList(); OBJZERO(inv); od = INVSH_GetItemByIDSilent("rpg"); CU_ASSERT_PTR_NOT_NULL(od); container = INVSH_GetInventoryDefinitionByID("right"); CU_ASSERT_PTR_NOT_NULL(container); item.t = od; item.m = NULL; item.a = 0; CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == false); addedItem = i.AddToInventory(&i, &inv, &item, container, NONE, NONE, 1); CU_ASSERT_PTR_NOT_NULL(addedItem); CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == true); ad = INVSH_GetItemByIDSilent("rpg_ammo"); CU_ASSERT_PTR_NOT_NULL(ad); ammo.t = ad; ammo.m = NULL; ammo.a = 0; containerFrom = INVSH_GetInventoryDefinitionByID("backpack"); CU_ASSERT_PTR_NOT_NULL(containerFrom); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammo) == false); addedItem = i.AddToInventory(&i, &inv, &ammo, containerFrom, NONE, NONE, 1); CU_ASSERT_PTR_NOT_NULL(addedItem); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammo) == true); CU_ASSERT_EQUAL(IA_RELOAD, i.MoveInInventory(&i, &inv, containerFrom, addedItem, container, NONE, NONE, NULL, NULL)); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammo) == false); item.m = ad; item.a = 1; CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == true); ad = INVSH_GetItemByIDSilent("rpg_incendiary_ammo"); CU_ASSERT_PTR_NOT_NULL(ad); ammoFrom.t = ad; ammoFrom.m = NULL; ammoFrom.a = 0; CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammoFrom) == false); addedItem = i.AddToInventory(&i, &inv, &ammoFrom, containerFrom, NONE, NONE, 1); CU_ASSERT_PTR_NOT_NULL(addedItem); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammoFrom) == true); CU_ASSERT_EQUAL(IA_RELOAD_SWAP, i.MoveInInventory(&i, &inv, containerFrom, addedItem, container, NONE, NONE, NULL, NULL)); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammoFrom) == false); CU_ASSERT(INVSH_ExistsInInventory(&inv, containerFrom, &ammo) == true); item.m = ad; CU_ASSERT(INVSH_ExistsInInventory(&inv, container, &item) == true); }
/** * @brief Call into the target when the DND hover it * @return True if the DND is accepted */ bool uiContainerNode::onDndMove (uiNode_t *target, int x, int y) { vec2_t nodepos; bool exists; int itemX = 0; int itemY = 0; item_t *dragItem = UI_DNDGetItem(); /* we already check it when the node accept the DND */ assert(EXTRADATA(target).container); UI_GetNodeAbsPos(target, nodepos); /** We calculate the position of the top-left corner of the dragged * item in oder to compensate for the centered-drawn cursor-item. * Or to be more exact, we calculate the relative offset from the cursor * location to the middle of the top-left square of the item. * @sa UI_LeftClick */ if (dragItem->item) { itemX = C_UNIT * dragItem->item->sx / 2; /* Half item-width. */ itemY = C_UNIT * dragItem->item->sy / 2; /* Half item-height. */ /* Place relative center in the middle of the square. */ itemX -= C_UNIT / 2; itemY -= C_UNIT / 2; } dragInfoToX = (mousePosX - nodepos[0] - itemX) / C_UNIT; dragInfoToY = (mousePosY - nodepos[1] - itemY) / C_UNIT; /* Check if the items already exists in the container. i.e. there is already at least one item. */ exists = false; if ((INV_IsFloorDef(EXTRADATA(target).container) || INV_IsEquipDef(EXTRADATA(target).container)) && (dragInfoToX < 0 || dragInfoToY < 0 || dragInfoToX >= SHAPE_BIG_MAX_WIDTH || dragInfoToY >= SHAPE_BIG_MAX_HEIGHT) && INVSH_ExistsInInventory(ui_inventory, EXTRADATA(target).container, dragItem)) { exists = true; } /* Search for a suitable position to render the item at if * the container is "single", the cursor is out of bound of the container. */ if (!exists && dragItem->item && (EXTRADATA(target).container->single || dragInfoToX < 0 || dragInfoToY < 0 || dragInfoToX >= SHAPE_BIG_MAX_WIDTH || dragInfoToY >= SHAPE_BIG_MAX_HEIGHT)) { #if 0 /* ... or there is something in the way. */ /* We would need to check for weapon/ammo as well here, otherwise a preview would be drawn as well when hovering over the correct weapon to reload. */ || (INVSH_CheckToInventory(ui_inventory, dragItem->item, EXTRADATA(target).container, dragInfoToX, dragInfoToY) == INV_DOES_NOT_FIT)) { #endif INVSH_FindSpace(ui_inventory, dragItem, EXTRADATA(target).container, &dragInfoToX, &dragInfoToY, dragInfoIC); } /* we can drag every thing */ if (UI_IsScrollContainerNode(target)) { return true; } { invList_t *fItem; /* is there empty slot? */ const int checkedTo = INVSH_CheckToInventory(ui_inventory, dragItem->item, EXTRADATA(target).container, dragInfoToX, dragInfoToY, dragInfoIC); if (checkedTo != INV_DOES_NOT_FIT) return true; /* can we equip dragging item into the target item? */ fItem = INVSH_SearchInInventory(ui_inventory, EXTRADATA(target).container, dragInfoToX, dragInfoToY); if (!fItem) return false; if (EXTRADATA(target).container->single) return true; return INVSH_LoadableInWeapon(dragItem->item, fItem->item.item); } } /** * @brief Call when a DND enter into the node */ void uiContainerNode::onDndLeave (uiNode_t *node) { dragInfoToX = -1; dragInfoToY = -1; }