Skip to content

Commit

Permalink
Give names to units
Browse files Browse the repository at this point in the history
The name is empty by default, but can be set by sending unit_rename packets. It
is limited to MAX_LEN_NAME which is 48 bytes including the terminating zero.

Requested by jwrober in longturn#281.
  • Loading branch information
lmoureaux committed Apr 6, 2021
1 parent 2df37ef commit 7e9fd01
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
3 changes: 3 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
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
13 changes: 13 additions & 0 deletions server/unithand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,19 @@ 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);
}

/**
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

0 comments on commit 7e9fd01

Please sign in to comment.