updated GPL headers in source code
[dribble.git] / player.c
blob51f102bde2dd6f73dfb8404b8203e1f2d38276df
1 /*
2 dribble - a football manager game
3 Copyright (C) 2008 Jonas Sandberg
5 This file is part of dribble.
7 dribble is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
12 dribble is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with dribble. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "defs.h"
24 enum position { Goalie, Defender, Midfielder, Attacker };
25 const char *position_name[4] = { "Goalie", "Defender", "Midfielder", "Attacker" };
27 struct player
29 int number;
30 int age;
31 int skill;
32 int wage;
33 int pos;
34 const char *name;
37 static int el;
38 static int pnum = 0;
39 static struct player *pvec;
41 int player_init(int elements)
43 el = elements;
44 pvec = calloc(elements, sizeof(*pvec));
46 if(pvec == NULL)
48 return -1;
50 else
52 return 0;
56 int player_generate(int country, int player_index, int division_order_number)
58 struct player *p;
60 p = &pvec[pnum];
61 p->name = country_random_player_name(country);
62 p->number = player_index+2;
63 p->age = 17 + (rand() % 17);
64 p->skill = (rand() % 25) - (division_order_number) + 60 - (rand() % (34 - p->age));
65 p->wage = (p->skill*1200)-(rand()%1500);
66 if(player_index < 2+rand()%2)
68 p->pos = Goalie;
70 else if(player_index < 9+rand()%2)
72 p->pos = Defender;
74 else if(player_index >= 9 && player_index<15+rand()%2)
76 p->pos = Midfielder;
78 else if(player_index >= 15)
80 p->pos = Attacker;
83 return pnum++;
86 void player_print(int player)
88 struct player *p;
89 p = &pvec[player];
90 printf("%3d. %-15s \t Skill: %-3d \t Age: %-3d \t Wage: %-9d \t Position: %-10s\n",
91 p->number,
92 p->name,
93 p->skill,
94 p->age,
95 p->wage,
96 position_name[p->pos]);
99 void player_free()
101 free(pvec);
104 #ifdef FTEST_PLAYER
105 #include <assert.h>
106 #include <time.h>
107 int main(int argc, char *argv[])
109 int italy;
110 int p;
111 srand(time(NULL));
112 italy = country_read(DATADIR "italy.txt");
113 assert(player_init(2) == 0);
114 p = player_generate(italy, 0, 0);
115 assert(p == 0);
116 p = player_generate(italy, 1, 0);
117 assert(p == 1);
118 assert(pvec[p].number == 3);
119 player_free();
120 country_free();
121 return 0;
123 #endif