-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswplanetgen.go
514 lines (418 loc) · 11.2 KB
/
swplanetgen.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package swplanetgen
import (
"database/sql"
"fmt"
"log"
"math/rand"
"strings"
// MySQL
_ "github.com/go-sql-driver/mysql"
)
// Set the category enum values
const (
_ = iota
CategoryFunction = iota
CategoryGovernment
CategoryType
CategoryTerrain
CategoryTemperature
CategoryGravity
CategoryAtmosphere
CategoryHydrosphere
CategoryStarport
CategoryTechlevel
)
// Category describes a category of planet generation
type Category struct {
Ident int
Name string
Table string
MapTable string
RollType string
}
// Planet is the full data representing a generated planet
type Planet struct {
Function *Result
Government *Result
Type *Result
Terrain *Result
Temperature *Result
Gravity *Result
Atmosphere *Result
Hydrosphere *Result
Starport *Result
TechLevel *Result
HoursPerDay int
DaysPerYear int
Population int
}
// Incompatibility includes all the data to describe an incompatible set of generation parameters
type Incompatibility struct {
Category int
CategoryIdent int
IncompatibleCategory int
IncompatibleIdent int
Description string
}
// IncompatibilityDescription is a human-readable version of an incompatibility
type IncompatibilityDescription struct {
Category string
Name string
IncompatibleCategory string
IncompatibleName string
Description string
}
// Modifier indicates that a category choice impacts the roll for another category
type Modifier struct {
Category int
CategoryIdent int
ModifiedCategory int
CategoryDescription string
ModifierDirection string
Amount int
}
// Result from sub-method
type Result struct {
Ident int
Raw int
Name string
Description string
Modifiers []Modifier
Incompatibilities []Incompatibility
}
func d6() int {
return rand.Intn(6) + 1
}
func twod6() int {
return (rand.Intn(6) + 1) + (rand.Intn(6) + 1)
}
func d6percentile() int {
return (d6() * 10) + d6()
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// GeneratePlanet puts all the pieces together
func GeneratePlanet(dbConnectionString string) (*Planet, error) {
db, err := sql.Open("mysql", dbConnectionString)
if err != nil {
return nil, err
}
defer db.Close()
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
return nil, err
}
var allCategories []Category
var allIncompatibilities []Incompatibility
var allModifiers []Modifier
planet := &Planet{}
allCategories, err = categories(db)
for _, category := range allCategories {
incompatibleRolls, err := incompatibleRolls(allIncompatibilities, category, db)
if err != nil {
return nil, err
}
roll := -1
for roll < 0 {
switch category.RollType {
case "d6percentile":
roll = modifiedRoll(d6percentile(), category, allModifiers)
if roll < 11 {
roll = 11
}
if roll > 66 {
roll = 66
}
case "2d6":
roll = modifiedRoll(twod6(), category, allModifiers)
if roll < 2 {
roll = 2
}
if roll > 12 {
roll = 12
}
default:
roll = 0
}
if contains(incompatibleRolls, roll) {
roll = -1
}
}
if roll > 0 {
var result Result
query := fmt.Sprintf("select `t`.`id`, `t`.`name`, `t`.`description` from `%s` `m` join `%s` `t` on `m`.`target_id` = `t`.`id` where `m`.`roll` = ?", category.MapTable, category.Table)
err := db.QueryRow(query, roll).Scan(&result.Ident, &result.Name, &result.Description)
if err != nil {
return nil, err
}
result.Raw = roll
result.Incompatibilities, err = categoryIncompatibilities(category.Ident, result.Ident, db)
for _, i := range result.Incompatibilities {
allIncompatibilities = append(allIncompatibilities, i)
}
result.Modifiers, err = categoryModifiers(category.Ident, result.Ident, db)
for _, m := range result.Modifiers {
allModifiers = append(allModifiers, m)
}
switch category.Ident {
case CategoryFunction:
planet.Function = &result
case CategoryGovernment:
planet.Government = &result
case CategoryType:
planet.Type = &result
case CategoryTerrain:
planet.Terrain = &result
case CategoryTemperature:
planet.Temperature = &result
case CategoryGravity:
planet.Gravity = &result
case CategoryAtmosphere:
planet.Atmosphere = &result
case CategoryHydrosphere:
planet.Hydrosphere = &result
case CategoryStarport:
planet.Starport = &result
case CategoryTechlevel:
planet.TechLevel = &result
}
}
}
planet.HoursPerDay = hoursPerDay()
planet.DaysPerYear = daysPerYear()
populationModifier := 0
if planet.Type.Name == "Asteroid Belt" || planet.Type.Name == "Artificial" {
populationModifier -= 2
}
if planet.Terrain.Name == "Barren" || planet.Terrain.Name == "Cave" || planet.Terrain.Name == "Volcanic" {
populationModifier -= 2
} else if planet.Terrain.Name == "Ocean" {
populationModifier--
} else if planet.Terrain.Name == "Urban" {
populationModifier++
}
planet.Population = population(populationModifier)
return planet, nil
}
// IncompatibleConditions returns a human-readable version of the incompatibility map
func IncompatibleConditions(dbConnectionString string) ([]IncompatibilityDescription, error) {
db, err := sql.Open("mysql", dbConnectionString)
if err != nil {
return nil, err
}
defer db.Close()
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
return nil, err
}
var retval []IncompatibilityDescription
const query = "SELECT `c1`.`name`, `c1`.`table`, `c2`.`name`, `c2`.`table`, `ic`.`category_id`, `ic`.`incompatible_category_id`, `ic`.`description` FROM `incompatible_conditions` `ic` JOIN `categories` `c1` ON (`ic`.`category` = `c1`.`id`) JOIN `categories` `c2` ON (`ic`.`incompatible_category` = `c2`.`id`)"
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var name1, name2 string
var table1, table2 string
var id1, id2 int
var type1, type2 string
var description string
if err := rows.Scan(&name1, &table1, &name2, &table2, &id1, &id2, &description); err != nil {
log.Fatal(err)
}
subquery := "SELECT `name` FROM `%s` WHERE `id` = ?"
db.QueryRow(fmt.Sprintf(subquery, table1), id1).Scan(&type1)
db.QueryRow(fmt.Sprintf(subquery, table2), id2).Scan(&type2)
incompatibility := IncompatibilityDescription{
Category: name1,
Name: type1,
IncompatibleCategory: name2,
IncompatibleName: type2,
Description: description,
}
retval = append(retval, incompatibility)
}
if err := rows.Err(); err != nil {
return nil, err
}
return retval, nil
}
func categories(db *sql.DB) ([]Category, error) {
const query = "SELECT `id`, `name`, `table`, `map_table`, `roll_type` FROM `categories` ORDER BY `order` ASC"
var categories []Category
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
category := Category{}
if err := rows.Scan(&category.Ident, &category.Name, &category.Table, &category.MapTable, &category.RollType); err != nil {
log.Fatal(err)
}
categories = append(categories, category)
}
if err := rows.Err(); err != nil {
return nil, err
}
return categories, nil
}
func hoursPerDay() int {
switch d6() {
case 1, 2:
return 10 + twod6()
case 3, 4:
return 20 + d6()
case 5:
return 25 + d6()
case 6:
return 30 + d6()
}
return 0
}
func daysPerYear() int {
var base = d6() * 15
switch d6() {
case 1:
return base + 75
case 2:
return base + 150
case 3, 4:
return base + 225
case 5:
return base + 300
case 6:
return base + 375
}
return 0
}
func population(rollMod int) int {
var population int
magnitude := d6() + rollMod
order := d6()
number := d6()
if magnitude < 1 {
magnitude = 1
}
if magnitude > 6 {
magnitude = 6
}
if number < 4 {
population = rand.Intn(5) + 1
} else {
population = rand.Intn(4) + 6
}
switch order {
case 3, 4:
population *= 10
break
case 5, 6:
population *= 100
break
}
switch magnitude {
case 1:
return population
case 2, 3:
return population * 1000
case 4, 5:
return population * 1000000
case 6:
return population * 1000000000
}
return 0
}
func incompatibleRolls(incompatibilities []Incompatibility, category Category, db *sql.DB) ([]int, error) {
var incompatibleRolls []int
var incompatibleTargets []string
for _, i := range incompatibilities {
if i.IncompatibleCategory == category.Ident {
incompatibleTargets = append(incompatibleTargets, fmt.Sprintf("%d", i.IncompatibleIdent))
}
}
if len(incompatibleTargets) == 0 {
return incompatibleRolls, nil
}
query := fmt.Sprintf("select `m`.`roll` from `%s` `m` where `m`.`target_id` IN (%s)", category.MapTable, strings.Join(incompatibleTargets, ", "))
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var roll int
if err := rows.Scan(&roll); err != nil {
log.Fatal(err)
}
incompatibleRolls = append(incompatibleRolls, roll)
}
if err := rows.Err(); err != nil {
return nil, err
}
return incompatibleRolls, nil
}
func categoryIncompatibilities(categoryID int, resultID int, db *sql.DB) ([]Incompatibility, error) {
subquery := "SELECT `category`, `category_id`, `incompatible_category`, `incompatible_category_id`, `description` FROM `incompatible_conditions` WHERE `category` = ? AND `category_id` = ?"
rows, err := db.Query(subquery, categoryID, resultID)
if err != nil {
return nil, err
}
defer rows.Close()
var incoms []Incompatibility
for rows.Next() {
incompatibility := Incompatibility{}
if err := rows.Scan(&incompatibility.Category, &incompatibility.CategoryIdent, &incompatibility.IncompatibleCategory, &incompatibility.IncompatibleIdent, &incompatibility.Description); err != nil {
return nil, err
}
incoms = append(incoms, incompatibility)
}
if err := rows.Err(); err != nil {
return nil, err
}
return incoms, nil
}
func categoryModifiers(categoryID int, resultID int, db *sql.DB) ([]Modifier, error) {
subquery := "SELECT `m`.`category`, `m`.`category_id`, `m`.`modified_category`, `c`.`name`, `m`.`modifier_direction`, `m`.`modifier` FROM `modifiers` `m` JOIN `categories` `c` ON (`m`.`modified_category` = `c`.`id`) WHERE `m`.`category` = ? AND `m`.`category_id` = ?"
rows, err := db.Query(subquery, categoryID, resultID)
if err != nil {
return nil, err
}
defer rows.Close()
var mods []Modifier
for rows.Next() {
mod := Modifier{}
if err := rows.Scan(&mod.Category, &mod.CategoryIdent, &mod.ModifiedCategory, &mod.CategoryDescription, &mod.ModifierDirection, &mod.Amount); err != nil {
return nil, err
}
mods = append(mods, mod)
}
if err := rows.Err(); err != nil {
return nil, err
}
return mods, nil
}
func modifiedRoll(roll int, category Category, modifiers []Modifier) int {
if len(modifiers) == 0 {
return roll
}
moddedRoll := roll
for _, mod := range modifiers {
if mod.ModifiedCategory == category.Ident {
if mod.ModifierDirection == "+" {
moddedRoll = roll + mod.Amount
} else {
moddedRoll = roll - mod.Amount
}
}
}
return moddedRoll
}