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
#include "fsIRunningOrder.h"

#include <algorithm>
#include <set>

#include <fsCore/fsStr.h>
#include <fsCore/fsIFile.h>
#include <fsCore/fsIResourceNamePathAdjuster.h>
#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/src/fsCAppSettings.h>

#include "fsCRunningOrderGrammar.h"
#include "src/fsCAppCoreMain.h"
#include "src/components/gui/fsCLocaleOptions.h"
#include "src/components/gui/fsIGuiComponent.h"
#include "src/fsILevelBasedAppCoreMain.h"

const fsStr fsIRunningOrder::mMapDestinationPrefix("map->");

void fsIRunningOrder::replace(const std::vector<fsStr>& pNewOrder)
{
	replaceDo(pNewOrder);
}

fsS32 fsIRunningOrder::levelCountGet() const
{
	return mSequence.size();
}

fsS32 fsIRunningOrder::currLevelPosGet() const
{
	return currentLevelIteratorGet(mCurrentLocation) - mSequence.begin();
}

const fsStr& fsIRunningOrder::firstLocationGet() const
{
	return mSequence.front();
}

void fsIRunningOrder::nextLocationClear()
{
	mCurrentLocation = mNextLocation;
	mNextLocation = "";
}

fsBool fsIRunningOrder::nextLocationReady() const
{
	return !nextLocationGet().emptyGet();
}

fsStr fsIRunningOrder::nextLocationGet(const fsStr& pCurrentLocation) const
{
	return nextLocationGetDo(pCurrentLocation);
}

fsStr fsIRunningOrder::nextLocationGetDo(const fsStr& pCurrentLocation) const
{
	fsStr location = pCurrentLocation;
#if defined fsDownCaseFilename
	location.downcase();
#endif
	auto curr = currentLevelIteratorGet(location);
	++curr;

	fsStr nextLev = *curr;
	return nextLev;
}

std::vector<fsStr>::const_iterator fsIRunningOrder::currentLevelIteratorGet(const fsStr& pCurrentLocation) const
{
	return currentLevelIteratorGetDo(pCurrentLocation);
}

std::vector<fsStr>::const_iterator fsIRunningOrder::currentLevelIteratorGetDo(const fsStr& pCurrentLocation) const
{
	return std::find_if(
		mSequence.begin(), mSequence.end(),
		[&](const fsStr lev)
		{
			return pCurrentLocation == lev;
		});
}

void fsIRunningOrder::levelCompleteSet()
{
	if (mNextLocation.emptyGet())
	{
		auto curr = currentLevelIteratorGet(mCurrentLocation);
		++curr;
		mCurrentLocation = *curr;
		nextLocationSet(mCurrentLocation);
	}
}

void fsIRunningOrder::singleLevel()
{
	mCurrentLocation = mNextLocation;
}

void fsIRunningOrder::gameContinue()
{
	gameContinueDo();
}

void fsIRunningOrder::newGameStart()
{
	newGameStartDo();
}

void fsIRunningOrder::newGameStartDo()
{
	nextLocationSet(mSequence.front());
	mCurrentLocation = mNextLocation;
}

const fsStr& fsIRunningOrder::nextLocationGet() const
{
	return mNextLocation;
}

void fsIRunningOrder::nextLocationSet(const fsStr& pNextLocation)
{
	nextLocationSetDo(pNextLocation);
}

void fsIRunningOrder::nextLocationSetDo(const fsStr& pNextLocation)
{
	mNextLocation = pNextLocation;
}

fsBool fsIRunningOrder::loopsCheck() const
{
	std::set<fsStr> loops(mSequence.begin(), mSequence.end());
	return mSequence.size() != loops.size();
}

void fsIRunningOrder::sequenceCheck()
{
	sequenceCheckDo();
	mHasLoops = loopsCheck();
	fsAssert(!mHasLoops, "Running order has loops");
}

void fsIRunningOrder::sequenceCheckDo() const
{
	std::for_each(mSequence.begin(), mSequence.end(),
	              [](fsStr level)
	              {
		              fsAssert(
			              fsIFile::fileExistsGet(fsCResourceName(
								fsILevelBasedAppCoreMain::scenesFolderGet()
								+ fsIResourceNamePathAdjuster::mPathSeparator
								+ level
								+ fsIResourceNamePathAdjuster::mPathSeparator
								+ level
								+ ".txt",
				              fsCResourceName::eRES_LOCATION_ASSETS)),
			              "runningOrder broken, no such level: %s", level.utf8Get());
	              });
}

const VarTableMap* fsIRunningOrder::resolvedSceneConfigGet(const fsStr& pSceneName) const
{
	const auto found = mResolvedConfigs.find(pSceneName);
	return found == mResolvedConfigs.end() ? nullptr : &found->second;
}

void fsIRunningOrder::guiSequenceLoad(const fsCResourceName& pResolvedFile, fsBool pMapNavigationUsed)
{
	const fsStr text = fsIFile::strGetFromFile(pResolvedFile);

	const fsStr flavour =
		fsCAppCoreMain::instanceGet()->settingsGet()->buildFlavourGet() == "CE" ? "collectors" : "standard";
	const fsStr language = fsCAppCoreMain::instanceGet()->localeGet()->localeCodeGet();

	const std::vector<fsCRunningOrderGrammar::ResolvedScene> resolved =
		fsCRunningOrderGrammar::resolve(text, flavour, language);

	for (const fsCRunningOrderGrammar::ResolvedScene& scene : resolved)
	{
		// Reproduce the legacy list: bare scene name, re-adding the map->
		// prefix only for HOG map-first scenes so the subclass's existing
		// full-path/navigation code sees exactly what runningOrder.txt gave it.
		const fsBool mapFirst = pMapNavigationUsed && scene.mGoesToMap;
		mSequence.push_back(mapFirst ? mMapDestinationPrefix + scene.mName : scene.mName);
		mResolvedConfigs[scene.mName] = scene.mConfig;
	}
}

fsIRunningOrder::fsIRunningOrder(const fsCResourceName& pFilename, fsBool pMapNavigationUsed):
	mNextLocation("")
{
	// Extension dispatch, reusing the GUI resolver: runningOrder.gui present ->
	// parse+resolve+filter; otherwise the legacy bare-list read.
	fsStr resolvedName;
	mGuiMode = fsIGuiComponent::newGrammarFileNameResolve(pFilename.nameGet(), resolvedName);
	if (mGuiMode)
	{
		guiSequenceLoad(fsCResourceName(resolvedName, pFilename.locationGet()), pMapNavigationUsed);
		return;
	}

	fsStr levelSequence = fsIFile::strGetFromFile(pFilename);
	fsStr level = levelSequence.chomp("\n");
	while (!level.emptyGet())
	{
		mSequence.push_back(level);
		level = levelSequence.chomp("\n");
	}
}

fsIRunningOrder::~fsIRunningOrder()
{
}