2005-10-20 17:45:00 +02:00
|
|
|
/*
|
|
|
|
Bygfoot Football Manager -- a small and simple GTK2-based
|
|
|
|
football management game.
|
|
|
|
|
|
|
|
http://bygfoot.sourceforge.net
|
|
|
|
|
|
|
|
Copyright (C) 2005 Gyözö Both (gyboth@bygfoot.com)
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2005-06-20 14:46:57 +02:00
|
|
|
#include "main.h"
|
2005-06-01 20:19:02 +02:00
|
|
|
#include "name.h"
|
|
|
|
#include "option.h"
|
|
|
|
#include "variables.h"
|
|
|
|
#include "xml_name.h"
|
|
|
|
|
|
|
|
/** Get a random player name from the given
|
|
|
|
names list. If the names list is not found, create
|
|
|
|
it from file. If the file can't be found, either,
|
|
|
|
make some fuss and take one from the general names. */
|
2005-10-09 11:35:44 +02:00
|
|
|
gchar*
|
2005-06-01 20:19:02 +02:00
|
|
|
name_get(const gchar *names_file)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
NameList new;
|
|
|
|
|
|
|
|
if(name_lists->len > 1 &&
|
|
|
|
math_rnd(0, 1) < const_float("float_name_random_list_prob"))
|
|
|
|
return name_get_from_random_list();
|
|
|
|
|
|
|
|
for(i=0;i<name_lists->len;i++)
|
2005-10-09 11:35:44 +02:00
|
|
|
if(strcmp(names_file, nli(i).sid) == 0)
|
2005-06-01 20:19:02 +02:00
|
|
|
return name_get_from_list(&nli(i));
|
|
|
|
|
|
|
|
/** Create new name list. */
|
|
|
|
new.sid = NULL;
|
|
|
|
new.first_names = new.last_names = NULL;
|
|
|
|
|
|
|
|
xml_name_read(names_file, &new);
|
|
|
|
|
|
|
|
if(new.sid == NULL)
|
|
|
|
{
|
2005-06-15 10:22:40 +02:00
|
|
|
g_warning("name_get_new: names file with sid '%s' not found, taking general names file.\n",
|
2005-06-01 20:19:02 +02:00
|
|
|
names_file);
|
|
|
|
return name_get(opt_str("string_opt_player_names_file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stat5 != STATUS_GENERATE_TEAMS)
|
|
|
|
name_shorten_list(&new);
|
|
|
|
|
|
|
|
g_array_append_val(name_lists, new);
|
|
|
|
|
|
|
|
return name_get_from_list(&nli(name_lists->len - 1));
|
|
|
|
}
|
|
|
|
|
2005-10-09 11:35:44 +02:00
|
|
|
/** Return a newly allocated string with a randomly
|
2005-06-01 20:19:02 +02:00
|
|
|
picked combined name from the list. */
|
2005-10-09 11:35:44 +02:00
|
|
|
gchar*
|
2005-06-01 20:19:02 +02:00
|
|
|
name_get_from_list(const NameList *namelist)
|
|
|
|
{
|
|
|
|
gchar buf[SMALL];
|
|
|
|
|
|
|
|
sprintf(buf, "%s %s", name_get_random_first_name(namelist),
|
|
|
|
name_get_random_last_name(namelist));
|
|
|
|
|
2005-10-09 11:35:44 +02:00
|
|
|
return g_strdup(buf);
|
2005-06-01 20:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Shorten a name list so that it doesn't occupy
|
|
|
|
too much memory. */
|
|
|
|
void
|
|
|
|
name_shorten_list(NameList *namelist)
|
|
|
|
{
|
|
|
|
gint idx;
|
|
|
|
|
|
|
|
while(namelist->first_names->len * namelist->last_names->len >
|
|
|
|
const_int("int_name_max_product"))
|
|
|
|
{
|
|
|
|
if((gfloat)(namelist->first_names->len) /
|
|
|
|
(gfloat)(namelist->last_names->len) >
|
|
|
|
const_float("float_name_first_last_ratio"))
|
|
|
|
{
|
|
|
|
idx = math_rndi(0, namelist->first_names->len - 1);
|
2005-10-09 11:35:44 +02:00
|
|
|
g_free(g_ptr_array_index(namelist->first_names, idx));
|
2005-06-01 20:19:02 +02:00
|
|
|
g_ptr_array_remove_index_fast(namelist->first_names, idx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idx = math_rndi(0, namelist->last_names->len - 1);
|
2005-10-09 11:35:44 +02:00
|
|
|
g_free(g_ptr_array_index(namelist->last_names, idx));
|
2005-06-01 20:19:02 +02:00
|
|
|
g_ptr_array_remove_index_fast(namelist->last_names, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-20 14:46:57 +02:00
|
|
|
|
|
|
|
/** Find the namelist with the given sid. */
|
|
|
|
NameList*
|
|
|
|
name_get_list_from_sid(const gchar *sid)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
NameList new;
|
|
|
|
|
|
|
|
for(i=0;i<name_lists->len;i++)
|
2005-10-09 11:35:44 +02:00
|
|
|
if(strcmp(sid, nli(i).sid) == 0)
|
2005-06-20 14:46:57 +02:00
|
|
|
return &nli(i);
|
|
|
|
|
|
|
|
new.sid = NULL;
|
|
|
|
new.first_names = new.last_names = NULL;
|
|
|
|
|
|
|
|
xml_name_read(sid, &new);
|
|
|
|
|
|
|
|
if(new.sid == NULL)
|
2005-10-24 22:50:48 +02:00
|
|
|
main_exit_program(EXIT_POINTER_NOT_FOUND,
|
|
|
|
"name_get_list_from_sid: namelist with sid %s not found", sid);
|
2005-06-20 14:46:57 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
name_shorten_list(&new);
|
|
|
|
g_array_append_val(name_lists, new);
|
|
|
|
return &nli(name_lists->len - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|