File: mknames.c

package info (click to toggle)
xconq 7.2.2-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,296 kB
  • ctags: 9,199
  • sloc: ansic: 107,849; sh: 2,108; perl: 2,057; makefile: 1,177; sed: 161; csh: 50; awk: 49; lisp: 39
file content (436 lines) | stat: -rw-r--r-- 10,436 bytes parent folder | download | duplicates (2)
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
/* Name generation for Xconq.
   Copyright (C) 1991-1998 Stanley T. Shebs.

Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.  See the file COPYING.  */

/* Naming is a special class of init method, since it may run both during
   init and throughout a game.  Name generation has a very strong influence
   on the flavor of a game, so it has some extra flexibility, including
   a capability to use a simple context-free grammar to generate names. */

#include "conq.h"

Obj *choose_from_weighted_list PARAMS ((Obj *lis, int *totalweightp));

Obj *namerlist;

Obj *lastnamer;

static int totalsideweights;

void
init_namers()
{
    namerlist = lastnamer = lispnil;
    totalsideweights = 0;
}

Obj *
choose_from_weighted_list(lis, totalweightp)
Obj *lis;
int *totalweightp;
{
    int n, sofar;
    Obj *rest, *head, *rslt;

    if (*totalweightp <= 0) {
	for_all_list(lis, rest) {
	    head = car(rest);
	    *totalweightp +=
	      ((consp(head) && numberp(car(head))) ? c_number(car(head)) : 1);
	}
    }
    n = xrandom(*totalweightp);
    sofar = 0;
    rslt = lispnil;
    for_all_list(lis, rest) {
	head = car(rest);
	sofar += ((consp(head) && numberp(car(head))) ? c_number(car(head)) : 1);
	if (sofar > n) {
	    rslt = head;
	    break;
	}
    }
    /* Remove the weighting if present. */
    if (consp(rslt) && numberp(car(rslt))) {
	rslt = cdr(rslt);
    }
    return rslt;
}

/* Pick a side name that is not already being used. */
/* (should reindent) */
void
make_up_side_name(side)
Side *side;
{
    int uniq = FALSE, tries = 0, n, sofar;
    Obj *sidelib, *subobj, *subelts, *head, *filler;

    sidelib = g_side_lib();
    filler = lispnil;
    if (sidelib != lispnil) {
      while (tries++ < 100 * numsides) {
	subobj = choose_from_weighted_list(sidelib, &totalsideweights);
	uniq = TRUE;
	for_all_list(subobj, subelts) {
	    head = car(subelts);
	    if (stringp(cadr(head))) {
		if (name_in_use(side, c_string(cadr(head)))) {
		    uniq = FALSE;
		    break;
		}
	    }
	}
	if (uniq) {
	    filler = subobj;
	    break;
	}
      }
    } else {
	/* The fallback is to label the side 'A', 'B', etc.  Note that
	   we don't test for these "names" being already in use, but this
	   should be OK, because real games should rarely need these. */
	n = side_number(side) - 1;
	sprintf(spbuf, "%c", 'A' + n);
	sprintf(tmpbuf, "%cian", 'A' + n); /* should be in nlang.c? */
	filler = cons(cons(intern_symbol(keyword_name(K_NAME)),
			   cons(new_string(copy_string(spbuf)),
				lispnil)),
		      filler);
	filler = cons(cons(intern_symbol(keyword_name(K_NOUN)),
			   cons(new_string(copy_string(tmpbuf)),
				lispnil)),
		      filler);
    }
    /* Now fill the side from the chosen obj - no effect if it is nil. */
    fill_in_side(side, filler, FALSE);
}

/* This tests whether a given string is already being used by a side. */

int
name_in_use(side, str)
Side *side;
char *str;
{
    Side *side2;

    if (empty_string(str))
      return FALSE;
    for_all_sides(side2) {
	if (side2 != side) {
	    if ((side2->name && strcmp(str, side2->name) == 0)
		|| (side2->noun && strcmp(str, side2->noun) == 0)
		|| (side2->pluralnoun && strcmp(str, side2->pluralnoun) == 0)
		|| (side2->adjective && strcmp(str, side2->adjective) == 0)
		)
	      return TRUE;
	}
    }
    return FALSE;
}

Obj *
make_namer(sym, meth)
Obj *sym, *meth;
{
    Obj *namer = new_pointer(sym, (char *) meth), *tmpobj;

    /* Append the new namer to the end of the list of namers. */
    tmpobj = cons(namer, lispnil);
    if (lastnamer != lispnil) {
	set_cdr(lastnamer, tmpobj);
	lastnamer = tmpobj;
    } else {
	namerlist = lastnamer = tmpobj;
    }
    return namer;
}

/* Method to add names to units that want them and don't have them already. */

int
name_units_randomly(calls, runs)
int calls, runs;
{
    Unit *unit;

    /* There's never any reason not to run this method. */
    /* (should this announce progress?) */
    for_all_units(unit) {
	make_up_unit_name(unit);
	assign_unit_number(unit);
    }
    return TRUE;
}

/* Given a unit, return its naming method if it has one. */

char *
unit_namer(unit)
Unit *unit;
{
    Side *side;

    if (unit == NULL)
      return NULL;
    /* Look for and return a side-specific namer if found. */
    side = (unit->side ? unit->side : indepside);
    if (side->unitnamers != NULL && side->unitnamers[unit->type] != NULL) {
    	return side->unitnamers[unit->type];
    }
    return u_namer(unit->type);
}

/* Generate a name for a unit, using an appropriate method.

   It is possible (in fact encouraged) to add cool new unit name generation
   methods in here, especially when the grammar-based or thematic methods
   don't give the desired results. */

char *
propose_unit_name(unit)
Unit *unit;
{
    int u;
    char *method;

    if (unit == NULL)
      return NULL;
    u = unit->type;
    method = unit_namer(unit);
    if (empty_string(method)) {
	/* Nothing to work with. */
    } else if (boundp(intern_symbol(method))) {
	return run_namer(symbol_value(intern_symbol(method)));
    } else {
	/* Do builtin naming methods. */
	switch (keyword_code(method)) {
	  case K_JUNKY:
	    /* Kind of a bizarre thing, but flavorful sometimes. */
	    if (unit->side) {
		sprintf(spbuf, "%c%c-%s-%02ld",
			uppercase(unit->side->name[0]),
			uppercase(unit->side->name[1]),
			utype_name_n(u, 3), unit->number);
	    } else {
		sprintf(spbuf, "%s-%d", utype_name_n(u, 3), unit->id);
	    }
	    return copy_string(spbuf);
	  default:
	    init_warning("No naming method `%s', ignoring", method);
	    break;
	}
    }
    return NULL;
}

/* This names only units that do not already have names. */

void
make_up_unit_name(unit)
Unit *unit;
{
    if (unit == NULL || unit->name != NULL)
      return;
    /* (should check that proposed name is not in use by matching side and type?) */
    unit->name = propose_unit_name(unit);
}

/* Unit numbering only happens to designated types that are on a side. */

void
assign_unit_number(unit)
Unit *unit;
{
    if (u_assign_number(unit->type)
	&& unit->side != NULL) {
	/* If unnumbered, give it the next available number and increment. */
	if (unit->number == 0)
	  unit->number = (unit->side->counts)[unit->type]++;
    } else {
	/* Note that this will erase any already-assigned number,
	   if the type is one that is not supposed to be numbered. */
	unit->number = 0;
    }
}

/* Given a naming method, run it and get back a string. */

char *
run_namer(namer)
Obj *namer;
{
    int len, ix;
    Obj *prev;
    Obj *rslt;
    Obj *code = (Obj *) namer->v.ptr.data;
    Obj *type;

    if (!consp(code))
      return "?format?";
    type = car(code);
    if (!symbolp(type))
      return "?type?";
    switch (keyword_code(c_string(type))) {
      case K_JUNKY:
      case K_RANDOM:
        len = length(cdr(code));
	if (len > 0) {
	    ix = xrandom(len - 1) + 1;
	    prev = code;
	    while (--ix)
	      prev = cdr(prev);
	    rslt = cadr(prev);
	    /* Splice out our desired name. */
	    set_cdr(prev, cddr(prev));
	    return c_string(rslt);
	} else {
	    return "?no more names?";
	}
	break;
      case K_GRAMMAR:
	return name_from_grammar(code);
      default:
	return "?method?";
    }
}

static int maxdepth;

char *
name_from_grammar(grammar)
Obj *grammar;
{
    char rslt[500];  /* not really safe... */
    Obj *root = cadr(grammar);
    Obj *depth = caddr(grammar);
    Obj *rules = cdr(cddr(grammar));

    maxdepth = 5;
    if (numberp(depth))
      maxdepth = c_number(depth);
    rslt[0] = '\0';
    gen_name(root, rules, 0, rslt);
    /* This should be optional maybe. */
    rslt[0] = uppercase(rslt[0]);
    return copy_string(rslt);
}

/* Given a nonterminal and a set of rules, find and apply the right rule. */

void
gen_name(nonterm, rules, depth, rslt)
Obj *nonterm, *rules;
int depth;
char *rslt;
{
    Obj *lis;

    for (lis = rules; lis != lispnil; lis = cdr(lis)) {
	if (equal(nonterm, car(car(lis)))) {
	    gen_from_rule(cadr(car(lis)), rules, depth, rslt);
	    return;
	}
    }
    if (symbolp(nonterm)
	&& boundp(nonterm)
	&& pointerp(symbol_value(nonterm))) {
	strcat(rslt, run_namer(symbol_value(nonterm)));
    } else {
	/* Assume that the purported nonterm symbol is actually a terminal. */
	strcat(rslt, c_string(nonterm));
    }
}

/* Given a rule body, decide how to add to the output string.  This may
   recurse, so there is a limit check. */

void
gen_from_rule(rule, rules, depth, rslt)
Obj *rule, *rules;
int depth;
char *rslt;
{
    Obj *lis;
    int total, num, oldlen;
    
    if (depth >= maxdepth)
      return;
    switch (rule->type) {
      case NUMBER:
      case NIL:
      case UTYPE:
      case MTYPE:
      case TTYPE:
      case POINTER:
      case EOFOBJ:
	break;  /* ignore for now.. */
      case SYMBOL:
	gen_name(rule, rules, depth, rslt);
	break;
      case STRING:
	strcat(rslt, c_string(rule));
	break;
      case CONS:
        if (symbolp(car(rule))) {
	    switch (keyword_code(c_string(car(rule)))) {
	      case K_OR:
		/* weighted selection */
		total = 0;
		for (lis = cdr(rule); lis != lispnil; lis = cdr(lis)) {
		    if (numberp(car(lis))) {
			total += c_number(car(lis));
			lis = cdr(lis);
		    } else {
			total += 1;
		    }
		}
		/* We now know the range, make a random index into it. */
		num = xrandom(total);
		/* Go through again to figure out which choice the index
		   references. */
		total = 0;
		for (lis = cdr(rule); lis != lispnil; lis = cdr(lis)) {
		    if (numberp(car(lis))) {
			total += c_number(car(lis));
			lis = cdr(lis);
		    } else {
			total += 1;
		    }
		    if (total > num) {
			gen_from_rule(car(lis), rules, depth + 1, rslt);
			return;
		    }
		}
		break;
	      case K_ANY:
		/* uniform selection */
		strcat(rslt, c_string(elt(cdr(rule),
					  xrandom(length(cdr(rule))))));
		break;
	      case K_CAPITALIZE:
		oldlen = strlen(rslt);
		gen_name(cadr(rule), rules, depth + 1, rslt);
		if (islower(rslt[oldlen]))
		  rslt[oldlen] = uppercase(rslt[oldlen]);
		break;
	      default:
		/* Nested subsequence. */
		for (lis = rule; lis != lispnil; lis = cdr(lis)) {
		    gen_from_rule(car(lis), rules, depth + 1, rslt);
		}
	    }
	} else {
	    /* syntax error */
	}
	break;
      default:
	case_panic("lisp type in grammar", rule->type);
	break;
    }
}