chiark / gitweb /
d212dca7aeb6ffa54b4760045b49eb0769ff793f
[otter.git] / docs / shapelibs.rst
1 Shape libaries
2 ==============
3
4 Most pieces in a game in Otter will be from a shape library.
5
6 Introduction; general structure
7 -------------------------------
8
9 A shape library is a set of named pieces.  Mostly, it defines their
10 appearance.
11
12 Shape library pieces can have more or more sides (**faces**), and they
13 can also have an **occulted view** which is used when the identity of
14 the piece is to be hidden (possibly from only some of the players).
15
16 For example:
17
18 Playing cards have two faces: one is the front, which identifies the
19 card, and the other is the back, which is a generic card back.  Cards
20 also have an occulted view: again, the back.
21
22 Chess pieces usually have only one face.  The Knight has two faces,
23 because it is not symmetrical: the first face looks to the right, and
24 the other face to the left.  Chess pieces can be occulted, but when
25 they are occulted they reveal their shape but not their colour.  The
26 occulted view of a chess piece is a grey version of the piece.
27
28 A library has a **library name**.  This is a string.  For a library in
29 a bundle, it's the ``LIB`` part of the filename
30 ``libraries/LIB.toml``.
31
32 Each piece in a library has an **item name**.  Item names are unique
33 within a library.  The item name is used within Otter to refer to the
34 piece (for example, with ``otter library-add``).
35
36 Pieces in shape libraries cannot have "behaviours": they can't do
37 anything "special" like react to being moved or clicked on.  Pieces
38 with special functionality do exist, but they are built into Otter.
39 (See :ref:`gamespec:Piece specs` for all the kinds of piece.)
40
41 A library consists of a **catalogue**, and a set of **image files**
42 which contain the actual appearances.  When a library is in a bundle
43 the catalogue is ``libraries/LIB.toml`` and the image files are in a
44 directory ``libraries/LIB.toml``.  The layout of Otter's builtin
45 libraries is similar.
46
47 Image files
48 -----------
49
50 Otter supports uploading of SVG and PNG files.  They should be in the
51 directory ``library/LIB``, named after the item.  (See `Files entry`_
52 for details of how to specify the file names.)
53
54 Image files should be small --- ideally a handful of kilobytes, or
55 less.  SVG images should be of modest complexity.
56
57 Large image files don't just upload slowly; they also make the game
58 perform poorly when playing.  This is because the image files are
59 frequently (re)transmitted by the server to each client.
60
61 It is not normally necessary to specify images in great detail: they
62 take only a small space on the players' screens, so the resolution
63 does not need to be awesome.  As an example, the playing cards in the
64 builtin ``cards-oxymoron`` library are image files of just 73x97
65 pixels.
66
67 Catalogue
68 ---------
69
70 The catalogue defines what pieces the library contains.  For each
71 piece it defines each face looks like, how big it is on the screen,
72 whether and how the piece can be occulted, and what source image files
73 are to be used to display it.
74
75 The catalogue is a TOML file.
76
77 The file should have a toplevel key ``format``, specifying which
78 version of the Otter data formats the file was written to.
79 This document describes ``format=1``.
80 See :ref:`Bundle compatibility <bundle-compatibility>`.
81
82 The catalogue's main contents is a dictionary
83 ``groups``, mapping each group name to a sub-dict of parameters:
84
85 Each catalogue is organised into named **groups**.  Each group defines
86 some pieces.  It specifies various **parameters**, and also gives a
87 list of indvidual image files which should be processed according to
88 those parameters.
89
90 For example::
91
92   [group.dried]
93   outline = "Circle"
94   size = [14]
95   orig_size = [64, 48]
96   centre = [32,24]
97
98   files = """
99   dried-lemon   -       a dried lemon
100   """
101
102 This defines a group ``dried``, with parameters such as ``size`` and
103 ``outline``.  The ``files`` entry defines the list of pieces.
104
105 The group names are not visible when using the library, but they can
106 be used within the library using the ``inherit`` feature.
107
108 The builtin catalogues also have a toplevel dict ``scraper``, which
109 controls how the builtin shape data is processed during the build, and
110 how it is to be updated.  (Downloads are never automatically run
111 during the build.  If you updated the catalougue in a way that means
112 files should be re-downloaded, you should re-run ``./media-scraper
113 library/LIB.toml``.)
114
115 Files entry
116 -----------
117
118 Each group has an entry ``files``.  This is a string, which is treated
119 as a series of lines (so it is best to use the TOML multi-line string
120 syntax).
121
122 Each line of which has (normally) three fields (the leading ones
123 terminated by whitespace).  ``#`` comment lines are supported and
124 blank lines are ignored.
125
126 Each non-empty non-comment line in ``files`` specifies a single piece,
127 like this::
128
129    ITEM-SPEC SRC DESCRIPTION...
130
131 The **item name** of the piece will be ``ITEM-SPEC`` sandwiched
132 between the ``item_prefix`` and ``item_suffix`` parameters (see
133 below).
134
135 The **image filename** is derived from ``SRC`` or the item name, as
136 follows: ``library/LIB/SRC.svg`` or ``.png``.  If ``SRC`` is ``-``
137 then the item name is used for ``SRC``.  (Builtin libraries: these
138 support SVG only; and the ``SRC`` is not used at runtime, or when
139 loading shapes --- rather, only when scraping or building.)
140
141 ``DESCRIPTION`` is the **description**, a string which will be used to
142 describe the piece (eg in in-game log messages).  In English, it
143 should contain an article.  Eg, ``the black queen``, ``a white pawn``.
144 It will be HTML-escaped, so it should be plain text, not HTML.
145
146 It is also possible to specify additional data for each piece by
147 adding fields to each line in ``files``.  This is done by adding a
148 line at the start starting with ``:`` listing the extra fields, and
149 then additng one additional whitespace separated value before the
150 description on each data line.  Values given for unknown field are
151 ignored.
152
153 Currently the extra fields supported are:
154
155  * ``sort``: Specifies the sort key.  See the ``sort`` group
156    parameter.
157
158 The values for these extra fields come just before the
159 ``DWSCRIPTION``, after the other whitespace-delimited fields, in the
160 same order as specified in the ``:`` heading line.
161
162 Item names
163 ``````````
164
165 Item names do not need to be unique within a game, but there are
166 places where a piece is found *just* by the item name, so pieces
167 should have the same item name (only) if they are in some sense
168 equivalent.
169
170 Item names are conventionally structured using a hierarchical name
171 with ``-`` between the components.
172
173 The item name is a string but may contain only ASCII alphanumerics,
174 plain ASCII spaces, and the punctuation characters ``-._``.  Do not
175 put ``/`` or ``_`` in item names.  ``/`` is forbidden and ``_`` can
176 interfere with the template substitution system.
177
178 See the existing examples to see what item names usually look like.
179
180 Parameters
181 ----------
182
183 These are the entries which can appear in each ``group.GROUP``
184 dictionary:
185
186 Mandatory parameters
187 ````````````````````
188
189  * ``files``: The list of pieces to define, one per line.  See `Files
190    entry`_.  [multi-line string]
191
192  * ``outline`` [dictionary]: Defines the outline of the piece,
193    which is used for drawing "haloes" around the piece,
194    indicating selection and movement.
195
196  * ``outline`` [string]: Abbreviated way of specifying
197    ``outline = { shape: ... }``.
198    The shape mkust be specified, either via ``outline = "Shape"``
199    or by setting ``outline.shape``.
200
201  * ``outline.shape`` [``"Circle"`` or ``"Rect"`` ]:
202    If ``outline`` is ``Circle``, and the the nominal size
203    (calculated from all the other parameters)
204    has different width and height, the larger of the width and height is used
205    (ellipses are not supported).
206
207  * ``outline.size``: nominal size of the piece, used for calculating
208    the size of "haloes".  Note that this is not the szie of the
209    "haloes"; it is the (nominal) size of the piece image itself:
210    the "haloes" indicating selection etc. will be somewhat larger.
211    [1- or 2-element array of numbers: width and height;
212    default: calculated from the piece's ``size`` and ``scale`` ]
213
214  * ``outline.scale``: Adjusts the nominal size of the outline,
215    multiplying it by this factor.  Not meaningful together with
216    ``outline.size``.  [number; default ``1.0``]
217
218 Important parameters
219 ````````````````````
220
221  * ``inherit``: Causes this group to inherit
222    every parameter (except ``files``) from the group named by
223    ``inherit`` (recursively, if applicable).  [string: group name]
224
225    When inheritance is happening, there is of course a difference
226    between leaving a value unspecified, and specifying it to have
227    the usual default value: the latter would override any inherited
228    setting.
229
230  * ``item_prefix``, ``item_suffix``.  Prepended and appended to
231    ``ITEM-SPEC`` in ``files`` to produce the item name.  [strings]
232
233 Geometry parameters
234 ```````````````````
235
236  * ``centre``: The centre of the image, measured from the top left in
237    the image's own internal units.  If not supplied, calculated from
238    the size.  [2-element array]
239
240  * ``size``:
241    The size at which the piece will show up in the game, in nominal
242    game coordinate units.
243    The supplied SVG will be reiszed from its internall specified
244    witdth and height, to that specified.
245    If the aspect ratio does not match the supplied image,
246    ``scale`` controls the behaviour.
247    (Specifying only one value for ``size`` means a square of that size.)
248    [1- or 2-element array of numbers: width and height]
249
250    For reference: the builtin library's chess
251    pieces are 9.5 units; the builtin playing cards are 9.65, 17.125.
252
253  * ``scale`` [string]: Specifies what to do if the SVG's own size does
254    not have the same aspect ratio as a sspecified ``size``:
255
256     - ``"fit"``: the image is placed within the specified ``size``;
257     - ``"cover"``: the image is placed around the specified ``size``;
258     - ``"stretch"``: the image is distorted to become exactly ``size``.
259
260  * ``scale`` [number]: Specifies that the image SVG should be rescaled
261     by this amount.  This use of ``scale`` cannot be combined with ``size``.
262
263 Parameters for defining faces
264 `````````````````````````````
265
266  * ``flip``: Whether this piece can "flip".  If true, the piece will
267    have two faces, one of which is a mirror image of the other.  The
268    default face will be un-reflected version; the other face is the
269    same image, but flipped left-to-right.  [boolean]
270
271    It doesn't make sense to enable this for pieces with a symmetrical
272    appearance.  (It is a bad idea to have the game contain state which
273    is not visible to the players.)  Not compatible with ``back``.
274
275  * ``back``: [:ref:`Piece spec <gamespec:Piece Specs>`].  The back of
276    this piece looks like some other piece (typically, another library
277    item such as a card back).  If specified, the piece will have two
278    faces: the one implied by its ``files`` entry, and a 2nd face
279    looking like ``back``.  If you want to make the piece be
280    occultable, you must also specify ``occulted``.  ``back`` is not
281    compatible with ``flip``.
282
283 Other group parameters
284 ```````````````````````
285    
286  * ``sort``.  The sort key.  Used for item sorting in hands.  When the
287    user asks to sort their hand, all the items in it are sorted
288    according to (primarily) simply this sort key, interpreted
289    lexicographically.  [string]
290
291    The sort key should generally contain all of the information in the
292    item name; if the item name contains an element referring to style
293    or aesthetic, that should appear at the end of the sort key (if at
294    all).
295
296    If neither the group parameter, nor the ``files`` extra field
297    ``sort``, are specified, the item name is used as the sort key.
298
299    If both are specified, the group parameter is used as a template:
300    ``_s`` is replaced by the ``sort`` extra field from the ``files``
301    list; ``_c`` is replaced by the colour, if applicable.
302
303  * ``colors`` [dictionary].
304    If specified and non-empty, specifies that this group should be
305    instantiated multiple times, for different colours.
306
307    For each entry in the ``colours`` dict, a separate piece is
308    generated for each item in the ``files`` list.  The keys of the
309    ``colours`` are recolouring names, and the values are sub-tables.
310
311    Every effective item name (i.e., after the ``item_prefix`` and
312    ``item_suffix`` have been added) must contain the substring ``_c``
313    exactly once, and every item description must contain the substring
314    ``${colour}`` exactly once.  ``_c`` in the item name will be
315    replaced with the value of the recoluring's ``abbrev``, and
316    ``${colour}`` with the recolouring name (the key of the ``colours``
317    dict).
318
319    For libraries in bundles, a separate image file must be supplied
320    for each recolouring.  If ``SRC`` is not ``-``, it also must
321    contain ``_c`` which will be substitued with ``abbrev`` to find the
322    colour-specific image file.
323
324    For builtin libraries, the Otter build system will do the
325    recolouring automatically at build time; each recolouring should
326    have a ``map`` entry which is a sub-sub-dict mapping input colours
327    (strings in ``#rrggbb`` format) to output colours.
328
329  * ``desc`` [string: template]: : If specified, provides a template
330    for the description, to allow formulaic descriptions of pieces in
331    this group.  The string specified by ``desc`` must contain ``${desc}``
332    exactly once; that will be replaced with the description calculated
333    according to the other rules.  (``${desc}`` substitution happens
334    after ``${colour}`` substitution.)
335
336  * ``occulted`` [dict, contents depend on ``occulted.method``].  If
337    specified, these pieces be occulted.  For example, when a player
338    has them in their hand and the hand is active and owned by them,
339    only the occulted view (eg, the back of a playing card) will be
340    shown.  ``occulted`` is a dict whose other contents depend on its
341    ``method`` entry, which must be a string:
342
343   * ``"ByColour"``: Occult by displaying a particular recolouring of
344     this piece.  The sub-entry ``occulted.colour`` names a
345     recolouring - one of the keys of the ``colours`` group parameter.
346     When the piece is occulted it will show that colour, instead of
347     its actual colour.  In the description, ``${colour}`` will be elided
348     rather than substituted (along with up to one of any spaces either
349     side of it).
350
351   * ``"ByBack"``: Occult by displaying the back of this piece, as
352     specified by the ``back`` group parameter.  The ``occulted`` dict
353     must also contain a sub-entry ``ilk``, a string.  Pieces which
354     have the same ``ilk`` display identically when occulted, even if
355     the different piece definitions imply different backs.  (Whichever
356     pieces are first loaded define what the backs of a particular ilk
357     look like.)
358
359     For pieces that are like cards, the ilk should be different for
360     cards which have different backs in the game.  Generally, standard
361     playing cards should all specify ``card-back``.