src/newgrf_townname.cpp
author convert-repo
Mon, 07 Apr 2008 16:21:55 +0000
changeset 10076 dfd70e42c4ae
parent 8760 ce0891c412ce
permissions -rw-r--r--
update tags
7452
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     1
/* $Id$ */
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     2
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     3
/** @file newgrf_townname.cpp
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     4
 * Implementation of  Action 0F "universal holder" structure and functions.
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     5
 * This file implements a linked-lists of townname generators,
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     6
 * holding everything that the newgrf action 0F will send over to OpenTTD.
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     7
 */
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     8
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
     9
#include "stdafx.h"
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    10
#include "openttd.h"
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    11
#include "newgrf_townname.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8424
diff changeset
    12
#include "core/alloc_func.hpp"
8710
52015340050c (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8626
diff changeset
    13
#include "string_func.h"
7452
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    14
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8710
diff changeset
    15
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8710
diff changeset
    16
7452
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    17
static GRFTownName *_grf_townnames = NULL;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    18
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    19
GRFTownName *GetGRFTownName(uint32 grfid)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    20
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    21
	GRFTownName *t = _grf_townnames;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    22
	for (; t != NULL; t = t->next) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    23
		if (t->grfid == grfid) return t;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    24
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    25
	return NULL;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    26
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    27
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    28
GRFTownName *AddGRFTownName(uint32 grfid)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    29
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    30
	GRFTownName *t = GetGRFTownName(grfid);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    31
	if (t == NULL) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    32
		t = CallocT<GRFTownName>(1);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    33
		t->grfid = grfid;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    34
		t->next = _grf_townnames;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    35
		_grf_townnames = t;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    36
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    37
	return t;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    38
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    39
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    40
void DelGRFTownName(uint32 grfid)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    41
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    42
	GRFTownName *t = _grf_townnames;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    43
	GRFTownName *p = NULL;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    44
	for (;t != NULL; p = t, t = t->next) if (t->grfid == grfid) break;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    45
	if (t != NULL) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    46
		for (int i = 0; i < 128; i++) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    47
			for (int j = 0; j < t->nbparts[i]; j++) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    48
				for (int k = 0; k < t->partlist[i][j].partcount; k++) {
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7452
diff changeset
    49
					if (!HasBit(t->partlist[i][j].parts[k].prob, 7)) free(t->partlist[i][j].parts[k].data.text);
7452
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    50
				}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    51
				free(t->partlist[i][j].parts);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    52
			}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    53
			free(t->partlist[i]);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    54
		}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    55
		if (p != NULL) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    56
			p->next = t->next;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    57
		} else {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    58
			_grf_townnames = t->next;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    59
		}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    60
		free(t);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    61
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    62
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    63
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    64
static char *RandomPart(char *buf, GRFTownName *t, uint32 seed, byte id, const char *last)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    65
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    66
	assert(t != NULL);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    67
	for (int i = 0; i < t->nbparts[id]; i++) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    68
		byte count = t->partlist[id][i].bitcount;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    69
		uint16 maxprob = t->partlist[id][i].maxprob;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    70
		uint32 r = (GB(seed, t->partlist[id][i].bitstart, count) * maxprob) >> count;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    71
		for (int j = 0; j < t->partlist[id][i].partcount; j++) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    72
			byte prob = t->partlist[id][i].parts[j].prob;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    73
			maxprob -= GB(prob, 0, 7);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    74
			if (maxprob > r) continue;
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7452
diff changeset
    75
			if (HasBit(prob, 7)) {
7452
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    76
				buf = RandomPart(buf, t, seed, t->partlist[id][i].parts[j].data.id, last);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    77
			} else {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    78
				buf = strecat(buf, t->partlist[id][i].parts[j].data.text, last);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    79
			}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    80
			break;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    81
		}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    82
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    83
	return buf;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    84
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    85
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    86
char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, const char *last)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    87
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    88
	strecpy(buf, "", last);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    89
	for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    90
		if (t->grfid == grfid) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    91
			assert(gen < t->nb_gen);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    92
			buf = RandomPart(buf, t, seed, t->id[gen], last);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    93
			break;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    94
		}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    95
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    96
	return buf;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    97
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    98
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
    99
StringID *GetGRFTownNameList()
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   100
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   101
	int nb_names = 0, n = 0;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   102
	for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) nb_names += t->nb_gen;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   103
	StringID *list = MallocT<StringID>(nb_names + 1);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   104
	for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   105
		for (int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j];
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   106
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   107
	list[n] = INVALID_STRING_ID;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   108
	return list;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   109
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   110
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   111
void CleanUpGRFTownNames()
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   112
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   113
	while (_grf_townnames != NULL) DelGRFTownName(_grf_townnames->grfid);
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   114
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   115
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   116
uint32 GetGRFTownNameId(int gen)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   117
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   118
	for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   119
		if (gen < t->nb_gen) return t->grfid;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   120
		gen -= t->nb_gen;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   121
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   122
	/* Fallback to no NewGRF */
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   123
	return 0;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   124
}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   125
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   126
uint16 GetGRFTownNameType(int gen)
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   127
{
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   128
	for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   129
		if (gen < t->nb_gen) return gen;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   130
		gen -= t->nb_gen;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   131
	}
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   132
	/* Fallback to english original */
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   133
	return SPECSTR_TOWNNAME_ENGLISH;
f6fd23727af0 (svn r10211) -Feature: [NewGRF] Add support for action 0F
glx
parents:
diff changeset
   134
}