gemwarrior 0.15.10 → 0.15.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,243 +1,243 @@
1
- # lib/gemwarrior/inventory.rb
2
- # Collection of items a creature possesses
3
-
4
- require_relative 'game_options'
5
-
6
- module Gemwarrior
7
- class Inventory
8
- # CONSTANTS
9
- ERROR_ITEM_REMOVE_INVALID = 'Your inventory does not contain that item, so you cannot drop it.'
10
- ERROR_ITEM_ADD_UNTAKEABLE = 'That would be great if you could take that, wouldn\'t it? Huh!'
11
- ERROR_ITEM_ADD_INVALID = 'That item cannot be taken or does not exist.'
12
- ERROR_ITEM_DESCRIBE_INVALID = 'That does not seem to be in the inventory.'
13
- ERROR_ITEM_EQUIP_INVALID = 'You do not possess anything called that to equip.'
14
- ERROR_ITEM_EQUIP_NONARMAMENT = 'That item cannot be equipped.'
15
- ERROR_ITEM_UNEQUIP_INVALID = 'You do not possess anything called that to unequip.'
16
- ERROR_ITEM_UNEQUIP_NONARMAMENT = 'That item cannot be unequipped.'
17
- VOWELS = 'aeiou'
18
-
19
- attr_accessor :items,
20
- :weapon,
21
- :armor
22
-
23
- def initialize(items = [], weapon = nil, armor = nil)
24
- self.items = items
25
- self.weapon = weapon
26
- self.armor = armor
27
- end
28
-
29
- def an_words
30
- ['herb']
31
- end
32
-
33
- def article_chooser(word)
34
- (VOWELS.include?(word.to_s[0]) or an_words.include?(word.to_s)) ? 'an' : 'a'
35
- end
36
-
37
- def is_empty?
38
- self.items.nil? || self.items.empty?
39
- end
40
-
41
- # non-English-like contents of inventory for simple lists
42
- def contents
43
- if is_empty?
44
- nil
45
- else
46
- item_hash = {}
47
- self.items.map(&:name).each do |i|
48
- i_sym = i.to_sym
49
- if item_hash.keys.include? i_sym
50
- item_hash[i_sym] += 1
51
- else
52
- item_hash[i_sym] = 1
53
- end
54
- end
55
-
56
- # one item? return string
57
- if item_hash.length == 1
58
- i = item_hash.keys.join
59
- q = item_hash.values.join.to_i
60
- return q > 1 ? "#{i}s x#{q}" : i
61
- # multiple items? return array of strings to mush together
62
- else
63
- item_arr = []
64
- item_hash.each do |i, q|
65
- if q > 1
66
- item_arr.push("#{i}s x#{q}")
67
- else
68
- item_arr.push(i)
69
- end
70
- end
71
-
72
- return item_arr.join(', ')
73
- end
74
- end
75
- end
76
-
77
- # English-like sentence summary of inventory
78
- def list_contents
79
- if is_empty?
80
- 'You possess nothing.'
81
- else
82
- # build hash of inventory's items
83
- item_hash = {}
84
- self.items.map(&:name).each do |i|
85
- i_sym = i.to_sym
86
- if item_hash.keys.include? i_sym
87
- item_hash[i_sym] += 1
88
- else
89
- item_hash[i_sym] = 1
90
- end
91
- end
92
-
93
- # one item? return string
94
- if item_hash.length == 1
95
- i = item_hash.keys.join
96
- q = item_hash.values.join.to_i
97
- if q > 1
98
- return "You have #{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}."
99
- else
100
- return "You have #{self.article_chooser(i)} #{i.to_s.colorize(:yellow)}."
101
- end
102
- # multiple items? return array of strings to mush together
103
- else
104
- item_list_text = 'You have '
105
- item_arr = []
106
- item_hash.each do |i, q|
107
- if q > 1
108
- item_arr.push("#{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}")
109
- else
110
- item_arr.push("#{self.article_chooser(i)} #{i.to_s.colorize(:yellow)}")
111
- end
112
- end
113
-
114
- item_arr[-1].replace("and #{item_arr[-1]}.")
115
-
116
- return item_list_text << item_arr.join(', ')
117
- end
118
- end
119
- end
120
-
121
- def contains_item?(item_name)
122
- self.items.map{ |i| i.name.downcase }.include?(item_name.downcase)
123
- end
124
-
125
- def contains_battle_item?
126
- battle_item_found = false
127
- self.items.each do |i|
128
- battle_item_found = true if i.useable_battle
129
- end
130
- battle_item_found
131
- end
132
-
133
- def list_battle_items
134
- battle_items = []
135
- self.items.each do |i|
136
- battle_items.push(i) if i.useable_battle
137
- end
138
- battle_items
139
- end
140
-
141
- def describe_item(item_name, world)
142
- if contains_item?(item_name)
143
- self.items.each do |i|
144
- if i.name.eql?(item_name)
145
- if GameOptions.data['debug_mode']
146
- return i.describe_detailed(world)
147
- else
148
- return i.describe(world)
149
- end
150
- end
151
- end
152
- else
153
- ERROR_ITEM_DESCRIBE_INVALID
154
- end
155
- end
156
-
157
- def equip_item(item_name)
158
- if contains_item?(item_name)
159
- self.items.each do |i|
160
- if i.name.eql?(item_name)
161
- if i.equippable
162
- i.equipped = true
163
- if i.is_weapon
164
- self.weapon = i
165
- return "The #{i.name.colorize(:yellow)} has taken charge, and been equipped."
166
- elsif i.is_armor
167
- self.armor = i
168
- return "The #{i.name.colorize(:yellow)} has fortified you, and been equipped."
169
- end
170
- else
171
- return ERROR_ITEM_EQUIP_NONARMAMENT
172
- end
173
- end
174
- end
175
- else
176
- ERROR_ITEM_EQUIP_INVALID
177
- end
178
- end
179
-
180
- def unequip_item(item_name)
181
- if contains_item?(item_name)
182
- self.items.each do |i|
183
- if i.name.eql?(item_name)
184
- if i.equippable
185
- i.equipped = false
186
- if i.is_weapon
187
- self.weapon = nil
188
- return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
189
- elsif i.is_armor
190
- self.armor = nil
191
- return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
192
- end
193
- else
194
- return ERROR_ITEM_UNEQUIP_NONARMAMENT
195
- end
196
- end
197
- end
198
- else
199
- ERROR_ITEM_UNEQUIP_INVALID
200
- end
201
- end
202
-
203
- def add_item(item_name, cur_loc = nil, player = nil)
204
- if cur_loc.nil?
205
- self.items.push(item_name)
206
- else
207
- cur_loc.items.each do |i|
208
- if i.name.eql?(item_name)
209
- if i.takeable
210
- self.items.push(i)
211
- cur_loc.remove_item(item_name)
212
-
213
- # stats
214
- player.items_taken += 1
215
-
216
- return "#{"Added".colorize(:green)} #{item_name.colorize(:yellow)} #{"to your increasing collection of bits of tid".colorize(:green)}."
217
- else
218
- return ERROR_ITEM_ADD_UNTAKEABLE.colorize(:red)
219
- end
220
- end
221
- end
222
- end
223
- ERROR_ITEM_ADD_INVALID.colorize(:red)
224
- end
225
-
226
- def drop_item(item_name, cur_loc)
227
- if contains_item?(item_name)
228
- remove_item(item_name)
229
- cur_loc.add_item(item_name)
230
- "You dropped #{item_name.colorize(:yellow)}."
231
- else
232
- ERROR_ITEM_REMOVE_INVALID
233
- end
234
- end
235
-
236
- def remove_item(item_name)
237
- self.items.delete_at(self.items.map(&:name).index(item_name) || self.items.length)
238
- unless self.weapon.nil?
239
- self.weapon = nil if self.weapon.name.eql?(item_name)
240
- end
241
- end
242
- end
243
- end
1
+ # lib/gemwarrior/inventory.rb
2
+ # Collection of items a creature possesses
3
+
4
+ require_relative 'game_options'
5
+
6
+ module Gemwarrior
7
+ class Inventory
8
+ # CONSTANTS
9
+ ERROR_ITEM_REMOVE_INVALID = 'Your inventory does not contain that item, so you cannot drop it.'
10
+ ERROR_ITEM_ADD_UNTAKEABLE = 'That would be great if you could take that, wouldn\'t it? Huh!'
11
+ ERROR_ITEM_ADD_INVALID = 'That item cannot be taken or does not exist.'
12
+ ERROR_ITEM_DESCRIBE_INVALID = 'That does not seem to be in the inventory.'
13
+ ERROR_ITEM_EQUIP_INVALID = 'You do not possess anything called that to equip.'
14
+ ERROR_ITEM_EQUIP_NONARMAMENT = 'That item cannot be equipped.'
15
+ ERROR_ITEM_UNEQUIP_INVALID = 'You do not possess anything called that to unequip.'
16
+ ERROR_ITEM_UNEQUIP_NONARMAMENT = 'That item cannot be unequipped.'
17
+ VOWELS = 'aeiou'
18
+
19
+ attr_accessor :items,
20
+ :weapon,
21
+ :armor
22
+
23
+ def initialize(items = [], weapon = nil, armor = nil)
24
+ self.items = items
25
+ self.weapon = weapon
26
+ self.armor = armor
27
+ end
28
+
29
+ def an_words
30
+ ['herb']
31
+ end
32
+
33
+ def article_chooser(word)
34
+ (VOWELS.include?(word.to_s[0]) or an_words.include?(word.to_s)) ? 'an' : 'a'
35
+ end
36
+
37
+ def is_empty?
38
+ self.items.nil? || self.items.empty?
39
+ end
40
+
41
+ # non-English-like contents of inventory for simple lists
42
+ def contents
43
+ if is_empty?
44
+ nil
45
+ else
46
+ item_hash = {}
47
+ self.items.map(&:name).each do |i|
48
+ i_sym = i.to_sym
49
+ if item_hash.keys.include? i_sym
50
+ item_hash[i_sym] += 1
51
+ else
52
+ item_hash[i_sym] = 1
53
+ end
54
+ end
55
+
56
+ # one item? return string
57
+ if item_hash.length == 1
58
+ i = item_hash.keys.join
59
+ q = item_hash.values.join.to_i
60
+ return q > 1 ? "#{i}s x#{q}" : i
61
+ # multiple items? return array of strings to mush together
62
+ else
63
+ item_arr = []
64
+ item_hash.each do |i, q|
65
+ if q > 1
66
+ item_arr.push("#{i}s x#{q}")
67
+ else
68
+ item_arr.push(i)
69
+ end
70
+ end
71
+
72
+ return item_arr.join(', ')
73
+ end
74
+ end
75
+ end
76
+
77
+ # English-like sentence summary of inventory
78
+ def list_contents
79
+ if is_empty?
80
+ 'You possess nothing.'
81
+ else
82
+ # build hash of inventory's items
83
+ item_hash = {}
84
+ self.items.map(&:name).each do |i|
85
+ i_sym = i.to_sym
86
+ if item_hash.keys.include? i_sym
87
+ item_hash[i_sym] += 1
88
+ else
89
+ item_hash[i_sym] = 1
90
+ end
91
+ end
92
+
93
+ # one item? return string
94
+ if item_hash.length == 1
95
+ i = item_hash.keys.join
96
+ q = item_hash.values.join.to_i
97
+ if q > 1
98
+ return "You have #{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}."
99
+ else
100
+ return "You have #{self.article_chooser(i)} #{i.to_s.colorize(:yellow)}."
101
+ end
102
+ # multiple items? return array of strings to mush together
103
+ else
104
+ item_list_text = 'You have '
105
+ item_arr = []
106
+ item_hash.each do |i, q|
107
+ if q > 1
108
+ item_arr.push("#{q} #{i.to_s.colorize(:yellow)}#{'s'.colorize(:yellow)}")
109
+ else
110
+ item_arr.push("#{self.article_chooser(i)} #{i.to_s.colorize(:yellow)}")
111
+ end
112
+ end
113
+
114
+ item_arr[-1].replace("and #{item_arr[-1]}.")
115
+
116
+ return item_list_text << item_arr.join(', ')
117
+ end
118
+ end
119
+ end
120
+
121
+ def contains_item?(item_name)
122
+ self.items.map{ |i| i.name.downcase }.include?(item_name.downcase)
123
+ end
124
+
125
+ def contains_battle_item?
126
+ battle_item_found = false
127
+ self.items.each do |i|
128
+ battle_item_found = true if i.useable_battle
129
+ end
130
+ battle_item_found
131
+ end
132
+
133
+ def list_battle_items
134
+ battle_items = []
135
+ self.items.each do |i|
136
+ battle_items.push(i) if i.useable_battle
137
+ end
138
+ battle_items
139
+ end
140
+
141
+ def describe_item(item_name, world)
142
+ if contains_item?(item_name)
143
+ self.items.each do |i|
144
+ if i.name.eql?(item_name)
145
+ if GameOptions.data['debug_mode']
146
+ return i.describe_detailed(world)
147
+ else
148
+ return i.describe(world)
149
+ end
150
+ end
151
+ end
152
+ else
153
+ ERROR_ITEM_DESCRIBE_INVALID
154
+ end
155
+ end
156
+
157
+ def equip_item(item_name)
158
+ if contains_item?(item_name)
159
+ self.items.each do |i|
160
+ if i.name.eql?(item_name)
161
+ if i.equippable
162
+ i.equipped = true
163
+ if i.is_weapon
164
+ self.weapon = i
165
+ return "The #{i.name.colorize(:yellow)} has taken charge, and been equipped."
166
+ elsif i.is_armor
167
+ self.armor = i
168
+ return "The #{i.name.colorize(:yellow)} has fortified you, and been equipped."
169
+ end
170
+ else
171
+ return ERROR_ITEM_EQUIP_NONARMAMENT
172
+ end
173
+ end
174
+ end
175
+ else
176
+ ERROR_ITEM_EQUIP_INVALID
177
+ end
178
+ end
179
+
180
+ def unequip_item(item_name)
181
+ if contains_item?(item_name)
182
+ self.items.each do |i|
183
+ if i.name.eql?(item_name)
184
+ if i.equippable
185
+ i.equipped = false
186
+ if i.is_weapon
187
+ self.weapon = nil
188
+ return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
189
+ elsif i.is_armor
190
+ self.armor = nil
191
+ return "The #{i.name.colorize(:yellow)} has been demoted to unequipped."
192
+ end
193
+ else
194
+ return ERROR_ITEM_UNEQUIP_NONARMAMENT
195
+ end
196
+ end
197
+ end
198
+ else
199
+ ERROR_ITEM_UNEQUIP_INVALID
200
+ end
201
+ end
202
+
203
+ def add_item(item_name, cur_loc = nil, player = nil)
204
+ if cur_loc.nil?
205
+ self.items.push(item_name)
206
+ else
207
+ cur_loc.items.each do |i|
208
+ if i.name.eql?(item_name)
209
+ if i.takeable
210
+ self.items.push(i)
211
+ cur_loc.remove_item(item_name)
212
+
213
+ # stats
214
+ player.items_taken += 1
215
+
216
+ return "#{"Added".colorize(:green)} #{item_name.colorize(:yellow)} #{"to your increasing collection of bits of tid".colorize(:green)}."
217
+ else
218
+ return ERROR_ITEM_ADD_UNTAKEABLE.colorize(:red)
219
+ end
220
+ end
221
+ end
222
+ end
223
+ ERROR_ITEM_ADD_INVALID.colorize(:red)
224
+ end
225
+
226
+ def drop_item(item_name, cur_loc)
227
+ if contains_item?(item_name)
228
+ remove_item(item_name)
229
+ cur_loc.add_item(item_name)
230
+ "You dropped #{item_name.colorize(:yellow)}."
231
+ else
232
+ ERROR_ITEM_REMOVE_INVALID
233
+ end
234
+ end
235
+
236
+ def remove_item(item_name)
237
+ self.items.delete_at(self.items.map(&:name).index(item_name) || self.items.length)
238
+ unless self.weapon.nil?
239
+ self.weapon = nil if self.weapon.name.eql?(item_name)
240
+ end
241
+ end
242
+ end
243
+ end