summaryrefslogtreecommitdiff
path: root/src/data/mg/map.c
blob: 20feb8f1e646ac3f1b7d00343abeb986c2789297 (plain)
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
#include <stdio.h>
#include <string.h>
#include "debug.h"
#include "plugin.h"
#include "maptype.h"
#include "projection.h"
#include "mg.h"


struct map_priv * map_new_mg(struct map_methods *meth, char *dirname, char **charset, enum projection *pro);

static int map_id;

static char *file[]={
	[file_border_ply]="border.ply",
        [file_bridge_ply]="bridge.ply",
        [file_build_ply]="build.ply",
        [file_golf_ply]="golf.ply",
        [file_height_ply]="height.ply",
        [file_natpark_ply]="natpark.ply",
        [file_nature_ply]="nature.ply",
        [file_other_ply]="other.ply",
        [file_rail_ply]="rail.ply",
        [file_sea_ply]="sea.ply",
        [file_street_bti]="street.bti",
        [file_street_str]="street.str",
        [file_strname_stn]="strname.stn",
        [file_town_twn]="town.twn",
        [file_tunnel_ply]="tunnel.ply",
        [file_water_ply]="water.ply",
        [file_woodland_ply]="woodland.ply",
};


static int
file_next(struct map_rect_priv *mr)
{
	int debug=0;
	enum layer_type layer;

	for (;;) {
		mr->current_file++;
		if (mr->current_file >= file_end)
			return 0;
		mr->file=mr->m->file[mr->current_file];
		if (! mr->file)
			continue;
		switch (mr->current_file) {
		case file_strname_stn:
			continue;
		case file_town_twn:
			layer=layer_town;
			break;
		case file_street_str:
			layer=layer_street;
			break;
		default:
			layer=layer_poly;
		}
		if (mr->cur_sel && !mr->cur_sel->order[layer])
			continue;
		if (debug)
			printf("current file: '%s'\n", file[mr->current_file]);
		mr->cur_sel=mr->xsel;
		if (block_init(mr))
			return 1;
	}
}

static void
map_destroy_mg(struct map_priv *m)
{
	int i;

	printf("mg_map_destroy\n");
	for (i = 0 ; i < file_end ; i++) {
		if (m->file[i])
			file_destroy(m->file[i]);
	}
}

extern int block_lin_count,block_idx_count,block_active_count,block_mem,block_active_mem;

static struct map_rect_priv *
map_rect_new_mg(struct map_priv *map, struct map_selection *sel)
{
	struct map_rect_priv *mr;
	int i;

	block_lin_count=0;
	block_idx_count=0;
	block_active_count=0;
	block_mem=0;
	block_active_mem=0;
	mr=g_new0(struct map_rect_priv, 1);
	mr->m=map;
	mr->xsel=sel;
	mr->current_file=-1;
	if (sel && sel->next)
		for (i=0 ; i < file_end ; i++) 
			mr->block_hash[i]=g_hash_table_new(g_int_hash,g_int_equal);
		
	file_next(mr);
	return mr;
}


static struct item *
map_rect_get_item_mg(struct map_rect_priv *mr)
{
	for (;;) {
		switch (mr->current_file) {
		case file_town_twn:
			if (town_get(mr, &mr->town, &mr->item))
				return &mr->item;
			break;
		case file_border_ply:
		/* case file_bridge_ply: */
		case file_build_ply: 
		case file_golf_ply: 
		/* case file_height_ply: */
		case file_natpark_ply: 
		case file_nature_ply: 
		case file_other_ply:
		case file_rail_ply:
		case file_sea_ply:
		/* case file_tunnel_ply: */
		case file_water_ply:
		case file_woodland_ply:
			if (poly_get(mr, &mr->poly, &mr->item))
				return &mr->item;
			break;
		case file_street_str:
			if (street_get(mr, &mr->street, &mr->item))
				return &mr->item;
			break;
		case file_end:
			return NULL;
		default:
			break;
		}
		if (block_next(mr))
			continue;
		if (mr->cur_sel->next) {
			mr->cur_sel=mr->cur_sel->next;
			if (block_init(mr))
				continue;
		}
		if (file_next(mr))
			continue;
		dbg(1,"lin_count %d idx_count %d active_count %d %d kB (%d kB)\n", block_lin_count, block_idx_count, block_active_count, (block_mem+block_active_mem)/1024, block_active_mem/1024);
		return NULL;
	}
}

static struct item *
map_rect_get_item_byid_mg(struct map_rect_priv *mr, int id_hi, int id_lo)
{
	mr->current_file = id_hi >> 16;
	switch (mr->current_file) {
	case file_town_twn:
		if (town_get_byid(mr, &mr->town, id_hi, id_lo, &mr->item))
			return &mr->item;
		break;
	case file_street_str:
		if (street_get_byid(mr, &mr->street, id_hi, id_lo, &mr->item))
			return &mr->item;
		break;
	default:	
		if (poly_get_byid(mr, &mr->poly, id_hi, id_lo, &mr->item))
			return &mr->item;
		break;
	}
	return NULL;
}


static void
map_rect_destroy_mg(struct map_rect_priv *mr)
{
	int i;
	for (i=0 ; i < file_end ; i++) 
		if (mr->block_hash[i])
			g_hash_table_destroy(mr->block_hash[i]);	
	g_free(mr);
}

static struct map_search_priv *
map_search_new_mg(struct map_priv *map, struct item *item, struct attr *search, int partial)
{
	struct map_rect_priv *mr=g_new0(struct map_rect_priv, 1);
	dbg(1,"id_lo=0x%x\n", item->id_lo);
	dbg(1,"search=%s\n", search->u.str);
	mr->m=map;
	mr->search_type=search->type;
	switch (search->type) {
	case attr_town_name:
		if (item->type != type_country_label)
			return NULL;
		tree_search_init(map->dirname, "town.b2", &mr->ts, 0x1000);
		break;
	case attr_street_name:
		if (item->type != type_town_streets)
			return NULL;
		dbg(1,"street_assoc=0x%x\n", item->id_lo);
		tree_search_init(map->dirname, "strname.b1", &mr->ts, 0);
		break;
	default:
		dbg(0,"unknown search\n");
		g_free(mr);
		return NULL;
	}
	mr->search_item=*item;
	mr->search_country=item->id_lo;
	mr->search_str=search->u.str;
	mr->search_partial=partial;
	mr->current_file=file_town_twn-1;
	file_next(mr);
	return (struct map_search_priv *)mr;
}

static void
map_search_destroy_mg(struct map_search_priv *ms)
{
	struct map_rect_priv *mr=(struct map_rect_priv *)ms;

	if (! mr)
		return;
	tree_search_free(&mr->ts);
	g_free(mr);
}

static struct item *
map_search_get_item_mg(struct map_search_priv *ms)
{
	struct map_rect_priv *mr=(struct map_rect_priv *)ms;

	if (! mr)
		return NULL;
	switch (mr->search_type) {
	case attr_town_name:
		return town_search_get_item(mr);
	case attr_street_name:
		return street_search_get_item(mr);
	default:
		return NULL;
	}
}

static struct map_methods map_methods_mg = {
	map_destroy_mg,
	map_rect_new_mg,
	map_rect_destroy_mg,
	map_rect_get_item_mg,
	map_rect_get_item_byid_mg,
	map_search_new_mg,
	map_search_destroy_mg,
	map_search_get_item_mg,
};

struct map_priv *
map_new_mg(struct map_methods *meth, char *dirname, char **charset, enum projection *pro)
{
	struct map_priv *m;
	int i,maybe_missing,len=strlen(dirname);
	char filename[len+16];
	
	*meth=map_methods_mg;
	*charset="iso8859-1";
	*pro=projection_mg;

	m=g_new(struct map_priv, 1);
	m->id=++map_id;
	m->dirname=g_strdup(dirname);
	strcpy(filename, dirname);
	filename[len]='/';
	for (i = 0 ; i < file_end ; i++) {
		if (file[i]) {
			strcpy(filename+len+1, file[i]);
			m->file[i]=file_create_caseinsensitive(filename);
			if (! m->file[i]) {
				maybe_missing=(i == file_border_ply || i == file_height_ply || i == file_sea_ply);
				if (! maybe_missing)
					g_warning("Failed to load %s", filename);
			}
		}
	}

	return m;
}

void
plugin_init(void)
{
	plugin_register_map_type("mg", map_new_mg);
}