-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjects.go
272 lines (249 loc) · 8.45 KB
/
objects.go
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
/*
Copyright (c) 2018, Tomasz "VedVid" Nowakowski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"errors"
"fmt"
"unicode/utf8"
)
const (
/* Slots for inventory handling.
Their order here is important, because it
will be order of slots in Equipment menu. */
SlotNA = iota - 1
SlotWeaponPrimary
SlotWeaponSecondary
SlotWeaponMelee
SlotMax
)
var SlotStrings = map[int]string{
SlotWeaponPrimary: "weapon1",
SlotWeaponSecondary: "weapon2",
SlotWeaponMelee: "weapon3",
}
const (
// Use cases, mostly for consumables.
UseNA = iota
UseHeal
)
const (
// Values for handling inventory actions.
ItemPass = "pass"
ItemDrop = "drop"
ItemEquip = "equip"
ItemDequip = "dequip"
ItemUse = "use"
)
const (
// Ranges of weapons.
RangeShort = 10
RangeMedium = 20
RangeLong = 30
)
var MeleeWeapons = []string{
"meleeBowieKnife.json", "meleeBowieKnife.json", "meleeBowieKnife.json",
"meleeButcherKnife.json", "meleeButcherKnife.json",
"meleeArkansasToothpick.json", "meleeArkansasToothpick.json",
"meleeFoldingKnife.json",
"meleeSaber.json",
}
var SecondaryWeapons = []string{
"secondaryColt1851Navy.json", "secondaryColt1851Navy.json", "secondaryColt1851Navy.json",
"secondaryColtPeacemaker.json",
"secondaryRemington1858Pocket.json", "secondaryRemington1858Pocket.json",
"secondaryRemington1875.json", "secondaryRemington1875.json",
"secondarySmithAndWessonModel3.json",
}
var PrimaryWeapons = []string{
"primarySpringfieldM1865.json",
"primaryWinchesterModel1886.json",
"primarySpencerRepeater.json",
}
type Object struct {
/* Objects are every other things on map;
statues, tables, chairs; but also weapons,
armor parts, etc. */
BasicProperties
VisibilityProperties
CollisionProperties
ObjectProperties
Ranges []int
AmmoMax int
AmmoCurrent int
Cock bool
Cocked bool
}
// Objects holds every object on map.
type Objects []*Object
func NewObject(x, y int, objectPath string) (*Object, error) {
/* NewObject is function that returns new Creature from
json file passed as argument. It replaced old code that
was encouraging hardcoding data in go files.
Errors returned by json package are not very helpful, and
hard to work with, so there is lazy panic for them. */
var object = &Object{}
err := ObjectFromJson(ObjectsPathJson+objectPath, object)
if err != nil {
fmt.Println(err)
panic(-1)
}
object.X, object.Y = x, y
var err2 error
if object.Layer < 0 {
txt := LayerError(object.Layer)
err = errors.New("Object layer is smaller than 0. " + txt)
}
if object.Layer != ObjectsLayer {
txt := LayerWarning(object.Layer, ObjectsLayer)
err2 = errors.New("Creature layer is not equal to CreaturesLayer constant." + txt)
}
if object.X < 0 || object.X >= MapSizeX || object.Y < 0 || object.Y >= MapSizeY {
txt := CoordsError(object.X, object.Y)
err = errors.New("Object coords is out of window range." + txt)
}
if utf8.RuneCountInString(object.Char) != 1 {
txt := CharacterLengthError(object.Char)
err = errors.New("Object character string length is not equal to 1." + txt)
}
if object.Consumable == true && object.Use == UseNA {
txt := ConsumableWithoutUseError()
err = errors.New("Object is consumable, but has undefined use case." + txt)
}
if (object.Equippable == false && object.Slot != SlotNA) ||
(object.Equippable == true && object.Slot == SlotNA) {
txt := EquippableSlotError(object.Equippable, object.Slot)
err = errors.New("'equippable' and 'slot' values does not match." + txt)
}
if object.Equippable == true && object.Consumable == true {
//TODO: temporary
err = errors.New("For now, <equippable> and <consumable> should not exists at the same time.")
}
return object, err2
}
func GatherItemOptions(o *Object) ([]string, error) {
/* Function GatherItemOptions takes pointer to specific Object
as argument and returns slice of strings that is list of
possible actions. ItemBack that is necessary, yet last value
to include, to provide way to close menu. */
var options = []string{}
var err error
if o.Equippable == true {
options = append(options, ItemEquip)
}
if o.Use != UseNA {
options = append(options, ItemUse)
}
if o.Pickable == true {
options = append(options, ItemDrop)
}
if len(options) == 0 {
txt := ItemOptionsEmptyError()
err = errors.New("Object " + o.Name + " has no valid properties." + txt)
}
return options, err
}
func GatherEquipmentOptions(o *Object) ([]string, error) {
/* Function GatherEquipmentOptions takes pointer to specific Object
as argument and returns slice of strings that is list of
possible actions. ItemBack that is necessary, yet last value
to include, to provide way to close menu. */
var options = []string{}
var err error
if o.Equippable == true {
options = append(options, ItemDequip)
}
if o.Use != UseNA {
options = append(options, ItemUse)
}
if o.Pickable == true {
options = append(options, ItemDrop)
}
if len(options) == 0 {
txt := ItemOptionsEmptyError()
err = errors.New("Object " + o.Name + " has no valid properties." + txt)
}
return options, err
}
func GetEquippablesFromInventory(c *Creature, slot int) Objects {
/* GetEquippablesFromInventory is function that takes *Creature as arguments
and returns []*Object.
It creates empty slice of Object pointers, then adds every *Object
from *Creature's Inventory that is not nil, and has Equippable bool set to true.
This function is used to create list of all equippable items from
someone's Inventory. */
var eq = Objects{}
for i := 0; i < len(c.Inventory); i++ {
item := c.Inventory[i]
if item != nil && item.Equippable == true && item.Slot == slot {
eq = append(eq, item)
}
}
return eq
}
func (o *Object) UseItem(c *Creature) (bool, error) {
/* Method UseItem has Object as receiver and takes Creature as argument.
It uses Use value of receiver to determine what action will be performed.
If there is no valid o.Use, it breaks switch statement (need proper
error handling).
It tries to remove item from inventory by calling DestroyItem function,
but item will be removed only if its Consumable is set to true.
Returns turnSpent that is true, unless o.Use is invalid. */
turnSpent := false
var err error
switch o.Use {
case UseHeal:
c.HPCurrent = c.HPMax
turnSpent = true
default:
txt := UseItemError()
err = errors.New("Item has wrong use case specified." + txt)
break
}
if err == nil {
AddMessage("You used [color=" + o.Color + "]" + o.Name + "[/color].")
err2 := DestroyItem(o, c)
if err2 != nil {
fmt.Println(err2)
// It could be case to set turnSpent to false again.
}
}
return turnSpent, err
}
func DestroyItem(o *Object, c *Creature) error {
/* Function DestroyItem takes Object and Creature as arguments, and returns error.
At first, it iterates through Creature's Inventory, and creates an error if
proper index is not found. Otherwise, it removes item from inventory. */
var err error
if o.Consumable == true {
index, err_ := FindObjectIndex(o, c.Inventory)
if err_ != nil {
err = err_ // It looks like ugly hack.
txt := ItemToDestroyNotFoundError()
fmt.Println(txt)
} else {
copy(c.Inventory[index:], c.Inventory[index+1:])
c.Inventory[len(c.Inventory)-1] = nil
c.Inventory = c.Inventory[:len(c.Inventory)-1]
}
}
return err
}