-
Notifications
You must be signed in to change notification settings - Fork 76
/
Mapping.cpp
352 lines (270 loc) · 10.2 KB
/
Mapping.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
#include "stdafx.h"
#include "MUSHclient.h"
#include "doc.h"
#include "dialogs\MapDlg.h"
#include "dialogs\MapMoveDlg.h"
#include "dialogs\MapCommentDlg.h"
#include "mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
CMapDirectionsMap MapDirectionsMap;
void LoadMapDirections (void)
{
//-------------------------------------------------------------------
// direction log full reverse
MapDirectionsMap ["north"] = CMapDirection ("n", "north", "s");
MapDirectionsMap ["south"] = CMapDirection ("s", "south", "n");
MapDirectionsMap ["east"] = CMapDirection ("e", "east", "w");
MapDirectionsMap ["west"] = CMapDirection ("w", "west", "e");
MapDirectionsMap ["up"] = CMapDirection ("u", "up", "d");
MapDirectionsMap ["down"] = CMapDirection ("d", "down", "u");
MapDirectionsMap ["ne"] = CMapDirection ("ne", "ne", "sw");
MapDirectionsMap ["nw"] = CMapDirection ("nw", "nw", "se");
MapDirectionsMap ["se"] = CMapDirection ("se", "se", "nw");
MapDirectionsMap ["sw"] = CMapDirection ("sw", "sw", "ne");
// filler
MapDirectionsMap ["f"] = CMapDirection ("f", "f", "f");
// now do abbreviations (copy above entries)
MapDirectionsMap ["n"] = MapDirectionsMap ["north"];
MapDirectionsMap ["s"] = MapDirectionsMap ["south"];
MapDirectionsMap ["e"] = MapDirectionsMap ["east"];
MapDirectionsMap ["w"] = MapDirectionsMap ["west"];
MapDirectionsMap ["u"] = MapDirectionsMap ["up"];
MapDirectionsMap ["d"] = MapDirectionsMap ["down"];
} // end of LoadMapDirections
void CMUSHclientDoc::OnGameMapper()
{
CMapDlg dlg;
dlg.m_bEnable = m_bMapping;
dlg.m_bRemoveMapReverses = m_bRemoveMapReverses;
dlg.m_pDoc = this;
dlg.m_bMapFailureRegexp = m_bMapFailureRegexp;
dlg.m_strMappingFailure = m_strMappingFailure;
// if they open the mapper with no path, assume they want to start one
if (m_strMapList.IsEmpty ())
dlg.m_bEnable = true;
// what I am worried about here is that they will do a special move while
// the map dialog is open, and that the error message won't be detected,
// thus, I will force mapping on while we look at the dialog, so that any
// moves made within it are removed from the list if they cause an error
bool bSave = m_bMapping;
m_bMapping = true;
if (dlg.DoModal () != IDOK)
{
m_bMapping = bSave;
// check for the case where they open the map dialog box, see enabled is checked
// then press Cancel. This would in fact not enable it, so we should warn them.
if (!m_bMapping && m_strMapList.IsEmpty ())
if (::TMessageBox ("Warning - mapping has not been turned on because you pressed 'Cancel'.\n\n"
"Do you want mapping enabled now?", MB_ICONQUESTION | MB_YESNO) == IDYES)
m_bMapping = true;
return;
}
m_bMapping = dlg.m_bEnable;
m_bRemoveMapReverses = dlg.m_bRemoveMapReverses;
// document is modified if they have changed these
if (m_bMapFailureRegexp != dlg.m_bMapFailureRegexp ||
m_strMappingFailure != dlg.m_strMappingFailure)
SetModifiedFlag ();
m_bMapFailureRegexp = dlg.m_bMapFailureRegexp;
m_strMappingFailure = dlg.m_strMappingFailure;
if (m_bMapFailureRegexp && !m_strMappingFailure.IsEmpty ())
{
try
{
m_MapFailureRegexp = regcomp (m_strMappingFailure, (m_bUTF_8 ? PCRE_UTF8 : 0));
}
catch (CException* e)
{
char sMessage [1000];
e->GetErrorMessage (sMessage, sizeof sMessage);
::UMessageBox (TFormat ("Error \"%s\" processing mapping failure regular expression \"%s\"",
sMessage,
(LPCTSTR) m_strMappingFailure));
e->Delete ();
m_bMapFailureRegexp = FALSE;
m_MapFailureRegexp = NULL;
} // end of catch
} // end compiling regular expression
} // end of CMUSHclientDoc::OnGameMapper
void CMUSHclientDoc::OnUpdateGameMapper(CCmdUI* pCmdUI)
{
DoFixMenus (pCmdUI); // remove accelerators from menus
pCmdUI->Enable ();
pCmdUI->SetCheck (m_bMapping);
} // end of CMUSHclientDoc::OnUpdateGameMapper
void CMUSHclientDoc::AddToMap (CString str)
{
MapDirectionsIterator i;
str.MakeLower ();
str.TrimLeft ();
int iNewline;
while ((iNewline = str.Find (ENDLINE)) != -1)
{
CString strDirection = str.Left (iNewline);
// see if they have sent a "direction"
i = MapDirectionsMap.find ((LPCTSTR) strDirection);
// if not, give up
if (i != MapDirectionsMap.end ())
{
// Remove reverses ...
// If they have gone, for example, N then S, then the net result
// is that they haven't gone anywhere, so remove the N rather than adding the S
// We let them disable this because sometimes rooms are not symmetrical, and
// you might go North, and then South, and *not* end up where you were.
if (m_bRemoveMapReverses &&
!m_strMapList.IsEmpty ())
{
CString strComment = m_strMapList.GetTail ();
// if last thing was comment get it and try second last thing
if (strComment.GetLength () >= 2 &&
strComment.Left (1) == "{" &&
strComment.Right (1) == "}" &&
m_strMapList.GetCount () > 1)
m_strMapList.RemoveTail ();
else
strComment.Empty ();
// if this direction had a non-standard reverse, extract that
CString strLast = m_strMapList.GetTail ();
int iSlashPos = strLast.Find ("/");
if (iSlashPos != -1)
{
strLast = strLast.Mid (iSlashPos + 1); // this is the reverse direction
MapDirectionsIterator i2;
i2 = MapDirectionsMap.find ((LPCTSTR) strLast); // convert "west" to "w"
// if this is the reverse direction, remove the earlier one rather than
// adding this one
if (i2 != MapDirectionsMap.end () &&
i2->second.m_sDirectionToLog == i->second.m_sDirectionToLog)
m_strMapList.RemoveTail ();
else
m_strMapList.AddTail (i->second.m_sDirectionToLog.c_str ());
}
else if (m_strMapList.GetTail () == i->second.m_sReverseDirection.c_str ())
m_strMapList.RemoveTail ();
else
{
// different direction? Put comment back if we had one
if (!strComment.IsEmpty ())
m_strMapList.AddTail (strComment);
// and add the new direction
m_strMapList.AddTail (i->second.m_sDirectionToLog.c_str ());
}
}
else
m_strMapList.AddTail (i->second.m_sDirectionToLog.c_str ());
// update status line
DrawMappingStatusLine ();
} // end of finding the direction
str = str.Mid (iNewline + strlen (ENDLINE));
} // end of breaking the text into lines
} // end of CMUSHclientDoc::AddToMap
void CMUSHclientDoc::OnGameDomapperspecial()
{
CMapMoveDlg dlg;
dlg.m_bSendToMUD = true;
dlg.m_strAction = m_strSpecialForwards;
dlg.m_strReverse = m_strSpecialBackwards;
if (dlg.DoModal () != IDOK)
return;
// remember it in case they misspelt it and want to try again
m_strSpecialForwards = dlg.m_strAction;
m_strSpecialBackwards = dlg.m_strReverse;
if (dlg.m_bSendToMUD && !dlg.m_strAction.IsEmpty ())
{
// don't log it twice (eg. if they enter "ne")
bool bSave = m_bMapping;
m_bMapping = false;
SendMsg (dlg.m_strAction, m_display_my_input, false, LoggingInput ());
m_bMapping = bSave;
}
CString strDirection;
strDirection = dlg.m_strAction + "/" + dlg.m_strReverse;
m_strMapList.AddTail (strDirection);
// update status line
DrawMappingStatusLine ();
}
void CMUSHclientDoc::OnUpdateGameDomapperspecial(CCmdUI* pCmdUI)
{
DoFixMenus (pCmdUI); // remove accelerators from menus
pCmdUI->Enable (m_bMapping);
}
void CMUSHclientDoc::OnGameDomappercomment()
{
CMapCommentDlg dlg;
if (dlg.DoModal () != IDOK)
return;
CString str = "{";
str += dlg.m_strComment;
str += "}";
m_strMapList.AddTail (str);
// update status line
DrawMappingStatusLine ();
}
void CMUSHclientDoc::OnUpdateGameDomappercomment(CCmdUI* pCmdUI)
{
DoFixMenus (pCmdUI); // remove accelerators from menus
pCmdUI->Enable (m_bMapping);
}
#define OUTPUT_PREVIOUS_ONE \
do { \
if (strLastDir.GetLength () > 1) \
strLastDir = "(" + strLastDir + ")"; \
if (iCount == 1) \
strForwards += strLastDir + " "; \
else if (iCount > 1) \
strForwards += CFormat ("%i%s ", iCount, (LPCTSTR) strLastDir); \
strLastDir = str; \
iCount = 1; \
} while (false)
CString CMUSHclientDoc::CalculateSpeedWalkString (const bool bOmitComments)
{
CString str;
CString strLastDir;
CString strForwards;
int iCount = 0;
for (POSITION pos = m_strMapList.GetHeadPosition (); pos; )
{
// get next direction from list
str = m_strMapList.GetNext (pos);
if (str.GetLength () > 2 &&
str.Left (1) == "{" &&
str.Right (1) == "}")
{
OUTPUT_PREVIOUS_ONE; // output directions up to comment
if (!bOmitComments)
{
if (!strForwards.IsEmpty ())
strForwards += ENDLINE; // put comments on their own lines
strForwards += str;
strForwards += ENDLINE;
}
strLastDir.Empty ();
iCount = 0;
}
else // not comment
// if same as before, count them
if (str == strLastDir && iCount <= 98)
iCount++;
else
OUTPUT_PREVIOUS_ONE;
} // end of processing each direction
// output final one
OUTPUT_PREVIOUS_ONE;
return strForwards;
} // end of CMUSHclientDoc::CalculateSpeedWalkString
void CMUSHclientDoc::DrawMappingStatusLine (void)
{
CString strSpeedWalk = CalculateSpeedWalkString (true); // omit comments
if (!strSpeedWalk.IsEmpty ())
{
if (strSpeedWalk.GetLength () > 50)
strSpeedWalk = strSpeedWalk.Left (50) + " ...";
Frame.SetStatusMessageNow (TFormat ("Mapper: %s", (LPCTSTR) strSpeedWalk));
m_tStatusDisplayed = CTime::GetCurrentTime ();
m_bShowingMapperStatus = true;
}
} // end of CMUSHclientDoc::DrawMappingStatusLine