summaryrefslogtreecommitdiffstats
path: root/hexmap.scm
blob: 91a84f6a119b3f6d42344c4b96fcb395531b5fd9 (plain)
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
; hexmap.scm
; by Jérémy Zurcher <jeremy@asynk.ch>
; based on hex_grid by Rob Antonishen

; Version 1.0 (2015)
;

; Description
;
;  build hex map with nice options
;

; Issues
;
;  you must first select the pencil tool beforce calling this script ?!??!?!?
;

; License:
;
; Copyright (c) <2015> <Jérémy Zurcher>
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.




(define (search l x y)
  (if (null? l)
    #f
    (if (and (= (vector-ref (car l) 0) x)
             (= (vector-ref (car l) 1) y))
      #t
      (search (cdr l) x y)
    )
  )
)

(define (maybe_add l x y)
  (set! x (/ (trunc (* x 1000)) 1000))
  (set! y (/ (trunc (* y 1000)) 1000))
  (if (search l x y)
    l
    (list* (vector x y) l)
  )
)

(define (crawl_hex_edges edges mask pressure)
  (if (not (null? edges))
    (begin
      (gimp-airbrush mask pressure 2 (car edges))
      (crawl_hex_edges (cdr edges) mask pressure)
    )
  )
)

(define (erase_hex_edges edges mask size)
  (gimp-context-set-brush-size (* 1.4 size))
  (gimp-context-set-foreground "black")
  (gimp-context-set-opacity 80)
  (gimp-context-set-brush "2. Hardness 025")
  (crawl_hex_edges edges mask 80)
)

(define (build_border_path border_edges border_path)
  (let*
    (
      (i 0)
      (y 0)
      (s (length border_edges))
      (n (* s 3))
      (v (make-vector n 0))
    )
    (while (< i s)
      (vector-set! v y       (list-ref border_edges i))
      (vector-set! v (+ y 1) (list-ref border_edges (+ i 1)))
      (vector-set! v (+ y 2) (list-ref border_edges i))
      (vector-set! v (+ y 3) (list-ref border_edges (+ i 1)))
      (vector-set! v (+ y 4) (list-ref border_edges i))
      (vector-set! v (+ y 5) (list-ref border_edges (+ i 1)))
      (set! i (+ i 2))
      (set! y (+ y 6))
    )
    (gimp-vectors-stroke-new-from-points border_path 0 n v TRUE)
  )
)

(define (build_h_border_edges border_edges vx vy x y xBorder yBorder bRight bBottom)
  (if (and (>= x 0) (>= y 0) (<= x xBorder) (<= y yBorder))
    ; top ?
    (if (= y 0)
      (if (or (equal? bRight #t) (< x xBorder))
        (set! border_edges (append border_edges
                                   (list
                                     (vector-ref vx 4)
                                     (vector-ref vy 4)
                                     (vector-ref vx 3)
                                     (vector-ref vy 3)
                                     (vector-ref vx 2)
                                     (vector-ref vy 2)
                                     (vector-ref vx 1)
                                     (vector-ref vy 1)
                                     ))
        )
        (set! border_edges (append border_edges
                                   (list
                                     (vector-ref vx 4)
                                     (vector-ref vy 4)
                                     (vector-ref vx 5)
                                     (vector-ref vy 5)
                                     ))
        )
      )
      ; else bottom ?
      (if (= y yBorder)
        (if (= x 0)
          (set! border_edges (list*
                               (vector-ref vx 7)
                               (vector-ref vy 7)
                               (vector-ref vx 6)
                               (vector-ref vy 6)
                               (vector-ref vx 5)
                               (vector-ref vy 5)
                               (vector-ref vx 4)
                               (vector-ref vy 4)
                               (vector-ref vx 3)
                               (vector-ref vy 3)
                               border_edges)
          )
          (if (< x xBorder)
            (set! border_edges (list*
                                 (vector-ref vx 7)
                                 (vector-ref vy 7)
                                 (vector-ref vx 6)
                                 (vector-ref vy 6)
                                 (vector-ref vx 5)
                                 (vector-ref vy 5)
                                 (vector-ref vx 4)
                                 (vector-ref vy 4)
                                 border_edges)
            )
            (if (equal? bRight #t)
              (set! border_edges (list*
                                   (vector-ref vx 2)
                                   (vector-ref vy 2)
                                   (vector-ref vx 1)
                                   (vector-ref vy 1)
                                   (vector-ref vx 6)
                                   (vector-ref vy 6)
                                   (vector-ref vx 5)
                                   (vector-ref vy 5)
                                   (vector-ref vx 4)
                                   (vector-ref vy 4)
                                   border_edges)
              )
              (set! border_edges (list*
                                   (vector-ref vx 4)
                                   (vector-ref vy 4)
                                   border_edges)
              )
            )
          )
        )
        ; else left ?
        (if (= x 0)
          (set! border_edges (list*
                               (vector-ref vx 4)
                               (vector-ref vy 4)
                               (vector-ref vx 3)
                               (vector-ref vy 3)
                               border_edges)
          )
          ; else right ?
          (if (= x xBorder)
            (if (equal? bRight #t)
              (set! border_edges (append border_edges
                                         (list
                                           (vector-ref vx 2)
                                           (vector-ref vy 2)
                                           (vector-ref vx 1)
                                           (vector-ref vy 1)
                                           ))
              )
              (set! border_edges (append border_edges
                                         (list
                                           (vector-ref vx 4)
                                           (vector-ref vy 4)
                                           (vector-ref vx 5)
                                           (vector-ref vy 5)
                                           ))
              )
            )
          )
        )
      )
    )
  )
  border_edges
)

(define (build_v_border_edges border_edges vx vy x y xBorder yBorder bRight bBottom)
  (if (and (>= x 0) (>= y 0) (<= x xBorder) (<= y yBorder))
    ; top ?
    (if (= y 0)
      ; top left
      (if (= x 0)
        (set! border_edges (append border_edges
                                   (list
                                     (vector-ref vx 1)
                                     (vector-ref vy 1)
                                     (vector-ref vx 2)
                                     (vector-ref vy 2)
                                     (vector-ref vx 3)
                                     (vector-ref vy 3)
                                     (vector-ref vx 4)
                                     (vector-ref vy 4)
                                     (vector-ref vx 5)
                                     (vector-ref vy 5)
                                     ))
        )
        (if (= x xBorder)
          (set! border_edges (append border_edges
                                     (list
                                       (vector-ref vx 4)
                                       (vector-ref vy 4)
                                       (vector-ref vx 5)
                                       (vector-ref vy 5)
                                       (vector-ref vx 6)
                                       (vector-ref vy 6)
                                       (vector-ref vx 7)
                                       (vector-ref vy 7)
                                       ))
          )
          (set! border_edges (append border_edges
                                     (list
                                       (vector-ref vx 4)
                                       (vector-ref vy 4)
                                       (vector-ref vx 5)
                                       (vector-ref vy 5)
                                       ))
          )
        )
      )
      ; else bottom ?
      (if (= y yBorder)
        (if (equal? bBottom #t)
          (if (= x 0)
            (set! border_edges (list*
                                 (vector-ref vx 6)
                                 (vector-ref vy 6)
                                 (vector-ref vx 1)
                                 (vector-ref vy 1)
                                 (vector-ref vx 2)
                                 (vector-ref vy 2)
                                 (vector-ref vx 3)
                                 (vector-ref vy 3)
                                 (vector-ref vx 4)
                                 (vector-ref vy 4)
                                 border_edges)
            )
            (if (< x xBorder)
              (set! border_edges (list*
                                   (vector-ref vx 6)
                                   (vector-ref vy 6)
                                   (vector-ref vx 7)
                                   (vector-ref vy 7)
                                   border_edges)
              )
              (set! border_edges (list*
                                   (vector-ref vx 4)
                                   (vector-ref vy 4)
                                   (vector-ref vx 5)
                                   (vector-ref vy 5)
                                   (vector-ref vx 6)
                                   (vector-ref vy 6)
                                   (vector-ref vx 7)
                                   (vector-ref vy 7)
                                   border_edges)
              )
            )
          )
          (if (< x xBorder)
            (set! border_edges (list*
                                 (vector-ref vx 5)
                                 (vector-ref vy 5)
                                 (vector-ref vx 4)
                                 (vector-ref vy 4)
                                 border_edges)
            )
            (set! border_edges (list*
                                 (vector-ref vx 4)
                                 (vector-ref vy 4)
                                 border_edges)
            )
          )
        )
        ; else left ?
        (if (= x 0)
          (set! border_edges (list*
                               (vector-ref vx 1)
                               (vector-ref vy 1)
                               (vector-ref vx 2)
                               (vector-ref vy 2)
                               (vector-ref vx 3)
                               (vector-ref vy 3)
                               (vector-ref vx 4)
                               (vector-ref vy 4)
                               border_edges)
          )
          ; else right ?
          (if (= x xBorder)
            (set! border_edges (append border_edges
                                       (list
                                         (vector-ref vx 4)
                                         (vector-ref vy 4)
                                         (vector-ref vx 5)
                                         (vector-ref vy 5)
                                         (vector-ref vx 6)
                                         (vector-ref vy 6)
                                         (vector-ref vx 7)
                                         (vector-ref vy 7)
                                         ))
            )
          )
        )
      )
    )
  )
  border_edges
)

(define (stroke_grid_hex grip_path vx vy)
  (gimp-vectors-stroke-new-from-points grip_path 0 (* 8 2 3)
                                       (vector
                                               (vector-ref vx 0) (vector-ref vy 0)
                                               (vector-ref vx 0) (vector-ref vy 0)
                                               (vector-ref vx 0) (vector-ref vy 0)
                                               (vector-ref vx 1) (vector-ref vy 1)
                                               (vector-ref vx 1) (vector-ref vy 1)
                                               (vector-ref vx 1) (vector-ref vy 1)
                                               (vector-ref vx 2) (vector-ref vy 2)
                                               (vector-ref vx 2) (vector-ref vy 2)
                                               (vector-ref vx 2) (vector-ref vy 2)
                                               (vector-ref vx 3) (vector-ref vy 3)
                                               (vector-ref vx 3) (vector-ref vy 3)
                                               (vector-ref vx 3) (vector-ref vy 3)
                                               (vector-ref vx 4) (vector-ref vy 4)
                                               (vector-ref vx 4) (vector-ref vy 4)
                                               (vector-ref vx 4) (vector-ref vy 4)
                                               (vector-ref vx 5) (vector-ref vy 5)
                                               (vector-ref vx 5) (vector-ref vy 5)
                                               (vector-ref vx 5) (vector-ref vy 5)
                                               (vector-ref vx 6) (vector-ref vy 6)
                                               (vector-ref vx 6) (vector-ref vy 6)
                                               (vector-ref vx 6) (vector-ref vy 6)
                                               (vector-ref vx 7) (vector-ref vy 7)
                                               (vector-ref vx 7) (vector-ref vy 7)
                                               (vector-ref vx 7) (vector-ref vy 7)
                                               ) FALSE
  )
)

(define (stroke_hex hex_path vx vy)
  (gimp-vectors-stroke-new-from-points hex_path 0 (* 6 2 3)
                                       (vector
                                               (vector-ref vx 1) (vector-ref vy 1)
                                               (vector-ref vx 1) (vector-ref vy 1)
                                               (vector-ref vx 1) (vector-ref vy 1)
                                               (vector-ref vx 2) (vector-ref vy 2)
                                               (vector-ref vx 2) (vector-ref vy 2)
                                               (vector-ref vx 2) (vector-ref vy 2)
                                               (vector-ref vx 3) (vector-ref vy 3)
                                               (vector-ref vx 3) (vector-ref vy 3)
                                               (vector-ref vx 3) (vector-ref vy 3)
                                               (vector-ref vx 4) (vector-ref vy 4)
                                               (vector-ref vx 4) (vector-ref vy 4)
                                               (vector-ref vx 4) (vector-ref vy 4)
                                               (vector-ref vx 5) (vector-ref vy 5)
                                               (vector-ref vx 5) (vector-ref vy 5)
                                               (vector-ref vx 5) (vector-ref vy 5)
                                               (vector-ref vx 6) (vector-ref vy 6)
                                               (vector-ref vx 6) (vector-ref vy 6)
                                               (vector-ref vx 6) (vector-ref vy 6)
                                               ) TRUE
  )
)

(define (build_grid hex_path grid_path border_path width height sideLength orientation xOff yOff)
  (let*
    (
      (w (- width xOff))
      (h (- height yOff))
      (x (if (> xOff 0) -1 0))
      (y (if (> yOff 0) -1 0))
      (vx (make-vector 8 0))
      (vy (make-vector 8 0))
      (hex_edges '())
      (border_edges '())
      (hX 0)
      (hY 0)
      (xAdd 0)
      (yAdd 0)
      (xLast 0)
      (yLast 0)
      (xBorder 0)
      (yBorder 0)
      (bRight #t)
      (bBottom #t)
    )

    (if (= orientation 0)
      ; horizontal
      (begin
        (set! xLast (trunc (/ w (* sideLength 3.0))))
        (set! yLast (trunc (/ h (* sideLength 1.73205))))
        (set! xBorder (trunc (/ (- w xOff) (* sideLength 3.0))))
        (set! yBorder (- yLast 1))
        (set! bRight (if (> (- w xOff (* xBorder sideLength 3.0)) sideLength) #t #f))
        ; (set! bBottom #t)
        (set! hX (vector (* sideLength 3.0) (* sideLength 2.0) (* sideLength 1.5) (* sideLength 0.5) 0 (* sideLength 0.5) (* sideLength 1.5) (* sideLength 2.0)))
        (set! hY (vector (* sideLength 1.73205 0.5) (* sideLength 1.73205 0.5) 0 0 (* sideLength 1.73205 0.5) (* sideLength 1.73205) (* sideLength 1.73205) (* sideLength 1.73205 0.5)))
      )
      (begin
        (set! xLast (trunc (/ w (* sideLength 1.73205))))
        (set! yLast (trunc (/ h (* sideLength 3.0))))
        (set! xBorder (- xLast 1))
        (set! yBorder (trunc (/ (- h yOff) (* sideLength 3.0))))
        ; (set! bRight #t)
        (set! bBottom (if (> (- h yOff (* yBorder sideLength 3.0)) sideLength) #t #f))
        (set! hX (vector (* sideLength 1.73205 0.5) (* sideLength 1.73205 0.5) 0 0 (* sideLength 1.73205 0.5) (* sideLength 1.73205) (* sideLength 1.73205) (* sideLength 1.73205 0.5)))
        (set! hY (vector (* sideLength 3.0) (* sideLength 2.0) (* sideLength 1.5) (* sideLength 0.5) 0 (* sideLength 0.5) (* sideLength 1.5) (* sideLength 2.0)))
      )
    )

    (while (<= y yLast)
      (while (<= x xLast)
        (if (= orientation 0)
          ; horizontal
          (begin
            (set! xAdd (+ (* x sideLength 3.0) xOff))
            (set! yAdd (+ (* y sideLength 1.73205) yOff))
          )
          (begin
            (set! xAdd (+ (* x sideLength 1.73205) xOff))
            (set! yAdd (+ (* y sideLength 3.0) yOff))
          )
        )
        (vector-set! vx 0 (+ (vector-ref hX 0) xAdd))
        (vector-set! vx 1 (+ (vector-ref hX 1) xAdd))
        (vector-set! vx 2 (+ (vector-ref hX 2) xAdd))
        (vector-set! vx 3 (+ (vector-ref hX 3) xAdd))
        (vector-set! vx 4 (+ (vector-ref hX 4) xAdd))
        (vector-set! vx 5 (+ (vector-ref hX 5) xAdd))
        (vector-set! vx 6 (+ (vector-ref hX 6) xAdd))
        (vector-set! vx 7 (+ (vector-ref hX 7) xAdd))
        (vector-set! vy 0 (+ (vector-ref hY 0) yAdd))
        (vector-set! vy 1 (+ (vector-ref hY 1) yAdd))
        (vector-set! vy 2 (+ (vector-ref hY 2) yAdd))
        (vector-set! vy 3 (+ (vector-ref hY 3) yAdd))
        (vector-set! vy 4 (+ (vector-ref hY 4) yAdd))
        (vector-set! vy 5 (+ (vector-ref hY 5) yAdd))
        (vector-set! vy 6 (+ (vector-ref hY 6) yAdd))
        (vector-set! vy 7 (+ (vector-ref hY 7) yAdd))
        ; grid path
        (stroke_grid_hex grid_path vx vy)
        ; hex path
        (if (and (= y 0) (= x 0))
          (stroke_hex hex_path vx vy)
        )
        ; border
        (if (= orientation 0) ; horizontal
          (set! border_edges (build_h_border_edges border_edges vx vy x y xBorder yBorder bRight bBottom))
          (set! border_edges (build_v_border_edges border_edges vx vy x y xBorder yBorder bRight bBottom))
        )
        ; hex edges
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 0) (vector-ref vy 0)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 1) (vector-ref vy 1)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 2) (vector-ref vy 2)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 3) (vector-ref vy 3)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 4) (vector-ref vy 4)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 5) (vector-ref vy 5)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 6) (vector-ref vy 6)))
        (set! hex_edges (maybe_add hex_edges (vector-ref vx 7) (vector-ref vy 7)))
        ; next loop values
        (set! x (+ x 1))
      )
      (set! y (+ y 1))
      (set! x (if (> xOff 0) -1 0))
    )
    (display border_edges)
    (build_border_path border_edges border_path)
    hex_edges
  )
)

(define (script-fu-hex_map orientation elm len xN yN xOff yOff erase gStroke gColour bStroke bColour bOpacity)
  (let*
    (
      (img 0)
      (gridLayer 0)
      (borderLayer 0)
      (mask 0)
      (width 0)
      (height 0)
      (hex_path 0)
      (grid_path 0)
      (hex_edges 0)
      (border_path 0)
      (border_edges '())
      (sideLength (cond ((equal? elm 0) len) ((equal? elm 1) (/ len 2.0)) ((equal? elm 2) (/ len 1.73205))))
      (brushTemp (car (gimp-brush-new "HexMapBrush")))
    )

    (if (= orientation 0)
      ; horizontal
      (begin
        (set! height (+ (* 2 yOff) (* 1.73205 yN sideLength)))
        (set! width  (+ (* 2 xOff) (* (+ 0.5 (* 1.5 xN)) sideLength)))
      )
      (begin
        (set! width  (+ (* 2 xOff) (* 1.73205 xN sideLength)))
        (set! height (+ (* 2 yOff) (* (+ 0.5 (* 1.5 yN)) sideLength)))
      )
    )

    ; START
    (gimp-context-push)

    (set! img (car (gimp-image-new width height RGB)))
    (gimp-image-undo-group-start img)

    ; set brush
    (gimp-brush-set-angle brushTemp 0)
    (gimp-brush-set-aspect-ratio brushTemp 1)
    (gimp-brush-set-hardness brushTemp 1)
    (gimp-brush-set-shape brushTemp BRUSH-GENERATED-CIRCLE)
    (gimp-brush-set-spacing brushTemp 0)
    (gimp-brush-set-spikes brushTemp 1)
    (gimp-brushes-refresh)

    (gimp-context-set-brush "HexMapBrush")
    (gimp-context-set-opacity 100)
    (gimp-context-set-paint-mode NORMAL-MODE)
    (gimp-context-set-paint-method "gimp-pencil")

    ; paths
    (set! hex_path (car (gimp-vectors-new img "Hex")))
    (gimp-image-add-vectors img hex_path -1)
    (set! grid_path (car (gimp-vectors-new img "Hex Grid")))
    (gimp-image-add-vectors img grid_path -1)
    (set! border_path (car (gimp-vectors-new img "Map Border")))
    (gimp-image-add-vectors img border_path -1)
    (set! hex_edges (build_grid hex_path grid_path border_path width height sideLength orientation xOff yOff))

    ; grid layer
    (set! gridLayer (car (gimp-layer-new img width height RGBA-IMAGE "Grid" 100 NORMAL-MODE)))
    (gimp-image-add-layer img gridLayer -1)
    (gimp-context-set-brush-size gStroke)
    (gimp-context-set-foreground gColour)
    (if (> gStroke 0)
      (begin
        (gimp-edit-stroke-vectors gridLayer grid_path)
        (gimp-path-to-selection img "Map Border" 2 0 0 0 0)
        (gimp-selection-invert img)
        (gimp-edit-clear gridLayer)
      )
    )

    ; border layer
    (set! borderLayer (car (gimp-layer-new img width height RGBA-IMAGE "Border" 100 NORMAL-MODE)))
    (gimp-image-add-layer img borderLayer -1)
    ; transparent border
    (gimp-context-set-foreground "black")
    (gimp-edit-bucket-fill borderLayer 0 0 bOpacity 20 0 0 0)
    ; border stroke
    (if (> bStroke 0)
      (begin
        (gimp-context-set-brush-size bStroke)
        (gimp-context-set-foreground bColour)
        (gimp-edit-stroke-vectors borderLayer border_path)
      )
    )

    (gimp-selection-none img)
    (gimp-brush-delete brushTemp)

    ; grid mask
    (if(not (= erase 0))
      (begin
        (set! mask (car (gimp-layer-create-mask gridLayer ADD-WHITE-MASK)))
        (gimp-layer-add-mask gridLayer mask)
        (erase_hex_edges hex_edges mask sideLength)
        (if(= erase 2)
          (gimp-invert mask)
        )
      )
    )

    (gimp-display-new img)
    (gimp-image-clean-all img)

    ; END
    (gimp-image-undo-group-end img)
    (gimp-displays-flush)
    (gimp-context-pop)
  )
)

; (define (script-fu-hex_mapp)
;   (script-fu-hex_map 0 0 100 5 5 140 50 2 "black" 6 "red")
; )

(script-fu-register "script-fu-hex_map"
                    "Hex Map..."
                    "Draw a hex grid on the image."
                    "Jérémy Zurcher"
                    "Copyright 2015, Jérémy Zurcher"
                    "Nov 2015"
                    ""
                    SF-OPTION     "Hex Orientation"         '("Horizontal" "Vertical")
                    SF-OPTION     "Element to Specify"      '("Side" "Point to Point" "Side to Side")
                    SF-ADJUSTMENT "Length of Element"       '(100 2 400 1 10 0 SF-SPINNER)
                    SF-ADJUSTMENT "Horizontal Hex (#)"      '(18 2 500 1 10 0 SF-SPINNER)
                    SF-ADJUSTMENT "Vertical Hex (#)"        '(10 2 500 1 10 0 SF-SPINNER)
                    SF-ADJUSTMENT "Horizontal Offset (px)"  '(50 0 399 0.5 10 1 SF-SPINNER)
                    SF-ADJUSTMENT "Vertical Offset (px)"    '(50 0 399 0.5 10 1 SF-SPINNER)
                    SF-OPTION     "Erase"                   '("None" "Points" "Segments")
                    SF-ADJUSTMENT "Line Width (px)"         '(2 1 20 1 10 0 SF-SPINNER)
                    SF-COLOR      "Line Colour"             '(244 244 244)
                    SF-ADJUSTMENT "Border Width (px)"       '(6 0 20 1 10 0 SF-SPINNER)
                    SF-COLOR      "Border Colour"           '(244 244 244) ;'(69 70 11)
                    SF-ADJUSTMENT "Border Opacity (px)"     '(40 0 100 1 10 0 SF-SPINNER)
)
(script-fu-menu-register "script-fu-hex_map" "<Image>/File/Create")