File: player.cc

package info (click to toggle)
starvoyager 0.4.4-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 1,508 kB
  • ctags: 922
  • sloc: cpp: 7,651; ansic: 825; sh: 129; makefile: 93
file content (276 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
	player.cc
	
	(c) Richard Thrippleton
	Licensing terms are in the 'LICENSE' file
	If that file is not included with this source then permission is not given to use this source in any way whatsoever.
*/

#include <stdio.h>
#include <string.h>
#include "calc.h"
#include "ship.h"
#include "error.h"
#include "planet.h"
#include "database.h"
#include "alliance.h"
#include "server.h"
#include "player.h"

long lcount=0;

player::player()
{
	lcount++;
}

player::player(char* nam)
{
	self=-1;
	nam[32]='\0';
	for(int i=0;i<ISIZE && self==-1;i++)
	{
		if(!players[i])
			self=i;
	}
	if(self==-1)
		throw error("No more space for new accounts");
	players[self]=this;
	sprintf(this->nam,"%s",nam);
	pass[0]='\0';
	in=NULL;
	if(self==0)
		op=true;
	else
		op=false;
	mshp=NULL;
	cash=0;
	cashi=0;
	lcount++;
}

player::~player()
{
	server::notifydelete(this);
	if(self>=0 && self<ISIZE)
		players[self]=NULL;
	if(mshp)
		delete mshp;
	if(in)
		delete in;
	lcount--;
}

void player::init()
{
	for(int i=0;i<ISIZE;i++)
	{
		players[i]=NULL;
	}
}

void player::purgeall()
{
	for(int i=0;i<ISIZE;i++)
		if(players[i])
			delete players[i];
}

void player::saveall()
{
	char obsc[33]; //Object name scratchpad

	for(int i=0;i<ISIZE;i++)
	{
		if(players[i] && players[i]->mshp)
		{
			sprintf(obsc,"Account%hd",i);
			database::putobject(obsc);
			players[i]->save();
		}
	}
}

void player::loadall()
{
	char obsc[33]; //Object name scratchpad

	for(int i=0;i<ISIZE;i++)
	{
		try
		{
			sprintf(obsc,"Account%hd",i);
			database::switchobj(obsc);
			players[i]=new player();
			players[i]->self=i;
			players[i]->load();
		}
		catch(error it)
		{
		}
	}
}

player* player::get(char* nam)
{
	for(int i=0;i<ISIZE;i++)
		if(players[i] && strcmp(nam,players[i]->nam)==0)
			return players[i];
	return NULL;
}

void player::spawn(alliance* tali)
{
	planet* tpln; //Planet to spawn near
	cord sloc; //Location to spawn at

	tpln=planet::pick(tali);
	if(tpln)
	{	
		sloc=tpln->loc;
		sloc.x+=calc::rnd(150)-calc::rnd(150);
		sloc.y+=calc::rnd(150)-calc::rnd(150);
		if(mshp)
			delete mshp;
		if(!(tali->spw))
		{
			throw error("Cannot play for this alliance");
		}
		try
		{
			mshp=new ship(sloc,tali->spw,tali,ship::AI_NULL);
		}
		catch(error it)
		{
			throw it;
		}
		in=new ship();
		*in=*mshp;
		in->assign(this);
		try
		{
			in->insert();
			server::bulletin("%s entered the game",nam);
		}
		catch(error it)
		{
			delete in;
			throw it;
		}
	}
	else
		throw error("Can't find a suitable body to spawn at");
}

void player::login(char* pass)
{
	if(in)
		throw error("Already logged in");
	if(!mshp)
		throw error("No ship associated with this user");
	if(pass)
	{
		pass[32]='\0';
		if(pass[0]=='\0')
			throw error("Invalid password");
		calc::obscure(pass);
		if(strcmp(pass,this->pass)!=0)
			throw error("Invalid password");
	}
	in=new ship();
	*in=*mshp;
	in->assign(this);
	try
	{
		in->insert();
		server::bulletin("%s entered the game",nam);
	}
	catch(error it)
	{
		delete in;
		throw it;
	}
	cashi=cash;
}

void player::setpass(char* pass)
{
	sprintf(this->pass,"%s",pass);
	calc::obscure(this->pass);
}

void player::commit()
{
	if(!in)
		return;
	if(mshp)
	{
		mshp->ply=NULL;
		delete mshp;
	}
	mshp=new ship();
	*mshp=*in;
	mshp->self=-1;
	cash=cashi;
}

void player::transfer(ship* tshp)
{
	in->assign(NULL);
	in->frnd=tshp;
	in->aity=ship::AI_FLEET;
	tshp->all=in->all;
	in=tshp;
	in->assign(this);
}

void player::debit(long amt)
{
	if(amt>cashi)
		throw error("Not enough credits");
	cashi-=amt;
}

void player::credit(long amt)
{
	cashi+=amt;
	if(cashi>999999999)
		cashi=999999999;
}
	
void player::notifydelete()
{
	server::notifykill(this);
	in=NULL;
}

void player::logout()
{
	if(in)
		delete in;
	server::bulletin("%s left the game",nam);
	if(!mshp || pass[0]=='\0')
		delete this;
}

void player::save()
{
	database::putvalue("Name",nam);
	database::putvalue("Password",pass);
	database::putvalue("Op",op);
	database::putvalue("Cash",cash);
	mshp->save();
}

void player::load()
{
	in=NULL;
	database::getvalue("Name",nam);
	database::getvalue("Password",pass);
	op=database::getvalue("Op");
	cash=database::getvalue("Cash");
	mshp=new ship();
	mshp->load();
}

player* player::players[ISIZE];