static bool G_InventoryPlaceItemAdjacent (edict_t *ent) { vec2_t oldPos; /* if we have to place it to adjacent */ edict_t *floorAdjacent; int i; Vector2Copy(ent->pos, oldPos); floorAdjacent = NULL; for (i = 0; i < DIRECTIONS; i++) { /** @todo Check whether movement is possible here - otherwise don't use this field */ /* extend pos with the direction vectors */ /** @todo Don't know why the adjacent stuff has been disabled, but if it was buggy, it's probably */ /** because the third ent->pos in the next line should be pos[1] ?!. (Duke, 13.1.11) */ Vector2Set(ent->pos, ent->pos[0] + dvecs[i][0], ent->pos[0] + dvecs[i][1]); /* now try to get a floor entity for that new location */ floorAdjacent = G_GetFloorItems(ent); if (!floorAdjacent) { floorAdjacent = G_SpawnFloor(ent->pos); } else { /* destroy this edict (send this event to all clients that see the edict) */ G_EventPerish(floorAdjacent); G_VisFlagsReset(floorAdjacent); } INVSH_FindSpace(&floorAdjacent->i, &ic->item, INVDEF(gi.csi->idFloor), &x, &y, ic); if (x != NONE) { ic->x = x; ic->y = y; ic->next = FLOOR(floorAdjacent); FLOOR(floorAdjacent) = ic; break; } /* restore original pos */ Vector2Copy(oldPos, ent->pos); } /* added to adjacent pos? */ if (i < DIRECTIONS) { /* restore original pos - if no free space, this was done * already in the for loop */ Vector2Copy(oldPos, ent->pos); return false; } if (floorAdjacent) G_CheckVis(floorAdjacent, true); return true; }
/** * @brief Adds a new item to an existing or new floor container edict at the given grid location * @param pos The grid location to spawn the item on the floor * @param itemID The item to spawn */ bool G_AddItemToFloor (const pos3_t pos, const char *itemID) { edict_t *floor; item_t item = {NONE_AMMO, NULL, NULL, 0, 0}; const objDef_t *od = INVSH_GetItemByIDSilent(itemID); if (!od) { gi.DPrintf("Could not find item '%s'\n", itemID); return false; } /* Also sets FLOOR(ent) to correct value. */ floor = G_GetFloorItemsFromPos(pos); /* nothing on the ground yet? */ if (!floor) floor = G_SpawnFloor(pos); item.item = od; return game.i.TryAddToInventory(&game.i, &floor->chr.i, &item, INVDEF(gi.csi->idFloor)); }
/** * @brief Moves an item inside an inventory. Floors are handled special. * @param[in] actor The pointer to the selected/used edict/soldier. * @param[in] fromContType The container (-id) the item should be moved from. * @param[in] fItem The item you want to move. * @param[in] toContType The container (-def) the item should be moved to. * @param[in] tx x position where you want the item to go in the destination container * @param[in] ty y position where you want the item to go in the destination container * @param[in] checkaction Set this to true if you want to check for TUs, otherwise false. * @sa event PA_INVMOVE * @sa AI_ActorThink */ bool G_ActorInvMove (Edict* actor, const invDef_t* fromContType, Item* fItem, const invDef_t* toContType, int tx, int ty, bool checkaction) { Edict* floor; bool newFloor; Item* tc; playermask_t mask; inventory_action_t ia; Item fromItemBackup, toItemBackup; int fx, fy; int originalTU, reservedTU = 0; Player& player = actor->getPlayer(); assert(fItem); assert(fItem->def()); /* Store the location/item of 'from' BEFORE actually moving items with moveInInventory. */ fromItemBackup = *fItem; /* Store the location of 'to' BEFORE actually moving items with moveInInventory * so in case we swap ammo the client can be updated correctly */ tc = actor->chr.inv.getItemAtPos(toContType, tx, ty); if (tc) toItemBackup = *tc; else toItemBackup = *fItem; /* Get first used bit in item. */ fItem->getFirstShapePosition(&fx, &fy); fx += fItem->getX(); fy += fItem->getY(); /* Check if action is possible */ /* TUs are 1 here - but this is only a dummy - the real TU check is done in the inventory functions below */ if (checkaction && !G_ActionCheckForCurrentTeam(player, actor, 1)) return false; if (!actor->chr.inv.canHoldItemWeight(fromContType->id, toContType->id, *fItem, actor->chr.score.skills[ABILITY_POWER])) { G_ClientPrintf(player, PRINT_HUD, _("This soldier can not carry anything else.")); return false; } /* "get floor ready" - searching for existing floor-edict */ floor = G_GetFloorItems(actor); if (toContType->isFloorDef() && !floor) { /* We are moving to the floor, but no existing edict for this floor-tile found -> create new one */ floor = G_SpawnFloor(actor->pos); newFloor = true; } else if (fromContType->isFloorDef() && !floor) { /* We are moving from the floor, but no existing edict for this floor-tile found -> this should never be the case. */ gi.DPrintf("G_ClientInvMove: No source-floor found.\n"); return false; } else { /* There already exists an edict for this floor-tile. */ newFloor = false; } /* search for space */ Item* item2; if (tx == NONE) { item2 = actor->chr.inv.getItemAtPos(fromContType, fItem->getX(), fItem->getY()); if (item2) actor->chr.inv.findSpace(toContType, item2, &tx, &ty, fItem); if (tx == NONE) return false; } /** @todo what if we don't have enough TUs after subtracting the reserved ones? */ /* Because moveInInventory don't know anything about character_t and it updates actor->TU, * we need to save original actor->TU for the sake of checking TU reservations. */ originalTU = actor->TU; reservedTU = G_ActorGetReservedTUs(actor); /* Temporary decrease actor->TU to make moveInInventory do what expected. */ G_ActorUseTU(actor, reservedTU); /* Try to actually move the item and check the return value after restoring valid actor->TU. */ ia = game.i.moveInInventory(&actor->chr.inv, fromContType, fItem, toContType, tx, ty, checkaction ? &actor->TU : nullptr, &item2); /* Now restore the original actor->TU and decrease it for TU used for inventory move. */ G_ActorSetTU(actor, originalTU - (originalTU - reservedTU - actor->TU)); switch (ia) { case IA_NONE: /* No action possible - abort */ return false; case IA_NOTIME: G_ClientPrintf(player, PRINT_HUD, _("Can't perform action - not enough TUs!")); return false; case IA_NORELOAD: G_ClientPrintf(player, PRINT_HUD, _("Can't perform action - weapon already fully loaded with the same ammunition!")); return false; default: /* Continue below. */ break; } /* successful inventory change; remove the item in clients */ if (fromContType->isFloorDef()) { /* We removed an item from the floor - check how the client * needs to be updated. */ assert(!newFloor); if (actor->getFloor()) { /* There is still something on the floor. */ floor->setFloor(actor); /* Delay this if swapping ammo, otherwise the le will be removed in the client before we can add back * the current ammo because removeNextFrame is set in LE_PlaceItem() if the floor le has no items */ if (ia != IA_RELOAD_SWAP) G_EventInventoryDelete(*floor, G_VisToPM(floor->visflags), fromContType->id, fx, fy); } else { /* Floor is empty, remove the edict (from server + client) if we are * not moving to it. */ if (!toContType->isFloorDef()) { G_EventPerish(*floor); G_FreeEdict(floor); } else G_EventInventoryDelete(*floor, G_VisToPM(floor->visflags), fromContType->id, fx, fy); } } else { G_EventInventoryDelete(*actor, G_TeamToPM(actor->team), fromContType->id, fx, fy); } /* send tu's */ G_SendStats(*actor); assert(item2); Item item = *item2; if (ia == IA_RELOAD || ia == IA_RELOAD_SWAP) { /* reload */ if (toContType->isFloorDef()) mask = G_VisToPM(floor->visflags); else mask = G_TeamToPM(actor->team); G_EventInventoryReload(toContType->isFloorDef() ? *floor : *actor, mask, &item, toContType, item2); if (ia == IA_RELOAD) { return true; } else { /* ia == IA_RELOAD_SWAP */ item.setAmmoLeft(NONE_AMMO); item.setAmmoDef(nullptr); item.setDef(toItemBackup.ammoDef()); item.rotated = fromItemBackup.rotated; item.setAmount(toItemBackup.getAmount()); toContType = fromContType; if (toContType->isFloorDef()) { /* moveInInventory placed the swapped ammo in an available space, check where it was placed * so we can place it at the same place in the client, otherwise since fItem hasn't been removed * this could end in a different place in the client - will cause an error if trying to use it again */ item2 = actor->chr.inv.findInContainer(toContType->id, &item); assert(item2); fromItemBackup = item; fromItemBackup.setX(item2->getX()); fromItemBackup.setY(item2->getY()); } tx = fromItemBackup.getX(); ty = fromItemBackup.getY(); } } /* We moved an item to the floor - check how the client needs to be updated. */ if (toContType->isFloorDef()) { /* we have to link the temp floor container to the new floor edict or add * the item to an already existing floor edict - the floor container that * is already linked might be from a different entity (this might happen * in case of a throw by another actor) */ floor->setFloor(actor); /* A new container was created for the floor. */ if (newFloor) { /* Send item info to the clients */ G_CheckVis(floor); } else { /* use the backup item to use the old amount values, because the clients have to use the same actions * on the original amount. Otherwise they would end in a different amount of items as the server (+1) */ G_EventInventoryAdd(*floor, G_VisToPM(floor->visflags), 1); G_WriteItem(fromItemBackup, toContType->id, tx, ty); G_EventEnd(); /* Couldn't remove it before because that would remove the le from the client and would cause battlescape to crash * when trying to add back the swapped ammo above */ if (ia == IA_RELOAD_SWAP) G_EventInventoryDelete(*floor, G_VisToPM(floor->visflags), fromContType->id, fx, fy); } } else { G_EventInventoryAdd(*actor, G_TeamToPM(actor->team), 1); G_WriteItem(item, toContType->id, tx, ty); G_EventEnd(); } G_ReactionFireSettingsUpdate(actor, actor->chr.RFmode.getFmIdx(), actor->chr.RFmode.getHand(), actor->chr.RFmode.getWeapon()); /* Other players receive weapon info only. */ mask = G_VisToPM(actor->visflags) & ~G_TeamToPM(actor->team); if (mask) { if (fromContType->isRightDef() || fromContType->isLeftDef()) { G_EventInventoryDelete(*actor, mask, fromContType->id, fx, fy); } if (toContType->isRightDef() || toContType->isLeftDef()) { G_EventInventoryAdd(*actor, mask, 1); G_WriteItem(item, toContType->id, tx, ty); G_EventEnd(); } } return true; }
/** * @brief Move the whole given inventory to the floor and destroy the items that do not fit there. * @param[in] ent Pointer to an edict_t being an actor. * @sa G_ActorDie */ void G_InventoryToFloor (edict_t *ent) { invList_t *ic, *next; containerIndex_t container; edict_t *floor; item_t item; /* check for items */ for (container = 0; container < gi.csi->numIDs; container++) { /* ignore items linked from any temp container */ if (INVDEF(container)->temp) continue; if (G_InventoryDropToFloorCheck(ent, container)) break; } /* edict is not carrying any items */ if (container >= gi.csi->numIDs) return; /* find the floor */ floor = G_GetFloorItems(ent); if (!floor) { floor = G_SpawnFloor(ent->pos); } else { /* destroy this edict (send this event to all clients that see the edict) */ G_EventPerish(floor); G_VisFlagsReset(floor); } /* drop items */ /* cycle through all containers */ for (container = 0; container < gi.csi->numIDs; container++) { /* skip floor - we want to drop to floor */ if (container == gi.csi->idFloor) continue; /* skip csi->idArmour, we will collect armours using idArmour container, * not idFloor */ if (container == gi.csi->idArmour) continue; /* now cycle through all items for the container of the character (or the entity) */ for (ic = CONTAINER(ent, container); ic; ic = next) { /* Save the next inv-list before it gets overwritten below. * Do not put this in the "for" statement, * unless you want an endless loop. ;) */ next = ic->next; item = ic->item; /* only floor can summarize, so everything on the actor must have amount=1 */ assert(item.amount == 1); if (!game.i.RemoveFromInventory(&game.i, &ent->chr.i, INVDEF(container), ic)) gi.Error("Could not remove item '%s' from inventory %i of entity %i", ic->item.item->id, container, ent->number); if (game.i.AddToInventory(&game.i, &floor->chr.i, &item, INVDEF(gi.csi->idFloor), NONE, NONE, 1) == NULL) gi.Error("Could not add item '%s' from inventory %i of entity %i to floor container", ic->item.item->id, container, ent->number); #ifdef ADJACENT G_InventoryPlaceItemAdjacent(ent); #endif } /* destroy link */ CONTAINER(ent, container) = NULL; } FLOOR(ent) = FLOOR(floor); /* send item info to the clients */ G_CheckVis(floor); }