Fission FSN
Loading...
Searching...
No Matches
gfIMinigameNextMove.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: gfIMinigameNextMove.h
4*
5* Purpose: Interface for minigames that can say how their next move is made
6*
7*********************************************************************/
8
9#ifndef gfIMinigameNextMovehdr
10#define gfIMinigameNextMovehdr
11
12#include <algorithm>
13#include <functional>
14#include <vector>
15
16#include <fsCore/fsBaseTypes.h>
17#include <fsCore/fsPoint.h>
18#include <fsCore/fsStr.h>
19
20class fsCEntity;
21
22
23// All positions are pointer space, which is screen space (screenPositionGet()).
24// The move is made with real pointer events - this only says where and what to expect.
26{
27public:
28
29 struct sMove
30 {
31 enum eKIND
32 {
33 eKIND_NOTHING_TAKEN, // the press picked nothing up
34 eKIND_TAP, // release on the spot
35 eKIND_DRAG // drag to mDropAt
36 };
37
39 // What the press actually picked up - not always what was pressed.
40 fsCEntity* mTakenEnt = nullptr;
42 // Tap: handed back to tapTookEffectGet().
45 // Tap: further taps made once this one is released (swap this, then that).
46 std::vector<fsPoint> mThenTaps;
47 // Tap: the move is done when its taps are, so the verdict counts as an attempt.
48 fsBool mFinal = false;
50 // Drag: why a drop that does not land is a miss.
52 // Drag: points to pass through, in order, on the way to mDropAt (a word that turns).
53 std::vector<fsPoint> mDragVia;
54 };
55
56 // False when this has nothing left to do. The press goes at pAt.
57 virtual fsBool pressGet(fsPoint& pAt) const = 0;
58 // False if pAt misses the thing's own hit area - the fault is then the area, not the input.
59 virtual fsBool pressHitGet(const fsPoint& pAt) const { return true; }
60 // True if only a drag moves it (a tap rotates it, say), so blind input
61 // should be drags too. Everything else is tapped.
62 virtual fsBool dragOnlyGet() const { return false; }
63 // True if blind input in the minigame does harm, so aimed input is all it should get
64 // (a missed tap raises a modal that swallows the presses after it).
65 virtual fsBool aimOnlyGet() const { return false; }
66 // True if the hint button may mark this minigame's next move when it has no hint of its own
67 // (it is not a gfIMinigameHintable). False for one that hints itself some other way.
68 virtual fsBool hintByMoveGet() const { return true; }
69 // Presses a move may take: a rotated piece is tapped upright before it can be dragged.
70 virtual fsS32 pressLimitGet() const { return 1; }
71
72 // Called with the press down and nothing yet released. Which of pPeers took a
73 // press is the dispatcher's call, not draw order, so look at what was taken.
74 virtual sMove moveAfterPress(const std::vector<fsCEntity*>& pPeers, const fsPoint& pPressedAt) const = 0;
75
76 // Did the tap, and any taps after it, change anything?
77 virtual fsBool tapTookEffectGet(const sMove& pMove) const { return true; }
78 // Drags only. Once a drag has dropped: did it land? pOffBy is appended to the miss note.
79 virtual fsBool dropLandedGet(fsCEntity* pTakenEnt, fsStr& pOffBy) const { return false; }
80
81 // The middle of the hit area, found by scanning around pOrigin. A shape's origin can
82 // sit on an edge shared with its neighbour (the neighbour then takes the press) or off
83 // the shape altogether. pOrigin itself if nothing around it is hit.
84 static fsPoint hitCentreGet(const fsPoint& pOrigin, fsS32 pWidth, fsS32 pHeight,
85 const std::function<fsBool(const fsPoint&)>& pHit)
86 {
87 const fsS32 reachW = pWidth;
88 const fsS32 reachH = pHeight;
89 const fsS32 step = std::max(4, std::max(pWidth, pHeight) / 32);
90 fsS64 sumX = 0;
91 fsS64 sumY = 0;
92 fsS64 hits = 0;
93 for (fsS32 y = -reachH; y <= reachH; y += step)
94 {
95 for (fsS32 x = -reachW; x <= reachW; x += step)
96 {
97 if (pHit(fsPoint(pOrigin.x + x, pOrigin.y + y)))
98 {
99 sumX += x;
100 sumY += y;
101 ++hits;
102 }
103 }
104 }
105 if (0 == hits)
106 {
107 return pOrigin;
108 }
109
110 // A shape with a hole has its centroid in the hole - fall back to the nearest hit.
111 const fsS32 cx = static_cast<fsS32>(sumX / hits);
112 const fsS32 cy = static_cast<fsS32>(sumY / hits);
113 fsPoint best(pOrigin.x + cx, pOrigin.y + cy);
114 if (pHit(best))
115 {
116 return best;
117 }
118 fsS64 bestDist = -1;
119 for (fsS32 y = -reachH; y <= reachH; y += step)
120 {
121 for (fsS32 x = -reachW; x <= reachW; x += step)
122 {
123 const fsPoint candidate(pOrigin.x + x, pOrigin.y + y);
124 const fsS64 dist = static_cast<fsS64>(x - cx) * (x - cx) + static_cast<fsS64>(y - cy) * (y - cy);
125 if ((bestDist < 0 || dist < bestDist) && pHit(candidate))
126 {
127 bestDist = dist;
128 best = candidate;
129 }
130 }
131 }
132 return best;
133 }
134
135 // Like hitCentreGet(), but where nothing else would take the press: pieces can stack on
136 // one pivot or span the canvas. Falls back to any point the piece itself is hit at.
137 static fsPoint ownedCentreGet(const fsPoint& pOrigin, fsS32 pWidth, fsS32 pHeight,
138 const std::function<fsBool(const fsPoint&)>& pHit,
139 const std::function<fsBool(const fsPoint&)>& pOtherHit)
140 {
141 const auto ownedGet = [&pHit, &pOtherHit](const fsPoint& pPos) { return pHit(pPos) && !pOtherHit(pPos); };
142 const fsPoint owned = hitCentreGet(pOrigin, pWidth, pHeight, ownedGet);
143 return ownedGet(owned) ? owned : hitCentreGet(pOrigin, pWidth, pHeight, pHit);
144 }
145
147};
148
149#endif
Definition fsCEntity.h:39
Definition fsStr.h:23
Definition gfIMinigameNextMove.h:26
virtual sMove moveAfterPress(const std::vector< fsCEntity * > &pPeers, const fsPoint &pPressedAt) const =0
virtual fsBool pressGet(fsPoint &pAt) const =0
static fsPoint ownedCentreGet(const fsPoint &pOrigin, fsS32 pWidth, fsS32 pHeight, const std::function< fsBool(const fsPoint &)> &pHit, const std::function< fsBool(const fsPoint &)> &pOtherHit)
Definition gfIMinigameNextMove.h:137
virtual ~gfIMinigameNextMove()
Definition gfIMinigameNextMove.h:146
virtual fsBool aimOnlyGet() const
Definition gfIMinigameNextMove.h:65
static fsPoint hitCentreGet(const fsPoint &pOrigin, fsS32 pWidth, fsS32 pHeight, const std::function< fsBool(const fsPoint &)> &pHit)
Definition gfIMinigameNextMove.h:84
virtual fsBool dragOnlyGet() const
Definition gfIMinigameNextMove.h:62
virtual fsBool dropLandedGet(fsCEntity *pTakenEnt, fsStr &pOffBy) const
Definition gfIMinigameNextMove.h:79
virtual fsS32 pressLimitGet() const
Definition gfIMinigameNextMove.h:70
virtual fsBool tapTookEffectGet(const sMove &pMove) const
Definition gfIMinigameNextMove.h:77
virtual fsBool pressHitGet(const fsPoint &pAt) const
Definition gfIMinigameNextMove.h:59
virtual fsBool hintByMoveGet() const
Definition gfIMinigameNextMove.h:68
bool fsBool
Definition fsBaseTypes.h:26
int fsS32
Definition fsBaseTypes.h:20
long long fsS64
Definition fsBaseTypes.h:23
Definition fsPoint.h:17
fsS32 x
Definition fsPoint.h:39
fsS32 y
Definition fsPoint.h:40
Definition gfIMinigameNextMove.h:30
fsPoint mPosBefore
Definition gfIMinigameNextMove.h:44
fsS32 mStateBefore
Definition gfIMinigameNextMove.h:43
std::vector< fsPoint > mThenTaps
Definition gfIMinigameNextMove.h:46
std::vector< fsPoint > mDragVia
Definition gfIMinigameNextMove.h:53
eKIND mKind
Definition gfIMinigameNextMove.h:38
fsStr mTapFailReason
Definition gfIMinigameNextMove.h:49
fsCEntity * mTakenEnt
Definition gfIMinigameNextMove.h:40
fsBool mFinal
Definition gfIMinigameNextMove.h:48
eKIND
Definition gfIMinigameNextMove.h:32
@ eKIND_DRAG
Definition gfIMinigameNextMove.h:35
@ eKIND_NOTHING_TAKEN
Definition gfIMinigameNextMove.h:33
@ eKIND_TAP
Definition gfIMinigameNextMove.h:34
fsPoint mDropAt
Definition gfIMinigameNextMove.h:41
fsStr mDragFailReason
Definition gfIMinigameNextMove.h:51