-
Notifications
You must be signed in to change notification settings - Fork 2
/
ship.c
141 lines (112 loc) · 2.98 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
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
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "ship.h"
#include "cargo.h"
#include "common.h"
#include "item.h"
#include "stringtrie.h"
static void ship_init(struct ship *ship)
{
memset(ship, 0, sizeof(*ship));
INIT_LIST_HEAD(&ship->list);
INIT_LIST_HEAD(&ship->cargo);
st_init(&ship->cargo_names);
pthread_rwlock_init(&ship->cargo_lock, NULL);
}
void ship_free(struct ship *ship)
{
free(ship->name);
pthread_rwlock_destroy(&ship->cargo_lock);
st_destroy(&ship->cargo_names, ST_DONT_FREE_DATA);
struct cargo *c, *_c;
list_for_each_entry_safe(c, _c, &ship->cargo, list) {
list_del(&c->list);
cargo_free(c);
free(c);
}
}
int ship_go(struct ship *ship, enum postype postype, void *pos)
{
ship->postype = postype;
ship->pos = pos;
return 0;
}
int new_ship_to_player(struct ship_type *ship_type, struct player *player)
{
struct ship *ship;
ship = malloc(sizeof(*ship));
if (!ship)
return -1;
ship_init(ship);
ship->name = strdup("Le Fancy Ship With Fancy Name");
if (!ship->name)
goto err;
ship->type = ship_type;
ship->owner = player;
list_add(&ship->list, &player->ships);
return 0;
err:
ship_free(ship);
free(ship);
return -1;
}
static struct cargo* new_cargo_to_ship(struct ship * const ship, const struct cargo * const cargo)
{
struct cargo *ship_cargo;
ship_cargo = malloc(sizeof(*ship_cargo));
if (!ship_cargo)
return NULL;
cargo_init(ship_cargo);
ship_cargo->item = cargo->item;
ship_cargo->max = LONG_MAX; /* FIXME */
if (st_add_string(&ship->cargo_names, cargo->item->name, ship_cargo)) {
cargo_free(ship_cargo);
free(ship_cargo);
return NULL;
}
list_add(&ship_cargo->list, &ship->cargo);
return ship_cargo;
}
/*
* Must be called with the appropriate locks (i.e. ship->cargo_lock) held
*/
int move_cargo_to_ship(struct ship * const ship, struct cargo * const cargo, long amount)
{
assert(cargo->amount >= 0);
struct cargo *ship_cargo = st_lookup_string(&ship->cargo_names, cargo->item->name);
if (!ship_cargo) {
ship_cargo = new_cargo_to_ship(ship, cargo);
if (!ship_cargo)
return -1;
}
assert(ship_cargo->amount <= ship_cargo->max);
amount = MIN(amount, cargo->amount);
amount = MIN(amount, ship_cargo->max - ship_cargo->amount);
cargo->amount -= amount;
ship_cargo->amount += amount;
return amount;
}
/*
* Must be called with the appropriate locks (i.e. ship->cargo_lock) held
*/
int move_cargo_from_ship(struct ship * const ship, struct cargo * const cargo, long amount)
{
struct cargo *ship_cargo;
assert(cargo->amount >= 0);
assert(cargo->amount <= cargo->max);
ship_cargo = st_lookup_string(&ship->cargo_names, cargo->item->name);
assert(ship_cargo);
amount = MIN(amount, ship_cargo->amount);
amount = MIN(amount, cargo->max - cargo->amount);
ship_cargo->amount -= amount;
cargo->amount += amount;
if (!ship_cargo->amount) {
st_rm_string(&ship->cargo_names, cargo->item->name);
list_del(&ship_cargo->list);
cargo_free(ship_cargo);
free(ship_cargo);
}
return amount;
}