-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCTradingComputer.cpp
121 lines (89 loc) · 3.09 KB
/
CTradingComputer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// CTradingComputer.cpp
//
// CTradingComputer class
// Copyright (c) 2017 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
int CTradingComputer::GetItemEstimatedPrice (const CTopologyNode *pNode, CItemType *pItemType)
// GetItemEstimatedPrice
//
// Return the standard price for the item in the given node.
{
CItem Item(pItemType, 1);
int iPrice = Item.GetTradePrice();
int iAdj;
if (!pNode->GetTradingEconomy().FindPriceAdj(pItemType, &iAdj))
return iPrice;
return iAdj * iPrice / 100;
}
void CTradingComputer::GetItemEstimatedPriceList (const CUniverse &Universe, CItemType *pItemType, DWORD dwFlags, TSortMap<int, TArray<const CTopologyNode *>> &NodesAtPrice)
// GetItemEstimatedPriceList
//
// Returns a sorted list of prices. For each price, we associate an
// array of nodes that sell the given item at that price. The price is returned
// in the item's default currency.
{
int i;
bool bKnownOnly = ((dwFlags & FLAG_KNOWN_ONLY) ? true : false);
NodesAtPrice.DeleteAll();
// Get the price of the item
CItem Item(pItemType, 1);
int iPrice = Item.GetTradePrice();
// Loop over all nodes
const CTopology &Topology = Universe.GetTopology();
for (i = 0; i < Topology.GetTopologyNodeCount(); i++)
{
const CTopologyNode *pNode = Topology.GetTopologyNode(i);
if (pNode->IsEndGame())
continue;
// Skip unknown nodes, if required.
if (bKnownOnly && !pNode->IsKnown())
continue;
// Compute the actual price with the adjustment
int iActualPrice = iPrice;
int iAdj;
if (pNode->GetTradingEconomy().FindPriceAdj(pItemType, &iAdj))
iActualPrice = iAdj * iActualPrice / 100;
// Set this price in the result
TArray<const CTopologyNode *> *pNodeList = NodesAtPrice.SetAt(iActualPrice);
pNodeList->Insert(pNode);
}
}
int CTradingComputer::GetItemBuyPrice (const CUniverse &Universe, const CTopologyNode *pNode, const CDesignTypeCriteria &Criteria, const CItem &Item, DWORD *retdwObjID)
// GetItemTradePrice
//
// Return the best price available for the player to sell the given item in
// the given system. Optionally returns the Object ID of the buyer. If no
// station will buy the item, then we return a price <= 0.
{
int i;
const CObjectTracker &Objects = Universe.GetGlobalObjects();
TArray<CObjectTracker::SObjEntry> TradingObjs;
Objects.GetTradingObjects(pNode, TradingObjs);
STradeServiceCtx TradeCtx;
TradeCtx.pNode = pNode;
TradeCtx.pCurrency = Item.GetType()->GetCurrencyType();
TradeCtx.pItem = &Item;
TradeCtx.iCount = 1;
// Look for the trading object with the best price
DWORD dwBestObjID = 0;
int iBestPrice = 0;
for (i = 0; i < TradingObjs.GetCount(); i++)
{
CDesignType *pType = TradingObjs[i].pType;
CTradingDesc *pTrade = pType->GetTradingDesc();
if (pTrade == NULL)
continue;
if (!Criteria.IsEmpty() && !pType->MatchesCriteria(Criteria))
continue;
int iPrice;
if (pTrade->Buys(TradeCtx, Item, CTradingDesc::FLAG_NO_DONATION, &iPrice) && iPrice > iBestPrice)
{
iBestPrice = iPrice;
dwBestObjID = TradingObjs[i].dwObjID;
}
}
// Done
if (retdwObjID)
*retdwObjID = dwBestObjID;
return iBestPrice;
}