-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.rb
executable file
·231 lines (220 loc) · 6.2 KB
/
render.rb
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
#! /usr/bin/env ruby
#--
# Copyright © 2022 Ken Coar
#
# 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.
#++
# frozen_string_literal: true
#
# Load in a TAGF game definition from a `YAML` file, build it as a
# digraph using `ruby-graphviz`, and render it into a PNG file.
#
# Invisible Location and Path elements are depicted in red, with the
# paths using dashed rather than solid lines.
#
# Path elements which are marked reversible use a single arrowhead to
# retain the indication of the main direction. If a path is
# <em>not</em> reversible, a double-arrowhead is used to emphasise
# that it's a one-way-only path. So:
#
# * One arrowhead (`→`)
# : The path is reversible, and commands for 'go back' will backtrace
# along it.
# * Double arrowhead (`↠`)
# : The path is IRreversible; once followed, 'go back' commands won't
# return along it.
#
# Paths which are sealable (`kind_of?(TAGF::Mixin::Sealable)`)
# <em>and</em> openable will be annotated with either a door "🚪" (if
# not lockable) or a padlock. If it's locked, a padlock & key "🔐"
# will be used; if it's unlocked, an open padlock "🔓" will be used.
#
require('bundler')
Bundler.setup
require('tagf')
require('ostruct')
require('pathname')
require('rgl/adjacency')
require('rgl/dijkstra')
require('rgl/dot')
require('byebug')
edgeweights = {}
graph_attr = OpenStruct.new(
vertex: OpenStruct.new(
#
# By default, vertices will look like this.
#
default: {
color: 'black',
shape: 'rectangle',
style: 'filled',
fillcolor: 'silver',
},
#
# If a location is invisible, modify its appearance as follows.
#
invisible: {
shape: 'ellipse',
fillcolor: 'red',
},
start: {
shape: 'parallelogram',
fillcolor: 'lime',
}),
edge: OpenStruct.new(
#
# Normal path depiction.
#
default: {
color: 'slategrey',
style: 'solid',
arrowhead: 'normal',
},
#
# If it's invisible, its looks are modified as follows.
#
invisible: {
color: 'red',
style: 'dashed',
},
#
# And if it can only be traversed one direction, mark it so.
#
irreversible: {
arrowhead: 'normalnormal',
arrowtail: 'tee',
})
)
#
# Read in the game definition.
#
fioer = TAGF::Filer.new
yamlfile = ARGV[0] || 'locx.yaml'
yamlpath = Pathname(yamlfile)
basename = yamlpath.to_s.sub(%r!#{yamlpath.extname}$!,
'')
game = fioer.load_game(yamlpath)
game.graphinfo.assemble
game.graphinfo.graph.write_to_graphic_file('png', basename)
exit(0)
__END__
#
# This is the code that was used to prototype the mechanisms used to
# build the graph within the context of the game code itself.
# Retained here for potential future expansion or experimentation.
#
game.filter(klass: TAGF::Location).each do |loc|
cxs = game.filter(klass: TAGF::Path, origin: loc)
loc.add_path(*cxs) unless (cxs.nil? || cxs.empty?)
end
#
# Prepare to graph this bugger.
#
locgraf = RGL::DirectedAdjacencyGraph.new
#
# We keep hashes of location elements and the graph vertices keyed by
# the location EIDs. This will be useful later for navigating through
# things.
#
lochash = {}
vertexhash = {}
pathhash = {}
edgehash = {}
#
# Go through the list of game elements and create a graph vertex for
# each Location object. Ignore all other types of elements.
#
game.inventory.each do |eid,locelt|
next unless (locelt.kind_of?(TAGF::Location))
attr = {
label: locelt.label,
tooltip: locelt.desc,
}.merge(graph_attr.vertex.default)
if (! locelt.visible?)
attr = attr.merge(graph_attr.vertex.invisible)
end
if (locelt == game.start)
attr = attr.merge(graph_attr.vertex.start)
end
locgraf.add_vertex(locelt)
locgraf.set_vertex_options(locelt, **attr)
lochash[eid] = locelt
vertexhash[eid] = locelt
end
#
# Go through the Location elements we've stored, and turn their paths
# into graph edges.
#
lochash.values.each do |locelt|
locelt.paths.each do |cxelt|
eid = cxelt.eid
label_prefix = ''
if (cxelt.sealable? &&
cxelt.openable?)
if (cxelt.lockable?)
label_prefix = cxelt.locked ? '🔐 ' : '🔓 '
else
label_prefix = '🚪 '
end
end
vias = cxelt.via.map { |k|
game.keyword(k).root
}.join(',')
tip = label_prefix + vias
if (cxelt.respond_to?(:tooltip) &&
cxelt.tooltip.kind_of?(String))
tip = format('%s[%s] %s',
label_prefix,
vias,
cxelt.tooltip)
end
attr = {
#
# Since a path may be accessed by more than one keyword, the
# label lists all keywords that apply.
#
label: tip, #label + vias,
id: eid,
tooltip: tip,
}.merge(graph_attr.edge.default)
if (! cxelt.visible?)
attr = attr.merge(graph_attr.edge.invisible)
end
if (! cxelt.reversible?)
attr = attr.merge(graph_attr.edge.irreversible)
end
origin = lochash[cxelt.origin.eid]
destination = lochash[cxelt.destination.eid]
locgraf.add_edge(origin,
destination)
edgeweights[[origin,destination]] = 0.0
edge = locgraf.edges.find { |e|
(e.source == origin) &&
(e.target == destination)
}
locgraf.set_edge_options(origin, destination, **attr)
edgehash[eid] = edge
pathhash[eid] = cxelt
end
end
#
# Doughnut batter is ready, time to pop them in the eazy bacon oven!
#
locgraf.write_to_graphic_file('png',
format('location-map-%s', basename))
# Local Variables:
# mode: ruby
# indent-tabs-mode: nil
# eval: (if (intern-soft "fci-mode") (fci-mode 1))
# eval: (auto-fill-mode 1)
# End: