EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ItemFactory.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24  Rewrite: Allan
25 */
26 
27 #include "eve-server.h"
28 
29 #include "Client.h"
30 #include "EVEServerConfig.h"
31 #include "character/Character.h"
32 #include "exploration/Probes.h"
33 #include "inventory/InventoryDB.h"
34 #include "inventory/ItemFactory.h"
35 #include "inventory/ItemType.h"
37 #include "pos/Structure.h"
38 #include "ship/Missile.h"
39 #include "ship/Ship.h"
41 #include "station/Station.h"
42 #include "station/StationOffice.h"
43 #include "system/Asteroid.h"
44 #include "system/Container.h"
45 #include "system/SolarSystem.h"
46 #include "system/SystemManager.h"
47 
49 :m_pClient(nullptr),
50 m_nextTempID(0),
51 m_nextNPCID(0),
52 m_nextDroneID(0),
53 m_nextMissileID(0),
54 m_db(nullptr)
55 {
56 }
57 
59 {
61 }
62 
64 {
65  ManagerDB::DeleteSpawnedRats(); // takes ~31.2s to run on main, 0.005s on dev
66 
67  if (sConfig.debug.DeleteTrackingCans)
69 
70  m_items.clear();
71 
72  // Initialize ID Authority variables:
77 
78  m_db = new InventoryDB();
79 
80  sLog.Blue(" ItemFactory", "Item Factory Initialized.");
81  return 1;
82 }
83 
85 {
86  sLog.Warning(" ItemFactory", "%u Items, %u Types still in list", \
87  m_items.size(), m_types.size());
88  // types
89  for (auto cur : m_types)
90  SafeDelete(cur.second);
91  m_types.clear();
92  // items
93  //for (auto cur : m_items)
94  // delete(cur.second.get());
95  m_items.clear();
96  // Set Client pointer to NULL
97  m_pClient = nullptr;
98 }
99 
101  if (sConfig.debug.DeleteTrackingCans)
103  uint32 count(0);
104  double startTime = GetTimeMSeconds();
105  std::vector<Inv::SaveData> items;
106  items.clear();
107  for (auto cur : m_items) {
108  if (IsPlayerItem(cur.first)) { // this is a hack for now. will eventually move to static/dynamic item maps
109  cur.second->SaveAttributes();
110  Inv::SaveData data = Inv::SaveData();
111  data.itemID = cur.first;
112  data.contraband = cur.second->contraband();
113  data.flag = cur.second->flag();
114  data.locationID = cur.second->locationID();
115  data.ownerID = cur.second->ownerID();
116  data.position = cur.second->position();
117  data.quantity = cur.second->quantity();
118  data.singleton = cur.second->isSingleton();
119  data.typeID = cur.second->typeID();
120  data.customInfo = cur.second->customInfo();
121  items.push_back(data);
122  ++count;
123  }
124  }
125  ItemDB::SaveItems(items);
126  sLog.Warning(" SaveItems", "Saved %u Dynamic Items in %.3fms.", count, (GetTimeMSeconds() -startTime));
127 }
128 
130 {
131  if (IsTempItem(iRef->itemID()))
132  return;
133 
134  if (iRef->itemID() < minAgent) {
135  sLog.Warning("ItemFactory::AddItem()", "Trying to Add invalid UID %u for %s", iRef->itemID(), iRef->name());
136  return;
137  }
138  m_items.emplace(iRef->itemID(), iRef);
139 }
140 
142 {
143  m_items.erase(itemID);
144 }
145 
147 {
148  if (m_nextTempID < PLANET_PIN_ID) {
149  ++m_nextTempID;
150  } else {
152  }
153 
154  return m_nextTempID;
155 }
156 
158 {
159  return ++m_nextNPCID;
160 }
161 
163  return ++m_nextDroneID;
164 }
165 
167 {
168  return ++m_nextMissileID;
169 }
170 
172  // do we need to check trade containers here?
173  if (!IsValidLocation(itemID))
174  return nullptr;
175 
176  InventoryItemRef iRef(nullptr);
177  std::map<uint32, InventoryItemRef>::iterator itr = m_items.find(itemID);
178  if (itr != m_items.end()) {
179  iRef = itr->second;
180  } else if (load) {
181  iRef = GetItem(itemID);
182  }
183 
184  if (iRef.get() == nullptr)
185  return nullptr;
186 
187  return iRef->GetMyInventory();
188 }
189 
191  InventoryItemRef iRef(nullptr);
192  std::map<uint32, InventoryItemRef>::iterator itr = m_items.find(itemID);
193  if (itr != m_items.end()) {
194  iRef = itr->second;
195  } else if (load) {
196  iRef = GetItem(itemID);
197  }
198 
199  return iRef;
200 }
201 
203 {
204  InventoryItemRef iRef(nullptr);
205  std::map<uint32, InventoryItemRef>::iterator itr = m_items.find(itemID);
206  if (itr != m_items.end()) {
207  iRef = itr->second;
208  itr = m_items.find(iRef->locationID());
209  iRef = itr->second;
210  } else if (load) {
211  iRef = GetItem(itemID);
212  itr = m_items.find(itemID);
213  if (itr != m_items.end()) {
214  iRef = itr->second;
215  itr = m_items.find(iRef->locationID());
216  iRef = itr->second;
217  }
218  }
219 
220  if (iRef.get() == nullptr)
221  return InventoryItemRef(nullptr);
222 
223  return iRef;
224 }
225 
227 {
228  InventoryItemRef iRef(nullptr);
229  std::map<uint32, InventoryItemRef>::iterator itr = m_items.find(itemID);
230  if (itr != m_items.end()) {
231  iRef = itr->second;
232  itr = m_items.find(iRef->locationID());
233  iRef = itr->second;
234  } else if (load) {
235  iRef = GetItem(itemID);
236  itr = m_items.find(itemID);
237  if (itr != m_items.end()) {
238  iRef = itr->second;
239  itr = m_items.find(iRef->locationID());
240  iRef = itr->second;
241  }
242  }
243 
244  if (iRef.get() == nullptr)
245  return nullptr;
246 
247  return iRef->GetMyInventory();
248 }
249 
250 template<class _Ty>
251 const _Ty* ItemFactory::_GetType(uint16 typeID) {
252  std::map<uint16, ItemType*>::iterator itr = m_types.find(typeID);
253  if (itr == m_types.end()) {
254  _Ty* type = _Ty::Load(typeID);
255  if (type == nullptr)
256  return nullptr;
257 
258  // insert into cache
259  itr = m_types.insert(std::make_pair(typeID, type)).first;
260  }
261  return static_cast<const _Ty *>(itr->second);
262 }
263 
265  return _GetType<ItemType>(typeID);
266 }
267 
269  return _GetType<BlueprintType>(blueprintTypeID);
270 }
271 
273  return _GetType<CharacterType>(characterTypeID);
274 }
275 
277  // Unfortunately, we have it indexed by typeID, so we must get it ...
278  uint16 characterTypeID;
279  if (!InventoryDB::GetCharacterTypeByBloodline(bloodlineID, characterTypeID))
280  return nullptr;
281  return GetCharacterType(characterTypeID);
282 }
284  return _GetType<StationType>(stationTypeID);
285 }
286 
287 template<class _Ty>
289 {
290  std::map<uint32, InventoryItemRef>::iterator itr = m_items.find(itemID);
291  if (itr == m_items.end()) {
292  if (itemID < minAgent) {
293  _log(ITEM__WARNING, "ItemFactory::_GetItem() called on invalid Item %u", itemID);
294  //if (sConfig.debug.StackTrace)
295  // EvE::traceStack();
296  return RefPtr<_Ty>();
297  }
298 
299  // load the item
300  RefPtr<_Ty> item = _Ty::Load(itemID);
301  if (!item)
302  return RefPtr<_Ty>();
303 
304  //we keep the original ref.
305  itr = m_items.insert(std::make_pair(itemID, item)).first;
306  }
307  // return to the user.
308  return RefPtr<_Ty>::StaticCast(itr->second);
309 }
310 
312 {
313  return _GetItem<InventoryItem>(itemID);
314 }
315 
317 {
318  return _GetItem<Blueprint>(blueprintID);
319 }
320 
322 {
323  return _GetItem<Character>(characterID);
324 }
325 
327 {
328  return _GetItem<ShipItem>(shipID);
329 }
330 
332 {
333  return _GetItem<CelestialObject>(celestialID);
334 }
335 
337 {
338  return _GetItem<SolarSystem>(solarSystemID);
339 }
340 
342 {
343  return _GetItem<StationItem>(stationID);
344 }
345 
347 {
348  return _GetItem<Skill>(skillID);
349 }
350 
352 {
353  return _GetItem<AsteroidItem>(asteroidID);
354 }
355 
357 {
358  return _GetItem<StationOffice>(officeID);
359 }
360 
362 {
363  return _GetItem<StructureItem>(structureID);
364 }
365 
367 {
368  return _GetItem<CargoContainer>(containerID);
369 }
370 
372 {
373  return _GetItem<WreckContainer>(containerID);
374 }
375 
377 {
378  return _GetItem<ModuleItem>(moduleID);
379 }
380 
382  return _GetItem<ProbeItem>(probeID);
383 }
384 
385 
386 
389  if (iRef.get() != nullptr)
390  AddItem(iRef);
391 
392  return iRef;
393 }
394 
397  return iRef;
398 }
399 
401  CharacterRef iRef = Character::Spawn(charData, corpData);
402  return iRef;
403 }
404 
406  ShipItemRef iRef = ShipItem::Spawn(data);
407  if (iRef.get() != nullptr)
408  AddItem(iRef);
409 
410  return iRef;
411 }
412 
414 {
415  SkillRef iRef = Skill::Spawn(data);
416  if (iRef.get() != nullptr)
417  AddItem(iRef);
418 
419  return iRef;
420 }
421 
423 {
425  if (iRef.get() != nullptr)
426  AddItem(iRef);
427 
428  return iRef;
429 }
430 
432 {
433  AsteroidItemRef iRef = AsteroidItem::Spawn(idata, adata);
434  if (iRef.get() != nullptr)
435  AddItem(iRef);
436 
437  return iRef;
438 }
439 
441 {
442  StationOfficeRef iRef = StationOffice::Spawn(idata, odata);
443  if (iRef.get() != nullptr)
444  AddItem(iRef);
445 
446  return iRef;
447 }
448 
450 {
452  if (iRef.get() != nullptr)
453  AddItem(iRef);
454 
455  return iRef;
456 }
457 
459 {
461  if (iRef.get() != nullptr)
462  AddItem(iRef);
463 
464  return iRef;
465 }
466 
468 {
469  ModuleItemRef iRef = ModuleItem::Spawn(data);
470  if (iRef.get() != nullptr)
471  AddItem(iRef);
472 
473  return iRef;
474 }
475 
477 {
478  ProbeItemRef iRef = ProbeItem::Spawn(data);
479  if (iRef.get() != nullptr)
480  AddItem(iRef);
481 
482  return iRef;
483 }
InventoryItemRef GetItem(uint32 itemID)
#define sConfig
A macro for easier access to the singleton.
uint32 m_nextMissileID
Definition: ItemFactory.h:160
uint32 m_nextNPCID
Definition: ItemFactory.h:157
ProbeItemRef GetProbeItem(uint32 probeID)
static ShipItemRef Spawn(ItemData &data)
Definition: Ship.cpp:51
Inventory * GetInventoryFromId(uint32 itemID, bool load=true)
const ItemType * GetType(uint16 typeID)
int Initialize()
Definition: ItemFactory.cpp:63
static bool GetCharacterTypeByBloodline(uint8 bloodlineID, uint16 &characterTypeID)
Definition: InventoryDB.cpp:91
StationItemRef GetStationItem(uint32 stationID)
const StationType * GetStationType(uint16 stationTypeID)
#define _log(type, fmt,...)
Definition: logsys.h:124
uint32 m_nextDroneID
Definition: ItemFactory.h:159
AsteroidItemRef SpawnAsteroid(ItemData &idata, AsteroidData &adata)
static AsteroidItemRef Spawn(ItemData &idata, AsteroidData &adata)
Definition: Asteroid.cpp:33
InventoryItemRef SpawnTempItem(ItemData &data)
void RemoveItem(uint32 itemID)
SkillRef GetSkill(uint32 skillID)
const CharacterType * GetCharacterType(uint16 characterTypeID)
const CharacterType * GetCharacterTypeByBloodline(uint16 bloodlineID)
static void SaveItems(std::vector< Inv::SaveData > &data)
Definition: ItemDB.cpp:264
std::map< uint16, ItemType * > m_types
Definition: ItemFactory.h:143
CharacterRef GetCharacter(uint32 characterID)
static CharacterRef Spawn(CharacterData &charData, CorpData &corpData)
Definition: Character.cpp:321
static void DeleteSpawnedRats()
Definition: ManagerDB.cpp:462
SolarSystemRef GetSolarSystem(uint32 solarSystemID)
static SkillRef Spawn(ItemData &data)
Definition: Skill.cpp:52
CharacterRef SpawnCharacter(CharacterData &charData, CorpData &corpData)
SkillRef SpawnSkill(ItemData &data)
Client * m_pClient
Definition: ItemFactory.h:141
itemID[count] Create count or of the specified item(from Insider)" ) COMMAND( goto
CelestialObjectRef GetCelestialObject(uint32 celestialID)
uint32 GetNextMissileID()
StationOfficeRef GetOffice(uint32 officeID)
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
#define NPC_ID
Definition: EVE_Defines.h:182
#define DRONE_ID
Definition: EVE_Defines.h:181
static ModuleItemRef Spawn(ItemData &data)
Definition: ModuleItem.cpp:27
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
typeID Spawn an NPC with the specified type text Search for items matching the specified query() type()() itemID() copy() materialLevel()()() itemID() itemID Fits selected item to active ship() skillID(level)-gives skillID to specified level." ) COMMAND( online
InventoryItemRef SpawnItem(ItemData &data)
#define minAgent
Definition: EVE_Defines.h:101
std::string customInfo
void SaveItems()
double GetTimeMSeconds()
Definition: utils_time.cpp:104
WreckContainerRef SpawnWreckContainer(ItemData &data)
#define IsPlayerItem(itemID)
Definition: EVE_Defines.h:256
EVEItemFlags flag
Definition: EVE_Inventory.h:97
std::map< uint32, InventoryItemRef > m_items
Definition: ItemFactory.h:144
AsteroidItemRef GetAsteroid(uint32 asteroidID)
static InventoryItemRef SpawnTemp(ItemData &data)
static void DeleteTrackingCans()
InventoryDB * m_db
Definition: ItemFactory.h:140
const _Ty * _GetType(uint16 typeID)
static CargoContainerRef Spawn(ItemData &data)
Definition: Container.cpp:70
X * get() const
Definition: RefPtr.h:213
const BlueprintType * GetBlueprintType(uint16 blueprintTypeID)
StationOfficeRef SpawnOffice(ItemData &idata, OfficeData &odata)
static WreckContainerRef Spawn(ItemData &data)
Definition: Container.cpp:410
CargoContainerRef GetCargoContainer(uint32 containerID)
static ProbeItemRef Spawn(ItemData &data)
Definition: Probes.cpp:52
static InventoryItemRef Spawn(ItemData &data)
StructureItemRef GetStructure(uint32 structureID)
static RefPtr StaticCast(const RefPtr< Y > &oth)
Acts as static_cast from one RefPtr to another.
Definition: RefPtr.h:238
unsigned __int32 uint32
Definition: eve-compat.h:50
ProbeItemRef SpawnProbe(ItemData &data)
#define PLANET_PIN_ID
Definition: EVE_Defines.h:179
ShipItemRef GetShip(uint32 shipID)
RefPtr< InventoryItem > InventoryItemRef
Definition: ItemRef.h:52
#define MISSILE_ID
Definition: EVE_Defines.h:186
#define IsTempItem(itemID)
Definition: EVE_Defines.h:333
#define TEMP_ENTITY_ID
Definition: EVE_Defines.h:178
uint32 GetNextTempID()
uint32 m_nextTempID
Definition: ItemFactory.h:158
void Close()
Definition: ItemFactory.cpp:84
ModuleItemRef GetModuleItem(uint32 moduleID)
typeID Spawn an NPC with the specified type text Search for items matching the specified query() type()() itemID() copy() materialLevel()() itemID(attributeID)-Retrieves attribute value." ) COMMAND( setattr
ModuleItemRef SpawnModule(ItemData &data)
static StationOfficeRef Spawn(ItemData &idata, OfficeData &odata)
void AddItem(InventoryItemRef iRef)
RefPtr< _Ty > _GetItem(uint32 itemID)
CargoContainerRef SpawnCargoContainer(ItemData &data)
Inventory * GetItemContainerInventory(uint32 itemID, bool load=true)
InventoryItemRef GetItemContainer(uint32 itemID, bool load=true)
BlueprintRef GetBlueprint(uint32 blueprintID)
InventoryItemRef GetInventoryItemFromID(uint32 itemID, bool load=true)
unsigned __int16 uint16
Definition: eve-compat.h:48
entityID heal the character with the entityID note giving you detailed ship status information gives a list of all dynamic entities and players and their destinyState in this bubble shows some current destiny variables save all items
static StructureItemRef Spawn(ItemData &data)
Definition: Structure.cpp:71
uint32 GetNextDroneID()
WreckContainerRef GetWreckContainer(uint32 containerID)
#define IsValidLocation(itemID)
Definition: EVE_Defines.h:209
uint32 GetNextNPCID()
StructureItemRef SpawnStructure(ItemData &data)
ShipItemRef SpawnShip(ItemData &data)
Reference-counting-based smart pointer.
Definition: RefPtr.h:133