-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.go
423 lines (393 loc) · 7.91 KB
/
room.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"fmt"
"math/rand"
"sort"
)
// room types
const (
START RoomType = iota
HALLWAY RoomType = iota
GREAT_HALL RoomType = iota
DUNGEON RoomType = iota
CHEST RoomType = iota
MYSTIC RoomType = iota
)
const chestLockedChance float64 = 0.4
const (
noChest float64 = 0.4
oneChest float64 = 0.4
twoChest float64 = 0.15
threeChest float64 = 0.05
)
type Door struct {
exists bool
locked bool
}
type Chest struct {
locked bool
item *Item
}
type RoomType int8
type Room struct {
rType RoomType
id int64
loc Location
chests []*Chest
enemies []*Enemy
dUp Door
dDown Door
dLeft Door
dRight Door
}
func getGenetateableTypes() [6]RoomType {
return [6]RoomType{START, HALLWAY, GREAT_HALL, DUNGEON, CHEST, MYSTIC}
}
func (r *Room) canLeaveFrom(direction Direction) bool {
switch direction {
case UP:
return r.dUp.exists /* && !r.dUp.locked */
case DOWN:
return r.dDown.exists /* && !r.dDown.locked */
case LEFT:
return r.dLeft.exists /* && !r.dLeft.locked */
case RIGHT:
return r.dRight.exists /* && !r.dRight.locked */
default:
fmt.Println("D E F A U L T C A S E ")
return false
}
}
func (r *Room) canRunFrom(chance float64) bool {
switch r.rType {
case START:
// should not have to call this but what ever
return true
case HALLWAY:
return chance < .7
case GREAT_HALL:
return chance < .6
case DUNGEON:
return chance < .2
case CHEST:
return chance < .4
case MYSTIC:
return chance < .3
default:
fmt.Println("D E F A U L T C A S E ")
return false
}
}
func (r *Room) canRunTo(chance float64) bool {
switch r.rType {
case START:
// should not have to call this but what ever
return true
case HALLWAY:
return chance < 1
case GREAT_HALL:
return chance < .9
case DUNGEON:
return chance < .3
case CHEST:
return chance < .7
case MYSTIC:
return chance < .6
default:
fmt.Println("D E F A U L T C A S E ")
return false
}
}
func (r *Room) initChests() {
r.chests = make([]*Chest, 3)
var numChests int
chanceNeeded := rand.Float64()
switch r.rType {
case START:
return // numChests is 0
case HALLWAY:
switch {
case .85 > chanceNeeded:
return // numChests is 0
case .85+.15 > chanceNeeded:
numChests = 1
}
case GREAT_HALL:
switch {
case .65 > chanceNeeded:
return // numChests is 0
case .65+.3 > chanceNeeded:
numChests = 1
case .65+.3+.05 > chanceNeeded:
numChests = 2
}
case DUNGEON:
switch {
case .5 > chanceNeeded:
return // numChests is 0
case .5+.3 > chanceNeeded:
numChests = 1
case .5+.3+.075 > chanceNeeded:
numChests = 2
}
case CHEST:
switch {
case .0 > chanceNeeded:
return // numChests is 0
case .0+.25 > chanceNeeded:
numChests = 1
case .0+.25+.5 > chanceNeeded:
numChests = 2
case .0+.25+.5+.25 > chanceNeeded:
numChests = 3
}
case MYSTIC:
switch {
case .05 > chanceNeeded:
return // numChests is 0
case .05+.35 > chanceNeeded:
numChests = 1
case .05+.35+.45 > chanceNeeded:
numChests = 2
case .05+.35+.45+.15 > chanceNeeded:
numChests = 3
}
}
for i := 0; i < numChests; i++ {
chest := new(Chest)
chanceNeeded = rand.Float64()
if chestLockedChance > chanceNeeded {
chest.locked = true
}
itemChances := getGenetateableItemsWithChance()
var generatedType ItemType
chance := 0.0
chanceNeeded = rand.Float64()
keys := make([]ItemType, len(itemChances))
ind := 0
for key := range itemChances {
keys[ind] = key
ind++
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] > keys[j]
})
for _, val := range keys {
chance += itemChances[val]
if chance > chanceNeeded {
generatedType = val
break
}
}
chest.item = createItemWithType(generatedType)
r.chests[i] = chest
}
}
func (r *Room) initEnemies(x, y, raid int64) {
if r.rType == DUNGEON || r.rType == MYSTIC {
r.enemies = make([]*Enemy, 2)
} else {
r.enemies = make([]*Enemy, 1)
}
chanceNeeded := rand.Float64()
// NOTE: Any chance that is greater than one is assuming that the radius is large enough to
switch r.rType {
case START:
// impossible case but...
return
case HALLWAY:
switch {
case .7 > chanceNeeded:
return
case .7+.25 > chanceNeeded:
r.enemies[0] = NewEnemy(PEON)
case .7+.25+.05 > chanceNeeded:
r.enemies[0] = NewEnemy(WARRIOR)
}
case GREAT_HALL:
switch {
case .6 > chanceNeeded:
return
case .6+.1 > chanceNeeded:
r.enemies[0] = NewEnemy(PEON)
case .6+.1+.3 > chanceNeeded:
r.enemies[0] = NewEnemy(WARRIOR)
}
case DUNGEON:
switch {
case .025 > chanceNeeded:
r.enemies[0] = NewEnemy(PEON)
case .025+.025 > chanceNeeded:
r.enemies[0] = NewEnemy(PEON)
r.enemies[1] = NewEnemy(PEON)
case .025+.025+.05 > chanceNeeded:
r.enemies[0] = NewEnemy(WARRIOR)
case .025+.025+.05+.15 > chanceNeeded:
r.enemies[0] = NewEnemy(WARRIOR)
r.enemies[1] = NewEnemy(WARRIOR)
case .025+.025+.05+.15+.65 > chanceNeeded:
r.enemies[0] = NewEnemy(BRUTE)
case .025+.025+.05+.15+.65+.1 > chanceNeeded:
r.enemies[0] = NewEnemy(BRUTE)
r.enemies[1] = NewEnemy(BRUTE)
}
case CHEST:
switch {
case .4 > chanceNeeded:
return
case .4+.4 > chanceNeeded:
r.enemies[0] = NewEnemy(WARRIOR)
case .4+.4+.2 > chanceNeeded:
r.enemies[0] = NewEnemy(BRUTE)
}
case MYSTIC:
switch {
case .3 > chanceNeeded:
return
case .3+.2 > chanceNeeded:
r.enemies[0] = NewEnemy(WARRIOR)
case .3+.2+.25 > chanceNeeded:
r.enemies[0] = NewEnemy(E_MYSTIC)
case .3+.2+.25+.25 > chanceNeeded:
r.enemies[0] = NewEnemy(E_MYSTIC)
r.enemies[1] = NewEnemy(E_MYSTIC)
}
default:
// also impossible but...
return
}
}
func (r *Room) initDoors(up Door, do Door, le Door, ri Door) {
if DEBUG_MODE {
fmt.Println("init doors ", r.loc.x, r.loc.y, up, do, le, ri)
}
r.dUp = Door{up.exists, up.locked}
r.dDown = Door{do.exists, do.locked}
r.dLeft = Door{le.exists, le.locked}
r.dRight = Door{ri.exists, ri.locked}
if DEBUG_MODE {
fmt.Println("post init doors", r.dUp, r.dDown, r.dLeft, r.dRight)
}
}
func (r *Room) getCurrentEnemy() *Enemy {
for _, enemy := range r.enemies {
if enemy != nil && enemy.health >= 0 {
return enemy
}
}
return nil
}
func (r *Room) getNumEnemies() int {
num := 0
for _, val := range r.enemies {
if val != nil {
num++
}
}
return num
}
func (r *Room) getNumEnemiesAlive() int {
num := 0
for _, val := range r.enemies {
if val != nil && val.health >= 0 {
num++
}
}
return num
}
func (r *Room) getNumChests() int {
numChests := 0
for _, val := range r.chests {
if val != nil {
numChests++
}
}
return numChests
}
func (r *Room) getNumChestsWithItem() int {
numChests := 0
for _, val := range r.chests {
if val != nil {
if val.item != nil {
numChests++
}
}
}
return numChests
}
func (r *Room) getNumLockedChests() int {
numChests := 0
for _, val := range r.chests {
if val != nil {
if val.locked {
numChests++
}
}
}
return numChests
}
func (r *Room) getNumLootableChests() int {
numChests := 0
for _, val := range r.chests {
if val != nil {
if !val.locked {
if val.item != nil {
numChests++
}
}
}
}
return numChests
}
func (r *Room) unlockChests(amount int) {
if amount > r.getNumLockedChests() {
amount = r.getNumLockedChests()
}
for _, val := range r.chests {
if val != nil {
if val.locked {
val.locked = false
amount--
}
if amount == 0 {
break
}
}
}
}
func getPrintStringFromRoomType(rType RoomType) string {
switch rType {
case START:
return "Start Room"
case HALLWAY:
return "Hallway"
case GREAT_HALL:
return "Great Hall"
case DUNGEON:
return "Dungeon"
case CHEST:
return "Chest Room"
case MYSTIC:
return "Mystical Room"
default:
return "_"
}
}
func getPrintCharFromRoomType(rType RoomType) string {
switch rType {
case START:
return "S"
case HALLWAY:
return "H"
case GREAT_HALL:
return "G"
case DUNGEON:
return "D"
case CHEST:
return "C"
case MYSTIC:
return "M"
default:
return "_"
}
}