-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmonsters.go
329 lines (310 loc) · 10.7 KB
/
monsters.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
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 (
// Special characters.
CorpseChar = "%"
)
type Creature struct {
/* Creatures are living objects that
moves, attacks, dies, etc. */
BasicProperties
VisibilityProperties
CollisionProperties
FighterProperties
EquipmentComponent
}
// Creatures holds every creature on map.
type Creatures []*Creature
func NewCreature(x, y int, monsterFile string) (*Creature, error) {
/* NewCreature 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 monster = &Creature{}
err := CreatureFromJson(CreaturesPathJson+monsterFile, monster)
if err != nil {
fmt.Println(err)
panic(-1)
}
monster.X, monster.Y = x, y
var err2 error
if monster.Layer < 0 {
txt := LayerError(monster.Layer)
err2 = errors.New("Creature layer is smaller than 0." + txt)
}
if monster.Layer != CreaturesLayer {
txt := LayerWarning(monster.Layer, CreaturesLayer)
err2 = errors.New("Creature layer is not equal to CreaturesLayer constant." + txt)
}
if monster.X < 0 || monster.X >= MapSizeX || monster.Y < 0 || monster.Y >= MapSizeY {
txt := CoordsError(monster.X, monster.Y)
err2 = errors.New("Creature coords is out of window range." + txt)
}
if utf8.RuneCountInString(monster.Char) != 1 {
txt := CharacterLengthError(monster.Char)
err2 = errors.New("Creature character string length is not equal to 1." + txt)
}
if monster.HPMax < 0 {
txt := InitialHPError(monster.HPMax)
err2 = errors.New("Creature HPMax is smaller than 0." + txt)
}
if monster.Attack < 0 {
txt := InitialAttackError(monster.Attack)
err2 = errors.New("Creature attack value is smaller than 0." + txt)
}
if monster.Defense < 0 {
txt := InitialDefenseError(monster.Defense)
err2 = errors.New("Creature defense value is smaller than 0." + txt)
}
if monster.Equipment == nil {
monster.Equipment = Objects{}
}
if monster.Inventory == nil {
monster.Inventory = Objects{}
}
return monster, err2
}
func (c *Creature) MoveOrAttack(tx, ty int, b Board, o *Objects, all Creatures) bool {
/* Method MoveOrAttack decides if Creature will move or attack other Creature;
It has *Creature receiver, and takes tx, ty (coords) integers as arguments,
and map of current level, and list of all Creatures.
Starts by target that is nil, then iterates through Creatures. If there is
Creature on targeted tile, that Creature becomes new target for attack.
Otherwise, Creature moves to specified Tile.
It's supposed to take player as receiver (attack / moving enemies is
handled differently - check ai.go and combat.go). */
var target *Creature
turnSpent := false
for i, _ := range all {
if all[i].X == c.X+tx && all[i].Y == c.Y+ty {
if all[i].HPCurrent > 0 {
target = all[i]
break
}
}
}
if target != nil {
c.AttackTarget(target, o)
turnSpent = true
} else {
turnSpent = c.Move(tx, ty, b)
}
return turnSpent
}
func (c *Creature) Move(tx, ty int, b Board) bool {
/* Move is method of Creature; it takes target x, y as arguments;
check if next move won't put Creature off the screen, then updates
Creature coords. */
turnSpent := false
newX, newY := c.X+tx, c.Y+ty
if newX >= 0 &&
newX <= MapSizeX-1 &&
newY >= 0 &&
newY <= MapSizeY-1 {
if b[newX][newY].Blocked == false {
c.X = newX
c.Y = newY
turnSpent = true
}
}
return turnSpent
}
func (c *Creature) PickUp(o *Objects) bool {
/* PickUp is method that has *Creature as receiver
and slice of *Object as argument.
Creature tries to pick object up.
If creature stands on object that is possible to pick,
object is added to c's inventory, and removed
from "global" slice of objects.
Picking objects up takes turn only if it is
successful attempt. */
turnSpent := false
obj := *o
for i := 0; i < len(obj); i++ {
if obj[i].X == c.X && obj[i].Y == c.Y && obj[i].Pickable == true {
if c.AIType == PlayerAI {
AddMessage("You found " + obj[i].Name + ".")
}
c.Inventory = append(c.Inventory, obj[i])
copy(obj[i:], obj[i+1:])
obj[len(obj)-1] = nil
*o = obj[:len(obj)-1]
turnSpent = true
break
}
}
return turnSpent
}
func (c *Creature) DropFromInventory(objects *Objects, index int) bool {
/* Drop is method that has Creature as receiver and takes
"global" list of objects as main argument, and additional
integer that is index of item to be dropped from c's Inventory.
At first, turnSpent is set to false, to make it true
at the end of function. It may be considered as obsolete WET,
because 'return true' would be sufficient, but it is
a bit more readable now.
Objs is dereferenced objects and it is absolutely necessary
to do any actions on these objects.
Drop do two things:
at first, it adds specific item to the game map,
then it removes this item from its owner Inventory. */
turnSpent := false
objs := *objects
if c.AIType == PlayerAI {
AddMessage("You dropped " + c.Inventory[index].Name + ".")
}
// Add item to the map.
object := c.Inventory[index]
object.X, object.Y = c.X, c.Y
objs = append(objs, object)
*objects = objs
// Then remove item from inventory.
copy(c.Inventory[index:], c.Inventory[index+1:])
c.Inventory[len(c.Inventory)-1] = nil
c.Inventory = c.Inventory[:len(c.Inventory)-1]
turnSpent = true
return turnSpent
}
func (c *Creature) DropFromEquipment(objects *Objects, slot int) bool {
/* DropFromEquipment is method of *Creature that takes "global" objects,
and int (as index) as arguments, and returns bool (result depends if
action was successful, therefore if took a turn).
This function is very similar to DropFromInventory, but is kept
due to explicitness.
The difference is that Equipment checks Equipment index, not
specific object, so additionally checks for nils, and instead of
removing item from slice, makes it nil.
This behavior is important, because while Inventory is "dynamic"
slice, Equipment is supposed to be "fixed size" - slots are present
all the time, but the can be empty (ie nil) or occupied (ie object). */
turnSpent := false
objs := *objects
object := c.Equipment[slot]
if object == nil {
return turnSpent // turn is not spent because there is no object to drop
}
// else {
if c.AIType == PlayerAI {
AddMessage("You removed and dropped " + object.Name + ".")
}
// add item to map
object.X, object.Y = c.X, c.Y
objs = append(objs, object)
*objects = objs
// then remove from slot
c.Equipment[slot] = nil
turnSpent = true
return turnSpent
}
func (c *Creature) EquipItem(o *Object, slot int) (bool, error) {
/* EquipItem is method of *Creature that takes *Object and int (that is
indicator to index of Equipment slot) as arguments; it returns
bool and error.
At first, EquipItem checks for errors:
- if object to equip exists
- if this equipment slot is not occupied
then equips item and removes it from inventory. */
var err error
if o == nil {
txt := EquipNilError(c)
err = errors.New("Creature tried to equip *Object that was nil." + txt)
}
if c.Equipment[slot] != nil {
txt := EquipSlotNotNilError(c, slot)
err = errors.New("Creature tried to equip item into already occupied slot." + txt)
}
if o.Slot != slot {
txt := EquipWrongSlotError(o.Slot, slot)
err = errors.New("Creature tried to equip item into wrong slot." + txt)
}
turnSpent := false
// Equip item...
c.Equipment[slot] = o
// ...then remove it from inventory.
index, err := FindObjectIndex(o, c.Inventory)
if err != nil {
fmt.Println(err)
}
copy(c.Inventory[index:], c.Inventory[index+1:])
c.Inventory[len(c.Inventory)-1] = nil
c.Inventory = c.Inventory[:len(c.Inventory)-1]
if c.AIType == PlayerAI {
AddMessage("You equipped " + o.Name + ".")
}
turnSpent = true
return turnSpent, err
}
func (c *Creature) DequipItem(slot int) (bool, error) {
/* DequipItem is method of Creature. It is called when receiver is about
to dequip weapon from "ready" equipment slot.
At first, weapon is added to Inventory, then Equipment slot is set to nil. */
var err error
if c.Equipment[slot] == nil {
txt := DequipNilError(c, slot)
err = errors.New("Creature tried to DequipItem that was nil." + txt)
}
if c.AIType == PlayerAI {
AddMessage("You dequipped " + c.Equipment[slot].Name + ".")
}
turnSpent := false
c.Inventory = append(c.Inventory, c.Equipment[slot]) //adding items to inventory should have own function, that will check "bounds" of inventory
c.Equipment[slot] = nil
turnSpent = true
return turnSpent, err
}
func (c *Creature) Die(o *Objects) {
/* Method Die is called when Creature's HP drops below zero.
Die() has *Creature as receiver.
Receiver properties changes to fit better to corpse. */
c.Layer = DeadLayer
c.Name = "corpse of " + c.Name
c.Color = "dark red"
c.ColorDark = "dark red"
c.Char = CorpseChar
c.Blocked = false
c.BlocksSight = false
c.AIType = NoAI
for i, _ := range SlotStrings {
c.DropFromEquipment(o, i)
}
ZeroLastTarget(c)
}
func FindMonsterByXY(x, y int, c Creatures) *Creature {
/* Function FindMonsterByXY takes desired coords and list
of all available creatures. It iterates through this list,
and returns nil or creature that occupies specified coords. */
var monster *Creature
for i := 0; i < len(c); i++ {
if x == c[i].X && y == c[i].Y {
monster = c[i]
break
}
}
return monster
}