Skip to content

Commit 181e9c6

Browse files
committed
Merge git://github.com/raduprv/Eternal-Lands
2 parents 508b5a7 + 46671d1 commit 181e9c6

File tree

1 file changed

+57
-64
lines changed

1 file changed

+57
-64
lines changed

new_actors.c

+57-64
Original file line numberDiff line numberDiff line change
@@ -321,35 +321,47 @@ void custom_path(char * path, char * custom1, char * custom2) {
321321
}
322322
#endif //CUSTOM_LOOK
323323

324-
void actor_wear_item(int actor_id,Uint8 which_part, Uint8 which_id)
324+
/*
325+
* Common function to return only the player name in the specified buffer, stripped of possible leading colour
326+
* codes and terminated at the end of the name, before any guild tag.
327+
* The function returns index in name_buf of the character immediately after the name.
328+
*/
329+
static size_t get_onlyname(const char *name_buf, size_t name_buf_size, char *onlyname_buf, size_t onlyname_buf_size)
325330
{
326-
int i;
327-
#ifdef CUSTOM_LOOK
328-
char playerpath[256], guildpath[256], onlyname[32]={0};
329-
int j;
330-
#endif
331+
size_t i, j;
332+
333+
/* step past any leading colour codes */
334+
for (i = 0; (name_buf[i] != '\0') && (i < name_buf_size) && is_color(name_buf[i]); i++) /* do nothing */;
331335

336+
/* copy characters, terminate at the end of the name or the colour code before the guild tag */
337+
for (j = 0; (name_buf[i] != '\0') && (i < name_buf_size) && is_printable(name_buf[i]) && (j < (onlyname_buf_size - 1)); i++, j++)
338+
onlyname_buf[j] = name_buf[i];
339+
340+
/* if the name index is now on the colour code after the space bewtween name and guild tag, move back to the space */
341+
if ((name_buf[i] != '\0') && !is_printable(name_buf[i]) && (i > 0) && (j > 0) && (name_buf[i-1] == ' '))
342+
{
343+
i--;
344+
j--;
345+
}
346+
onlyname_buf[j] = '\0';
347+
my_tolower(onlyname_buf);
348+
349+
return i;
350+
}
351+
352+
void actor_wear_item(int actor_id,Uint8 which_part, Uint8 which_id)
353+
{
354+
size_t i;
332355

333356
for(i=0;i<max_actors;i++)
334357
{
335358
if(actors_list[i])
336359
if(actors_list[i]->actor_id==actor_id)
337360
{
338361
#ifdef CUSTOM_LOOK
362+
char playerpath[256], guildpath[256], onlyname[32]={0};
363+
get_onlyname(actors_list[i]->actor_name, sizeof(actors_list[i]->actor_name), onlyname, sizeof(onlyname));
339364
safe_snprintf(guildpath, sizeof(guildpath), "custom/guild/%d/", actors_list[i]->body_parts->guild_id);
340-
for(j=0;j<30;j++){
341-
if(actors_list[i]->actor_name[j]==' ' || actors_list[i]->actor_name[j]>125){
342-
j=31;
343-
}
344-
else if(actors_list[i]->actor_name[0]>'z'){
345-
onlyname[j]=actors_list[i]->actor_name[j+1];
346-
}
347-
else
348-
{
349-
onlyname[j]=actors_list[i]->actor_name[j];
350-
}
351-
}
352-
my_tolower(onlyname);
353365
safe_snprintf(playerpath, sizeof(playerpath), "custom/player/%s/", onlyname);
354366
#endif
355367
if (which_part==KIND_OF_WEAPON)
@@ -621,10 +633,11 @@ void add_enhanced_actor_from_server (const char *in_data, int len)
621633
int dead=0;
622634
int kind_of_actor;
623635
enhanced_actor *this_actor;
636+
#ifdef CUSTOM_LOOK
624637
char playerpath[256], guildpath[256];
638+
#endif
625639
char onlyname[32]={0};
626-
Uint32 j;
627-
Uint32 uniq_id; // - Post ported.... We'll come up with something later...
640+
Uint32 uniq_id = 0; // - Post ported.... We'll come up with something later...
628641
Uint32 guild_id;
629642
double f_x_pos,f_y_pos,f_z_rot;
630643
float scale=1.0f;
@@ -811,54 +824,34 @@ void add_enhanced_actor_from_server (const char *in_data, int len)
811824

812825
/* build a clean player name and a guild id */
813826
{
814-
/* get the name string into a working buffer */
815-
char buffer[256], *name, *guild;
816827
#ifdef UID
817-
safe_strncpy(buffer,&in_data[32],sizeof(buffer));
828+
const size_t name_index = 32;
818829
#else
819-
safe_strncpy(buffer,&in_data[28],sizeof(buffer));
820-
uniq_id = 0;
830+
const size_t name_index = 28;
821831
#endif
832+
#ifdef NEW_EYES
833+
const size_t max_name_len = len - name_index - 5;
834+
#else
835+
const size_t max_name_len = len - name_index - 4;
836+
#endif
837+
size_t guild_name_index = get_onlyname(&in_data[name_index], max_name_len, onlyname, sizeof(onlyname));
822838

823-
/* skip leading color codes */
824-
for (name=buffer; *name && is_color (*name); name++) /* nothing */ ;
825-
/* trim off any guild tag, leaving solely the name (onlyname)*/
826-
for(j=0; name[j] && name[j]>32;j++){
827-
onlyname[j]=name[j];
828-
}
829-
830-
/* search for string end or color mark */
839+
/* the guild tag is space + colour code + name (max 4 characters) */
840+
guild_id = 0;
831841
this_actor->guild_tag_color = 0;
832-
for (guild = name; *guild && is_printable (*guild); guild++);
833-
if (*guild) {
834-
/* separate the two strings */
835-
this_actor->guild_tag_color = from_color_char (*guild);
836-
*guild = 0;
837-
guild++;
838-
}
839-
840-
/* perform case insensitive comparison/hashing */
841-
my_tolower(name);
842-
my_tolower(onlyname);
843-
844-
//perfect hashing of guildtag
845-
switch(strlen(guild))
846-
{
847-
case 0:
848-
guild_id = 0;
849-
break;
850-
case 1:
851-
guild_id = guild[0];
852-
break;
853-
case 2:
854-
guild_id = guild[0] + (guild[1] << 8);
855-
break;
856-
case 3:
857-
guild_id = guild[0] + (guild[1] << 8) + (guild[2] << 16);
858-
break;
859-
default:
860-
guild_id = guild[0] + (guild[1] << 8) + (guild[2] << 16) + (guild[3] << 24);
861-
break;
842+
if (guild_name_index < max_name_len)
843+
{
844+
const char *guild = &in_data[name_index + guild_name_index];
845+
size_t guild_tag_len = strnlen(guild, max_name_len - guild_name_index);
846+
/* get the colour code and hash of guildtag */
847+
if ((guild_tag_len > 2) && (guild_tag_len < 7) && (guild[0] == ' ') && is_color(guild[1]))
848+
{
849+
unsigned int shift = 0;
850+
size_t j;
851+
this_actor->guild_tag_color = from_color_char(guild[1]);
852+
for (j = 0; (j < (guild_tag_len - 2)) && (j < 4); j++, shift += 8)
853+
guild_id += guild[2+j] << shift;
854+
}
862855
}
863856
}
864857

0 commit comments

Comments
 (0)