SpaceCadetPinball/SpaceCadetPinball/TPinballTable.cpp

715 lines
17 KiB
C++
Raw Permalink Normal View History

#include "pch.h"
#include "TPinballTable.h"
2020-12-19 15:49:31 +01:00
#include "control.h"
#include "loader.h"
#include "midi.h"
2020-12-25 14:46:06 +01:00
#include "pb.h"
#include "render.h"
#include "TBall.h"
#include "TBlocker.h"
#include "TBumper.h"
#include "TComponentGroup.h"
#include "TDemo.h"
#include "TFlagSpinner.h"
#include "TGate.h"
#include "THole.h"
2020-12-25 14:46:06 +01:00
#include "timer.h"
#include "TKickback.h"
#include "TKickout.h"
#include "TLight.h"
#include "TLightBargraph.h"
#include "TLightGroup.h"
#include "TLightRollover.h"
#include "TOneway.h"
#include "TPopupTarget.h"
#include "TRamp.h"
#include "TRollover.h"
#include "TSink.h"
#include "TSoloTarget.h"
#include "TSound.h"
#include "TTableLayer.h"
#include "TTextBox.h"
#include "TTimer.h"
#include "TTripwire.h"
#include "TWall.h"
2020-12-25 14:46:06 +01:00
#include "TPlunger.h"
#include "TFlipper.h"
#include "TDrain.h"
#include "translations.h"
2020-12-25 14:46:06 +01:00
int TPinballTable::score_multipliers[5] = {1, 2, 3, 5, 10};
TPinballTable::TPinballTable(): TPinballComponent(nullptr, -1, false)
{
int shortArrLength;
2020-12-25 14:46:06 +01:00
CurScoreStruct = nullptr;
ScoreBallcount = nullptr;
ScorePlayerNumber1 = nullptr;
BallInDrainFlag = 0;
2021-01-28 16:01:26 +01:00
ActiveFlag = 1;
2020-12-25 14:46:06 +01:00
TiltLockFlag = 0;
EndGameTimeoutTimer = 0;
LightShowTimer = 0;
ReplayTimer = 0;
TiltTimeoutTimer = 0;
MultiballFlag = false;
2021-01-05 13:12:54 +01:00
PlayerCount = 0;
2020-12-25 14:46:06 +01:00
2023-03-13 06:25:49 +01:00
auto ball = AddBall({0.0f, 0.0f});
ball->Disable();
2020-12-20 12:13:12 +01:00
new TTableLayer(this);
2020-12-25 14:46:06 +01:00
LightGroup = new TLightGroup(this, 0);
2020-11-15 15:39:00 +01:00
auto score1 = score::create("score1", render::background_bitmap);
2020-12-25 14:46:06 +01:00
CurScoreStruct = score1;
PlayerScores[0].ScoreStruct = score1;
for (int scoreIndex = 1; scoreIndex < 4; scoreIndex++)
{
2020-12-25 14:46:06 +01:00
PlayerScores[scoreIndex].ScoreStruct = score::dup(CurScoreStruct, scoreIndex);
}
2020-12-25 14:46:06 +01:00
CurrentPlayer = 0;
MaxBallCount = 3;
ScoreBallcount = score::create("ballcount1", render::background_bitmap);
ScorePlayerNumber1 = score::create("player_number1", render::background_bitmap);
int groupIndexObjects = loader::query_handle("table_objects");
short* shortArr = loader::query_iattribute(groupIndexObjects, 1025, &shortArrLength);
if (shortArrLength > 0)
{
for (int i = 0; i < shortArrLength / 2; ++i)
{
int objectType = *shortArr;
short* shortArrPlus1 = shortArr + 1;
int groupIndex = *shortArrPlus1;
shortArr = shortArrPlus1 + 1;
switch (objectType)
{
case 1000:
case 1010:
2020-12-19 15:49:31 +01:00
new TWall(this, groupIndex);
break;
case 1001:
2020-12-25 14:46:06 +01:00
Plunger = new TPlunger(this, groupIndex);
break;
case 1002:
LightGroup->List.push_back(new TLight(this, groupIndex));
break;
case 1003:
2020-12-25 14:46:06 +01:00
FlipperL = new TFlipper(this, groupIndex);
break;
case 1004:
2020-12-25 14:46:06 +01:00
FlipperR = new TFlipper(this, groupIndex);
break;
case 1005:
new TBumper(this, groupIndex);
break;
case 1006:
new TPopupTarget(this, groupIndex);
break;
case 1007:
2020-12-25 14:46:06 +01:00
Drain = new TDrain(this, groupIndex);
break;
case 1011:
new TBlocker(this, groupIndex);
break;
case 1012:
new TKickout(this, groupIndex, true);
break;
case 1013:
new TGate(this, groupIndex);
break;
case 1014:
new TKickback(this, groupIndex);
break;
case 1015:
new TRollover(this, groupIndex);
break;
case 1016:
new TOneway(this, groupIndex);
break;
case 1017:
new TSink(this, groupIndex);
break;
case 1018:
new TFlagSpinner(this, groupIndex);
break;
case 1019:
new TSoloTarget(this, groupIndex);
break;
case 1020:
new TLightRollover(this, groupIndex);
break;
case 1021:
new TRamp(this, groupIndex);
break;
case 1022:
new THole(this, groupIndex);
break;
case 1023:
new TDemo(this, groupIndex);
break;
case 1024:
new TTripwire(this, groupIndex);
break;
case 1026:
new TLightGroup(this, groupIndex);
break;
case 1028:
new TComponentGroup(this, groupIndex);
break;
case 1029:
new TKickout(this, groupIndex, false);
break;
case 1030:
new TLightBargraph(this, groupIndex);
break;
case 1031:
new TSound(this, groupIndex);
break;
case 1032:
new TTimer(this, groupIndex);
break;
case 1033:
new TTextBox(this, groupIndex);
break;
default:
continue;
}
}
}
render::build_occlude_list();
pb::InfoTextBox = dynamic_cast<TTextBox*>(find_component("info_text_box"));
pb::MissTextBox = dynamic_cast<TTextBox*>(find_component("mission_text_box"));
2020-12-19 15:49:31 +01:00
control::make_links(this);
2020-11-04 14:22:52 +01:00
}
TPinballTable::~TPinballTable()
{
for (auto& PlayerScore : PlayerScores)
2020-11-04 14:22:52 +01:00
{
delete PlayerScore.ScoreStruct;
2020-11-04 14:22:52 +01:00
}
if (ScorePlayerNumber1)
{
delete ScorePlayerNumber1;
2020-11-04 14:22:52 +01:00
ScorePlayerNumber1 = nullptr;
}
if (ScoreBallcount)
{
delete ScoreBallcount;
2020-11-04 14:22:52 +01:00
ScoreBallcount = nullptr;
}
2020-12-20 12:13:12 +01:00
delete LightGroup;
while (!ComponentList.empty())
2020-11-04 14:22:52 +01:00
{
// Component destructor removes it from the list.
delete ComponentList[0];
2020-11-04 14:22:52 +01:00
}
control::ClearLinks();
pb::InfoTextBox = pb::MissTextBox = nullptr;
2020-11-04 14:22:52 +01:00
}
TPinballComponent* TPinballTable::find_component(LPCSTR componentName)
{
for (auto component : ComponentList)
2020-11-04 14:22:52 +01:00
{
const char* groupName = component->GroupName;
if (groupName && !strcmp(groupName, componentName))
2020-11-04 14:22:52 +01:00
{
return component;
2020-11-04 14:22:52 +01:00
}
}
pb::ShowMessageBox(SDL_MESSAGEBOX_WARNING, "Table cant find:", componentName);
2020-11-04 14:22:52 +01:00
return nullptr;
}
TPinballComponent* TPinballTable::find_component(int groupIndex)
{
char Buffer[40]{};
for (auto component : ComponentList)
2020-11-04 14:22:52 +01:00
{
if (component->GroupIndex == groupIndex)
return component;
2020-11-04 14:22:52 +01:00
}
snprintf(Buffer, sizeof Buffer, "%d", groupIndex);
pb::ShowMessageBox(SDL_MESSAGEBOX_WARNING, "Table cant find (lh):", Buffer);
2020-11-04 14:22:52 +01:00
return nullptr;
}
2020-12-25 14:46:06 +01:00
int TPinballTable::AddScore(int score)
{
if (JackpotScoreFlag)
2020-12-25 14:46:06 +01:00
{
JackpotScore += score;
const auto jackpotLimit = !pb::FullTiltMode ? 5000000 : 10000000;
if (JackpotScore > jackpotLimit)
JackpotScore = jackpotLimit;
2020-12-25 14:46:06 +01:00
}
if (BonusScoreFlag)
2020-12-25 14:46:06 +01:00
{
BonusScore += score;
if (BonusScore > 5000000)
BonusScore = 5000000;
2020-12-25 14:46:06 +01:00
}
2021-01-05 10:02:43 +01:00
int addedScore = ScoreAdded + score * score_multipliers[ScoreMultiplier];
CurScore += addedScore;
if (CurScore > 1000000000)
2020-12-25 14:46:06 +01:00
{
2021-01-05 10:02:43 +01:00
++CurScoreE9;
CurScore = CurScore - 1000000000;
2020-12-25 14:46:06 +01:00
}
2021-01-05 10:02:43 +01:00
score::set(CurScoreStruct, CurScore);
2020-12-25 14:46:06 +01:00
return addedScore;
}
void TPinballTable::ChangeBallCount(int count)
{
BallCount = count;
if (count <= 0)
{
score::erase(ScoreBallcount, 1);
}
else
{
score::set(ScoreBallcount, MaxBallCount - count + 1);
score::update(ScoreBallcount);
}
}
void TPinballTable::tilt(float time)
{
if (!TiltLockFlag && !BallInDrainFlag)
2020-12-25 14:46:06 +01:00
{
pb::InfoTextBox->Clear();
pb::MissTextBox->Clear();
pb::InfoTextBox->Display(pb::get_rc_string(Msg::STRING136), -1.0);
Implement stereo sound. (#138) * Implement stereo sound. Original Space Cadet has mono sound. To achieve stereo, the following steps were accomplished: - Add a game option to turn on/off stereo sound. Default is on. - TPinballComponent objects were extended with a method called get_coordinates() that returns a single 2D point, approximating the on-screen position of the object, re-mapped between 0 and 1 vertically and horizontally, {0, 0} being at the top-left. - For static objects like bumpers and lights, the coordinate refers to the geometric center of the corresponding graphic sprite, and is precalculated at initialization. - For ball objects, the coordinate refers to the geometric center of the ball, calculated during play when requested. - Extend all calls to sound-playing methods so that they include a TPinballComponent* argument that refers to the sound source, e.g. where the sound comes from. For instance, when a flipper is activated, its method call to emit a sound now includes a reference to the flipper object; when a ball goes under a SkillShotGate, its method call to emit a sound now includes a reference to the corresponding light; and so on. For some cases, like light rollovers, the sound source is taken from the ball that triggered the light rollover. For other cases, like holes, flags and targets, the sound source is taken from the object itself. For some special cases like ramp activation, sound source is taken from the nearest light position that makes sense. For all game-progress sounds, like mission completion sounds or ball drain sounds, the sound source is undefined (set to nullptr), and the Sound::PlaySound() method takes care of positioning them at a default location, where speakers on a pinball machine normally are. - Make the Sound::PlaySound() method accept a new argument, a TPinballComponent reference, as described above. If the stereo option is turned on, the Sound::PlaySound() method calls the get_coordinates() method of the TPinballComponent reference to get the sound position. This project uses SDL_mixer and there is a function called Mix_SetPosition() that allows placing a sound in the stereo field, by giving it a distance and an angle. We arbitrarily place the player's ears at the bottom of the table; we set the ears' height to half a table's length. Intensity of the stereo effect is directly related to this value; the farther the player's ears from the table, the narrowest the stereo picture gets, and vice-versa. From there we have all we need to calculate distance and angle; we do just that and position all the sounds. * Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
loader::play_sound(SoundIndex3, nullptr, "TPinballTable1");
2021-01-05 10:02:43 +01:00
TiltTimeoutTimer = timer::set(30.0, this, tilt_timeout);
2020-12-25 14:46:06 +01:00
for (auto component : ComponentList)
2020-12-25 14:46:06 +01:00
{
component->Message(MessageCode::SetTiltLock, time);
2020-12-25 14:46:06 +01:00
}
LightGroup->Message(MessageCode::TLightTurnOffTimed, 0);
2021-01-05 10:02:43 +01:00
TiltLockFlag = 1;
control::table_control_handler(MessageCode::SetTiltLock);
2020-12-25 14:46:06 +01:00
}
}
void TPinballTable::port_draw()
{
for (auto component : ComponentList)
{
component->port_draw();
}
2020-12-19 15:49:31 +01:00
}
2020-12-25 14:46:06 +01:00
int TPinballTable::Message(MessageCode code, float value)
2020-12-25 14:46:06 +01:00
{
const char* rc_text;
2020-12-25 14:46:06 +01:00
switch (code)
{
case MessageCode::LeftFlipperInputPressed:
2020-12-25 14:46:06 +01:00
if (!TiltLockFlag)
{
FlipperL->Message(MessageCode::TFlipperExtend, value);
2020-12-25 14:46:06 +01:00
}
break;
case MessageCode::LeftFlipperInputReleased:
2020-12-25 14:46:06 +01:00
if (!TiltLockFlag)
{
FlipperL->Message(MessageCode::TFlipperRetract, value);
2020-12-25 14:46:06 +01:00
}
break;
case MessageCode::RightFlipperInputPressed:
2020-12-25 14:46:06 +01:00
if (!TiltLockFlag)
{
FlipperR->Message(MessageCode::TFlipperExtend, value);
2020-12-25 14:46:06 +01:00
}
break;
case MessageCode::RightFlipperInputReleased:
2020-12-25 14:46:06 +01:00
if (!TiltLockFlag)
{
FlipperR->Message(MessageCode::TFlipperRetract, value);
2020-12-25 14:46:06 +01:00
}
break;
case MessageCode::PlungerInputPressed:
case MessageCode::PlungerInputReleased:
Plunger->Message(code, value);
2020-12-25 14:46:06 +01:00
break;
case MessageCode::Pause:
case MessageCode::Resume:
case MessageCode::LooseFocus:
for (auto component : ComponentList)
2020-12-25 14:46:06 +01:00
{
component->Message(code, value);
2020-12-25 14:46:06 +01:00
}
break;
case MessageCode::ClearTiltLock:
LightGroup->Message(MessageCode::TLightResetTimed, 0.0);
2020-12-25 14:46:06 +01:00
if (TiltLockFlag)
{
TiltLockFlag = 0;
if (TiltTimeoutTimer)
timer::kill(TiltTimeoutTimer);
TiltTimeoutTimer = 0;
}
break;
case MessageCode::StartGamePlayer1:
LightGroup->Message(MessageCode::TLightGroupReset, 0.0);
LightGroup->Message(MessageCode::TLightResetAndTurnOff, 0.0);
Plunger->Message(MessageCode::PlungerStartFeedTimer, 0.0);
if (Demo && Demo->ActiveFlag)
rc_text = pb::get_rc_string(Msg::STRING131);
2020-12-25 14:46:06 +01:00
else
rc_text = pb::get_rc_string(Msg::STRING127);
pb::InfoTextBox->Display(rc_text, -1.0);
2020-12-25 14:46:06 +01:00
if (Demo)
Demo->Message(MessageCode::NewGame, 0.0);
2020-12-25 14:46:06 +01:00
break;
case MessageCode::NewGame:
2020-12-25 14:46:06 +01:00
if (EndGameTimeoutTimer)
{
timer::kill(EndGameTimeoutTimer);
EndGame_timeout(0, this);
2022-01-05 09:38:50 +01:00
pb::mode_change(GameModes::InGame);
2020-12-25 14:46:06 +01:00
}
if (LightShowTimer)
{
timer::kill(LightShowTimer);
LightShowTimer = 0;
Message(MessageCode::StartGamePlayer1, 0.0);
2020-12-25 14:46:06 +01:00
}
else
{
// Some of the control cheats persist across games.
// Was this loose anti-cheat by design?
CheatsUsed = 0;
Message(MessageCode::Reset, 0.0);
auto ball = BallList[0];
ball->Position.Y = 0.0;
ball->Position.X = 0.0;
ball->Position.Z = -0.8f;
2020-12-25 14:46:06 +01:00
auto playerCount = static_cast<int>(floor(value));
PlayerCount = playerCount;
if (playerCount >= 1)
{
if (playerCount > 4)
PlayerCount = 4;
}
else
{
PlayerCount = 1;
}
auto plr1Score = PlayerScores[0].ScoreStruct;
CurrentPlayer = 0;
CurScoreStruct = plr1Score;
CurScore = 0;
score::set(plr1Score, 0);
ScoreMultiplier = 0;
for (int plrIndex = 1; plrIndex < PlayerCount; ++plrIndex)
{
auto scorePtr = &PlayerScores[plrIndex];
score::set(scorePtr->ScoreStruct, 0);
scorePtr->Score = 0;
scorePtr->ScoreE9Part = 0;
scorePtr->BallCount = MaxBallCount;
scorePtr->ExtraBalls = ExtraBalls;
scorePtr->BallLockedCounter = BallLockedCounter;
scorePtr->JackpotScore = JackpotScore;
2020-12-25 14:46:06 +01:00
}
BallCount = MaxBallCount;
ChangeBallCount(BallCount);
score::set(ScorePlayerNumber1, CurrentPlayer + 1);
score::update(ScorePlayerNumber1);
for (auto scoreIndex = 4 - PlayerCount; scoreIndex > 0; scoreIndex--)
{
score::set(PlayerScores[scoreIndex].ScoreStruct, -1);
}
JackpotScoreFlag = false;
BonusScoreFlag = false;
2020-12-25 14:46:06 +01:00
UnknownP71 = 0;
pb::InfoTextBox->Clear();
pb::MissTextBox->Clear();
LightGroup->Message(MessageCode::TLightGroupLightShowAnimation, 0.2f);
Implement stereo sound. (#138) * Implement stereo sound. Original Space Cadet has mono sound. To achieve stereo, the following steps were accomplished: - Add a game option to turn on/off stereo sound. Default is on. - TPinballComponent objects were extended with a method called get_coordinates() that returns a single 2D point, approximating the on-screen position of the object, re-mapped between 0 and 1 vertically and horizontally, {0, 0} being at the top-left. - For static objects like bumpers and lights, the coordinate refers to the geometric center of the corresponding graphic sprite, and is precalculated at initialization. - For ball objects, the coordinate refers to the geometric center of the ball, calculated during play when requested. - Extend all calls to sound-playing methods so that they include a TPinballComponent* argument that refers to the sound source, e.g. where the sound comes from. For instance, when a flipper is activated, its method call to emit a sound now includes a reference to the flipper object; when a ball goes under a SkillShotGate, its method call to emit a sound now includes a reference to the corresponding light; and so on. For some cases, like light rollovers, the sound source is taken from the ball that triggered the light rollover. For other cases, like holes, flags and targets, the sound source is taken from the object itself. For some special cases like ramp activation, sound source is taken from the nearest light position that makes sense. For all game-progress sounds, like mission completion sounds or ball drain sounds, the sound source is undefined (set to nullptr), and the Sound::PlaySound() method takes care of positioning them at a default location, where speakers on a pinball machine normally are. - Make the Sound::PlaySound() method accept a new argument, a TPinballComponent reference, as described above. If the stereo option is turned on, the Sound::PlaySound() method calls the get_coordinates() method of the TPinballComponent reference to get the sound position. This project uses SDL_mixer and there is a function called Mix_SetPosition() that allows placing a sound in the stereo field, by giving it a distance and an angle. We arbitrarily place the player's ears at the bottom of the table; we set the ears' height to half a table's length. Intensity of the stereo effect is directly related to this value; the farther the player's ears from the table, the narrowest the stereo picture gets, and vice-versa. From there we have all we need to calculate distance and angle; we do just that and position all the sounds. * Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
auto time = loader::play_sound(SoundIndex1, nullptr, "TPinballTable2");
if (time < 0)
time = 5.0f;
2020-12-25 14:46:06 +01:00
LightShowTimer = timer::set(time, this, LightShow_timeout);
}
if (pb::FullTiltMode)
{
// Multi-ball is FT exclusive feature, at least for now.
MultiballFlag = true;
JackpotScore = 500000;
}
midi::play_track(MidiTracks::Track1, true);
2020-12-25 14:46:06 +01:00
break;
case MessageCode::PlungerRelaunchBall:
2020-12-25 14:46:06 +01:00
if (ReplayTimer)
timer::kill(ReplayTimer);
ReplayTimer = timer::set(floor(value), this, replay_timer_callback);
ReplayActiveFlag = 1;
break;
case MessageCode::SwitchToNextPlayer:
2020-12-25 14:46:06 +01:00
{
if (PlayerCount <= 1)
{
const char* textboxText;
2021-01-28 16:01:26 +01:00
if (Demo->ActiveFlag)
textboxText = pb::get_rc_string(Msg::STRING131);
2020-12-25 14:46:06 +01:00
else
textboxText = pb::get_rc_string(Msg::STRING127);
pb::InfoTextBox->Display(textboxText, -1.0);
2020-12-25 14:46:06 +01:00
break;
}
auto nextPlayer = (CurrentPlayer + 1) % PlayerCount;
auto nextScorePtr = &PlayerScores[nextPlayer];
if (nextScorePtr->BallCount <= 0)
break;
PlayerScores[CurrentPlayer].Score = CurScore;
PlayerScores[CurrentPlayer].ScoreE9Part = CurScoreE9;
PlayerScores[CurrentPlayer].BallCount = BallCount;
PlayerScores[CurrentPlayer].ExtraBalls = ExtraBalls;
PlayerScores[CurrentPlayer].BallLockedCounter = BallLockedCounter;
PlayerScores[CurrentPlayer].JackpotScore = JackpotScore;
2020-12-25 14:46:06 +01:00
CurScore = nextScorePtr->Score;
CurScoreE9 = nextScorePtr->ScoreE9Part;
BallCount = nextScorePtr->BallCount;
ExtraBalls = nextScorePtr->ExtraBalls;
BallLockedCounter = nextScorePtr->BallLockedCounter;
JackpotScore = nextScorePtr->JackpotScore;
2020-12-25 14:46:06 +01:00
CurScoreStruct = nextScorePtr->ScoreStruct;
score::set(CurScoreStruct, CurScore);
CurScoreStruct->DirtyFlag = true;
ChangeBallCount(BallCount);
score::set(ScorePlayerNumber1, nextPlayer + 1);
score::update(ScorePlayerNumber1);
for (auto component : ComponentList)
2020-12-25 14:46:06 +01:00
{
component->Message(MessageCode::PlayerChanged, static_cast<float>(nextPlayer));
2020-12-25 14:46:06 +01:00
}
const char* textboxText = nullptr;
2020-12-25 14:46:06 +01:00
switch (nextPlayer)
{
case 0:
2021-01-28 16:01:26 +01:00
if (Demo->ActiveFlag)
textboxText = pb::get_rc_string(Msg::STRING131);
2020-12-25 14:46:06 +01:00
else
textboxText = pb::get_rc_string(Msg::STRING127);
2020-12-25 14:46:06 +01:00
break;
case 1:
2021-01-28 16:01:26 +01:00
if (Demo->ActiveFlag)
textboxText = pb::get_rc_string(Msg::STRING132);
2020-12-25 14:46:06 +01:00
else
textboxText = pb::get_rc_string(Msg::STRING128);
2020-12-25 14:46:06 +01:00
break;
case 2:
2021-01-28 16:01:26 +01:00
if (Demo->ActiveFlag)
textboxText = pb::get_rc_string(Msg::STRING133);
2020-12-25 14:46:06 +01:00
else
textboxText = pb::get_rc_string(Msg::STRING129);
2020-12-25 14:46:06 +01:00
break;
case 3:
2021-01-28 16:01:26 +01:00
if (Demo->ActiveFlag)
textboxText = pb::get_rc_string(Msg::STRING134);
2020-12-25 14:46:06 +01:00
else
textboxText = pb::get_rc_string(Msg::STRING130);
2020-12-25 14:46:06 +01:00
break;
default:
break;
}
if (textboxText != nullptr)
pb::InfoTextBox->Display(textboxText, -1);
JackpotScoreFlag = false;
BonusScoreFlag = false;
2020-12-25 14:46:06 +01:00
UnknownP71 = 0;
CurrentPlayer = nextPlayer;
}
break;
case MessageCode::GameOver:
Implement stereo sound. (#138) * Implement stereo sound. Original Space Cadet has mono sound. To achieve stereo, the following steps were accomplished: - Add a game option to turn on/off stereo sound. Default is on. - TPinballComponent objects were extended with a method called get_coordinates() that returns a single 2D point, approximating the on-screen position of the object, re-mapped between 0 and 1 vertically and horizontally, {0, 0} being at the top-left. - For static objects like bumpers and lights, the coordinate refers to the geometric center of the corresponding graphic sprite, and is precalculated at initialization. - For ball objects, the coordinate refers to the geometric center of the ball, calculated during play when requested. - Extend all calls to sound-playing methods so that they include a TPinballComponent* argument that refers to the sound source, e.g. where the sound comes from. For instance, when a flipper is activated, its method call to emit a sound now includes a reference to the flipper object; when a ball goes under a SkillShotGate, its method call to emit a sound now includes a reference to the corresponding light; and so on. For some cases, like light rollovers, the sound source is taken from the ball that triggered the light rollover. For other cases, like holes, flags and targets, the sound source is taken from the object itself. For some special cases like ramp activation, sound source is taken from the nearest light position that makes sense. For all game-progress sounds, like mission completion sounds or ball drain sounds, the sound source is undefined (set to nullptr), and the Sound::PlaySound() method takes care of positioning them at a default location, where speakers on a pinball machine normally are. - Make the Sound::PlaySound() method accept a new argument, a TPinballComponent reference, as described above. If the stereo option is turned on, the Sound::PlaySound() method calls the get_coordinates() method of the TPinballComponent reference to get the sound position. This project uses SDL_mixer and there is a function called Mix_SetPosition() that allows placing a sound in the stereo field, by giving it a distance and an angle. We arbitrarily place the player's ears at the bottom of the table; we set the ears' height to half a table's length. Intensity of the stereo effect is directly related to this value; the farther the player's ears from the table, the narrowest the stereo picture gets, and vice-versa. From there we have all we need to calculate distance and angle; we do just that and position all the sounds. * Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
loader::play_sound(SoundIndex2, nullptr, "TPinballTable3");
pb::MissTextBox->Clear();
pb::InfoTextBox->Display(pb::get_rc_string(Msg::STRING135), -1.0);
2020-12-25 14:46:06 +01:00
EndGameTimeoutTimer = timer::set(3.0, this, EndGame_timeout);
break;
case MessageCode::Reset:
for (auto component : ComponentList)
2020-12-25 14:46:06 +01:00
{
component->Message(MessageCode::Reset, 0);
2020-12-25 14:46:06 +01:00
}
if (ReplayTimer)
timer::kill(ReplayTimer);
ReplayTimer = 0;
if (LightShowTimer)
{
timer::kill(LightShowTimer);
LightGroup->Message(MessageCode::TLightGroupReset, 0.0);
2020-12-25 14:46:06 +01:00
}
LightShowTimer = 0;
ScoreMultiplier = 0;
ScoreAdded = 0;
ReflexShotScore = 0;
BonusScore = 10000;
BonusScoreFlag = false;
JackpotScore = 20000;
JackpotScoreFlag = false;
2020-12-25 14:46:06 +01:00
UnknownP71 = 0;
ExtraBalls = 0;
MultiballCount = 0;
2020-12-25 14:46:06 +01:00
BallLockedCounter = 0;
MultiballFlag = false;
2020-12-25 14:46:06 +01:00
UnknownP78 = 0;
ReplayActiveFlag = 0;
ReplayTimer = 0;
TiltLockFlag = 0;
break;
default: break;
}
control::table_control_handler(code);
2020-12-25 14:46:06 +01:00
return 0;
}
2023-03-13 06:25:49 +01:00
TBall* TPinballTable::AddBall(vector2 position)
{
TBall* ball = nullptr;
for (auto curBall : BallList)
{
if (!curBall->ActiveFlag)
{
ball = curBall;
break;
}
}
if (ball != nullptr)
{
ball->ActiveFlag = 1;
2023-03-05 12:16:07 +01:00
ball->Position.Z = ball->Radius;
ball->Direction = {};
ball->Speed = 0;
ball->TimeDelta = 0;
ball->EdgeCollisionCount = 0;
ball->CollisionFlag = 0;
ball->CollisionMask = 1;
ball->CollisionComp = nullptr;
}
else
{
if (BallList.size() >= 20)
return nullptr;
ball = new TBall(this, -1);
BallList.push_back(ball);
}
2023-03-13 06:25:49 +01:00
ball->Position.X = position.X;
ball->Position.Y = position.Y;
ball->PrevPosition = ball->Position;
2023-03-05 12:16:07 +01:00
ball->StuckCounter = 0;
ball->LastActiveTime = pb::time_ticks;
return ball;
}
int TPinballTable::BallCountInRect(const RectF& rect)
{
int count = 0;
for (const auto ball : BallList)
{
if (ball->ActiveFlag &&
ball->Position.X >= rect.XMin &&
ball->Position.Y >= rect.YMin &&
ball->Position.X <= rect.XMax &&
ball->Position.Y <= rect.YMax)
{
count++;
}
}
return count;
}
int TPinballTable::BallCountInRect(const vector2& pos, float margin)
{
RectF rect{};
rect.XMin = pos.X - margin;
rect.XMax = pos.X + margin;
rect.YMin = pos.Y - margin;
rect.YMax = pos.Y + margin;
return BallCountInRect(rect);
}
2020-12-25 14:46:06 +01:00
void TPinballTable::EndGame_timeout(int timerId, void* caller)
{
auto table = static_cast<TPinballTable*>(caller);
table->EndGameTimeoutTimer = 0;
pb::end_game();
for (auto component : table->ComponentList)
2020-12-25 14:46:06 +01:00
{
component->Message(MessageCode::GameOver, 0);
2020-12-25 14:46:06 +01:00
}
if (table->Demo)
table->Demo->Message(MessageCode::GameOver, 0.0);
control::handler(MessageCode::ControlMissionStarted, pb::MissTextBox);
pb::InfoTextBox->Display(pb::get_rc_string(Msg::STRING125), -1.0);
2020-12-25 14:46:06 +01:00
}
void TPinballTable::LightShow_timeout(int timerId, void* caller)
{
auto table = static_cast<TPinballTable*>(caller);
table->LightShowTimer = 0;
table->Message(MessageCode::StartGamePlayer1, 0.0);
2020-12-25 14:46:06 +01:00
}
void TPinballTable::replay_timer_callback(int timerId, void* caller)
{
auto table = static_cast<TPinballTable*>(caller);
table->ReplayActiveFlag = 0;
table->ReplayTimer = 0;
}
void TPinballTable::tilt_timeout(int timerId, void* caller)
{
auto table = static_cast<TPinballTable*>(caller);
vector2 vec{};
2021-01-19 16:28:48 +01:00
2020-12-25 14:46:06 +01:00
table->TiltTimeoutTimer = 0;
if (table->TiltLockFlag)
{
for (auto ball : table->BallList)
2020-12-25 14:46:06 +01:00
{
table->Drain->Collision(ball, &vec, &vec, 0.0, nullptr);
2021-01-19 16:28:48 +01:00
}
2020-12-25 14:46:06 +01:00
}
}