Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give names to units #410

Merged
merged 7 commits into from
Apr 6, 2021
Merged
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
24 changes: 20 additions & 4 deletions client/gui-qt/hudwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,28 @@ void hud_units::update_actions(unit_list *punits)
tmp = tileset;
tileset = unscaled_tileset;
}
text_str = QString(unit_name_translation(punit));
owner = punit->owner;
pcity = player_city_by_number(owner, punit->homecity);
if (pcity != NULL) {
text_str = QString(("%1(%2)"))
.arg(unit_name_translation(punit), city_name_get(pcity));
if (punit->name.isEmpty()) {
if (pcity == nullptr) {
text_str = QString(unit_name_translation(punit));
} else {
// TRANS: <unit> (<home city>)
text_str =
QString(_("%1 (%2)"))
.arg(unit_name_translation(punit), city_name_get(pcity));
}
} else {
if (pcity == nullptr) {
// TRANS: <unit> "<unit name>"
text_str = QString(_("%1 \"%2\""))
.arg(punit->name, unit_name_translation(punit));
} else {
// TRANS: <unit> "<unit name>" (<home city>)
text_str = QString(_("%1 \"%2\" (%3)"))
.arg(unit_name_translation(punit), punit->name,
city_name_get(pcity));
}
}
text_str = text_str + " ";
mp = QString(move_points_text(punit->moves_left, false));
Expand Down
40 changes: 40 additions & 0 deletions client/gui-qt/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QActionGroup>
#include <QApplication>
#include <QFileDialog>
#include <QInputDialog>
#include <QMainWindow>
#include <QMessageBox>
#include <QScrollArea>
Expand Down Expand Up @@ -836,6 +837,11 @@ void mr_menu::setup_menus()
menu_list.insert(DISBAND, act);
connect(act, &QAction::triggered, this, &mr_menu::slot_disband);

menu->addSeparator();
act = menu->addAction(_("Rename..."));
menu_list.insert(RENAME, act);
connect(act, &QAction::triggered, this, &mr_menu::slot_rename);

// Combat Menu
menu = this->addMenu(_("Combat"));
act = menu->addAction(_("Fortify Unit"));
Expand Down Expand Up @@ -1866,6 +1872,10 @@ void mr_menu::menus_sensitive()
}
break;

case RENAME:
i.value()->setEnabled(get_num_units_in_focus() == 1);
break;

case CONNECT_RAIL:
proad = road_by_compat_special(ROCO_RAILROAD);
if (proad != NULL) {
Expand Down Expand Up @@ -2178,6 +2188,36 @@ void mr_menu::slot_convert() { key_unit_convert(); }
*/
void mr_menu::slot_disband() { popup_disband_dialog(get_units_in_focus()); }

/**
* Action "RENAME UNIT"
*/
void mr_menu::slot_rename()
{
if (get_num_units_in_focus() != 1) {
return;
}
unit_list_iterate(get_units_in_focus(), punit)
{
auto ask = new hud_input_box(king()->central_wdg);

ask->set_text_title_definput(_("New unit name:"), _("Rename Unit"),
punit->name);
ask->setAttribute(Qt::WA_DeleteOnClose);

int id = punit->id;
connect(ask, &QDialog::accepted, [ask, id]() {
// Unit might have been removed, make sure it's still there
auto unit = game_unit_by_number(id);
if (unit) {
dsend_packet_unit_rename(&client.conn, id,
ask->input_edit.text().toUtf8());
}
});
ask->show();
}
unit_list_iterate_end;
}

/**
Clears delayed orders
*/
Expand Down
2 changes: 2 additions & 0 deletions client/gui-qt/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum munit {
UNLOAD,
TRANSPORTER,
DISBAND,
RENAME,
CONVERT,
MINE,
PLANT,
Expand Down Expand Up @@ -256,6 +257,7 @@ private slots:
void slot_upgrade();
void slot_convert();
void slot_disband();
void slot_rename();

/*used by combat menu*/
void slot_unit_fortify();
Expand Down
13 changes: 13 additions & 0 deletions client/packhand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ static struct unit *unpackage_unit(const struct packet_unit_info *packet)
unit_tile_set(punit, index_to_tile(&(wld.map), packet->tile));
punit->facing = packet->facing;
punit->homecity = packet->homecity;
punit->name = QString::fromUtf8(QByteArray(packet->name,
ARRAY_SIZE(packet->name)));
output_type_iterate(o) { punit->upkeep[o] = packet->upkeep[o]; }
output_type_iterate_end;
punit->moves_left = packet->movesleft;
Expand Down Expand Up @@ -290,6 +292,7 @@ unpackage_short_unit(const struct packet_unit_short_info *packet)
punit->veteran = packet->veteran;
punit->hp = packet->hp;
punit->activity = static_cast<unit_activity>(packet->activity);
punit->name = QString::fromUtf8(packet->name, ARRAY_SIZE(packet->name));

if (packet->activity_tgt == EXTRA_NONE) {
punit->activity_target = NULL;
Expand Down Expand Up @@ -1704,6 +1707,16 @@ static bool handle_unit_packet_common(struct unit *packet_unit)
need_units_report_update = true;
}

if (punit->name != packet_unit->name) {
// Name changed
punit->name = packet_unit->name;

if (unit_is_in_focus(punit)) {
// Update the orders menu -- the name is shown there
need_menus_update = true;
}
}

if (punit->hp != packet_unit->hp) {
// hp changed
punit->hp = packet_unit->hp;
Expand Down
20 changes: 15 additions & 5 deletions client/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,21 @@ const QString popup_info_text(struct tile *ptile)
if (!client_player() || owner == client_player()) {
struct city *hcity = player_city_by_number(owner, punit->homecity);

// TRANS: "Unit: <unit type> | <username> (<nation + team>)"
str = str
+ QString(_("Unit: %1 | %2 (%3)"))
.arg(utype_name_translation(ptype), username, nation)
+ qendl();
if (punit->name.isEmpty()) {
// TRANS: "Unit: <unit type> | <username> (<nation + team>)"
str = str
+ QString(_("Unit: %1 | %2 (%3)"))
.arg(utype_name_translation(ptype), username, nation)
+ qendl();
} else {
// TRANS: "Unit: <unit type> "<unit name>" | <username> (<nation +
// team>)"
str = str
+ QString(_("Unit: %1 \"%2\" | %3 (%4)"))
.arg(utype_name_translation(ptype), punit->name,
username, nation)
+ qendl();
}
if (game.info.citizen_nationality
&& unit_nationality(punit) != unit_owner(punit)) {
if (hcity != NULL) {
Expand Down
7 changes: 7 additions & 0 deletions common/networking/packets.def
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ PACKET_UNIT_INFO = 63; sc, lsend, is-game-info, cancel(PACKET_UNIT_SHORT_INFO)
EXTRA changed_from_tgt;

SINT8 battlegroup;
STRING name[MAX_LEN_NAME];

BOOL has_orders;
UINT16 orders_length, orders_index;
Expand All @@ -1059,6 +1060,7 @@ PACKET_UNIT_SHORT_INFO = 64; sc, lsend, is-game-info, force, cancel(PACKET_UNIT_

UINT8 veteran;
BOOL occupied, transported;
STRING name[MAX_LEN_NAME];

UINT8 hp, activity;
EXTRA activity_tgt;
Expand Down Expand Up @@ -1152,6 +1154,11 @@ PACKET_UNIT_ACTIONS = 90; sc, dsend
ACT_PROB action_probabilities[MAX_NUM_ACTIONS];
end

PACKET_UNIT_RENAME = 91; cs, dsend
UNIT unit_id;
STRING name[MAX_LEN_NAME];
end

PACKET_UNIT_CHANGE_ACTIVITY = 222; cs, dsend
UNIT unit_id;
ACTIVITY activity;
Expand Down
1 change: 1 addition & 0 deletions common/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ struct unit {
struct player *nationality;
int id;
int homecity;
QString name;

int upkeep[O_LAST]; // unit upkeep with regards to the homecity

Expand Down
8 changes: 8 additions & 0 deletions server/savegame/savegame3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5668,6 +5668,12 @@ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr,
sg_warn_ret_val(secfile_lookup_int(loading->file, &punit->homecity,
"%s.homecity", unitstr),
false, "%s", secfile_error());

if (auto name = secfile_lookup_str(loading->file, "%s.name", unitstr)) {
// Support earlier saves
punit->name = QString::fromUtf8(name);
}

sg_warn_ret_val(secfile_lookup_int(loading->file, &punit->moves_left,
"%s.moves", unitstr),
false, "%s", secfile_error());
Expand Down Expand Up @@ -6206,6 +6212,8 @@ static void sg_save_player_units(struct savedata *saving, struct player *plr)
secfile_insert_int(saving->file, punit->veteran, "%s.veteran", buf);
secfile_insert_int(saving->file, punit->hp, "%s.hp", buf);
secfile_insert_int(saving->file, punit->homecity, "%s.homecity", buf);
secfile_insert_str(saving->file, punit->name.toUtf8(), "%s.name", buf);

secfile_insert_str(saving->file, unit_rule_name(punit),
"%s.type_by_name", buf);

Expand Down
14 changes: 14 additions & 0 deletions server/unithand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,20 @@ void handle_unit_get_actions(struct connection *pc, const int actor_unit_id,
}
}

/**
* Handle request to rename a unit.
*/
void handle_unit_rename(player *pplayer, int unit_id, const char *name)
{
auto unit = game_unit_by_number(unit_id);
fc_assert_ret(unit != nullptr);
fc_assert_ret(pplayer == unit->owner);

// Use the QByteArray overload to prevent unbounded read
unit->name = QString::fromUtf8(name, MAX_LEN_NAME);
send_unit_info(NULL, unit);
}

/**
Try to explain to the player why an action is illegal.

Expand Down
2 changes: 2 additions & 0 deletions server/unittools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,7 @@ void package_unit(struct unit *punit, struct packet_unit_info *packet)
packet->tile = tile_index(unit_tile(punit));
packet->facing = punit->facing;
packet->homecity = punit->homecity;
fc_strlcpy(packet->name, punit->name.toUtf8(), ARRAY_SIZE(packet->name));
output_type_iterate(o) { packet->upkeep[o] = punit->upkeep[o]; }
output_type_iterate_end;
packet->veteran = punit->veteran;
Expand Down Expand Up @@ -2624,6 +2625,7 @@ void package_short_unit(struct unit *punit,
packet->type = utype_number(unit_type_get(punit));
packet->hp = punit->hp;
packet->occupied = (get_transporter_occupancy(punit) > 0);
fc_strlcpy(packet->name, punit->name.toUtf8(), ARRAY_SIZE(packet->name));
if (punit->activity == ACTIVITY_EXPLORE
|| punit->activity == ACTIVITY_GOTO) {
packet->activity = ACTIVITY_IDLE;
Expand Down
2 changes: 1 addition & 1 deletion utility/fc_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define VERSION_STRING "3.0.1337.1-haxx"
#endif

#define NETWORK_CAPSTRING "+Freeciv21.21March06"
#define NETWORK_CAPSTRING "+Freeciv21.21April06"

#ifndef FOLLOWTAG
#define FOLLOWTAG "S_HAXXOR"
Expand Down