/
game_map.go
1106 lines (973 loc) · 26.6 KB
/
game_map.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"math"
"math/rand"
"time"
"github.com/adachic/lottery"
"image"
"image/png"
"os"
"github.com/satori/go.uuid"
"image/color"
"encoding/json"
"sort"
)
//マップの大きさ
type GameMapSize struct {
MaxX int
MaxY int
MaxZ int
}
//面積
func (s GameMapSize) area() int {
return s.MaxY * s.MaxX
}
//マップの難度
type Difficult int
const (
easy Difficult = 10
normal Difficult = 3
hard Difficult = 2
exhard Difficult = 1
)
//長方形の形式
type RectForm int
const (
horizontalLong RectForm = 6 //横長
verticalLong RectForm = 5
square RectForm = 2
)
//地形
type Geographical int
const (
GeographicalStep Geographical = 14 + 10
GeographicalCave Geographical = 8 + 10
GeographicalRemain Geographical = 7 + 10
GeographicalPoison Geographical = 6 + 10
GeographicalFire Geographical = 9 + 10
GeographicalSnow Geographical = 5 + 10
GeographicalJozen Geographical = 3 + 10
GeographicalCastle Geographical = 4 + 10
)
//マップメタ(ここから詳細なパーツを決定)
type MacroMapType int
const (
MacroMapTypeRoad = 1 + iota
MacroMapTypeRough
MacroMapTypeWall
MacroMapTypeCantEnter //進入不可地形 TODO:これつかってんだっけ?
MacroMapTypeSwampWater //水系
MacroMapTypeSwampRava
MacroMapTypeSwampPoison
MacroMapTypeSwampHeal
MacroMapTypeOther //他
MacroMapTypeAllyPoint
MacroMapTypeEnemyPoint
)
//alreadyに登録されてなくて自分を除く最も近いポイントを返す
//errがtrueなら見つからなかった
func (pos GameMapPosition) searchNearPositionWithOutMe(positions []GameMapPosition, alreadys []PathPosition) (nearPos GameMapPosition, err bool) {
err = true;
minDistance := 10000
for i := 0; i < len(positions); i++ {
tgtPos := positions[i]
if (pos.equalXYTo(tgtPos)) {
continue
}
if (containsPath(alreadys, PathPosition{pos, tgtPos})) {
continue
}
distance := pos.distance(tgtPos)
if (distance < minDistance) {
minDistance = distance
nearPos = tgtPos
err = false
}
}
return nearPos, err
}
func (pos GameMapPosition) equalXYTo(another GameMapPosition) bool {
return (pos.X == another.X) && (pos.Y == another.Y)
}
func (pos GameMapPosition) distance(another GameMapPosition) int {
absx := pos.X - another.X
absy := pos.Y - another.Y
if (absx < 0) {
absx *= -1
}
if (absy < 0) {
absy *= -1
}
return absx + absy
}
//座標
type GameMapPosition struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
}
//確率を返す
func (d Difficult) Prob() int {
return int(d)
}
func (d Category) Prob() int {
return int(d)
}
func (d RectForm) Prob() int {
return int(d)
}
func (d Geographical) Prob() int {
return int(d)
}
func NewGameMap(condition GameMapCondition) *GameMap {
game_map := &GameMap{}
return game_map.init(condition)
}
func (game_map *GameMap) init(condition GameMapCondition) *GameMap {
//難易度を初期化
game_map.initMapDifficult()
Dlog("difficult: %+v\n", game_map.Difficult)
//カテゴリを初期化
game_map.initMapCategory()
Dlog("category: %+v\n", game_map.Category)
//道の舗装度を初期化
game_map.initMapPavement()
//マップのサイズを初期化
game_map.initMapSize()
Dlog("mapSize: %+v\n", game_map.Size)
//大まかな地形を初期化
game_map.initMapGeographical()
//味方開始ポイントを初期化
game_map.initAllyStartPoint()
Dlog("allyStartPoint: %+v\n", game_map.AllyStartPoint)
//敵開始ポイントを決定
game_map.appendEnemyStartPoints(50, false, nil)
for _, enemyStartPoint := range game_map.EnemyStartPoints {
// キーは使われません
Dlog("enemyStartPoint: %+v\n", enemyStartPoint)
}
//2次元マップ生成
{
xymap := NewXYMap(game_map.Size)
//広場配置
xymap.putPlazas(game_map.Difficult, game_map.AllyStartPoint, game_map.EnemyStartPoints)
//道配置
xymap.putRoads(game_map.Difficult, game_map.AllyStartPoint, game_map.EnemyStartPoints)
//壁配置
//ラフ配置
//味方、敵ポイント
{
for i := 0; i < len(game_map.EnemyStartPoints); i++ {
xymap.putPoint(game_map.EnemyStartPoints[i], MacroMapTypeEnemyPoint)
}
xymap.putPoint(game_map.AllyStartPoint, MacroMapTypeAllyPoint)
}
//勾配を生成
xymap.makeGradient(game_map.Geographical)
//敵ポイント追加(ラフ部分にも作るため)
{
game_map.appendEnemyStartPoints(50, true, xymap)
for _, enemyStartPoint := range game_map.EnemyStartPoints {
// キーは使われません
Dlog("enemyStartPoint2: %+v\n", enemyStartPoint)
}
//味方、敵ポイント
{
for i := 0; i < len(game_map.EnemyStartPoints); i++ {
xymap.putPoint(game_map.EnemyStartPoints[i], MacroMapTypeEnemyPoint)
}
xymap.putPoint(game_map.AllyStartPoint, MacroMapTypeAllyPoint)
}
}
//バリデーション
xymap.validate()
//A*高速化のためのメタ情報生成
{
//A*の1ゾーンのMax面積
restricted := 25
//ゾーニング
zones := xymap.zoningForAstar(restricted)
//ゾーンのバリデーション
newZones := xymap.validateForZone(game_map, zones)
{
//ゾーンの丸め
done := false
tryCount := 100
for !done {
newZones, done = xymap.roundZones(newZones, restricted - 10)
tryCount--
if (tryCount <= 0 ) {
break
}
}
//xyのareaIdに反映
areaId := 0
for _, zone := range newZones {
for _, pos := range zone {
xymap.areaId[pos.Y][pos.X] = areaId
}
areaId++
}
xymap.maxAreaId = areaId
//各ゾーンの中心点を算出
xymap.calcCenterPoint(newZones)
//ゾーンをグラフ化
xymap.makeGraphForAstar(newZones)
}
}
//水、毒沼配置
xymap.makeSwamp(game_map.Geographical)
//alloc/init
game_map.allocToJungleGym(xymap.maxAreaId)
//xymap情報をコピー
game_map.copyFromXY(xymap)
//dump
xymap.printMapForDebug()
}
return game_map
}
//マップ難易度の抽選結果を返す
func (game_map *GameMap) initMapDifficult() {
lot := lottery.New(rand.New(rand.NewSource(time.Now().UnixNano())))
difficults := []lottery.Interface{
easy,
normal,
hard,
exhard,
}
result := lot.Lots(
difficults...,
)
game_map.Difficult = difficults[result].(Difficult)
}
//雪原かどうかの抽選結果を返す
func (game_map *GameMap) initMapSnow() {
dice := lottery.GetRandomInt(0, 100);
game_map.IsSnow = dice < 2
}
//舗装度の抽選結果を返す
func (game_map *GameMap) initMapPavement() {
dice := lottery.GetRandomInt(0, 100);
switch{
case dice < 20:
game_map.PavementLv = 1;
case dice < 40:
game_map.PavementLv = 2;
case dice < 60:
game_map.PavementLv = 3;
case dice < 80:
game_map.PavementLv = 4;
case dice < 100:
game_map.PavementLv = 5;
}
}
//マップカテゴリの抽選結果を返す
func (game_map *GameMap) initMapCategory() {
dice := lottery.GetRandomInt(0, 7)
category := CategoryStep
switch dice {
case 0:
category = CategoryStep
case 1:
category = CategoryCave
case 2:
category = CategoryRemains
case 3:
category = CategoryFire
case 4:
category = CategoryPoison
case 5:
category = CategorySnow
case 6:
category = CategoryCastle
case 7:
category = CategoryJozen
}
game_map.Category = category
//parts定義にない(MapEditorで定義されてないカテゴリ)
switch(game_map.Category){
case CategoryPoison:
fallthrough
case CategorySnow:
fallthrough
case CategoryJozen:
game_map.Category = CategoryStep
}
}
//マップサイズの抽選結果を返す
func (game_map *GameMap) initMapSize() {
//横長か縦長か
rectForm := CreateRectForm()
//x/yアスペクト比
aspect := CreateAspectOfRectFrom(rectForm)
//面積
area := CreateArea(game_map.Difficult)
DDlog("form: %+v\n", rectForm)
DDlog("area: %+v\n", area)
DDlog("aspect: %f\n", aspect)
yy := float32(area) / float32(aspect)
DDlog("yy: %f\n", yy)
y := int(math.Sqrt(float64(yy)))
if y < 1 {
y = 1
}
x := int(area / y)
game_map.Size = GameMapSize{x, y, 30}
}
//地形の抽選結果を返す
func (game_map *GameMap) initMapGeographical() {
//カテゴリに応じて、地形を対応させる
var geo Geographical
switch game_map.Category {
case CategoryStep:
dice := lottery.GetRandomInt(0, 3)
switch dice{
case 0:
geo = GeographicalStep
case 1:
geo = GeographicalPoison
case 2:
geo = GeographicalSnow
case 3:
geo = GeographicalJozen
}
case CategoryFire:
geo = GeographicalFire
case CategoryCave:
geo = GeographicalCave
case CategoryRemains:
geo = GeographicalRemain
case CategoryJozen:
geo = GeographicalJozen
case CategorySnow:
geo = GeographicalSnow
case CategoryCastle:
geo = GeographicalCastle
}
/*
GeographicalPoison
GeographicalSnow
GeographicalJozen
GeographicalFire
*/
game_map.Geographical = geo
}
//味方の出撃座標を返す
func (game_map *GameMap) initAllyStartPoint() {
var seed Range
//難度が高いと中央寄りになる
switch game_map.Difficult{
case easy:
seed = Range{70, 100}
break
case normal:
seed = Range{30, 80}
break
case hard:
seed = Range{0, 50}
break
case exhard:
seed = Range{0, 20}
break
}
DDlog("seed", seed.Min, " ", seed.Max)
distanceFrom := lottery.GetRandomInt(seed.Min, seed.Max)
game_map.AllyStartPoint, _ = CreateRandomPositionInMap(
game_map.Size,
GameMapPosition{game_map.Size.MaxX / 2, game_map.Size.MaxY / 2, 0},
distanceFrom)
}
//敵出現座標の一覧を返す
//レシオ・全体の量に対して何%のポイントにするか
//ラフに生成するか
func (game_map *GameMap) appendEnemyStartPoints(ratio int, laugh bool, xy *xymap) {
type rangeFromAlly struct {
Min int
Max int
}
//味方と敵との大体の距離感
var rangeFrom rangeFromAlly
//敵出撃座標の数
var sattyPointNum int
switch game_map.Difficult{
case easy:
sattyPointNum = lottery.GetRandomInt(1, 3)
rangeFrom = rangeFromAlly{50, 100}
case normal:
sattyPointNum = lottery.GetRandomInt(3, 6)
rangeFrom = rangeFromAlly{30, 100}
case hard:
sattyPointNum = lottery.GetRandomInt(5, 10)
rangeFrom = rangeFromAlly{13, 100}
case exhard:
sattyPointNum = lottery.GetRandomInt(10, 20)
rangeFrom = rangeFromAlly{10, 100}
}
sattyPointNum = sattyPointNum * ratio / 100
if (sattyPointNum < 1) {
sattyPointNum = 1
}
var sattyPoints []GameMapPosition
var radians []float64
tryCount := 0
allyStartPoint := game_map.AllyStartPoint
if (laugh) {
tmp := allyStartPoint.X
allyStartPoint.X = allyStartPoint.Y
allyStartPoint.Y = tmp
}
for sattyPointNum > 0 {
//味方ポイントからの距離
distance := lottery.GetRandomInt(rangeFrom.Min, rangeFrom.Max)
sattyPoint, radian := CreateRandomPositionInMap(game_map.Size, allyStartPoint, distance)
if (laugh) {
Dlog("sattyPoint:%+v\n", sattyPoint)
if (xy.matrix[sattyPoint.Y][sattyPoint.X] == MacroMapTypeRoad) {
tryCount++
if (tryCount > 10) {
break
}
continue
}
tryCount = 0
}
sattyPoints = append(sattyPoints, sattyPoint)
radians = append(radians, radian)
sattyPointNum--
}
//角度をソートする
sort.Float64s(radians)
//エッジ分を追加
var sattyPointsEdge []GameMapPosition
sattyPointsEdge = CreateEdgePositionInMap(game_map.Size, allyStartPoint, radians)
sattyPoints = append(sattyPoints, sattyPointsEdge...)
DDlog("ahoaho1:%+v\n", game_map.EnemyStartPoints)
game_map.EnemyStartPoints = append(game_map.EnemyStartPoints, sattyPoints...)
DDlog("ahoaho2:%+v\n", game_map.EnemyStartPoints)
}
func (game_map *GameMap)fillJungleGymToEmpty() {
for z := 0; z <= game_map.Size.MaxZ; z++ {
for y := 0; y < game_map.Size.MaxY; y++ {
for x := 0; x < game_map.Size.MaxX; x++ {
game_map.JungleGym[z][y][x] = GameParts{IsEmpty:true};
}
}
}
}
func (game_map *GameMap) allocToJungleGym(maxAreaId int) {
game_map.JungleGym = make([][][]GameParts, game_map.Size.MaxZ + 1)
for z := 0; z <= game_map.Size.MaxZ; z++ {
game_map.JungleGym[z] = make([][]GameParts, game_map.Size.MaxY)
for y := 0; y < game_map.Size.MaxY; y++ {
game_map.JungleGym[z][y] = make([]GameParts, game_map.Size.MaxX)
}
}
game_map.fillJungleGymToEmpty();
game_map.MacroMapTypes = make([][][]MacroMapType, game_map.Size.MaxZ + 1)
for z := 0; z <= game_map.Size.MaxZ; z++ {
game_map.MacroMapTypes[z] = make([][]MacroMapType, game_map.Size.MaxY)
for y := 0; y < game_map.Size.MaxY; y++ {
game_map.MacroMapTypes[z][y] = make([]MacroMapType, game_map.Size.MaxX)
}
}
game_map.High = make([][]int, game_map.Size.MaxY)
for y := 0; y < game_map.Size.MaxY; y++ {
game_map.High[y] = make([]int, game_map.Size.MaxX)
}
game_map.AreaId = make([][]int, game_map.Size.MaxY)
for y := 0; y < game_map.Size.MaxY; y++ {
game_map.AreaId[y] = make([]int, game_map.Size.MaxX)
}
/*
game_map.AreaPath = make([][]int, maxAreaId)
for id := 0; id < maxAreaId; id++ {
game_map.AreaPath[id] = []int{}
}
game_map.AreaCenter = make([]GameMapPosition, maxAreaId)
*/
}
//ここでxyをjungleGymへ移行
func (game_map *GameMap) copyFromXY(xy *xymap) {
for x := 0; x < xy.mapSize.MaxX; x++ {
for y := 0; y < xy.mapSize.MaxY; y++ {
macro := xy.getMatrix(x, y);
high := xy.getHigh(x, y);
game_map.High[y][x] = high;
game_map.AreaId[y][x] = xy.getAreaId(x, y)
DDlog("crash x:%d y:%d high%d, lenz:%d", x, y, high, len(game_map.MacroMapTypes))
for z := 0; z < high; z++ {
game_map.MacroMapTypes[z][y][x] = macro;
}
}
}
game_map.AreaCenter = xy.areaCenter
game_map.AreaPath = xy.areaPath
}
//パーツとのひも付け
//失敗ならfalse
func (game_map *GameMap) bindToGameParts(gamePartsDict map[string]GameParts) bool {
/*選定パーツのゾーニング*/
//1.主幹パーツの決定:道・ラフ・その他
if (game_map.Geographical == GeographicalFire ) {
DDDlogln("fire10");
}
idsRoadFull := GetIdsRoad(game_map, gamePartsDict, false)
idsRoughFull := GetIdsRough(game_map, gamePartsDict, false)
idsWallFull := GetIdsWall(game_map, gamePartsDict, false)
idsRoadHalf := GetIdsRoad(game_map, gamePartsDict, true)
idsRoughHalf := GetIdsRough(game_map, gamePartsDict, true)
idsWallHalf := GetIdsWall(game_map, gamePartsDict, true)
idsWaterHalf := GetIdsWater(game_map, gamePartsDict, true)
if (len(idsRoadHalf) == 0 || len(idsRoughHalf) == 0 || len(idsWallHalf) == 0) {
if (len(idsRoadHalf) == 0) {
if (len(idsRoughHalf) != 0) {
idsRoadHalf = idsRoughHalf
}
if (len(idsWallHalf) != 0) {
idsRoadHalf = idsWallHalf
}
}
if (len(idsRoughHalf) == 0) {
if (len(idsRoadHalf) != 0) {
idsRoughHalf = idsRoadHalf
}
if (len(idsWallHalf) != 0) {
idsRoughHalf = idsWallHalf
}
}
if (len(idsWallHalf) == 0) {
if (len(idsRoadHalf) != 0) {
idsWallHalf = idsRoughHalf
}
if (len(idsRoughHalf) != 0) {
idsWallHalf = idsRoughHalf
}
}
if (len(idsRoadHalf) == 0 || len(idsRoughHalf) == 0 || len(idsWallHalf) == 0) {
return false
}
}
//2.パーツ割当
for x := 0; x < game_map.Size.MaxX; x++ {
for y := 0; y < game_map.Size.MaxY; y++ {
high := game_map.High[y][x];
for z := 0; z < high; z++ {
shouldHalf := ((high - 1) == z && (high % 2) > 0 )
/*
//half段目か?であれば、halfとし、それ以外は非half
if((high - 1) == z && high%2 ){
//half確定
}else{
}
*/
macro := game_map.MacroMapTypes[z][y][x]
//1.土
if (z < high - 1) {
parts := GetGamePartsFoundation(idsWallFull, idsRoughFull, idsRoadFull, gamePartsDict, x, y, z);
DDDlog("found : %2d,%2d,%2d id:%s \n", z, y, x, parts.Id)
if (shouldHalf) {
//halfにコンバートする
before := parts.Id
parts = GetHalfParts(idsRoadHalf, idsRoughHalf, idsWallHalf, parts, gamePartsDict, macro, x, y, z)
DDDlog("converted from %s to %s \n", before, parts.Id)
}
game_map.JungleGym[z][y][x] = parts;
continue;
}
//2.表層(道,ラフ,壁)
parts := GetGamePartsSurface(idsWallFull, idsRoughFull, idsRoadFull, gamePartsDict, macro, x, y, z);
DDDlog("surface: %2d,%2d,%2d id:%s macro[%v]\n", z, y, x, parts.Id, macro)
if (shouldHalf) {
if (macro == MacroMapTypeSwampWater ||
macro == MacroMapTypeSwampRava ||
macro == MacroMapTypeSwampPoison ||
macro == MacroMapTypeSwampHeal) {
DDDlog("unko3000")
parts = GetGamePartsWater(idsWaterHalf, gamePartsDict, macro, x, y, z)
}else {
//halfにコンバートする
before := parts.Id
parts = GetHalfParts(idsRoadHalf, idsRoughHalf, idsWallHalf, parts, gamePartsDict, macro, x, y, z)
DDDlog("converted from %s to %s \n", before, parts.Id)
}
}
game_map.JungleGym[z][y][x] = parts;
}
}
}
return true
}
//tileのimageを得る
func imageTile32(tile Tile) *image.RGBA {
file, err := os.Open("./assets/" + tile.FilePath)
defer file.Close()
if err != nil {
Dlogln(err)
return nil
}
img, err := png.Decode(file)
if err != nil {
Dlogln(err)
return nil
}
// tileRect := image.Rect(tile.X, tile.Y, tile.Width, tile.Height)
yy := img.Bounds().Max.Y - tile.Y - tile.Height;
outputRect := image.Rect(0, 0, tile.Width, tile.Height)
outputImg := image.NewRGBA(outputRect)
for x := 0; x < outputRect.Max.X; x++ {
for y := 0; y < outputRect.Max.Y; y++ {
outputImg.Set(x, y, img.At(tile.X + x, yy + y))
}
}
return outputImg
}
//tileのimageを得る
func imageTile64(tile Tile) *image.RGBA {
file, err := os.Open("./assets/" + tile.FilePath)
defer file.Close()
if err != nil {
Dlogln(err)
return nil
}
img, err := png.Decode(file)
if err != nil {
Dlogln(err)
return nil
}
// tileRect := image.Rect(tile.X, tile.Y, tile.Width, tile.Height)
yy := img.Bounds().Max.Y - tile.Y - tile.Height;
outputRect := image.Rect(0, 0, tile.Width / 2, tile.Height / 2)
outputImg := image.NewRGBA(outputRect)
for x := 0; x < outputRect.Max.X; x++ {
for y := 0; y < outputRect.Max.Y; y++ {
outputImg.Set(x, y, img.At(tile.X + x * 2, yy + y * 2))
}
}
return outputImg
}
//srcImgのsrcRectを dstImgのdstRectにコピ-
func clipAfromB(srcImg *image.RGBA, srcRect image.Rectangle, dstImg *image.RGBA, dstRect image.Rectangle) {
for x := 0; x < srcRect.Max.X; x++ {
for y := 0; y < srcRect.Max.Y; y++ {
srcRGBA := srcImg.At(x, y)
_, _, _, a := srcRGBA.RGBA()
if (a == 0) {
//0は透明
continue
}
dstImg.Set(dstRect.Min.X + x, dstRect.Min.Y + y, srcRGBA)
}
}
}
//x,y,zの起点座標の計算
func (game_map *GameMap) targetDrawPoint(x int, y int, z int) image.Point {
xx := (x) * 16 + (y) * 16
yy := (game_map.Size.MaxZ - z) * 16 / 2 +
(game_map.Size.MaxY - y) * 8 +
x * 8;
// yy := (8 * x * -1) + (8 * y) + (16 * z)
return image.Point{xx, yy}
}
/*
CGFloat xOrigin = self.aspectX / 2.0f * matrix.x +
self.aspectX / 2.0f * matrix.y;
CGFloat yOrigin =
self.aspectY / 2.0f * matrix.x * -1.0f +
self.aspectY / 2.0f * matrix.y +
self.aspectT * matrix.z;
CGFloat yAid = [self aidOfZ0Position];
return CGPointMake(xOrigin, yOrigin + yAid);
*/
func (game_map *GameMap) updateMaxZ() {
maxHigh := 0
for y := 0; y < game_map.Size.MaxY; y++ {
for x := 0; x < game_map.Size.MaxX; x++ {
if (maxHigh < game_map.High[y][x]) {
maxHigh = game_map.High[y][x]
}
}
}
game_map.Size.MaxZ = maxHigh
}
//png生成
func (game_map *GameMap) createPng(gamePartsDict map[string]GameParts) {
game_map.updateMaxZ();
outputWidth := 16 + game_map.Size.MaxX * 16 + (game_map.Size.MaxY - 1) * 16
outputHeight := 16 + game_map.Size.MaxX * 8 + game_map.Size.MaxY * 8 + game_map.Size.MaxZ * 16
outputRect := image.Rect(0, 0, outputWidth, outputHeight)
DDDlog("aho1:%+v\n", outputRect)
//出力するイメージ
outputImg := image.NewRGBA(outputRect)
//塗りつぶす
for x := 0; x < outputRect.Max.X; x++ {
for y := 0; y < outputRect.Max.Y; y++ {
//fmt.Printf("aho111:%d,%d/,%d,%d\n",x,y,outputRect.Max.X,outputRect.Max.Y)
outputImg.Set(x, y, color.Black)
}
}
DDDlog("aho2:rendering...\n")
for z := 0; z <= game_map.Size.MaxZ; z++ {
for y := (game_map.Size.MaxY - 1); y >= 0; y-- {
for x := 0; x < game_map.Size.MaxX; x++ {
cube := game_map.JungleGym[z][y][x]
if (cube.IsEmpty) {
//fmt.Printf("gomi:%d,%d,%d\n", z, y, x)
continue
}
if (z % 2 > 0) {
//レンダリングしなくて良い
continue
}
if (game_map.shouldLightening(x, y, z)) {
//肉抜き
continue
}
if (!cube.Harf) {
cube = game_map.JungleGym[z + 1][y][x]
}
//fmt.Printf("unko: %d,%d,%d\n", z, y, x)
//fmt.Printf("cube: %+v\n", cube)
//切り出す
tile := cube.Tiles[0]
var tileImage *image.RGBA
if (cube.RezoType == RezoTypeRect64) {
tileImage = imageTile64(tile)
}else {
tileImage = imageTile32(tile)
}
tileRect := image.Rect(0, 0, tile.Width, tile.Height)
//バッファへ貼り付け
dstPoint := game_map.targetDrawPoint(x, y, z)
//dstPoint := targetDrawPoint(x, y, z)
dstRect := image.Rect(dstPoint.X, dstPoint.Y, outputWidth, outputHeight)
clipAfromB(tileImage, tileRect, outputImg, dstRect)
}
}
}
DDDlog("aho3:drawed\n")
//ディレクトリ作成
directoryName := game_map.CreateDirectoryName();
err := os.MkdirAll(directoryName, 0777)
if err != nil {
Dlogln(err)
return
}
//ファイル出力
fileName := uuid.NewV4().String()
game_map.Filename = fileName
file, err := os.Create(directoryName + fileName + ".png")
defer file.Close()
if err != nil {
Dlogln(err)
return
}
err = png.Encode(file, outputImg)
if err != nil {
Dlogln(err)
os.Exit(1)
}
DDDlog("aho4:png outputed\n")
}
func (game_map *GameMap)CreateDirectoryName() string {
prefix := "./output/"
suffix := "/"
word := ""
switch game_map.Geographical{
case GeographicalStep:
word = "平原"
case GeographicalCave:
word = "洞窟"
case GeographicalRemain:
word = "遺跡"
case GeographicalPoison:
word = "毒沼"
case GeographicalFire:
word = "火山"
case GeographicalSnow:
word = "雪原"
case GeographicalJozen:
word = "城前"
case GeographicalCastle:
word = "城"
}
return prefix + word + suffix
}
//マップ
type GameMap struct {
JungleGym [][][]GameParts
MacroMapTypes [][][]MacroMapType
High [][]int
AreaId [][]int
AreaPath [][]int //グラフの辺[Areaid] に移動可能なAreaIdの配列
AreaCenter []GameMapPosition
Size GameMapSize
AllyStartPoint GameMapPosition
EnemyStartPoints []GameMapPosition
Difficult Difficult
Geographical Geographical
Category Category
IsSnow bool
PavementLv int
Filename string
}
type JsonPanel struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
Id string `json:"id"`
AreaId int `json:"aid"`
}
//エリア追加情報
type AreaInfo struct {
Lines []int
Center GameMapPosition
}
//マップ
type JsonGameMap struct {
MaxX int `json:"maxX"`
MaxY int `json:"maxY"`
MaxZ int `json:"maxZ"`
AspectX int `json:"aspectX"`
AspectY int `json:"aspectY"`
AspectT int `json:"aspectT"`
JungleGym []JsonPanel `json:"jungleGym"`
GameParts []GameParts `json:"gameParts"`
Areas []AreaInfo
AllyStartPoint GameMapPosition `json:"allyStartPoint"`
EnemyStartPoints []GameMapPosition `json:"enemyStartPoints"`
Category Category `json:"category"`
}
//Json生成
func (game_map *GameMap) createJson(gamePartsDict map[string]GameParts) {
Dlog("==output json==\n")
{
//Ally
game_map.AllyStartPoint.Z = game_map.High[game_map.AllyStartPoint.Y][game_map.AllyStartPoint.X] / 2 // - 1
ahigh := game_map.High[game_map.AllyStartPoint.Y][game_map.AllyStartPoint.X]
if ahigh % 2 != 0 {
game_map.AllyStartPoint.Z += 1
}
}
for i, _ := range game_map.EnemyStartPoints {
// キーは使われません
game_map.EnemyStartPoints[i].Z =
game_map.High[game_map.EnemyStartPoints[i].Y][game_map.EnemyStartPoints[i].X] / 2 //- 1
ehigh := game_map.High[game_map.EnemyStartPoints[i].Y][game_map.EnemyStartPoints[i].X]
if ehigh % 2 != 0 {
game_map.EnemyStartPoints[i].Z += 1
}
}
for _, enemyStartPoint2 := range game_map.EnemyStartPoints {
Dlog("enemyStartPointF: %+v\n", enemyStartPoint2)
}
jsonStub := JsonGameMap{
MaxX:game_map.Size.MaxX,
MaxY:game_map.Size.MaxY,
MaxZ:game_map.Size.MaxZ,
AspectX:32,
AspectY:16,
AspectT:16,
AllyStartPoint:game_map.AllyStartPoint,
EnemyStartPoints:game_map.EnemyStartPoints,
Category:game_map.Category,
}
jsonStub.Areas = make([]AreaInfo, len(game_map.AreaCenter))
i := 0
for _, center := range game_map.AreaCenter {
jsonStub.Areas[i] = AreaInfo{Center:center, Lines:game_map.AreaPath[i]}
i++