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