forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.c
398 lines (348 loc) · 10.5 KB
/
player.c
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "base.h"
#include "cli.h"
#include "common.h"
#include "server.h"
#include "connection.h"
#include "data.h"
#include "stringtree.h"
#include "log.h"
#include "planet.h"
#include "player.h"
#include "ptrlist.h"
#include "sector.h"
#include "star.h"
#include "universe.h"
#include "names.h"
#define player_talk(PLAYER, ...) \
conn_send(PLAYER->conn, __VA_ARGS__)
void player_free(struct player *player)
{
free(player->name);
cli_tree_destroy(&player->cli);
free(player);
}
static char* hundreths(unsigned long l, char *buf, size_t len)
{
snprintf(buf, len, "%lu.0%lu", l / 100, l % 100);
return buf;
}
static void player_showsector(struct player *player, struct sector *sector)
{
struct sector *t;
struct list_head *lh;
struct star *sol;
struct planet *planet;
char buf[10];
player_talk(player,
"Sector %s (coordinates %ldx%ld), habitability %d\n"
"Habitable zone is from %lu to %lu Gm\n",
sector->name, sector->x, sector->y, sector->hab, sector->hablow, sector->habhigh);
player_talk(player, "Stars:\n");
ptrlist_for_each_entry(sol, §or->stars, lh)
player_talk(player,
" %s: Class %c %s\n"
" Surface temperature: %dK, habitability modifier: %d, luminosity: %s\n",
sol->name, stellar_cls[sol->cls],
stellar_lum[sol->lum], sol->temp, sol->hab,
hundreths(sol->lumval, buf, sizeof(buf)));
if (!list_empty(§or->planets.list)) {
player_talk(player, "Planets:\n");
ptrlist_for_each_entry(planet, §or->planets, lh) {
struct planet_type *type = &planet_types[planet->type];
player_talk(player,
" %s: Class %c (%s)\n"
" Diameter: %u km, distance from main star: %u Gm, atmosphere: %s. %s.\n",
planet->name, type->c, type->name,
planet->dia*100, planet->dist,
type->atmo, planet_life_desc[planet->life]);
}
} else {
player_talk(player, "Sector does not have any planets.\n");
}
if (!list_empty(§or->links.list)) {
player_talk(player, "This sector has hyperspace links to\n");
ptrlist_for_each_entry(t, §or->links, lh)
player_talk(player, " %s\n", t->name);
} else {
player_talk(player, "This sector does not have any hyperspace links.\n");
}
struct ptrlist neigh;
ptrlist_init(&neigh);
player_talk(player, "Sectors within 50 lys are:\n");
get_neighbouring_systems(&neigh, sector, 50 * TICK_PER_LY);
if (!list_empty(&neigh.list)) {
ptrlist_for_each_entry(t, &neigh, lh) {
if (t != sector)
player_talk(player, " %s at %.1f ly\n", t->name,
sector_distance(sector, t) / (double)TICK_PER_LY);
}
}
ptrlist_free(&neigh);
if (sector->owner != NULL) {
player_talk(player, "This sector is owned by civ %s\n", sector->owner->name);
} else {
player_talk(player, "This sector is not part of any civilization\n");
}
}
static void player_showbase(struct player *player, struct base *base)
{
char *o;
if (base->planet)
o = base->planet->name;
else
o = base->sector->name;
struct base_type *type = &base_types[base->type];
player_talk(player,
"Station %s, orbiting %s. %s\n"
"%s\n",
base->name, o, type->name,
type->desc);
}
static void player_describe_base(struct player *player, struct base *base)
{
player_talk(player, "%s\n", base->name);
}
static void player_showplanet(struct player *player, struct planet *planet)
{
struct planet_type *ptype = &planet_types[planet->type];
struct base *base;
struct list_head *lh;
if (planet->gname)
player_talk(player, "Planet %s (%s) in sector %s",
planet->gname, planet->name, planet->sector->name);
else
player_talk(player, "Planet %s in sector %s",
planet->name, planet->sector->name);
player_talk(player,
", class %c (%s).\n"
" Diameter: %u km, distance from main star: %u Gm, atmosphere: %s. %s.\n",
ptype->c, ptype->name,
planet->dia*100, planet->dist, ptype->atmo,
planet_life_desc[planet->life]);
if (!list_empty(&planet->bases.list)) {
player_talk(player, "Bases:\n");
ptrlist_for_each_entry(base, &planet->bases, lh) {
player_talk(player, " ");
player_describe_base(player, base);
}
} else {
player_talk(player, "No bases.\n");
}
if (!list_empty(&planet->stations.list)) {
player_talk(player, "Orbital stations:\n");
ptrlist_for_each_entry(base, &planet->stations, lh) {
player_talk(player, " ");
player_describe_base(player, base);
}
} else {
player_talk(player, "No orbital stations.\n");
}
}
static int cmd_help(void *ptr, char *param)
{
struct player *player = ptr;
/* FIXME: This should use conn_send instead of writing directly to the fd */
FILE *f = fdopen(dup(player->conn->peerfd), "w");
cli_print_help(f, &player->cli);
fclose(f);
return 0;
}
static char cmd_help_help[] = "Short help on available commands";
static int cmd_quit(void *ptr, char *param)
{
struct player *player = ptr;
player_talk(player, "Bye!\n");
server_disconnect_nicely(player->conn);
return 0;
}
static char cmd_quit_help[] = "Log off and terminate connection";
static int cmd_look(void *ptr, char *param)
{
struct player *player = ptr;
switch (player->postype) {
case SECTOR:
player_showsector(player, player->pos);
break;
case BASE:
player_showbase(player, player->pos);
break;
case PLANET:
player_showplanet(player, player->pos);
break;
default:
player_talk(player, "internal error: don't know where you are\n");
}
return 0;
}
static char cmd_look_help[] = "Look around";
static int cmd_hyper(void *ptr, char *param)
{
struct player *player = ptr;
assert(player->postype == SECTOR);
struct sector *sector;
pthread_rwlock_rdlock(&univ.sectornames_lock);
sector = st_lookup_string(&univ.sectornames, param);
pthread_rwlock_unlock(&univ.sectornames_lock);
if (sector == NULL) {
player_talk(player, "Sector not found.\n");
return 1;
}
int ok = 0;
struct sector *tmp;
struct sector *pos = player->pos;
struct list_head *lh;
ptrlist_for_each_entry(tmp, &pos->links, lh) {
if (tmp == sector) {
ok = 1;
break;
}
}
if (ok) {
player_talk(player, "Entering %s\n", sector->name);
player_go(player, SECTOR, sector);
return 0;
} else {
player_talk(player, "No hyperspace link found.\n");
return 1;
}
}
static char cmd_hyper_help[] = "Travel by hyperspace to sector";
static int cmd_jump(void *ptr, char *param)
{
struct player *player = ptr;
struct sector *sector;
pthread_rwlock_rdlock(&univ.sectornames_lock);
sector = st_lookup_string(&univ.sectornames, param);
pthread_rwlock_unlock(&univ.sectornames_lock);
if (sector != NULL) {
player_talk(player, "Jumping to %s\n", sector->name);
player_go(player, SECTOR, sector);
return 0;
}
player_talk(player, "Sector not found.\n");
return 1;
}
static char cmd_jump_help[] = "Travel by jumpdrive to sector";
static int cmd_dock(void *ptr, char *param)
{
struct player *player = ptr;
struct base *base;
pthread_rwlock_rdlock(&univ.basenames_lock);
base = st_lookup_string(&univ.basenames, param);
pthread_rwlock_unlock(&univ.basenames_lock);
if (base && ((player->postype == PLANET && base->planet == player->pos)
|| (player->postype == SECTOR && base->sector == player->pos))) {
player_talk(player, "Docking at %s\n", base->name);
player_go(player, BASE, base);
return 0;
}
player_talk(player, "No base or spacedock found by that name.\n");
return 1;
}
static char cmd_dock_help[] = "Dock at spacedock or base";
static int cmd_orbit(void *ptr, char *param)
{
struct player *player = ptr;
assert(player->postype == SECTOR);
struct sector *sector = player->pos;
struct planet *planet;
pthread_rwlock_rdlock(&univ.planetnames_lock);
planet = st_lookup_string(&univ.planetnames, param);
pthread_rwlock_unlock(&univ.planetnames_lock);
if (planet && planet->sector == sector) {
player_talk(player, "Entering orbit around %s\n", planet->name);
player_go(player, PLANET, planet);
return 0;
}
player_talk(player, "No such planet found in this sector.\n");
return 1;
}
static char cmd_orbit_help[] = "Enter orbit around planet";
static int cmd_leave_base(void *ptr, char *param)
{
struct player *player = ptr;
assert(player->postype == BASE);
struct base *base = player->pos;
if (base->planet)
player_go(player, PLANET, base->planet);
else if (base->sector)
player_go(player, SECTOR, base->sector);
else
bug("%s", "base %s does not have any positional information");
return 0;
}
static char cmd_leave_base_help[] = "Leave base and take off";
static int cmd_leave_planet(void *ptr, char *param)
{
struct player *player = ptr;
assert(player->postype == PLANET);
struct planet *planet = player->pos;
assert(planet->sector);
player_talk(player, "Leaving orbit around %s\n", planet->name);
player_go(player, SECTOR, planet->sector);
return 0;
}
static char cmd_leave_planet_help[] = "Leave planet orbit";
void player_go(struct player *player, enum postype postype, void *pos)
{
switch (player->postype) {
case SECTOR:
cli_rm_cmd(&player->cli, "jump");
cli_rm_cmd(&player->cli, "dock");
cli_rm_cmd(&player->cli, "orbit");
break;
case BASE:
cli_rm_cmd(&player->cli, "leave");
break;
case PLANET:
cli_rm_cmd(&player->cli, "dock");
cli_rm_cmd(&player->cli, "leave");
break;
case NONE:
break;
default:
bug("I don't know where player %s with connection %p is\n", player->name, player->conn);
}
player->postype = postype;
player->pos = pos;
cmd_look(player, NULL);
switch (postype) {
case SECTOR:
cli_add_cmd(&player->cli, "jump", cmd_jump, player, cmd_jump_help);
cli_add_cmd(&player->cli, "dock", cmd_dock, player, cmd_dock_help);
cli_add_cmd(&player->cli, "orbit", cmd_orbit, player, cmd_orbit_help);
break;
case BASE:
cli_add_cmd(&player->cli, "leave", cmd_leave_base, player, cmd_leave_base_help);
break;
case PLANET:
cli_add_cmd(&player->cli, "dock", cmd_dock, player, cmd_dock_help);
cli_add_cmd(&player->cli, "leave", cmd_leave_planet, player, cmd_leave_planet_help);
break;
case NONE:
/* Fall through to default as NONE can only valid right after init */
default:
bug("I don't know where player %s with connection %p is\n", player->name, player->conn);
}
}
int player_init(struct player *player)
{
memset(player, 0, sizeof(*player));
player->name = create_unique_name(&univ.avail_player_names);
if (!player->name)
return -1;
INIT_LIST_HEAD(&player->list);
INIT_LIST_HEAD(&player->cli);
player->postype = NONE;
cli_add_cmd(&player->cli, "help", cmd_help, player, cmd_help_help);
cli_add_cmd(&player->cli, "go", cmd_hyper, player, cmd_hyper_help);
cli_add_cmd(&player->cli, "quit", cmd_quit, player, cmd_quit_help);
cli_add_cmd(&player->cli, "look", cmd_look, player, cmd_look_help);
return 0;
}