-
Notifications
You must be signed in to change notification settings - Fork 4
/
ship.c
98 lines (83 loc) · 1.94 KB
/
ship.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
#include "ship.h"
#include "rtl.h"
#include <quicklist.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
quicklist *shiptypes = 0;
const char *shipnames[] = {
"longboat",
"clipper",
"galleon",
};
ship *create_ship(int no, const struct ship_type *type) {
ship *s = (ship *)calloc(1, sizeof(ship));
assert(type);
assert(no>0);
if (s) {
s->type = type;
s->left = 0;
s->no = no;
}
return s;
}
void free_ship(ship *s) {
free(s->name_);
free(s->display_);
free(s);
}
const char * ship_getname(const struct ship *self)
{
return self->name_;
}
void ship_setname(struct ship *self, const char *name)
{
assert(name);
self->name_ = (char *)realloc(self->name_, strlen(name)+1);
strcpy(self->name_, name);
}
const char * ship_getdisplay(const struct ship *self)
{
return self->display_;
}
void ship_setdisplay(struct ship *self, const char *display)
{
if (display) {
self->display_ = (char *)realloc(self->display_, strlen(display)+1);
strcpy(self->display_, display);
} else {
free(self->display_);
self->display_ = 0;
}
}
int ship_speed(const struct ship *sh)
{
return sh->type->speed;
}
struct ship_type *create_shiptype(const char *name)
{
ship_type * stype = (ship_type *)calloc(1, sizeof(ship_type));
stype->name = _strdup(name);
ql_push(&shiptypes, stype);
return stype;
}
void free_shiptype(struct ship_type *stype) {
free(stype->name);
free(stype);
}
struct ship_type *get_shiptype(ship_t type) {
assert(type<NUMSHIPS);
return get_shiptype_by_name(shipnames[type]);
}
struct ship_type *get_shiptype_by_name(const char *name) {
ql_iter qli = qli_init(&shiptypes);
assert(name);
while (qli_more(qli)) {
ship_type *stype = (ship_type *)qli_next(&qli);
if (strcmp(name, stype->name)==0) {
return stype;
}
}
return 0;
}