UFO: Alien Invasion
Doxygen documentation generating
e_event_invadd.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2023 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "../../../../client.h"
26 #include "../../../cl_localentity.h"
27 #include "e_event_invadd.h"
28 
35 static void CL_NetReceiveItem (dbuffer* buf, Item* item, containerIndex_t* container, int* x, int* y)
36 {
37  const eventRegister_t* eventData = CL_GetEvent(EV_INV_TRANSFER);
38 
39  /* reset */
40  int itemIdx, ammoIdx;
41  int ammoleft = NONE_AMMO;
42  int amount = 0;
43  item->setDef(nullptr);
44  item->setAmmoDef(nullptr);
45  NET_ReadFormat(buf, eventData->formatString, &itemIdx, &ammoleft, &ammoIdx, container, x, y, &item->rotated, &amount);
46  item->setAmmoLeft(ammoleft);
47  item->setAmount(amount);
48  item->setDef(INVSH_GetItemByIDX(itemIdx));
49  item->setAmmoDef(INVSH_GetItemByIDX(ammoIdx));
50 
51  if (!item->def())
52  Com_Error(ERR_DROP, "no weapon given for item");
53 }
54 
58 int CL_InvAddTime (const struct eventRegister_s* self, dbuffer* msg, eventTiming_t* eventTiming)
59 {
60  if (eventTiming->parsedShot) {
61  if (eventTiming->parsedDeath) { /* drop items after death (caused by impact) */
62  /* EV_INV_ADD messages are the last events sent after a death */
65  eventTiming->parsedDeath = false;
66  return eventTiming->impactTime + 1400;
67  } else if (eventTiming->impactTime > cl.time) { /* item thrown on the ground */
68  return eventTiming->impactTime + 75;
69  }
70  }
71 
72  return eventTiming->nextTime;
73 }
74 
80 void CL_InvAdd (const eventRegister_t* self, dbuffer* msg)
81 {
82  const int number = NET_ReadShort(msg);
83  le_t* le = LE_Get(number);
84  int nr = NET_ReadShort(msg);
85 
86  if (!le)
87  LE_NotFoundError(number);
88 
90 
91  for (; nr-- > 0;) {
92  Item item;
93  containerIndex_t container;
94  int x, y;
95  CL_NetReceiveItem(msg, &item, &container, &x, &y);
96 
97  if (LE_IsItem(le)) {
98  if (container != CID_FLOOR)
99  Com_Error(ERR_DROP, "InvAdd for ET_ITEM but target container is not the floor but %i", container);
100  } else if (INVDEF(container)->temp) {
101  Com_Error(ERR_DROP, "InvAdd for %i to temp container %i", le->type, container);
102  }
103 
104  if (cls.i.addToInventory(&le->inv, &item, INVDEF(container), x, y, item.getAmount()) == nullptr)
105  Com_Error(ERR_DROP, "InvAdd failed - could not add %i item(s) of %s to container %i",
106  item.getAmount(), item.def()->id, container);
107 
108  if (container == CID_RIGHT)
109  le->right = item.def()->idx;
110  else if (container == CID_LEFT)
111  le->left = item.def()->idx;
112  else if (container == CID_HEADGEAR)
113  le->headgear = item.def()->idx;
114  }
115 
116  switch (le->type) {
117  case ET_ACTOR:
118  case ET_ACTOR2x2:
119  if (LE_IsSelected(le))
120  Cmd_ExecuteString("hud_updateactorload");
122  break;
123  case ET_ITEM:
124  LE_PlaceItem(le);
125  break;
126  default:
127  break;
128  }
129 }
static void CL_NetReceiveItem(dbuffer *buf, Item *item, containerIndex_t *container, int *x, int *y)
const objDef_t * def(void) const
Definition: inv_shared.h:469
Inventory inv
const char * formatString
The format string that is used to write and parse this event.
Definition: e_main.h:54
int CL_InvAddTime(const struct eventRegister_s *self, dbuffer *msg, eventTiming_t *eventTiming)
Decides if following events should be delayed.
#define LE_IsItem(le)
Item * addToInventory(Inventory *const inv, const Item *const item, const invDef_t *container, int x, int y, int amount) __attribute__((warn_unused_result))
Add an item to a specified container in a given inventory.
Definition: inventory.cpp:91
int flags
void NET_ReadFormat(dbuffer *buf, const char *format,...)
The user-friendly version of NET_ReadFormat that reads variable arguments from a buffer according to ...
Definition: netpack.cpp:533
InventoryInterface i
Definition: client.h:101
int impactTime
Definition: e_main.h:33
void CL_InvAdd(const eventRegister_t *self, dbuffer *msg)
void LE_PlaceItem(le_t *le)
voidpf void * buf
Definition: ioapi.h:42
int right
void Com_Error(int code, const char *fmt,...)
Definition: common.cpp:417
void Cmd_ExecuteString(const char *text,...)
A complete command line has been parsed, so try to execute it.
Definition: cmd.cpp:1007
#define CID_LEFT
Definition: inv_shared.h:48
int rotated
Definition: inv_shared.h:412
int nextTime
Definition: e_main.h:31
client_static_t cls
Definition: cl_main.cpp:83
#define CID_HEADGEAR
Definition: inv_shared.h:50
void setAmount(int value)
Definition: inv_shared.h:438
a local entity
#define ERR_DROP
Definition: common.h:211
int headgear
#define CID_RIGHT
Definition: inv_shared.h:47
void setAmmoDef(const objDef_t *od)
Definition: inv_shared.h:435
int getAmount() const
Definition: inv_shared.h:463
le_t * LE_Get(int entnum)
Searches all local entities for the one with the searched entnum.
bool parsedShot
Definition: e_main.h:36
#define LE_IsSelected(le)
const eventRegister_t * CL_GetEvent(const event_t eType)
Definition: e_main.cpp:157
int left
int NET_ReadShort(dbuffer *buf)
Definition: netpack.cpp:242
clientBattleScape_t cl
#define CID_FLOOR
Definition: inv_shared.h:55
#define LE_REMOVE_NEXT_FRAME
item instance data, with linked list capability
Definition: inv_shared.h:402
void setDef(const objDef_t *objDef)
Definition: inv_shared.h:444
#define INVDEF(containerID)
Definition: cl_shared.h:48
int32_t containerIndex_t
Definition: inv_shared.h:46
const char * id
Definition: inv_shared.h:268
#define NONE_AMMO
Definition: defines.h:69
const objDef_t * INVSH_GetItemByIDX(int index)
Returns the item that belongs to the given index or nullptr if the index is invalid.
Definition: inv_shared.cpp:266
entity_type_t type
CL_ParseEvent timers and vars.
Definition: e_main.h:30
bool parsedDeath
Definition: e_main.h:35
void setAmmoLeft(int value)
Definition: inv_shared.h:441
void LE_SetThink(le_t *le, localEntityThinkFunc_t think)
void LET_StartIdle(le_t *le)
Change the animation of an actor to the idle animation (which can be panic, dead or stand) ...
Struct that defines one particular event with all its callbacks and data.
Definition: e_main.h:42
#define LE_NotFoundError(entnum)