/
inventory.js
127 lines (106 loc) · 2.98 KB
/
inventory.js
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
'use strict';
const deepEqual = require('deep-equal');
const ItemPile = require('itempile');
const EventEmitter = require('events').EventEmitter;
class Inventory extends EventEmitter {
constructor(xSize, ySize, opts) {
super();
if (xSize === undefined) xSize = 10;
if (ySize === undefined) ySize = 1;
if (xSize <= 0) throw new Error(`inventory invalid xSize: ${xSize}`);
if (ySize <= 0) throw new Error(`inventory invalid xSize: ${ySize}`);
const size = xSize * ySize
this.array = new Array(size);
this.width = xSize;
this.height = ySize;
}
changed() {
this.emit('changed');
}
give(itemPile) {
let excess = itemPile.count;
// first add to existing piles
for (let i = 0; i < this.array.length; ++i) {
if (this.array[i] !== undefined && this.array[i].canPileWith(itemPile)) {
excess = this.array[i].mergePile(itemPile);
}
if (itemPile.count === 0) break;
}
// then if we have to, add to empty slots
for (let i = 0; i < this.array.length; ++i) {
if (this.array[i] === undefined) {
// start with an 'empty pile' to merge into TODO: improve this hack
this.array[i] = itemPile.clone();
this.array[i].count = 0;
excess = this.array[i].mergePile(itemPile);
if (this.array[i].count === 0) this.array[i] = undefined;
}
if (itemPile.count == 0) break;
}
this.changed();
// what didn't fit
return excess;
}
take(itemPile) {
for (let i = 0; i < this.array.length; ++i) {
if (this.array[i] !== undefined && this.array[i].matchesTypeAndTags(itemPile)) {
const n = Math.min(itemPile.count, this.array[i].count);
itemPile.count -= n;
const given = this.takeAt(i, n); // TODO: return?
}
}
return this.changed();
}
takeAt(position, count) {
if (!this.array[position]) return false;
const ret = this.array[position].splitPile(count);
if (this.array[position].count === 0) {
this.array[position] = undefined;
}
this.changed();
return ret;
}
toString() {
const a = [];
for (let itemPile of this.array) {
if (itemPile === undefined) {
a.push('')
} else {
a.push(`${itemPile}`);
}
}
return a.join('\t');
}
static fromString(s) {
const strings = s.split('\t'); // literal tab not in JSON.stringify
const items = [];
for (let itemString of strings) {
items.push(ItemPile.fromString(itemString));
}
const ret = new Inventory(items.length);
ret.array = items;
return ret;
}
size() {
return this.array.length;
}
get(i) {
return this.array[i];
}
set(i, itemPile) {
this.array[i] = itemPile
return this.changed();
}
clear() {
for (let i = 0; i < this.size(); ++i) {
this.set(i, undefined);
}
}
transferTo(dest) {
for (let i = 0; i < this.size(); ++i) {
dest.set(i, this.get(i));
this.set(i, undefined);
}
}
}
module.exports = Inventory;