wavelength 1.0.0
Loading...
Searching...
No Matches
gamelogic.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5
6#include "game/btnevent.h"
7#include "game/catchtrack.h"
11#include "game/localplayer.h"
12#include "game/player.h"
13#include "game/playmap.h"
14#include "game/playnoteevent.h"
15#include "game/rotateevent.h"
16#include "game/song.h"
17#include "game/stickevent.h"
18#include "game/track.h"
19#include "game/worldbeat.h"
20#include "game/worldlogic.h"
21#include "math/rand.h"
22#include "os/command.h"
23#include "os/ptr.h"
24#include "os/ramp.h"
25#include "os/scheduler.h"
26#include "script/dataarray.h"
27#include "synth/songspeed.h"
28
39class GameLogic : public WorldLogic {
40public:
52
64
70 class SpeedRamp : public Ramp {
71 public:
78 SpeedRamp(Scheduler *pScheduler, float fSpeed) : Ramp(pScheduler, fSpeed) {
79 }
80
87 ~SpeedRamp() override {
88 }
89
98 void Apply(float fValue, [[maybe_unused]] int nTick) override {
99 SetSongSpeed(fValue);
100 }
101 };
102
109 public:
117 explicit PlayerData(Command *pEndFreestyle);
118
121 return mEndFreestyleCmd;
122 }
123
125 int GetNextPhraseBar() const {
126 return mNextPhraseBar;
127 }
128
134 void SetNextPhraseBar(int nBar) {
135 mNextPhraseBar = nBar;
136 }
137
140 return mLastNextPhraseBar;
141 }
142
148 void SetLastNextPhraseBar(int nBar) {
149 mLastNextPhraseBar = nBar;
150 }
151
153 int GetMissedBar() const {
154 return mMissedBar;
155 }
156
162 void SetMissedBar(int nBar) {
163 mMissedBar = nBar;
164 }
165
167 int GetSavedStreak() const {
168 return mSavedStreak;
169 }
170
176 void SetSavedStreak(int nStreak) {
177 mSavedStreak = nStreak;
178 }
179
181 int GetCapturedTrack() const {
182 return mCapturedTrack;
183 }
184
190 void SetCapturedTrack(int nTrack) {
191 mCapturedTrack = nTrack;
192 }
193
194 private:
195 Ptr<Command> mEndFreestyleCmd;
196 int mNextPhraseBar;
197 int mLastNextPhraseBar;
198 int mMissedBar;
199 int mSavedStreak;
200 int mCapturedTrack;
201 };
202
216 GameLogic(Song *pSong, DataArray *pConfig, int nSeed);
217
224 ~GameLogic() override;
225
232 void Start() override;
233
240 void Stop() override;
241
249 bool IsFinished() const override;
250
258 int HasQuit() const override;
259
267 int IsRestartRequested() const override;
268
277 bool IsFreestyling(int nPlayer) override;
278
288 void Poll() override;
289
297 float GetProgress() override;
298
306 void HandleInput(const RotateEvent &event) override;
307
315 void HandleInput(const PlayNoteEvent &event) override;
316
324 void HandleInput(const BtnEvent<3> &event) override;
325
333 void HandleInput(const BtnEvent<4> &event) override;
334
342 void HandleInput(const BtnEvent<5> &event) override;
343
351 void HandleInput([[maybe_unused]] const ChangeSectionEvent &event) override {
352 }
353
361 void HandleInput([[maybe_unused]] const BtnEvent<9> &event) override {
362 }
363
371 void HandleInput(const BtnEvent<10> &event) override;
372
380 void HandleInput([[maybe_unused]] const BtnEvent<8> &event) override {
381 }
382
390 void HandleInput(const StickEvent<2> &event) override;
391
399 void HandleInput(const StickEvent<6> &event) override;
400
410 void SetPaused(bool bPaused, int nPad, int nReason) override;
411
419 bool IsPlaying() const override {
420 return mState == kStatePlaying;
421 }
422
431 virtual bool DeploySlowdown(Player *pPlayer);
432
441 virtual bool DeployFreestyle(Player *pPlayer);
442
451 virtual bool DeployBumper([[maybe_unused]] Player *pPlayer) {
452 return false;
453 }
454
463 virtual bool DeployCrippler([[maybe_unused]] Player *pPlayer) {
464 return false;
465 }
466
474 virtual void EndFreestyle(Player *pPlayer);
475
477 virtual void OnStart() = 0;
478
484 virtual void OnFinish(bool bWon) = 0;
485
491 virtual void OnBar(int nBar) = 0;
492
494 virtual void OnSection() = 0;
495
504 virtual void OnPhraseCaptured(CatchTrack *pTrack, Player *pPlayer, int nBar, bool bStreak) = 0;
505
511 virtual void OnPhraseEnded(Track *pTrack) = 0;
512
518 virtual void OnPhraseMissed(Track *pTrack) = 0;
519
529
537 bool HasPhraseThisBar() const;
538
548 void EndSong();
549
560 void Finish(bool bWon);
561
572 void CapturePhrase(CatchTrack *pTrack, Player *pPlayer, int nBar, bool bStreak);
573
583 void ContinueStreak(Track *pTrack, int nBar, int nFromBar);
584
592 void EndPhrase(Track *pTrack);
593
601 void MissPhrase(Track *pTrack);
602
610 void EnableNextTracks(int nBar);
611
620 void EnterFreestyle(Player *pPlayer, bool bVictory);
621
630 void LeaveFreestyle(Player *pPlayer, int nTrack);
631
639
647
656 bool DeployAutocatcher(Player *pPlayer);
657
666 bool DeployMultiplier(Player *pPlayer);
667
668protected:
670 int mState;
675 std::vector<std::unique_ptr<Player>> mPlayers;
676 std::vector<LocalPlayer *> mLocalPlayers;
677 std::vector<std::unique_ptr<CatchTrack>> mCatchTracks;
678 std::unique_ptr<FreestyleTrack> mFreestyleTrack;
679 std::unique_ptr<GameTrackSelector> mTrackSelector;
680 std::unique_ptr<SpeedRamp> mSpeedRamp;
682 int mQuit;
683 int mReserved60; // +0x60, reported by IsRestartRequested().
689 int mReserved8c; // +0x8c, not written by the constructor.
692 std::vector<PlayerData> mPlayerData;
704
712 void ApplyFreestyleEffect(int nSet);
713
724 void ShowPowerupText(const char *pszToken, int nPlayer);
725
734 void CreateTracks(int *pFreestyleType, int *pFreestyleInstrument);
735
744 void TickBar();
745
753
761 void ClearNextPhrase(int nPlayer);
762
772 void FindNextPhrase(int nPlayer, int nTrack, int nFromBar);
773
781
789
798 static int GetControllerArgument(DataArray *pCommand);
799
808 static void OnAutocatcherCommand(DataArray *pCommand, void *pUserData);
809
818 static void OnMultiplierCommand(DataArray *pCommand, void *pUserData);
819
828 static void OnSlowCommand(DataArray *pCommand, void *pUserData);
829
838 static void OnFreestyleCommand(DataArray *pCommand, void *pUserData);
839
849 static void OnFreestyleToggleCommand(DataArray *pCommand, void *pUserData);
850
859 static void OnBumperCommand(DataArray *pCommand, void *pUserData);
860
869 static void OnCripplerCommand(DataArray *pCommand, void *pUserData);
870
880 static void OnPowerupCommand(DataArray *pCommand, void *pUserData);
881};
Input event of a button press that names only the player.
Definition btnevent.h:15
Track whose phrases a player captures by playing them.
Definition catchtrack.h:33
Input event that changes the section a player is on.
Definition changesectionevent.h:10
Unit of work a Scheduler runs at a set time.
Definition command.h:13
Reference-counted array of script data nodes.
Definition dataarray.h:20
PlayerData(Command *pEndFreestyle)
Construct the record around the command that ends the freestyle of the player.
void SetLastNextPhraseBar(int nBar)
Set the previous first bar of the phrase to play next.
Definition gamelogic.h:148
int GetCapturedTrack() const
Definition gamelogic.h:181
void SetSavedStreak(int nStreak)
Set the streak of the player when the phrase was found.
Definition gamelogic.h:176
int GetLastNextPhraseBar() const
Definition gamelogic.h:139
void SetMissedBar(int nBar)
Set the bar the player last missed a phrase in.
Definition gamelogic.h:162
void SetCapturedTrack(int nTrack)
Set the track the search for the next phrase skipped.
Definition gamelogic.h:190
int GetNextPhraseBar() const
Definition gamelogic.h:125
void SetNextPhraseBar(int nBar)
Set the first bar of the phrase to play next.
Definition gamelogic.h:134
Ptr< Command > & GetEndFreestyleCmd()
Definition gamelogic.h:120
int GetSavedStreak() const
Definition gamelogic.h:167
int GetMissedBar() const
Definition gamelogic.h:153
void Apply(float fValue, int nTick) override
Play the song at a new speed.
Definition gamelogic.h:98
SpeedRamp(Scheduler *pScheduler, float fSpeed)
Construct the ramp at a speed.
Definition gamelogic.h:78
~SpeedRamp() override
Release the ramp.
Definition gamelogic.h:87
void HandleInput(const BtnEvent< 9 > &event) override
Ignore the event.
Definition gamelogic.h:361
void HandleInput(const PlayNoteEvent &event) override
Pass a note to the player.
int HasQuit() const override
Report mQuit.
static void OnCripplerCommand(DataArray *pCommand, void *pUserData)
Deploy the crippler for a controller on the crippler script command.
static void OnSlowCommand(DataArray *pCommand, void *pUserData)
Turn the slowdown on or off outside an online game on the slow script command.
void ApplyFreestyleEffect(int nSet)
Apply one effect set of the song.
void HandleInput(const ChangeSectionEvent &event) override
Ignore the event.
Definition gamelogic.h:351
void EnterFreestyle(Player *pPlayer, bool bVictory)
Move a player to the freestyle track.
int mSectionStartBar
Definition gamelogic.h:684
int mEndTick
Definition gamelogic.h:696
void EnableNextTracks(int nBar)
Enable the catch tracks of the next step of the song's enable order.
virtual void OnPhraseMissed(Track *pTrack)=0
Act once a player missed a phrase.
bool IsFreestyling(int nPlayer) override
Report whether a player is on the freestyle track.
void Stop() override
Stop the song, the tracks, and the players.
std::unique_ptr< SpeedRamp > mSpeedRamp
Definition gamelogic.h:680
void HandleInput(const StickEvent< 2 > &event) override
Pass the stick event to the player.
void LeaveFreestyle(Player *pPlayer, int nTrack)
Move a player from the freestyle track to a catch track and find the next phrase.
~GameLogic() override
Stop the song and release the players and the tracks.
std::vector< PlayerData > mPlayerData
Definition gamelogic.h:692
void CapturePhrase(CatchTrack *pTrack, Player *pPlayer, int nBar, bool bStreak)
Record the capture of a phrase and find the next phrase of each local player.
void Finish(bool bWon)
Stop the players at the end of the song.
virtual void OnPhraseEnded(Track *pTrack)=0
Act once a phrase of a track ended.
std::vector< std::unique_ptr< CatchTrack > > mCatchTracks
Definition gamelogic.h:677
float GetProgress() override
Report how far the song has played.
virtual void OnFinish(bool bWon)=0
Act once Finish() stopped the players.
int mSection
Definition gamelogic.h:686
void TickBar()
Count one bar, starting the next section when it begins.
void SetPaused(bool bPaused, int nPad, int nReason) override
Pause or resume the song, the force feedback, and the controllers.
int mNumBars
Definition gamelogic.h:673
int mNextSectionBar
Definition gamelogic.h:685
std::vector< LocalPlayer * > mLocalPlayers
Definition gamelogic.h:676
void FindNextPhrase(int nPlayer, int nTrack, int nFromBar)
Find the first phrase on another catch track at or after a bar, and point the player at it.
State
Values of mState.
Definition gamelogic.h:42
@ kStateRunning
Definition gamelogic.h:48
@ kStateSuspended
Definition gamelogic.h:46
@ kStateFinished
Definition gamelogic.h:50
@ kStateEnding
Definition gamelogic.h:49
@ kStateIdle
Definition gamelogic.h:43
@ kStatePlaying
Definition gamelogic.h:44
@ kStateWon
Definition gamelogic.h:47
@ kStatePaused
Definition gamelogic.h:45
void StopSlowdown()
Return the song to its speed after StartSlowdown().
static int GetControllerArgument(DataArray *pCommand)
Report the controller argument of a script command.
void HandleInput(const BtnEvent< 10 > &event) override
Pass the button to the player.
Rand mRand
Definition gamelogic.h:695
WorldBeat mWorldBeat
Definition gamelogic.h:687
void ClearNextPhrase(int nPlayer)
Forget the next phrase of a player.
void HandleInput(const StickEvent< 6 > &event) override
Pass the stick event to the player.
int mState
Definition gamelogic.h:670
bool IsFreestyleTrackActive() const
Report whether a player is on the freestyle track while the song plays.
static void OnPowerupCommand(DataArray *pCommand, void *pUserData)
Give a power-up to the player of a controller on the powerup script command while the power-up cheat ...
void StartSlowdown()
Slow the song down.
GameLogic(Song *pSong, DataArray *pConfig, int nSeed)
Construct the logic of a song.
std::unique_ptr< FreestyleTrack > mFreestyleTrack
Definition gamelogic.h:678
virtual bool DeployBumper(Player *pPlayer)
Refuse a bumper.
Definition gamelogic.h:451
int mQuit
Definition gamelogic.h:682
void HandleInput(const BtnEvent< 5 > &event) override
Deploy the power-up of a player and log it.
void PlacePowerups()
Place the power-ups of every section in the busiest bars of the catch tracks.
virtual void OnBar(int nBar)=0
Act at the start of every bar.
virtual void OnStart()=0
Act once Start() finished.
virtual void EndFreestyle(Player *pPlayer)
Move a player from the freestyle track back to a catch track.
void AdvanceSection()
Start the next section.
Ptr< Command > mBarCmd
Definition gamelogic.h:693
int mSavedState
Definition gamelogic.h:688
void AssignTracks()
Assign each player to a random catch track.
int mPaused
Definition gamelogic.h:671
static void OnAutocatcherCommand(DataArray *pCommand, void *pUserData)
Deploy the autocatcher for a controller on the autocatcher script command.
void HandleInput(const BtnEvent< 3 > &event) override
Pause the song from the controller of the player.
void Start() override
Start the song, the tracks, the players, and the bar counting.
void ContinueStreak(Track *pTrack, int nBar, int nFromBar)
Lengthen or reset the streak of the player of a track and find the next phrase.
int mTicksPerBar
Definition gamelogic.h:672
bool DeployMultiplier(Player *pPlayer)
Raise the multiplier of a player.
bool IsFinished() const override
Report whether Stop() ran.
void HandleInput(const RotateEvent &event) override
Move a playing player to the next or previous catch track.
bool DeployAutocatcher(Player *pPlayer)
Capture a phrase of the track of a player and pulse the controllers of every player on it.
void CreateTracks(int *pFreestyleType, int *pFreestyleInstrument)
Create the catch tracks and the freestyle track of the song.
static void OnFreestyleToggleCommand(DataArray *pCommand, void *pUserData)
Move the player of a controller onto or off the freestyle track on the freestyle_toggle script comman...
std::vector< std::unique_ptr< Player > > mPlayers
Definition gamelogic.h:675
void HandleInput(const BtnEvent< 8 > &event) override
Ignore the event.
Definition gamelogic.h:380
bool HasPhraseThisBar() const
Report whether a catch track has a phrase in the current bar.
Song * mSong
Definition gamelogic.h:669
int mReserved60
Definition gamelogic.h:683
static void OnBumperCommand(DataArray *pCommand, void *pUserData)
Deploy the bumper for a controller on the bumper script command.
void HandleInput(const BtnEvent< 4 > &event) override
Toggle the display option of a single controller and save it in the profile.
virtual bool DeployCrippler(Player *pPlayer)
Refuse a crippler.
Definition gamelogic.h:463
Powerup
Kinds of power-up a player can hold.
Definition gamelogic.h:54
@ kPowerupAutocatcher
Definition gamelogic.h:56
@ kPowerupMultiplier
Definition gamelogic.h:57
@ kPowerupFreestyle
Definition gamelogic.h:59
@ kPowerupCount
Definition gamelogic.h:62
@ kPowerupSlowdown
Definition gamelogic.h:58
@ kPowerupNone
Definition gamelogic.h:55
@ kPowerupBumper
Definition gamelogic.h:60
@ kPowerupCrippler
Definition gamelogic.h:61
virtual void OnSection()=0
Act at the start of every section.
int IsRestartRequested() const override
Report whether the song is to start again once it ends.
static void OnFreestyleCommand(DataArray *pCommand, void *pUserData)
Deploy the freestyle for a controller on the freestyle script command.
void Poll() override
Stop the song once the ending finished.
void EndPhrase(Track *pTrack)
Report the end of a phrase to the triggers and to OnPhraseEnded().
int mSlowdown
Definition gamelogic.h:690
void EndSong()
Start the ending of the song.
Ptr< Command > mStopSlowdownCmd
Definition gamelogic.h:694
int mNextEnableStep
Definition gamelogic.h:691
std::unique_ptr< GameTrackSelector > mTrackSelector
Definition gamelogic.h:679
static void OnMultiplierCommand(DataArray *pCommand, void *pUserData)
Deploy the multiplier for a controller on the multiplier script command.
int mReserved8c
Definition gamelogic.h:689
PlayMap * mPlayMap
Definition gamelogic.h:674
int mPhraseThisBar
Definition gamelogic.h:681
virtual void OnPhraseCaptured(CatchTrack *pTrack, Player *pPlayer, int nBar, bool bStreak)=0
Act once CapturePhrase() found the next phrases.
bool IsPlaying() const override
Report whether the song is playing.
Definition gamelogic.h:419
void PlaceRandomPowerups()
Place a random power-up in every bar of every catch track.
virtual bool DeployFreestyle(Player *pPlayer)
Move a player to the freestyle track for the configured number of bars.
virtual bool DeploySlowdown(Player *pPlayer)
Slow the song down for a player.
void MissPhrase(Track *pTrack)
Record a missed phrase and report it to the triggers and to OnPhraseMissed().
void ShowPowerupText(const char *pszToken, int nPlayer)
Show the localised text of a power-up.
Map from the bars a song plays to the bars of the song as written, with the loops that repeat stretch...
Definition playmap.h:11
Input event that plays a note on one of the three gem buttons.
Definition playnoteevent.h:10
One participant in a song: the track the participant plays, the score, and the streak.
Definition player.h:19
Owner of one reference to an Attachment.
Definition ptr.h:16
Ramp(Scheduler *pScheduler, float fValue)
Construct a ramp that holds a value.
Generator of pseudo-random numbers with a lagged Fibonacci sequence of 256 words.
Definition rand.h:8
Input event that rotates a player's view of the tracks by one track.
Definition rotateevent.h:11
Queue of commands that run when the song clock reaches their tick.
Definition scheduler.h:20
One song: its configuration from the song text file and the tracks its MIDI file builds.
Definition song.h:38
Input event of an analog stick position.
Definition stickevent.h:15
One instrument track of the song that a player can play.
Definition track.h:18
Player of the events of the track named "WORLD", which runs each event at its tick.
Definition worldbeat.h:16
WorldLogic()
Construct the logic and give the input of every controller to the world.
void SetSongSpeed(float fSpeed)
Play the song at a speed.