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

#include <fsCore/src/fsNCoreUtils.h>
#include <fsCore/src/fsNMaths.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include <fsAppCore/src/components/fsCNameComponent.h>


void gfCJigsawCluster::loadLink(gfCJigsawMinigameComponent*& pLink, const fsStr& pLoadStr)
{
	if ("noLink" != pLoadStr)
	{
		pLink = pLink->namedSiblingsGet(pLoadStr);
	}
}

fsStr gfCJigsawCluster::saveLink(gfCJigsawMinigameComponent* const pLink)
{
	if (pLink)
	{
		return fsCNameComponent::get(pLink)->nameSansPrefixGet();
	}
	return "noLink";
}

void gfCJigsawCluster::clustersRelink(gfCJigsawMinigameComponent* const pPiece)
{
	std::vector<gfCJigsawMinigameComponent*> clusterRoots;
	auto siblings = pPiece->siblingssGet();

	std::for_each(siblings.begin(), siblings.end(),
	              [&clusterRoots](gfCJigsawMinigameComponent* jigComp)
	              {
		              if (clusterRoots.end() == std::find(clusterRoots.begin(), clusterRoots.end(), jigComp))
		              {
			              clusterRoots.push_back(jigComp);
		              }
	              });

	std::for_each(clusterRoots.begin(), clusterRoots.end(),
	              [](gfCJigsawMinigameComponent* jigComp)
	              {
		              clusterFncExecute(jigComp, &gfCJigsawMinigameComponent::relink);
	              });
}

void gfCJigsawCluster::clusterCreate(gfCJigsawMinigameComponent* const pPiece)
{
	auto siblings = pPiece->siblingssGet();
	std::vector<gfCJigsawMinigameComponent*> newCluster;

	gfCJigsawMinigameComponent* currClusterPiece = rootGet(pPiece);

	while (currClusterPiece)
	{
		std::for_each(siblings.begin(), siblings.end(),
			[currClusterPiece, &newCluster](gfCJigsawMinigameComponent* currSibling)
			{
				if (!inMyClusterGet(currClusterPiece, currSibling))
				{
					const fsBool inClusterAlreadyFound = newCluster.end() != std::find_if(
					  newCluster.begin(), newCluster.end(),
					  [currSibling](gfCJigsawMinigameComponent* clusterMember)
						{
						  return inMyClusterGet(clusterMember, currSibling);
						});

					if (!inClusterAlreadyFound && piecesCloseEnoughToCluster(currClusterPiece, currSibling))
					{
						newCluster.push_back(currSibling);
					}
				}
			});

		currClusterPiece = currClusterPiece->mNextInCluster;
	}

	if (!newCluster.empty())
	{
		for (auto curr = newCluster.begin(); curr != newCluster.end(); ++curr)
		{
			gfCJigsawMinigameComponent* myTail = tailOfClusterGet(pPiece);
			gfCJigsawMinigameComponent* yourRoot = rootGet(*curr);
			yourRoot->mPrevInCluster = myTail;
			myTail->mNextInCluster = yourRoot;
		}

		clusterFncExecute(pPiece, &gfCJigsawMinigameComponent::relink);
	}

	// Everything is clustered so move home and complete.
	if (clusterSizeGet(pPiece) == siblings.size())
	{
		std::for_each(siblings.begin(), siblings.end(),
		              [](gfCJigsawMinigameComponent* sibling)
		              {
			              sibling->completionFinalise(false);
		              });
	}
}

fsBool gfCJigsawCluster::piecesCloseEnoughToCluster(gfCJigsawMinigameComponent* const pClusterPiece,
	gfCJigsawMinigameComponent* const pCandidate)
{
	const fsPoint delta = pCandidate->parentGet()->posGet() - pClusterPiece->parentGet()->posGet();
	const fsPoint homeDelta = pCandidate->mHomePosition.originalHomeGet() - pClusterPiece->mHomePosition.originalHomeGet();

	const fsS32 width = gfCJigsawMinigameComponent::mBaseSize.x;
	const fsS32 height = gfCJigsawMinigameComponent::mBaseSize.y;

	if (!pClusterPiece->mInHomePosition && !pCandidate->mInHomePosition &&
		pClusterPiece->mCurrRotStep == pCandidate->mCurrRotStep &&
		(fsNMaths::absS32(delta.x) <= width && fsNMaths::absS32(delta.y) <= height) &&
        (fsNMaths::absS32(homeDelta.x) <= width && fsNMaths::absS32(homeDelta.y) <= height) &&
		(fsNMaths::absS32(homeDelta.x) <= width / 2 || fsNMaths::absS32(homeDelta.y) <= height / 2)
        )
	{
		gfCJigsawMinigameComponent* currClusterPiece = rootGet(pCandidate);<--- Variable 'currClusterPiece' can be declared as pointer to const

		while (currClusterPiece)
		{
			currClusterPiece->positionRelativeTo(pClusterPiece);
			currClusterPiece = currClusterPiece->mNextInCluster;
		}
		return true;
	}	
	return false;
}

void gfCJigsawCluster::clusterMoveFncExecute(gfCJigsawMinigameComponent* const pPiece, fsTClusterMemMoveFnc pMemMoveFnc,
                                             const fsPoint& pPos)
{
	gfCJigsawMinigameComponent* curr = rootGet(pPiece);
	while (curr)
	{
		CALL_MEMBER_FNC(curr, pMemMoveFnc)(pPos);
		curr = curr->mNextInCluster;
	}
}

void gfCJigsawCluster::clusterFncExecute(gfCJigsawMinigameComponent* const pPiece, fsTClusterMemFnc pMemFnc)
{
	gfCJigsawMinigameComponent* curr = rootGet(pPiece);
	while (curr)
	{
		CALL_MEMBER_FNC(curr, pMemFnc)();
		curr = curr->mNextInCluster;
	}
}

fsBool gfCJigsawCluster::inMyClusterGet(gfCJigsawMinigameComponent* const pPiece,
                                        gfCJigsawMinigameComponent* const pCandidate)<--- Parameter 'pCandidate' can be declared as pointer to const
{
	gfCJigsawMinigameComponent* curr = rootGet(pPiece);<--- Variable 'curr' can be declared as pointer to const
	while (curr)
	{
		if (curr == pCandidate)
		{
			return true;
		}
		curr = curr->mNextInCluster;
	}
	return false;
}

gfCJigsawMinigameComponent* const gfCJigsawCluster::tailOfClusterGet(gfCJigsawMinigameComponent* const pPiece)
{
	gfCJigsawMinigameComponent* tail = pPiece;
	if (pPiece->mNextInCluster)
	{
		tail = pPiece->mNextInCluster;
		while (tail->mNextInCluster)
		{
			tail = tail->mNextInCluster;
		}
	}
	return tail;
}

gfCJigsawMinigameComponent* const gfCJigsawCluster::rootGet(gfCJigsawMinigameComponent* const pPiece)
{
	gfCJigsawMinigameComponent* root = pPiece;
	if (pPiece->mPrevInCluster)
	{
		root = pPiece->mPrevInCluster;
		while (root->mPrevInCluster)
		{
			root = root->mPrevInCluster;
		}
	}
	return root;
}

fsS32 gfCJigsawCluster::clusterSizeGet(gfCJigsawMinigameComponent* const pPiece)
{
	gfCJigsawMinigameComponent* curr = rootGet(pPiece);<--- Variable 'curr' can be declared as pointer to const
	fsS32 size = 0;
	while (curr)
	{
		++size;
		curr = curr->mNextInCluster;
	}
	return size;
}