-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapmake_cmd_resource.go
210 lines (189 loc) · 5.92 KB
/
mapmake_cmd_resource.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
// Copyright 2014,2015,2016,2017,2018,2019,2020,2021 SeukWon Kang (kasworld@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terrain
import (
"fmt"
"path/filepath"
"github.com/kasworld/goguelike-single/enum/resourcetype"
"github.com/kasworld/goguelike-single/game/terrain/resourcetile"
"github.com/kasworld/goguelike-single/lib/g2log"
"github.com/kasworld/goguelike-single/lib/maze2"
"github.com/kasworld/goguelike-single/lib/scriptparse"
"github.com/kasworld/walk2d"
)
func cmdResourceAt(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x, y int
if err := ca.GetArgs(&rsctl, &amount, &x, &y); err != nil {
return err
}
tr.resourceTileArea[x][y][rsctl] = resourcetile.ResourceValue(amount)
return nil
}
func cmdResourceHLine(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x, w, y int
if err := ca.GetArgs(&rsctl, &amount, &x, &w, &y); err != nil {
return err
}
fn := func(ax, ay int) bool {
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
return false
}
walk2d.HLine(x, x+w, y, fn)
return nil
}
func cmdResourceVLine(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x, y, h int
if err := ca.GetArgs(&rsctl, &amount, &x, &y, &h); err != nil {
return err
}
fn := func(ax, ay int) bool {
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
return false
}
walk2d.VLine(y, y+h, x, fn)
return nil
}
func cmdResourceLine(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x1, y1, x2, y2 int
if err := ca.GetArgs(&rsctl, &amount, &x1, &y1, &x2, &y2); err != nil {
return err
}
fn := func(ax, ay int) bool {
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
return false
}
walk2d.Line(x1, y1, x2, y2, fn)
return nil
}
func cmdResourceRect(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x, w, y, h int
if err := ca.GetArgs(&rsctl, &amount, &x, &w, &y, &h); err != nil {
return err
}
fn := func(ax, ay int) bool {
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
return false
}
walk2d.Rect(x, y, x+w, y+h, fn)
return nil
}
func cmdResourceFillRect(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x, w, y, h int
if err := ca.GetArgs(&rsctl, &amount, &x, &w, &y, &h); err != nil {
return err
}
fn := func(ax, ay int) bool {
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
return false
}
walk2d.FillHV(x, y, x+w, y+h, fn)
return nil
}
func cmdResourceFillEllipses(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, x, w, y, h int
if err := ca.GetArgs(&rsctl, &amount, &x, &w, &y, &h); err != nil {
return err
}
fn := func(ax, ay int) bool {
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
return false
}
walk2d.Ellipses(x, y, x+w, y+h, fn)
return nil
}
func cmdResourceRand(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var mean, stddev, repeat int
if err := ca.GetArgs(&rsctl, &mean, &stddev, &repeat); err != nil {
return err
}
for i := 0; i < repeat; i++ {
xpos := tr.rnd.Intn(tr.Xlen)
ypos := tr.rnd.Intn(tr.Ylen)
amount := tr.rnd.NormIntRange(mean, stddev)
tr.resourceTileArea[xpos][ypos][rsctl] = resourcetile.ResourceValue(amount)
}
return nil
}
func cmdResourceMazeWall(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, xn, yn int
var conerFill bool
var maX, maY, maW, maH int
if err := ca.GetArgs(&rsctl, &amount, &maX, &maY, &maW, &maH, &xn, &yn, &conerFill); err != nil {
return err
}
m := maze2.New(tr.rnd, xn, yn)
ma, err := m.ToBoolMatrix(maW, maH, conerFill)
if err != nil {
return fmt.Errorf("tr %v %v", tr, err)
}
for x, xv := range ma {
for y, yv := range xv {
if yv {
ax, ay := tr.XWrapper.WrapSafe(maX+x), tr.YWrapper.WrapSafe(maY+y)
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
}
}
}
return nil
}
func cmdResourceMazeWalk(tr *Terrain, ca *scriptparse.CmdArgs) error {
var rsctl resourcetype.ResourceType
var amount, xn, yn int
var conerFill bool
var maX, maY, maW, maH int
if err := ca.GetArgs(&rsctl, &amount, &maX, &maY, &maW, &maH, &xn, &yn, &conerFill); err != nil {
return err
}
m := maze2.New(tr.rnd, xn, yn)
ma, err := m.ToBoolMatrix(tr.GetXLen(), tr.GetYLen(), conerFill)
if err != nil {
return fmt.Errorf("tr %v %v", tr, err)
}
for x, xv := range ma {
for y, yv := range xv {
if !yv {
ax, ay := tr.XWrapper.WrapSafe(maX+x), tr.YWrapper.WrapSafe(maY+y)
tr.resourceTileArea[ax][ay][rsctl] = resourcetile.ResourceValue(amount)
}
}
}
return nil
}
func cmdResourceFromPNG(tr *Terrain, ca *scriptparse.CmdArgs) error {
var name string
if err := ca.GetArgs(&name); err != nil {
return err
}
if err := tr.resourceTileArea.FromImage(filepath.Join(tr.dataDir, name)); err != nil {
g2log.Fatal("%v %v", tr, err)
return err
}
return nil
}
func cmdAgeing(tr *Terrain, ca *scriptparse.CmdArgs) error {
var initrun, msper, resetaftern int
if err := ca.GetArgs(&initrun, &msper, &resetaftern); err != nil {
return err
}
tr.MSPerAgeing = int64(msper)
tr.ResetAfterNAgeing = int64(resetaftern)
tr.resourceTileArea.Ageing(tr.rnd.Intn, initrun)
return nil
}