Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
# Includes
# -----------------------
set(COMMON_DAIDE_CLIENT
${SRC_DIR}/bots/basebot/basebot.cpp
${SRC_DIR}/daide_client/main.cpp
${SRC_DIR}/daide_client/adjudicator.cpp
${SRC_DIR}/daide_client/base_bot.cpp
${SRC_DIR}/daide_client/error_log.cpp
${SRC_DIR}/daide_client/map_and_units.cpp
${SRC_DIR}/daide_client/socket.cpp
Expand All @@ -27,12 +27,12 @@ add_executable(dumbbot
${COMMON_DAIDE_CLIENT})
target_include_directories(dumbbot PUBLIC ${SRC_DIR}/bots/dumbbot ${SRC_DIR}/bots/basebot ${SRC_DIR})

add_executable(randbot
${SRC_DIR}/bots/randbot/randbot.cpp
${COMMON_DAIDE_CLIENT})
target_include_directories(randbot PUBLIC ${SRC_DIR}/bots/randbot ${SRC_DIR}/bots/basebot ${SRC_DIR})

add_executable(holdbot
${SRC_DIR}/bots/holdbot/holdbot.cpp
${COMMON_DAIDE_CLIENT})
target_include_directories(holdbot PUBLIC ${SRC_DIR}/bots/holdbot ${SRC_DIR}/bots/basebot ${SRC_DIR})

add_executable(randbot
${SRC_DIR}/bots/randbot/randbot.cpp
${COMMON_DAIDE_CLIENT})
target_include_directories(randbot PUBLIC ${SRC_DIR}/bots/randbot ${SRC_DIR}/bots/basebot ${SRC_DIR})
Binary file modified bin/dumbbot
Binary file not shown.
Binary file modified bin/holdbot
Binary file not shown.
Binary file modified bin/randbot
Binary file not shown.
43 changes: 27 additions & 16 deletions daide_client/base_bot.cpp → bots/basebot/basebot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include <iostream>
#include <memory>
#include "basebot.h"
#include "daide_client/ai_client.h"
#include "daide_client/base_bot.h"
#include "daide_client/error_log.h"
#include "daide_client/map_and_units.h"
#include "daide_client/socket.h"
Expand Down Expand Up @@ -74,7 +74,7 @@ bool BaseBot::initialize(const std::string &command_line_a) {
}

// Connection failure
if (!m_socket.Connect(parameters.server_name, parameters.port_number)) {
if (!m_socket.Connect(parameters.server_name.c_str(), parameters.port_number)) {
log_error("Failed to connect to server");
return false;
}
Expand Down Expand Up @@ -150,15 +150,9 @@ void BaseBot::send_name_and_version_to_server(const std::string &name, const std
TokenMessage name_message {};
TokenMessage name_tokens {};
TokenMessage version_tokens {};
std::string name_prefix {};

// Prepending 'POW:' if a power has been requested
if (!m_parameters.reconnect_power.empty()) {
name_prefix = m_parameters.reconnect_power + ":";
}

// Setting and sending
name_tokens.set_message_from_text("'" + name_prefix + name + "'");
name_tokens.set_message_from_text("'" + name + "'");
version_tokens.set_message_from_text("'" + version + "'");
name_message = TOKEN_COMMAND_NME & name_tokens & version_tokens;
send_message_to_server(name_message);
Expand Down Expand Up @@ -261,6 +255,7 @@ void BaseBot::process_diplomacy_message(char *message, int message_length) {
// Act on that token
lead_token = incoming_msg.get_token(0);

// FIXME use a switch statement
// Messages that BaseBot handles initially
if (lead_token == TOKEN_COMMAND_HLO) {
process_hlo(incoming_msg);
Expand Down Expand Up @@ -421,6 +416,7 @@ void BaseBot::process_sco(const TokenMessage &incoming_msg) {
void BaseBot::process_not(const TokenMessage &incoming_msg) {
TokenMessage not_message = incoming_msg.get_submessage(1);

// FIXME using switch statement
if (not_message.get_token(0) == TOKEN_COMMAND_CCD) {
process_not_ccd(incoming_msg, not_message.get_submessage(1));
} else if (not_message.get_token(0) == TOKEN_COMMAND_TME) {
Expand All @@ -434,6 +430,7 @@ void BaseBot::process_not(const TokenMessage &incoming_msg) {
void BaseBot::process_rej(const TokenMessage &incoming_msg) {
TokenMessage rej_message = incoming_msg.get_submessage(1);

// FIXME using switch statement
if (rej_message.get_token(0) == TOKEN_COMMAND_NME) {
process_rej_nme_message(incoming_msg, rej_message.get_submessage(1));
} else if (rej_message.get_token(0) == TOKEN_COMMAND_IAM) {
Expand Down Expand Up @@ -471,7 +468,7 @@ void BaseBot::process_rej(const TokenMessage &incoming_msg) {

// Process the REJ(NOT()) message. Split according to next token
void BaseBot::process_rej_not(const TokenMessage &incoming_msg, const TokenMessage &rej_not_params) {

// FIXME using switch statement
if (rej_not_params.get_token(0) == TOKEN_COMMAND_GOF) {
process_rej_not_gof_message(incoming_msg, rej_not_params.get_submessage(1));
} else if (rej_not_params.get_token(0) == TOKEN_COMMAND_DRW) {
Expand All @@ -485,6 +482,7 @@ void BaseBot::process_rej_not(const TokenMessage &incoming_msg, const TokenMessa
void BaseBot::process_yes(const TokenMessage &incoming_msg) {
TokenMessage yes_message = incoming_msg.get_submessage(1);

// FIXME use switch statement
if (yes_message.get_token(0) == TOKEN_COMMAND_NME) {
process_yes_nme_message(incoming_msg, yes_message.get_submessage(1));
} else if (yes_message.get_token(0) == TOKEN_COMMAND_OBS) {
Expand All @@ -508,6 +506,7 @@ void BaseBot::process_yes(const TokenMessage &incoming_msg) {

// Process the YES(NOT()) message. Split according to next token
void BaseBot::process_yes_not(const TokenMessage &incoming_msg, const TokenMessage &yes_not_params) {
// FIXME use switch staetment
if (yes_not_params.get_token(0) == TOKEN_COMMAND_GOF) {
process_yes_not_gof_message(incoming_msg, yes_not_params.get_submessage(1));
} else if (yes_not_params.get_token(0) == TOKEN_COMMAND_DRW) {
Expand Down Expand Up @@ -628,10 +627,15 @@ void BaseBot::process_rej_nme_message(const TokenMessage & /*incoming_msg*/, con
}
}

std::string BaseBot::get_bot_name() const
{
return (m_parameters.power_specified ? m_parameters.power + "__" + BOT_FAMILY : BOT_FAMILY);
}

// Determine whether to try and reconnect to game. Default uses values passed on command line.
bool BaseBot::get_reconnect_details(Token &power, int &passcode) {
if (m_parameters.reconnection_specified) {
power = TokenTextMap::instance()->m_text_to_token_map[m_parameters.reconnect_power];
power = TokenTextMap::instance()->m_text_to_token_map[m_parameters.power];
passcode = m_parameters.reconnect_passcode;
}
return m_parameters.reconnection_specified;
Expand Down Expand Up @@ -856,21 +860,28 @@ bool BaseBot::extract_parameters(const std::string &command_line_a, COMMAND_LINE
parameters.log_level = stoi(parameter);
break;

case 'c':
parameters.power_specified = true;
parameters.power = parameter.substr(0, 3);
for (auto &c : parameters.power) { c = static_cast<char>(toupper(c)); }
break;

case 'r':
if (parameter[3] == ':') {
parameters.reconnection_specified = true;
parameters.reconnect_power = parameter.substr(0, 3);
for (auto &c : parameters.reconnect_power) { c = static_cast<char>(toupper(c)); }
parameters.power_specified = true;
parameters.power = parameter.substr(0, 3);
for (auto &c : parameters.power) { c = static_cast<char>(toupper(c)); }
parameters.reconnect_passcode = stoi(parameter.substr(4));
} else {
std::cout << "-r should be followed by 'POW:passcode'\nPOW should be three characters" << std::endl;
}
break;

default:
std::cout << std::string(BOT_FAMILY) << " - version " << std::string(BOT_GENERATION) << std::endl;
std::cout << "Usage: " << std::string(BOT_FAMILY)
<< " [-sServerName|-iIPAddress] [-pPortNumber] [-lLogLevel] [-rPOW:passcode]" << std::endl;
std::cout << BOT_FAMILY << " - version " << BOT_GENERATION << std::endl;
std::cout << "Usage: " << BOT_FAMILY
<< " [-sServerName|-iIPAddress] [-pPortNumber] [-lLogLevel] [-cPOW] [-rPOW:passcode]" << std::endl;
extracted_ok = false;
}
param_start = m_command_line.find('-', search_start);
Expand Down
9 changes: 6 additions & 3 deletions daide_client/base_bot.h → bots/basebot/basebot.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* Release 8~3
**/

#ifndef _DAIDE_CLIENT_DAIDE_CLIENT_BASE_BOT_H
#define _DAIDE_CLIENT_DAIDE_CLIENT_BASE_BOT_H
#ifndef DAIDE_CLIENT_DAIDE_CLIENT_BASE_BOT_H
#define DAIDE_CLIENT_DAIDE_CLIENT_BASE_BOT_H

#include "daide_client/ai_client_types.h"
#include "daide_client/error_log.h"
Expand Down Expand Up @@ -146,6 +146,9 @@ class BaseBot {
// Handle an incoming REJ( NME() ) message.
virtual void process_rej_nme_message(const TokenMessage &incoming_msg, const TokenMessage &msg_params);

// Get the name of the bot including the requested power if any
std::string get_bot_name() const;

// Get the details to reconnect to the game. Return true if reconnect required, or false if reconnect is not to
// be attempted. Default implementation uses parameters from the command line
virtual bool get_reconnect_details(Token &power, int &passcode);
Expand Down Expand Up @@ -395,4 +398,4 @@ class BaseBot {

} // namespace DAIDE

#endif // _DAIDE_CLIENT_DAIDE_CLIENT_BASE_BOT_H
#endif // DAIDE_CLIENT_DAIDE_CLIENT_BASE_BOT_H
8 changes: 4 additions & 4 deletions bots/basebot/bot_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* Change the #define BOT_GENERATION to specify the generation (base version) of your Bot. *
*******************************************************************************************/

#ifndef _DAIDE_CLIENT_BOTS_BASEBOT_BOT_TYPE_H
#define _DAIDE_CLIENT_BOTS_BASEBOT_BOT_TYPE_H
#ifndef DAIDE_CLIENT_BOTS_BASEBOT_BOT_TYPE_H
#define DAIDE_CLIENT_BOTS_BASEBOT_BOT_TYPE_H

#include "daide_client/base_bot.h"
#include "base_bot.h"

namespace DAIDE {

Expand All @@ -35,4 +35,4 @@ using BOT_TYPE = BaseBot;

} // namespace DAIDE

#endif // _DAIDE_CLIENT_BOTS_BASEBOT_BOT_TYPE_H
#endif // DAIDE_CLIENT_BOTS_BASEBOT_BOT_TYPE_H
8 changes: 4 additions & 4 deletions bots/dumbbot/bot_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
* Change the #define BOT_GENERATION to specify the generation (base version) of your Bot. *
*******************************************************************************************/

#ifndef _DAIDE_CLIENT_BOTS_DUMBBOT_BOT_TYPE_H
#define _DAIDE_CLIENT_BOTS_DUMBBOT_BOT_TYPE_H
#ifndef DAIDE_CLIENT_BOTS_DUMBBOT_BOT_TYPE_H
#define DAIDE_CLIENT_BOTS_DUMBBOT_BOT_TYPE_H

#include "bots/dumbbot/dumbbot.h"
#include "dumbbot.h"

namespace DAIDE {

Expand All @@ -35,4 +35,4 @@ using BOT_TYPE = DumbBot;

} // namespace DAIDE

#endif // _DAIDE_CLIENT_BOTS_DUMBBOT_BOT_TYPE_H
#endif // DAIDE_CLIENT_BOTS_DUMBBOT_BOT_TYPE_H
122 changes: 5 additions & 117 deletions bots/dumbbot/dumbbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
**/

#include <iostream>
#include "bots/dumbbot/bot_type.h"
#include "bots/dumbbot/dumbbot.h"
#include "bot_type.h"
#include "dumbbot.h"
#include "daide_client/error_log.h"
#include "daide_client/token_text_map.h"

Expand Down Expand Up @@ -80,7 +80,7 @@ DumbBot::DumbBot() :
{}

void DumbBot::send_nme_or_obs() {
send_name_and_version_to_server(BOT_FAMILY, BOT_GENERATION);
send_name_and_version_to_server(get_bot_name(), BOT_GENERATION);
}

void DumbBot::process_mdf_message(const TokenMessage & /*incoming_msg*/) {
Expand Down Expand Up @@ -174,6 +174,7 @@ int DumbBot::get_power_index(const Token &power_token) {
}

void DumbBot::process_now_message(const TokenMessage & /*incoming_msg*/) {
send_message_to_server(TOKEN_COMMAND_NOT & TOKEN_COMMAND_GOF);

// Spring Moves/Retreats
if ((m_map_and_units->current_season == DAIDE::TOKEN_SEASON_SPR)
Expand Down Expand Up @@ -227,6 +228,7 @@ void DumbBot::process_now_message(const TokenMessage & /*incoming_msg*/) {

// Submitting orders
send_orders_to_server();
send_message_to_server(TOKEN_COMMAND_GOF);
}

void DumbBot::calculate_factors(const WEIGHTING proximity_attack_weight, const WEIGHTING proximity_defense_weight) {
Expand Down Expand Up @@ -886,117 +888,3 @@ int DumbBot::rand_no(int max_value) {
int answer = rand() % max_value;
return answer;
}

bool DumbBot::extract_parameters(const std::string &command_line_a, DAIDE::COMMAND_LINE_PARAMETERS &parameters) {
int search_start; // Position to start searching command line
int param_start; // Start of the next parameter
int param_end; // End of the next parameter
char param_token; // The token specifying the parameter type
std::string parameter; // The parameter
bool extracted_ok = true; // Whether the parameters were OK

parameters.ip_specified = false;
parameters.name_specified = false;
parameters.port_specified = false;
parameters.log_level_specified = false;
parameters.reconnection_specified = false;

std::string m_command_line = command_line_a;

// Strip the program name off the command line
if (m_command_line[0] == '"') {
// Program name is in quotes.
param_start = m_command_line.find('"', 1);

if (param_start != std::string::npos) {
m_command_line = m_command_line.substr(param_start + 1);
}
} else {
// Program name is not quoted, so is terminated by a space
param_start = m_command_line.find(' ');

if (param_start != std::string::npos) {
m_command_line = m_command_line.substr(param_start);
}
}

param_start = m_command_line.find('-', 0);

while (param_start != std::string::npos) {
param_token = m_command_line[param_start + 1];

param_end = m_command_line.find(' ', param_start);

if (param_end == std::string::npos) {
parameter = m_command_line.substr(param_start + 2);

search_start = m_command_line.size();
} else {
parameter = m_command_line.substr(0, param_end).substr(param_start + 2);

search_start = param_end;
}

switch (param_token) {
case 's': {
parameters.name_specified = true;
parameters.server_name = parameter;

break;
}

case 'i': {
parameters.ip_specified = true;
parameters.server_name = parameter;

break;
}

case 'p': {
parameters.port_specified = true;
parameters.port_number = stoi(parameter);

break;
}

case 'l': {
parameters.log_level_specified = true;
parameters.log_level = stoi(parameter);

break;
}

case 'r': {
if (parameter[3] == ':') {
parameters.reconnection_specified = true;
parameters.reconnect_power = parameter.substr(0, 3);
for (auto &c : parameters.reconnect_power) { c = toupper(c); }
parameters.reconnect_passcode = stoi(parameter.substr(4));
} else {
std::cout << "-r should be followed by 'POW:passcode'\n"
"POW should be three characters" << std::endl;
}

break;
}

default: {
std::cout << std::string(BOT_FAMILY) << " - version " << std::string(BOT_GENERATION) << std::endl;
std::cout << "Usage: " << std::string(BOT_FAMILY) <<
" [-sServerName|-iIPAddress] [-pPortNumber] "
"[-lLogLevel] [-rPOW:passcode] [-d]" << std::endl;
extracted_ok = false;
}
}

param_start = m_command_line.find('-', search_start);
}

if ((parameters.ip_specified) && (parameters.name_specified)) {
std::cout << "You must not specify Server name and IP address" << std::endl;

extracted_ok = false;
}

return extracted_ok;
}
Loading