1 /*****************************************************************************
2  **  This is part of the SpaceZero program
3  **  Copyright(C) 2006-2013  MRevenga
4  **
5  **  This program is free software; you can redistribute it and/or modify
6  **  it under the terms of the GNU General Public License (version 3), or
7  **  (at your option) any later version, as published by the Free Software
8  **  Foundation.
9  **
10  **  This program is distributed in the hope that it will be useful,
11  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  **  GNU General Public License for more details.
14  **
15  **  You should have received a copy of the GNU General Public License
16  **  along with this program; if not, write to the Free Software
17  **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ******************************************************************************/
19 
20 /*************  SpaceZero  M.R.H. 2006-2013 ******************
21 		Author: MRevenga
22 		E-mail: mrevenga at users.sourceforge.net
23 		version 0.86 December 2013
24 **************************************************************/
25 
26 
27 #include <stdlib.h>
28 #include "players.h"
29 #include "objects.h"
30 #include "spacecomm.h"
31 #include "ai.h"
32 #include "functions.h"
33 #include "help.h"
34 
35 
36 struct Player *players;
37 
InitPlayers(void)38 struct Player *InitPlayers(void){
39   int np;
40   static int sw=0;
41 
42   np=GameParametres(GET,GNPLAYERS,0)+2;
43   if(sw==0){
44     players=malloc(np*sizeof(struct Player)); /* +1 system +1 pirates*/
45     if(players==NULL){
46       fprintf(stderr,"ERROR in malloc (players)\n");
47       exit(-1);
48     }
49     MemUsed(MADD,np*sizeof(struct Player));
50     sw++;
51   }
52 
53   else{
54     players=realloc(players,np*sizeof(struct Player));
55     if(players==NULL){
56       fprintf(stderr,"ERROR in realloc Execload(players)\n");
57       exit(-1);
58     }
59   }
60   return(players);
61 }
62 
CreatePlayers(char * clientname,struct Parametres param)63 void CreatePlayers(char *clientname,struct Parametres param){
64   int i,j;
65 
66   players=InitPlayers();
67 
68   CreateCCData();
69 
70   for(i=0;i<GameParametres(GET,GNPLAYERS,0)+2;i++){
71     snprintf(players[i].playername,MAXTEXTLEN,"comp%d",i);
72     players[i].id=i;
73     players[i].status=PLAYERMODIFIED;
74     players[i].pid=GameParametres(GET,GNPLANETS,0)+1;
75     players[i].proc=0;
76     players[i].control=COMPUTER;
77     players[i].team=i+1;
78     players[i].profile=PLAYERPROFDEFAULT;
79     players[i].strategy=PLAYERSTRATWEAK;
80     /* strategy is random weight choosed at WarCCPlanets() */
81     players[i].gmaxlevel=0;
82     players[i].maxlevel=0;
83     players[i].cv=0;
84     players[i].color=i;
85     players[i].nplanets=0;
86     players[i].nships=0;
87     players[i].nbuildships=0;
88     players[i].gold=10000*RESOURCEFACTOR;
89     players[i].lastaction=0;
90     players[i].ndeaths=0;
91     players[i].nkills=0;
92     players[i].points=0;
93     players[i].modified=SENDOBJUNMOD;
94     players[i].ttl=2000;
95     players[i].goldships=0;
96     players[i].goldupdates=0;
97     players[i].goldweapon=0;
98 
99     players[i].kplanets=NULL;
100     players[i].ksectors.n=0;
101     players[i].ksectors.n0=0;
102     players[i].ksectors.list=NULL;
103     for(j=0;j<NINDEXILIST;j++){
104       players[i].ksectors.index[j]=NULL;
105     }
106 
107     /* player control and assigment of processors */
108     players[i].control=COMPUTER;
109     players[i].proc=0;
110     if(i==1){
111       players[i].control=HUMAN;
112     }
113     if(GameParametres(GET,GNET,0)==TRUE){
114       if(i==1){
115 	players[i].proc=1;
116 	snprintf(players[i].playername,MAXTEXTLEN,"%s",PLAYERNAME);
117 	if(strlen(clientname)>0){
118 	  snprintf(players[i].playername,MAXTEXTLEN,"%s",clientname);
119 	}
120       }
121       if(i==2){
122 	players[i].control=HUMAN;
123 	snprintf(players[i].playername,MAXTEXTLEN,"%s",PLAYERNAME);
124 	if(strlen(param.playername)>0){
125 	  snprintf(players[i].playername,MAXTEXTLEN,"%s",param.playername);
126 	}
127       }
128     }
129     else{
130       if(i==1){
131 	players[i].control=HUMAN;
132 	if(strlen(param.playername)>0){
133 	  snprintf(players[i].playername,MAXTEXTLEN,"%s",param.playername);
134 	}
135       }
136     }
137   }
138 
139   snprintf(players[GameParametres(GET,GNPLAYERS,0)+1].playername,MAXTEXTLEN,"%s","pirates");
140 
141 }
142 
143 
CreateTeams(struct Parametres param)144 void CreateTeams(struct Parametres param){
145   int i,nteam=1;
146 
147 
148   players[0].team=1;   /* Universe objects */
149 
150   /* default mode:  All against all */
151 
152   for(i=0;i<GameParametres(GET,GNPLAYERS,0)+2;i++){
153     players[i].team=i+1;
154   }
155   nteam=2;
156 
157   /* human players */
158   for(i=1;i<GameParametres(GET,GNPLAYERS,0)+2;i++){
159     if(players[i].control==HUMAN){
160       players[i].team=nteam;
161       if(param.cooperative==FALSE){
162 	nteam++;
163       }
164     }
165   }
166   if(param.cooperative==TRUE){
167     nteam++;
168   }
169 
170   /* computer players */
171   for(i=1;i<GameParametres(GET,GNPLAYERS,0)+1;i++){
172     if(players[i].control==COMPUTER){
173       players[i].team=nteam;
174       if(param.compcooperative==FALSE){
175 	nteam++;
176       }
177     }
178   }
179   if(param.compcooperative==TRUE){
180     nteam++;
181   }
182 
183   /* pirate player*/
184   i=GameParametres(GET,GNPLAYERS,0)+1;
185   players[i].team=nteam;
186 }
187 
188 
GetPlayers(void)189 struct Player *GetPlayers(void){
190   return players;
191 }
192 
GetPlayer(int i)193 struct Player *GetPlayer(int i){
194 
195 return &players[i];
196 }
197 
198 
199 
200 
GetPlayerControl(int player)201 int GetPlayerControl(int player){
202   return(players[player].control);
203 }
204 
205 
PrintTeams(void)206 void PrintTeams(void){
207   int i;
208   for(i=1;i<GameParametres(GET,GNPLAYERS,0)+2;i++){
209     printf("PLAYER %d TEAM %d ",i,players[i].team);
210     if(players[i].control==HUMAN)printf("HUMAN");
211     if(players[i].control==COMPUTER)printf("COMPUTER");
212     printf(" name: \"%s\" ",players[i].playername);
213     printf("STRATEGY: %d %d",players[i].strategy,players[i].profile);
214     printf("\n");
215   }
216 }
217 
PrintPlayerResume(void)218 void PrintPlayerResume(void){
219   int i;
220   for(i=0;i<GameParametres(GET,GNPLAYERS,0)+2;i++){
221     printf("Player: %d gold used: ship: %d updates: %d weapon: %f\n",
222 	   players[i].id,players[i].goldships,players[i].goldupdates,players[i].goldweapon);
223   }
224 
225 }
226 
227 
228 
229 
AddPlayerGold(int player,int n)230 void AddPlayerGold(int player,int n){
231   players[player].gold+=n;
232 }
GetPlayerProc(int player)233 int GetPlayerProc(int player){
234   return(players[player].proc);
235 }
236