Skip to content

Instantly share code, notes, and snippets.

@wojpawlik
Last active April 28, 2024 11:15
Show Gist options
  • Save wojpawlik/ab51d51a81bd0d351efb06f614eb73f1 to your computer and use it in GitHub Desktop.
Save wojpawlik/ab51d51a81bd0d351efb06f614eb73f1 to your computer and use it in GitHub Desktop.
%!PS-Adobe-3.0
%%Creator: (Wojciech Pawlik)
%%BoundingBox: 0 0 600 600
%%EndComments
%%BeginProlog
<< /PageSize [600 600] >> setpagedevice
/Helvetica-Bold findfont 20 scalefont setfont
300 300 translate
1 1 1 setrgbcolor
realtime srand
/circle { 0 360 arc } def
/brick {.87 .36 .15} def
/desert {.83 .80 .56} def
/grain {.96 .74 .15} def
/ore {.62 .62 .62} def
/sheep {.60 .72 .02} def
/water {.04 .40 .65} def
/wood {.07 .59 .22} def
/hexsize 50 .75 sqrt div def
/terrains [
(desert)
(ore) dup dup
(brick) dup dup
(wood) dup dup dup
(grain) dup dup dup
(sheep) dup dup dup
] def
/fromhexpolar {
gsave
[100 0 0 100 0 0] setmatrix
36 add % avoids negative remainder
1 index 2 copy
idiv 60 mul rotate
mod exch
0 translate
120 rotate
0 transform
grestore
} bind def
/position {
% https://en.wikipedia.org/wiki/Centered_hexagonal_number
dup 3 div sqrt .5 add cvi % distance from center
dup 0 ne {
dup dup mul 3 mul
3 -1 roll sub
fromhexpolar
} if
} bind def
/hex {
1 dict begin
/a exch def
0 a moveto
6 {
60 rotate
0 a lineto
} repeat
closepath
end
} def
/number {
0 0 20 circle fill
% probability dots -- count
dup 7 sub abs
6 exch sub
% appearance
dup 5 ge {1 0 0} {0 0 0} ifelse setrgbcolor
[3 3] 0 setdash 3 setlinewidth
% probability dots -- position
6 mul 3 sub
dup -2 div -10 moveto
0 rlineto stroke
% text
2 string cvs
dup stringwidth pop -2 div
-3 moveto show
} def
/ppmsize {
dup 3 setfileposition
dup token pop exch
dup token pop exch
token pop pop
} def
% `--permit-file-read=sprites.ppm`
{ (sprites.ppm) dup (r) file def }
stopped {{pop}} {{
1 dict begin
0 get 95 sub 4 idiv % hash
/size [ sprites.ppm ppmsize 8 idiv ] cvx def
size 3 mul mul mul sprites.ppm fileposition add
sprites.ppm exch setfileposition
size 8 [1.25 0 0 -1.25 0 0] sprites.ppm false 3 colorimage
end
}} ifelse
/sprite exch def clear
/port {
gsave
3 1 roll
position translate
90 add rotate
-11 -60 translate
gsave
dup load exec setrgbcolor
[1 -20 20 20]
dup rectfill
dup rectclip
.5 .5 scale
exch sprite
grestore rectstroke
grestore
} def
/swap {
3 dict begin
/i exch def
/j exch def
/array exch def
array i get
array j get
array exch i exch put
array exch j exch put
end
} def
% shuffle
terrains length -1 1 {
/i exch def
terrains i 1 sub rand i mod swap
} for
%%EndProlog
% border
gsave
90 rotate 300 hex
water setrgbcolor fill
grestore
5 2 6 3 8 10 9 12 11 4 8 10 9 4 5 6 3 11
0 1 18 {
gsave
dup position translate
hexsize hex gsave
terrains exch get
dup load exec
setrgbcolor clip fill
-22 40 translate
dup sprite
grestore stroke
/sprites.ppm where {
0 -.375 hexsize mul translate pop
} if
(desert) ne {number} if
grestore
} for
% ports
60 rand mul rotate
/~3:1~ {1 1 1} def
180 17 (brick) port
240 16 (~3:1~) port
300 15 (~3:1~) port
300 13 (sheep) port
0 12 (~3:1~) port
60 11 (ore) port
60 9 (grain) port
120 8 (~3:1~) port
180 7 (wood) port
<< /PageSize [595 842] >> setpagedevice % A4
72 300 div dup scale % 300 DPI
215 215 translate
0 0 moveto
% https://en.wikipedia.org/wiki/Sierpiński_curve#Arrowhead_curve
<<
/A { /B /- /A /- /B }
/B { /A /+ /B /+ /A }
/- { /- }
/+ { /+ }
>> begin
[ /A ] 10
{ [ exch { load exec } forall ] } repeat
end
<<
/A { 1 0 rlineto }
/B { 1 0 rlineto }
/- { -60 rotate }
/+ { 60 rotate }
>> begin
dup { load exec } forall
dup { load exec } forall
pathbbox exch 4 div exch moveto
pop pop { load exec } forall
stroke
end
@wojpawlik
Copy link
Author

If you ever feel useless, remember the desert port:
desert port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment