-
Notifications
You must be signed in to change notification settings - Fork 8
/
GoodsRepoDlg.cpp
389 lines (359 loc) · 11.7 KB
/
GoodsRepoDlg.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// GoodsRepo.cpp : implementation file
//
#include "stdafx.h"
#include "FLCompanion.h"
#include "FLCompanionDlg.h"
#include "Datas.h"
#include "IniFile.h"
#include "Base.h"
#include "System.h"
#include "GoodsRepoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGoodsRepoDlg dialog
CGoodsRepoDlg *CGoodsRepoDlg::g_goodsRepoDlg;
CGoodsRepoDlg::CGoodsRepoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGoodsRepoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGoodsRepoDlg)
//}}AFX_DATA_INIT
}
void CGoodsRepoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGoodsRepoDlg)
DDX_Control(pDX, IDC_GOODS_LIST, m_goodsList);
DDX_Control(pDX, IDC_BASES_LIST, m_basesList);
DDX_Control(pDX, IDC_KIND_COMBO, m_kindCombo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGoodsRepoDlg, CDialog)
//{{AFX_MSG_MAP(CGoodsRepoDlg)
ON_CBN_SELCHANGE(IDC_KIND_COMBO, OnSelchangeKindCombo)
ON_WM_DESTROY()
ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickBasesList)
ON_BN_CLICKED(IDC_SHOWSELL, OnSelchangeGoodsList)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_GOODS_LIST, OnItemchangedGoodsList)
ON_NOTIFY(LVN_ITEMACTIVATE, IDC_BASES_LIST, OnItemactivateBasesList)
ON_BN_CLICKED(IDC_SHOWALL, OnSelchangeGoodsList)
ON_LBN_SELCHANGE(IDC_GOODS_LIST, OnSelchangeGoodsList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGoodsRepoDlg message handlers
void CGoodsRepoDlg::OnDestroy()
{
EmptyGoods();
CDialog::OnDestroy();
}
void CGoodsRepoDlg::EmptyGoods()
{
int nIndex = m_goodsList.GetItemCount();
while (nIndex)
free(LPVOID(m_goodsList.GetItemData(--nIndex)));
m_goodsList.DeleteAllItems();
ListView_HeaderSortReset(m_goodsList);
m_goodsList.DeleteColumn(2);
m_goodsList.DeleteColumn(1);
}
BOOL CGoodsRepoDlg::FindEquipKind(const CString &iniFilename)
{
IniSection section;
CString name;
{
IniFile iniFile(iniFilename);
section = NULL;
while (iniFile.GetNextSection(section, name))
{
DWORD value = iniFile.GetValueInt0(section, "ids_name");
if (value <= 1) continue;
if (m_kindCombo.FindStringExact(-1, name) == CB_ERR)
m_kindCombo.AddString(name);
}
}
return TRUE;
}
BOOL CGoodsRepoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
g_goodsRepoDlg = this;
m_goodsList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP);
m_goodsList.InsertColumn(0, L"Name", LVCFMT_LEFT, -1);
m_basesList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_TWOCLICKACTIVATE|LVS_EX_UNDERLINEHOT|LVS_EX_HEADERDRAGDROP);
m_basesList.InsertColumn(0, L"Base", LVCFMT_LEFT, 20);
m_basesList.InsertColumn(1, L"Price", LVCFMT_RIGHT, 20);
m_basesList.InsertColumn(2, L"System", LVCFMT_LEFT, 20);
m_basesList.InsertColumn(3, L"Faction", LVCFMT_LEFT, 20);
LoadFiles(_FindEquipKind, L"equipment");
m_kindCombo.SelectString(-1, L"Ship");
OnSelchangeKindCombo();
CheckDlgButton(IDC_SHOWSELL, BST_CHECKED);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CGoodsRepoDlg::FindEquipNames(const CString &iniFilename)
{
IniSection section;
CString name;
{
IniFile iniFile(iniFilename);
section = NULL;
while (iniFile.GetNextSection(section, name))
{
if (name != m_kind) continue;
CString nickname = iniFile.GetValue0(section, "nickname");
DWORD value = iniFile.GetValueInt0(section, "ids_name");
CString caption = g_mainDlg->m_displayNicknames ? nickname : g_resourceProvider.GetStringFromID(value);
caption.TrimLeft();
if (caption.IsEmpty()) caption = nickname;
CGood* good = NULL;
if ((m_kind != "Commodity") || !g_goodsByNick.Lookup(nickname, good))
caption = "*" + caption;
else
caption = " " + caption;
LVFINDINFO fi;
fi.flags = LVFI_STRING;
fi.psz = caption;
int nItem = m_goodsList.FindItem(&fi);
if (nItem != -1)
{
LPTSTR otherEntry = reinterpret_cast<LPTSTR>(m_goodsList.GetItemData(nItem));
if (otherEntry)
{
m_goodsList.SetItemData(nItem, NULL);
CString subtext = m_goodsList.GetItemText(nItem, 1);
nItem = m_goodsList.InsertItem(MAXLONG, caption+L" ("+otherEntry+L")");
m_goodsList.SetItemData(nItem, DWORD(otherEntry));
m_goodsList.SetItemText(nItem, 1, subtext);
}
caption += L" ("+nickname+L")";
}
nItem = m_goodsList.InsertItem(MAXLONG, caption);
m_goodsList.SetItemData(nItem, DWORD(_tcsdup(nickname)));
float defaultPrice;
if (good)
m_goodsList.SetItemText(nItem, 1, L'$'+DoubleToString(good->m_defaultPrice));
else if (g_equipPrices.Lookup(nickname, defaultPrice))
m_goodsList.SetItemText(nItem, 1, L'$'+DoubleToString(defaultPrice));
}
}
return TRUE;
}
void CGoodsRepoDlg::OnSelchangeKindCombo()
{
int nIndex = m_kindCombo.GetCurSel();
if (nIndex == CB_ERR) return;
m_kindCombo.GetLBText(nIndex, m_kind);
EmptyGoods();
g_resourceProvider.Init(g_flAppPath);
try
{
if (m_kind == L"Ship")
{
m_goodsList.InsertColumn(1, L"Price", LVCFMT_RIGHT, 20);
m_goodsList.InsertColumn(2, L"Hold", LVCFMT_RIGHT, 20);
POSITION pos = g_shippackToInfos.GetStartPosition();
CString nickname;
ShipInfo* shipInfo;
while (pos)
{
POSITION savedPos = pos;
g_shippackToInfos.GetNextAssoc(pos, nickname, shipInfo);
int nItem = m_goodsList.InsertItem(MAXLONG, g_mainDlg->m_displayNicknames ? nickname : g_resourceProvider.GetStringFromID(shipInfo->m_ids_name));
m_goodsList.SetItemText(nItem, 1, L'$'+DoubleToString(shipInfo->m_defaultPrice));
m_goodsList.SetItemText(nItem, 2, IntToString(shipInfo->m_hold_size));
m_goodsList.SetItemData(nItem, DWORD(_tcsdup(nickname)));
}
}
else
{
m_goodsList.InsertColumn(1, L"Price (default)", LVCFMT_RIGHT, 20);
LoadFiles(_FindEquipNames, L"equipment");
int nIndex = m_goodsList.GetItemCount();
while (nIndex--)
{
if (m_goodsList.GetItemData(nIndex) == NULL)
m_goodsList.DeleteItem(nIndex);
}
}
}
catch(...)
{
g_resourceProvider.Free();
throw;
}
g_resourceProvider.Free();
m_goodsList.SetColumnWidth(0, LVSCW_AUTOSIZE);
m_goodsList.SetColumnWidth(1, LVSCW_AUTOSIZE);
m_goodsList.SetColumnWidth(2, LVSCW_AUTOSIZE);
OnSelchangeGoodsList();
}
BOOL CGoodsRepoDlg::FindItemRefs(const CString &iniFilename)
{
IniSection section;
CString name;
float price;
TCHAR buf[32];
{
IniFile iniFile(iniFilename);
section = NULL;
while (iniFile.GetNextSection(section, name))
{
CHECK(name.CompareNoCase(L"BaseGood") == 0);
CBase* base;
name = iniFile.GetValue0(section, "base");
name.MakeLower();
if (g_basesByNick.Lookup(name, base))
{
IniEntry entry;
for (UINT entriesCount = iniFile.EnumEntries(section, entry); entriesCount; entriesCount--)
{
UINT valuesCount;
IniValue *values;
if (iniFile.GetNextEntry(entry, valuesCount, values).CompareNoCase(L"MarketGood") != 0)
continue;
name = iniFile.GetValue(values,0);
if (name != m_item)
continue;
if (valuesCount < 7)
continue;
// iniFile.GetValueInt(values,1) : Minimum character level to purchase
//
// iniFile.GetValueInt(values,2) : Minimum faction level to purchase (can be decimal)
// -1 = even if very hostile, 0 = must be at least neutral, 1 = need to be very friendly
UINT buyonly = iniFile.GetValueInt(values,5);
if (buyonly && !m_showAllBases)
continue; // we want to list only goods that are sold here
price = m_defaultPrice*iniFile.GetValueFloat(values,6);
if (m_kind == "Commodity")
{
CGood* good;
if (g_goodsByNick.Lookup(name, good))
price = base->m_buy[*good]; // use value from memory rather than from file (it might have been loaded from DynEcon)
}
int nItem = m_basesList.InsertItem(LVIF_TEXT|LVIF_PARAM, MAXLONG, base->m_caption, 0, 0, 0, long(base));
_stprintf(buf, L"$%d", INT(price));
//FormatNumber(buf, long(price)); // don't use FormatNumber because it breaks ListView sorting
m_basesList.SetItemText(nItem, 1, buf);
m_basesList.SetItemText(nItem, 2, base->m_system->m_caption);
m_basesList.SetItemText(nItem, 3, base->m_faction->m_caption);
}
}
}
}
return TRUE;
}
void CGoodsRepoDlg::DisplayGoodPrices(CGood* good)
{
UINT goodIndex = *good;
TCHAR buf[32];
for (UINT index = 0; index < BASES_COUNT; index++)
{
CBase* base = &g_bases[index];
if ((base->m_sell[goodIndex] == FLT_MAX) && !m_showAllBases)
continue; // we want to list only goods that are sold here
if (base->m_buy[goodIndex] == good->m_defaultPrice)
continue;
int nItem = m_basesList.InsertItem(LVIF_TEXT|LVIF_PARAM, MAXLONG, base->m_caption, 0, 0, 0, long(base));
_stprintf(buf, L"$%d", INT(base->m_buy[goodIndex]));
//FormatNumber(buf, long(price)); // don't use FormatNumber because it breaks ListView sorting
m_basesList.SetItemText(nItem, 1, buf);
m_basesList.SetItemText(nItem, 2, base->m_system->m_caption);
//m_basesList.SetItemText(nItem, 3, base->m_faction->m_caption);
}
}
void CGoodsRepoDlg::OnItemchangedGoodsList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
*pResult = 0;
if ((pNMListView->uNewState & LVIS_SELECTED) == 0)
return;
OnSelchangeGoodsList();
}
void CGoodsRepoDlg::OnSelchangeGoodsList()
{
POSITION pos = m_goodsList.GetFirstSelectedItemPosition();
if (pos == NULL) return;
int nIndex = m_goodsList.GetNextSelectedItem(pos);
m_basesList.DeleteAllItems();
m_selectedBase = NULL;
m_showAllBases = IsDlgButtonChecked(IDC_SHOWALL) == BST_CHECKED;
m_item = LPCTSTR(m_goodsList.GetItemData(nIndex));
CGood* good = NULL;
CString str;
TCHAR buf[32];
if (m_kind == L"Ship")
{
ShipInfo* shipInfo = g_shippackToInfos[m_item];
m_defaultPrice = shipInfo->m_defaultPrice;
FormatNumber(buf, long(m_defaultPrice));
str.Format(L"Default price: %s \x2014 Cargo hold size: %d units", buf, shipInfo->m_hold_size);
}
else if (g_equipPrices.Lookup(m_item, m_defaultPrice))
{
FormatNumber(buf, long(m_defaultPrice));
str.Format(L"Default price: %s", buf);
}
else if (g_goodsByNick.Lookup(m_item, good))
{
m_defaultPrice = good->m_defaultPrice;
FormatNumber(buf, long(m_defaultPrice));
str.Format(L"Default price: %s", buf);
}
else
m_defaultPrice = 0;
SetDlgItemText(IDC_DEFAULT_PRICE, str);
SetRedraw(FALSE);
if (good)
DisplayGoodPrices(good);
else
LoadFiles(_FindItemRefs, L"markets");// parse bases price
ListView_HeaderSort(m_basesList, 1, m_showAllBases);
m_basesList.SetColumnWidth(0, LVSCW_AUTOSIZE);
m_basesList.SetColumnWidth(1, LVSCW_AUTOSIZE);
m_basesList.SetColumnWidth(2, LVSCW_AUTOSIZE);
m_basesList.SetColumnWidth(3, LVSCW_AUTOSIZE);
SetRedraw(TRUE);
Invalidate();
}
void CGoodsRepoDlg::OnItemclickBasesList(NMHDR* pNMHDR, LRESULT* pResult)
{
ListView_HeaderSort(pNMHDR, pResult);
}
/*
ON_NOTIFY(NM_CUSTOMDRAW, IDC_BASES_LIST, OnCustomDrawBasesList)
void CGoodsRepoDlg::OnCustomDrawBasesList(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLVCUSTOMDRAW lplvcd = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
*pResult = CDRF_DODEFAULT;
switch (lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
{
TCHAR price[32];
m_basesList.GetItemText(lplvcd->nmcd.dwItemSpec,1,price,_countof(price));
int delta = _ttoi(price+1)-m_defaultPrice;
if (delta > 0)
lplvcd->clrText = RGB(255,0,0);
else if (delta < 0)
lplvcd->clrText = RGB(0,255,0);
*pResult = CDRF_NEWFONT;
break;
}
}
}
*/
void CGoodsRepoDlg::OnItemactivateBasesList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
m_selectedBase = reinterpret_cast<CBase*>(m_basesList.GetItemData(pNMListView->iItem));
*pResult = 0;
EndDialog(IDOK);
}