3 * $Id: elite.c,v 1.1 2003/02/24 01:13:12 mdw Exp $
7 * (c) 2003 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Revision history --------------------------------------------------*
30 * Revision 1.1 2003/02/24 01:13:12 mdw
35 /*----- Header files ------------------------------------------------------*/
44 /*----- Data structures ---------------------------------------------------*/
46 typedef struct world
{
50 typedef struct worldinfo
{
51 unsigned x
, y
, gov
, eco
, tech
, pop
, prod
, rad
;
54 /*----- The world type ----------------------------------------------------*/
56 static void world_fir(Tcl_Obj
*o
)
58 Tcl_Free(o
->internalRep
.otherValuePtr
);
61 static int xtoi(unsigned x
)
63 if (x
>= '0' && x
<= '9')
65 else if (x
>= 'a' && x
<= 'f')
66 return (x
- 'a' + 10);
67 else if (x
>= 'A' && x
<= 'F')
68 return (x
- 'A' + 10);
73 static Tcl_ObjType world_type
;
75 static int world_sfa(Tcl_Interp
*ti
, Tcl_Obj
*o
)
80 char *p
= Tcl_GetStringFromObj(o
, &l
);
83 for (i
= 0; i
< 12; i
+= 2) {
84 if (!isxdigit((unsigned char)p
[i
]) ||
85 !isxdigit((unsigned char)p
[i
+ 1]))
87 ww
.x
[i
>> 1] = (xtoi(p
[i
]) << 4) | (xtoi(p
[i
+ 1]));
89 w
= (world
*)Tcl_Alloc(sizeof(*w
));
91 o
->internalRep
.otherValuePtr
= w
;
92 o
->typePtr
= &world_type
;
97 Tcl_SetResult(ti
, "bad world seed string", TCL_STATIC
);
101 static void world_us(Tcl_Obj
*o
)
104 world
*w
= o
->internalRep
.otherValuePtr
;
111 for (i
= 0; i
< 6; i
++, p
+= 2)
112 sprintf(p
, "%02x", w
->x
[i
]);
115 static void world_dir(Tcl_Obj
*o
, Tcl_Obj
*oo
)
117 world
*w
= (world
*)Tcl_Alloc(sizeof(*w
));
118 memcpy(w
, o
->internalRep
.otherValuePtr
, sizeof(world
));
119 oo
->internalRep
.otherValuePtr
= w
;
120 oo
->typePtr
= &world_type
;
121 Tcl_InvalidateStringRep(oo
);
124 static /*const*/ Tcl_ObjType world_type
= {
125 "elite-world", world_fir
, world_dir
, world_us
, world_sfa
128 static world
*world_get(Tcl_Interp
*ti
, Tcl_Obj
*o
)
130 if (Tcl_ConvertToType(ti
, o
, &world_type
) != TCL_OK
)
132 return (o
->internalRep
.otherValuePtr
);
135 static Tcl_Obj
*world_new(const world
*w
)
138 Tcl_Obj
*o
= Tcl_NewObj();
139 ww
= (world
*)Tcl_Alloc(sizeof(*ww
));
141 o
->internalRep
.otherValuePtr
= ww
;
142 o
->typePtr
= &world_type
;
143 Tcl_InvalidateStringRep(o
);
147 /*----- Elite-specific hacking --------------------------------------------*
149 * Taken from `Elite: The New Kind' by Christian Pinder.
152 static void waggle(world
*w
, world
*ww
)
156 /* --- What goes on --- *
158 * 16-bit add of all three words, shift up, and insert the new value at the
165 h
+= w
->x
[3] + (l
>= 0x100);
166 l
&= 0xff; h
&= 0xff;
168 h
+= w
->x
[5] + (l
>= 0x100);
169 l
&= 0xff; h
&= 0xff;
170 ww
->x
[0] = w
->x
[2]; ww
->x
[1] = w
->x
[3];
171 ww
->x
[2] = w
->x
[4]; ww
->x
[3] = w
->x
[5];
172 ww
->x
[4] = l
; ww
->x
[5] = h
;
175 /*----- Tcl commands ------------------------------------------------------*/
177 static int err(Tcl_Interp
*ti
, /*const*/ char *p
)
179 Tcl_SetResult(ti
, p
, TCL_STATIC
);
183 /* --- elite-nextworld SEED --- */
185 static int cmd_nextworld(ClientData cd
, Tcl_Interp
*ti
,
186 int objc
, Tcl_Obj
*const *objv
)
190 return (err(ti
, "usage: elite-nextworld SEED"));
191 if ((w
= world_get(ti
, objv
[1])) == 0)
197 Tcl_SetObjResult(ti
, world_new(&ww
));
201 /* --- elite-nextgalaxy SEED --- */
203 static int cmd_nextgalaxy(ClientData cd
, Tcl_Interp
*ti
,
204 int objc
, Tcl_Obj
*const *objv
)
210 return (err(ti
, "usage: elite-nextgalaxy SEED"));
211 if ((w
= world_get(ti
, objv
[1])) == 0)
213 for (i
= 0; i
< 6; i
++)
214 ww
.x
[i
] = ((w
->x
[i
] << 1) | (w
->x
[i
] >> 7)) & 0xff;
215 Tcl_SetObjResult(ti
, world_new(&ww
));
219 /* --- elite-worldinfo ARR SEED --- */
221 static void getworldinfo(worldinfo
*wi
, world
*w
)
225 wi
->gov
= (w
->x
[2] >> 3) & 0x07;
226 wi
->eco
= w
->x
[1] & 0x07;
229 wi
->tech
= ((wi
->eco
^ 7) + (w
->x
[3] & 0x03) +
230 (wi
->gov
>> 1) + (wi
->gov
& 0x01) + 1);
231 wi
->pop
= wi
->tech
* 4 + wi
->gov
+ wi
->eco
- 3;
232 wi
->prod
= ((wi
->eco
^ 7) + 3) * (wi
->gov
+ 4) * wi
->pop
* 8;
233 wi
->rad
= (((w
->x
[5] & 0x0f) + 11) << 8) + w
->x
[3];
236 static const char digrams
[] =
237 "abouseitiletstonlonuthnoallexegezacebisouses"
238 "armaindirea?eratenberalavetiedorquanteisrion";
240 static const char *const desc
[][5] = {
241 /* 0 */ { "fabled", "notable", "well known", "famous", "noted" },
242 /* 1 */ { "very ", "mildly ", "most ", "reasonably ", "" },
243 /* 2 */ { "ancient", "<20>", "great", "vast", "pink" },
244 /* 3 */ { "<29> <28> plantations", "mountains", "<27>",
245 "<19> forests", "oceans" },
246 /* 4 */ { "shyness", "silliness", "mating traditions",
247 "loathing of <5>", "love for <5>" },
248 /* 5 */ { "food blenders", "tourists", "poetry", "discos", "<13>" },
249 /* 6 */ { "talking tree", "crab", "bat", "lobst", "%R" },
250 /* 7 */ { "beset", "plagued", "ravaged", "cursed", "scourged" },
251 /* 8 */ { "<21> civil war", "<26> <23> <24>s",
252 "a <26> disease", "<21> earthquakes", "<21> solar activity" },
253 /* 9 */ { "its <2> <3>", "the %I <23> <24>",
254 "its inhabitants' <25> <4>", "<32>", "its <12> <13>" },
255 /* 10 */ { "juice", "brandy", "water", "brew", "gargle blasters" },
256 /* 11 */ { "%R", "%I <24>", "%I %R", "%I <26>", "<26> %R" },
257 /* 12 */ { "fabulous", "exotic", "hoopy", "unusual", "exciting" },
258 /* 13 */ { "cuisine", "night life", "casinos", "sit coms", " <32>" },
259 /* 14 */ { "%H", "The planet %H", "The world %H",
260 "This planet", "This world" },
261 /* 15 */ { "n unremarkable", " boring", " dull", " tedious", " revolting" },
262 /* 16 */ { "planet", "world", "place", "little planet", "dump" },
263 /* 17 */ { "wasp", "moth", "grub", "ant", "%R" },
264 /* 18 */ { "poet", "arts graduate", "yak", "snail", "slug" },
265 /* 19 */ { "tropical", "dense", "rain", "impenetrable", "exuberant" },
266 /* 20 */ { "funny", "weird", "unusual", "strange", "peculiar" },
267 /* 21 */ { "frequent", "occasional", "unpredictable", "dreadful", "deadly" },
268 /* 22 */ { "<1><0> for <9>", "<1><0> for <9> and <9>",
269 "<7> by <8>", "<1><0> for <9> but <7> by <8>","a<15> <16>" },
270 /* 23 */ { "<26>", "mountain", "edible", "tree", "spotted" },
271 /* 24 */ { "<30>", "<31>", "<6>oid", "<18>", "<17>" },
272 /* 25 */ { "ancient", "exceptional", "eccentric", "ingrained", "<20>" },
273 /* 26 */ { "killer", "deadly", "evil", "lethal", "vicious" },
274 /* 27 */ { "parking meters", "dust clouds", "ice bergs",
275 "rock formations", "volcanoes" },
276 /* 28 */ { "plant", "tulip", "banana", "corn", "%Rweed" },
277 /* 29 */ { "%R", "%I %R", "%I <26>", "inhabitant", "%I %R" },
278 /* 30 */ { "shrew", "beast", "bison", "snake", "wolf" },
279 /* 31 */ { "leopard", "cat", "monkey", "goat", "fish" },
280 /* 32 */ { "<11> <10>", "%I <30> <33>", "its <12> <31> <33>",
281 "<34> <35>", "<11> <10>" },
282 /* 33 */ { "meat", "cutlet", "steak", "burgers", "soup" },
283 /* 34 */ { "ice", "mud", "Zero-G", "vacuum", "%I ultra" },
284 /* 35 */ { "hockey", "cricket", "karate", "polo", "tennis" }
287 static int mangle(world
*w
)
291 x
= (w
->x
[2] << 1) & 0xff;
299 a
= (a
+ x
+ w
->x
[5]) & 0xff;
305 static void goatsoup(Tcl_Obj
*d
, const char *pn
, world
*w
, const char *p
)
308 size_t sz
= strcspn(p
, "<%");
313 Tcl_AppendToObj(d
, (char *)p
, sz
);
320 i
= strtoul(p
+ 1, (char **)&p
, 10);
323 goatsoup(d
, pn
, w
, desc
[i
][(j
>= 0x33) + (j
>= 0x66) +
324 (j
>= 0x99) + (j
>= 0xcc)]);
330 Tcl_AppendToObj(d
, (char *)pn
, -1);
334 Tcl_AppendToObj(d
, (char *)pn
,
335 (pn
[sz
] == 'i' || pn
[sz
] == 'e') ? sz
: sz
+ 1);
336 Tcl_AppendToObj(d
, "ian", 3);
339 n
= (mangle(w
) & 0x03) + 1;
342 unsigned i
= mangle(w
) & 0x3e;
344 if (digrams
[i
] != '?')
347 *buf
= toupper(*buf
);
348 Tcl_AppendToObj(d
, buf
, q
- buf
);
360 static int cmd_worldinfo(ClientData cd
, Tcl_Interp
*ti
,
361 int objc
, Tcl_Obj
*const *objv
)
372 /* --- Check arguments --- */
375 return (err(ti
, "usage: elite-worldinfo ARR SEED"));
376 if ((w
= world_get(ti
, objv
[2])) == 0)
378 arr
= Tcl_GetString(objv
[1]);
380 /* --- Get the basic information --- */
382 getworldinfo(&wi
, w
);
383 if (!Tcl_SetVar2Ex(ti
, arr
, "x", Tcl_NewIntObj(wi
.x
* 4),
384 TCL_LEAVE_ERR_MSG
) ||
385 !Tcl_SetVar2Ex(ti
, arr
, "y", Tcl_NewIntObj(wi
.y
* 2),
386 TCL_LEAVE_ERR_MSG
) ||
387 !Tcl_SetVar2Ex(ti
, arr
, "government", Tcl_NewIntObj(wi
.gov
),
388 TCL_LEAVE_ERR_MSG
) ||
389 !Tcl_SetVar2Ex(ti
, arr
, "economy", Tcl_NewIntObj(wi
.eco
),
390 TCL_LEAVE_ERR_MSG
) ||
391 !Tcl_SetVar2Ex(ti
, arr
, "techlevel", Tcl_NewIntObj(wi
.tech
),
392 TCL_LEAVE_ERR_MSG
) ||
393 !Tcl_SetVar2Ex(ti
, arr
, "population", Tcl_NewIntObj(wi
.pop
),
394 TCL_LEAVE_ERR_MSG
) ||
395 !Tcl_SetVar2Ex(ti
, arr
, "productivity", Tcl_NewIntObj(wi
.prod
),
396 TCL_LEAVE_ERR_MSG
) ||
397 !Tcl_SetVar2Ex(ti
, arr
, "radius", Tcl_NewIntObj(wi
.rad
),
398 TCL_LEAVE_ERR_MSG
) ||
399 !Tcl_SetVar2Ex(ti
, arr
, "seed", objv
[2],
403 /* --- Work out the inhabitants --- */
405 if (!(w
->x
[4] & 0x80)) {
406 if (!Tcl_SetVar2(ti
, arr
, "inhabitants", "humans", TCL_LEAVE_ERR_MSG
))
409 static const char *const id_a
[] = { "large", "fierce", "small" };
410 static const char *const id_b
[] = { "green", "red", "yellow", "blue",
411 "black", "harmless" };
412 static const char *const id_c
[] = { "slimy", "bug-eyed", "horned",
413 "bony", "fat", "furry" };
414 static const char *const id_d
[] = { "rodents", "frogs", "lizards",
415 "lobsters", "birds", "humanoids",
416 "felines", "insects" };
418 o
= Tcl_NewListObj(0, 0);
419 j
= (w
->x
[5] >> 2) & 0x07;
421 Tcl_ListObjAppendElement(ti
, o
, Tcl_NewStringObj(id_a
[j
], -1));
422 j
= (w
->x
[5] >> 5) & 0x07;
424 Tcl_ListObjAppendElement(ti
, o
, Tcl_NewStringObj(id_b
[j
], -1));
425 j
= (w
->x
[1] ^ w
->x
[3]) & 0x07;
427 Tcl_ListObjAppendElement(ti
, o
, Tcl_NewStringObj(id_c
[j
], -1));
429 Tcl_ListObjAppendElement(ti
, o
, Tcl_NewStringObj(id_d
[j
& 0x07], -1));
430 if (!Tcl_SetVar2Ex(ti
, arr
, "inhabitants", o
, TCL_LEAVE_ERR_MSG
))
434 /* --- Work out the planet name --- */
436 n
= (w
->x
[0] & 0x40) ?
4 : 3;
444 if (digrams
[j
] != '?')
450 *buf
= toupper(*buf
);
451 if (!Tcl_SetVar2Ex(ti
, arr
, "name", Tcl_NewStringObj(buf
, -1),
455 /* --- Finally work out the goat-soup description --- */
458 o
= Tcl_NewStringObj(0, 0);
459 goatsoup(o
, buf
, &ww
, "<14> is <22>.");
460 if (!Tcl_SetVar2Ex(ti
, arr
, "description", o
, TCL_LEAVE_ERR_MSG
))
465 /* --- elite-market ARR SEED [FLUC] --- */
467 static const struct item
{
468 /*const*/ char *name
;
474 { "food", 19, -2, 6, 0x01 },
475 { "textiles", 20, -1, 10, 0x03 },
476 { "radioactives", 65, -3, 2, 0x07 },
477 { "slaves", 40, -5, 226, 0x1f },
478 { "liquor-wines", 83, -5, 251, 0x0f },
479 { "luxuries", 196, 8, 54, 0x03 },
480 { "narcotics", 235, 29, 8, 0x78 },
481 { "computers", 154, 14, 56, 0x03 },
482 { "machinery", 117, 6, 40, 0x07 },
483 { "alloys", 78, 1, 17, 0x1f },
484 { "firearms", 124, 13, 29, 0x07 },
485 { "furs", 176, -9, 220, 0x3f },
486 { "minerals", 32, -1, 53, 0x03 },
487 { "gold", 97, -1, 66, 0x07 },
488 { "platinum", 171, -2, 55, 0x1f },
489 { "gem-stones", 45, -1, 250, 0x0f },
490 { "alien-items", 53, 15, 192, 0x07 },
494 static int cmd_market(ClientData cd
, Tcl_Interp
*ti
,
495 int objc
, Tcl_Obj
*const *objv
)
500 const struct item
*i
;
503 if (objc
< 3 || objc
> 5)
504 return (err(ti
, "usage: elite-market ARR SEED [FLUC]"));
505 if ((w
= world_get(ti
, objv
[2])) == 0)
507 arr
= Tcl_GetString(objv
[1]);
508 if (objc
>= 4 && Tcl_GetIntFromObj(ti
, objv
[3], &fluc
) != TCL_OK
)
510 getworldinfo(&wi
, w
);
512 for (i
= items
; i
->name
; i
++) {
515 pr
= (i
->base
+ (fluc
& i
->mask
) + (wi
.eco
* i
->var
)) & 0xff;
516 qt
= (i
->qty
+ (fluc
& i
->mask
) - (wi
.eco
* i
->var
)) & 0xff;
519 oo
[0] = Tcl_NewIntObj(pr
<< 2);
520 oo
[1] = Tcl_NewIntObj(qt
& 0x3f);
521 if (!Tcl_SetVar2Ex(ti
, arr
, i
->name
, Tcl_NewListObj(2, oo
),
528 /*----- Commander file decomposition --------------------------------------*/
530 static unsigned cksum(const unsigned char *p
, size_t sz
)
532 unsigned a
= 0x49, c
= 0;
545 /* --- The big translation table --- */
548 /*const*/ char *name
;
550 int (*get
)(Tcl_Interp
*, /*const*/ char *,
551 const unsigned char *, const struct cmddata
*);
552 int (*put
)(Tcl_Interp
*, /*const*/ char *,
553 unsigned char *, const struct cmddata
*);
557 static int get_byte(Tcl_Interp
*ti
, /*const*/ char *arr
,
558 const unsigned char *p
, const struct cmddata
*cd
)
560 return (!Tcl_SetVar2Ex(ti
, arr
, cd
->name
,
561 Tcl_NewIntObj(*p
- cd
->x
), TCL_LEAVE_ERR_MSG
));
564 static int get_seed(Tcl_Interp
*ti
, /*const*/ char *arr
,
565 const unsigned char *p
, const struct cmddata
*cd
)
570 return (!Tcl_SetVar2Ex(ti
, arr
, cd
->name
,
571 world_new(&w
), TCL_LEAVE_ERR_MSG
));
574 static int get_word(Tcl_Interp
*ti
, /*const*/ char *arr
,
575 const unsigned char *p
, const struct cmddata
*cd
)
577 return (!Tcl_SetVar2Ex(ti
, arr
, cd
->name
,
578 Tcl_NewLongObj((p
[0] & 0xff) << 24 |
579 (p
[1] & 0xff) << 16 |
585 static int get_hword(Tcl_Interp
*ti
, /*const*/ char *arr
,
586 const unsigned char *p
, const struct cmddata
*cd
)
588 return (!Tcl_SetVar2Ex(ti
, arr
, cd
->name
,
589 Tcl_NewLongObj((p
[0] & 0xff) << 0 |
594 static int get_bool(Tcl_Interp
*ti
, /*const*/ char *arr
,
595 const unsigned char *p
, const struct cmddata
*cd
)
597 return (!Tcl_SetVar2Ex(ti
, arr
, cd
->name
,
598 Tcl_NewBooleanObj(*p
), TCL_LEAVE_ERR_MSG
));
601 static int get_items(Tcl_Interp
*ti
, /*const*/ char *arr
,
602 const unsigned char *p
, const struct cmddata
*cd
)
605 const struct item
*i
;
607 for (i
= items
; i
->name
; i
++) {
608 sprintf(buf
, "%s-%s", cd
->name
, i
->name
);
609 if (!Tcl_SetVar2Ex(ti
, arr
, buf
,
610 Tcl_NewIntObj(*p
++), TCL_LEAVE_ERR_MSG
))
616 static int put_byte(Tcl_Interp
*ti
, /*const*/ char *arr
,
617 unsigned char *p
, const struct cmddata
*cd
)
622 if ((o
= Tcl_GetVar2Ex(ti
, arr
, cd
->name
, TCL_LEAVE_ERR_MSG
)) == 0 ||
623 Tcl_GetIntFromObj(ti
, o
, &i
) != TCL_OK
)
629 static int put_word(Tcl_Interp
*ti
, /*const*/ char *arr
,
630 unsigned char *p
, const struct cmddata
*cd
)
635 if ((o
= Tcl_GetVar2Ex(ti
, arr
, cd
->name
, TCL_LEAVE_ERR_MSG
)) == 0 ||
636 Tcl_GetLongFromObj(ti
, o
, &l
) != TCL_OK
)
638 p
[0] = (l
>> 24) & 0xff;
639 p
[1] = (l
>> 16) & 0xff;
640 p
[2] = (l
>> 8) & 0xff;
641 p
[3] = (l
>> 0) & 0xff;
645 static int put_hword(Tcl_Interp
*ti
, /*const*/ char *arr
,
646 unsigned char *p
, const struct cmddata
*cd
)
651 if ((o
= Tcl_GetVar2Ex(ti
, arr
, cd
->name
, TCL_LEAVE_ERR_MSG
)) == 0 ||
652 Tcl_GetLongFromObj(ti
, o
, &l
) != TCL_OK
)
654 p
[0] = (l
>> 0) & 0xff;
655 p
[1] = (l
>> 8) & 0xff;
659 static int put_const(Tcl_Interp
*ti
, /*const*/ char *arr
,
660 unsigned char *p
, const struct cmddata
*cd
)
666 static int put_seed(Tcl_Interp
*ti
, /*const*/ char *arr
,
667 unsigned char *p
, const struct cmddata
*cd
)
672 if ((o
= Tcl_GetVar2Ex(ti
, arr
, cd
->name
, TCL_LEAVE_ERR_MSG
)) == 0 ||
673 (w
= world_get(ti
, o
)) == 0)
679 static int put_bool(Tcl_Interp
*ti
, /*const*/ char *arr
,
680 unsigned char *p
, const struct cmddata
*cd
)
685 if ((o
= Tcl_GetVar2Ex(ti
, arr
, cd
->name
, TCL_LEAVE_ERR_MSG
)) == 0 ||
686 Tcl_GetBooleanFromObj(ti
, o
, &b
) != TCL_OK
)
692 static int put_items(Tcl_Interp
*ti
, /*const*/ char *arr
,
693 unsigned char *p
, const struct cmddata
*cd
)
698 const struct item
*i
;
700 for (i
= items
; i
->name
; i
++) {
701 sprintf(buf
, "%s-%s", cd
->name
, i
->name
);
702 if ((o
= Tcl_GetVar2Ex(ti
, arr
, buf
, TCL_LEAVE_ERR_MSG
)) == 0 ||
703 Tcl_GetIntFromObj(ti
, o
, &ii
) != TCL_OK
)
710 static struct cmddata cmdtab
[] = {
711 { "mission", 0, get_byte
, put_byte
, 0 },
712 { "world-x", 1, get_byte
, put_byte
, 0 },
713 { "world-y", 2, get_byte
, put_byte
, 0 },
714 { "gal-seed", 3, get_seed
, put_seed
, 0 },
715 { "credits", 9, get_word
, put_word
, 0 },
716 { "fuel", 13, get_byte
, put_byte
, 0 },
717 { "", 14, 0, put_const
, 4 },
718 { "gal-number", 15, get_byte
, put_byte
, -1 },
719 { "front-laser", 16, get_byte
, put_byte
, 0 },
720 { "rear-laser", 17, get_byte
, put_byte
, 0 },
721 { "left-laser", 18, get_byte
, put_byte
, 0 },
722 { "right-laser", 19, get_byte
, put_byte
, 0 },
723 { "cargo", 22, get_byte
, put_byte
, 2 },
724 { "hold", 23, get_items
, put_items
, 0 },
725 { "ecm", 40, get_bool
, put_bool
, 255 },
726 { "fuel-scoop", 41, get_bool
, put_bool
, 255 },
727 { "energy-bomb", 42, get_bool
, put_bool
, 127 },
728 { "energy-unit", 43, get_byte
, put_byte
, 0 },
729 { "docking-computer", 44, get_bool
, put_bool
, 255 },
730 { "gal-hyperdrive", 45, get_bool
, put_bool
, 255 },
731 { "escape-pod", 46, get_bool
, put_bool
, 255 },
732 { "missiles", 51, get_byte
, put_byte
, 0 },
733 { "legal-status", 52, get_byte
, put_byte
, 0 },
734 { "station", 53, get_items
, put_items
, 0 },
735 { "market-fluc", 70, get_byte
, put_byte
, 0 },
736 { "score", 71, get_hword
, put_hword
, 0 },
737 { "", 74, 0, put_const
, 32 },
741 /* --- elite-unpackcmdr [-force] ARR DATA --- */
743 static int cmd_unpackcmdr(ClientData cd
, Tcl_Interp
*ti
,
744 int objc
, Tcl_Obj
*const *objv
)
747 unsigned char *p
, *q
;
751 const struct cmddata
*c
;
755 /* --- Read the arguments --- */
759 char *opt
= Tcl_GetString(*objv
);
760 if (strcmp(opt
, "-force") == 0)
762 else if (strcmp(opt
, "--") == 0) {
772 return (err(ti
, "usage: elite-unpackcmdr [-force] ARR DATA"));
773 arr
= Tcl_GetString(objv
[0]);
774 p
= Tcl_GetByteArrayFromObj(objv
[1], &sz
);
776 /* --- Check the data for correctness --- */
779 return (err(ti
, "bad commander data (bad length)"));
781 if (!(f
& f_force
)) {
782 if (sz
< 76 || p
[74] != (ck
^ 0xa9) || p
[75] != ck
)
783 return (err(ti
, "bad commander data (bad checksum)"));
784 for (q
= p
+ 77; q
< p
+ sz
; q
++)
786 return (err(ti
, "bad commander data (bad data at end)"));
789 /* --- Deconstruct the data --- */
791 for (c
= cmdtab
; c
->name
; c
++) {
792 if (c
->get
&& c
->get(ti
, arr
, p
+ c
->off
, c
))
798 /* --- elite-packcmdr ARR --- */
800 static int cmd_packcmdr(ClientData cd
, Tcl_Interp
*ti
,
801 int objc
, Tcl_Obj
*const *objv
)
804 unsigned char p
[256];
806 const struct cmddata
*c
;
809 return (err(ti
, "usage: elite-packcmdr ARR"));
810 arr
= Tcl_GetString(objv
[1]);
812 memset(p
, 0, sizeof(p
));
813 for (c
= cmdtab
; c
->name
; c
++) {
814 if (c
->put
&& c
->put(ti
, arr
, p
+ c
->off
, c
))
821 Tcl_SetObjResult(ti
, Tcl_NewByteArrayObj(p
, sizeof(p
)));
825 /*----- Initialization ----------------------------------------------------*/
827 int Elite_SafeInit(Tcl_Interp
*ti
)
829 static const struct cmd
{
830 /*const*/ char *name
;
831 Tcl_ObjCmdProc
*proc
;
833 { "elite-nextworld", cmd_nextworld
},
834 { "elite-nextgalaxy", cmd_nextgalaxy
},
835 { "elite-worldinfo", cmd_worldinfo
},
836 { "elite-market", cmd_market
},
837 { "elite-unpackcmdr", cmd_unpackcmdr
},
838 { "elite-packcmdr", cmd_packcmdr
},
843 for (c
= cmds
; c
->name
; c
++)
844 Tcl_CreateObjCommand(ti
, c
->name
, c
->proc
, 0, 0);
845 Tcl_RegisterObjType(&world_type
);
846 if (Tcl_PkgProvide(ti
, "elite-bits", "1.0.0"))
851 int Elite_Init(Tcl_Interp
*ti
)
853 return (Elite_SafeInit(ti
));
856 /*----- That's all, folks -------------------------------------------------*/