Skip to content

Commit

Permalink
Merge pull request diasurgical#3 from Songoo7/multiplayerReadyCommand
Browse files Browse the repository at this point in the history
F9 - ready, F10 - pause, F11 - play
  • Loading branch information
Songoo7 authored Feb 2, 2024
2 parents 3c8164d + f0ff8c4 commit 2c36855
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <algorithm>
#include <cmath>
#include <cstdint>

#include <set>
#include <fmt/core.h>

#include "control.h"
Expand Down Expand Up @@ -53,6 +53,7 @@ namespace devilution {
uint8_t MyPlayerId;
Player *MyPlayer;
std::vector<Player> Players;
std::set<std::string> UniquePlayerNames;//used to count number of players who are ready with their move
Player *InspectPlayer;
bool MyPlayerIsDead;

Expand Down
4 changes: 3 additions & 1 deletion Source/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <cstdint>
#include <vector>

#include <set>
#include <algorithm>
#include <array>

Expand Down Expand Up @@ -899,6 +899,8 @@ struct Player {
extern DVL_API_FOR_TEST uint8_t MyPlayerId;
extern DVL_API_FOR_TEST Player *MyPlayer;
extern DVL_API_FOR_TEST std::vector<Player> Players;
// Make UniquePlayerNames accessible from other files, it contains names of players who pressed 'ready' button
extern DVL_API_FOR_TEST std::set<std::string> UniquePlayerNames;
/** @brief What Player items and stats should be displayed? Normally this is identical to MyPlayer but can differ when /inspect was used. */
extern Player *InspectPlayer;
/** @brief Do we currently inspect a remote player (/inspect was used)? In this case the (remote) players items and stats can't be modified. */
Expand Down
87 changes: 69 additions & 18 deletions Source/plrmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Implementation of functionality for printing the ingame chat messages.
*/
#include "plrmsg.h"

#include <set>
#include <algorithm>
#include <cstdint>

Expand Down Expand Up @@ -72,28 +72,79 @@ void EventPlrMsg(std::string_view text, UiFlags style)

void SendPlrMsg(Player &player, std::string_view text)
{
PlayerMessage &message = GetNextMessage();
std::string_view readyText = "I need help! Come here!";// F9
std::string_view pauseText = "Follow me."; // F10
std::string_view unpauseText = "Here's something for you."; // F11


//if pause signal but already paused, do nothing
int skipMessage = 0;
if (text == pauseText && BattlePauseMode == 2) {
skipMessage = 1;
} else if (text == pauseText && BattlePauseMode != 2) {
Messages = {}; // clean chat
}

std::string from = fmt::format(fmt::runtime(_("{:s} (lvl {:d}): ")), player._pName, player.getCharacterLevel());
// if unpause signal but already unpaused, do nothing
if (text == unpauseText && BattlePauseMode == 0) {
skipMessage = 1;
} else if (text == unpauseText && BattlePauseMode != 0) {
Messages = {}; // clean chat
}

message.style = UiFlags::ColorWhite;
message.time = SDL_GetTicks();
message.text = from + std::string(text);
message.prefixLength = from.size();
message.lineHeight = GetLineHeight(message.text, GameFont12) + 3;
// ready signal works only in paused mode
if (text == readyText && BattlePauseMode != 2) {
skipMessage = 1; // ready message works only in pause mode
} else if (text == readyText && BattlePauseMode == 2) {
Messages = {}; // clean chat
}

// Check for specific messages and update BattlePauseMode accordingly
if (text == "I need help! Come here!") {
message.text = from + std::string("Pause, do commands");
BattlePauseMode = 2;
} else if (text == "Follow me.") {
message.text = from + std::string("Play");
BattlePauseMode = 0;
} else {
if (skipMessage == 0) {
PlayerMessage &message = GetNextMessage();
std::string from = fmt::format(fmt::runtime(_("{:s} (lvl {:d}): ")), player._pName, player.getCharacterLevel());
message.style = UiFlags::ColorWhite;
message.time = SDL_GetTicks();
message.text = from + std::string(text);
message.prefixLength = from.size();
message.lineHeight = GetLineHeight(message.text, GameFont12) + 3;


if (text == pauseText) {
// if pause signal, pause game
if (BattlePauseMode != 2) {
message.text = from + std::string("Pause, do commands");
BattlePauseMode = 2;
}
} else if (text == unpauseText) {
// if unpause signal, unpause game
if (BattlePauseMode != 0) {
message.text = from + std::string("Play");
BattlePauseMode = 0;
UniquePlayerNames.clear();
}
} else if (text == readyText) {
if (BattlePauseMode == 2) {

//number of all active players
int numberOfAllPlayers = 0;
for (const Player &player : Players) {
if (player.plractive)
numberOfAllPlayers++;
}

//insert name of player who pressed 'ready' in unique array which will count number of ready players
std::string playerNameString(player._pName);
UniquePlayerNames.insert(playerNameString);
std::string numUniquePlayerNamesStr = std::to_string(UniquePlayerNames.size());
//show text 'like 4/7 players ready'
message.text = numUniquePlayerNamesStr + " / " + std::to_string(numberOfAllPlayers) + " ready";
}
} else {
//every other message in game
message.text = from + std::string(text);
}
AddMessageToChatLog(text, &player);
}

AddMessageToChatLog(text, &player);
}

void InitPlrMsg()
Expand Down

0 comments on commit 2c36855

Please sign in to comment.