summaryrefslogtreecommitdiffstats
path: root/arts/builder/autorouter.cpp
blob: 6fdcce15676264d9b80e1a96f4ed00bef196b3cb (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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
	/*

	Copyright (C) 1998 Stefan Westerfeld <stefan@space.twc.de>,
				  2002 Hans Meine <hans_meine@gmx.net>

    This program 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 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    */

#include "autorouter.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <arts/debug.h>

#include <kdebug.h>

#ifndef HAVE_LIBPTHREAD
#define pthread_create(a, b, c, d) ;
#define pthread_join(a, b) ;
#define pthread_mutex_lock(a) ;
#define pthread_mutex_unlock(a) ;
#define pthread_mutex_init(a, b) ;
#define pthread_attr_init(a) ;
#endif

using namespace std;

const int COST_INIT = 200000;
const int PENALTY_OTHERCONNECTION = 100;
const int PENALTY_CORNER = 100;
const int IDLE_SLEEP_MILLISECONDS = 40;

void *startroute(void *where)
{
	((AutoRouter *)where)->thread_command_loop();
	// just to prevent the compiler warning
	return 0;
}

bool ARCommand::isDestructive()
{
	return false;
}

AutoRouter::AutoRouter(int width, int height)
	: width(width), height(height)
{
#ifdef HAVE_LIBPTHREAD
	pthread_attr_t attrs;

	pthread_attr_init(&attrs);
	pthread_mutex_init(&mutex_sync, 0);
	pthread_mutex_init(&mutex_queue, 0);
#endif

	// allocate memory for fields..
	field = new Field*[width];
	completeField = new Field*[width];

	for(int i = 0; i < width; i++)
	{
		field[i] = new Field[height];
		completeField[i] = new Field[height];
	}

	// ..and clear the freshly allocated memory
	thread_clear();
	thread_sync();

	newOwner = 1;

	// init those two arrays - they should be constant somehow..
	directionMask[DIR_DOWN] = ~(left | right);
	directionMask[DIR_UP] = ~(left | right);
	directionMask[DIR_LEFT] = ~(up | down);
	directionMask[DIR_RIGHT] = ~(up | down);

	ownerIndex[DIR_DOWN] = OWNER_UD;
	ownerIndex[DIR_UP] = OWNER_UD;
	ownerIndex[DIR_LEFT] = OWNER_LR;
	ownerIndex[DIR_RIGHT] = OWNER_LR;

	initPseudoRandom();
	
	thread_terminate_now = false; 		// not yet
#ifdef HAVE_LIBPTHREAD
	// setup thread and run thread_command_loop() in it
	pthread_create(&route_thread, &attrs, startroute, this);
#endif

	arts_debug("AR UP...");
}

void AutoRouter::initPseudoRandom()
{
	// init pseudoRandomDir array with random directions
	
	int remainingDirs[4];	
	for(int i = 0; i < PRSIZE; i++)
	{
		if((i & 3) == 0)
		{
			int j = 0;
			for(int dir = DIR_UP; dir <= DIR_RIGHT; dir++)
				remainingDirs[j++] = dir;
		}

		int rnd;
		do rnd = rand()&3;
		while(remainingDirs[rnd] == DIR_NONE);

		pseudoRandomDir[i] = remainingDirs[rnd];
		
		remainingDirs[rnd] = DIR_NONE;
	}
	nextPR = 0;
}

AutoRouter::~AutoRouter()
{
	// tell the thread to shut down
	pthread_mutex_lock(&mutex_queue);
	thread_terminate_now = true;
	pthread_mutex_unlock(&mutex_queue);
	
	// terminate thread
	void *rc;
	pthread_join(route_thread, &rc);

	// clean up
	for(int i = 0; i < width; i++)
	{
		delete[] field[i];
		delete[] completeField[i];
	}
	delete[] completeField;
	delete[] field;
	
	arts_debug("AR DOWN...");
}

void AutoRouter::enqueue(ARCommand *command)
{
#ifdef HAVE_LIBPTHREAD
	// threaded execution, locking the queue
	pthread_mutex_lock(&mutex_queue);
	
	if(command->isDestructive())
	{
		// ok, then we can kill the whole list, since this will clear
		// the whole results anyway

		command_queue.setAutoDelete(true);
		command_queue.clear();
		command_queue.setAutoDelete(false);
	}
	command_queue.append(command);
	
	pthread_mutex_unlock(&mutex_queue);
#else
	// immediate execution
	command->execute(this);
	delete command;
#endif
}

void AutoRouter::thread_command_loop()
{
	while(1)
	{
		ARCommand *command = 0;

		// pop one command from queue if possible..
		pthread_mutex_lock(&mutex_queue);
		if(!command_queue.isEmpty())
		{
			command = command_queue.first();
			command_queue.remove(unsigned(0));
		}
		else
		{
			if (thread_terminate_now) {
				pthread_mutex_unlock(&mutex_queue);
				return;
			}
		}
		pthread_mutex_unlock(&mutex_queue);

		// ..and execute command if we got one
		if(command)
		{
			command->execute(this);
			delete command;
		}
		else // no more commands->wait for some milliseconds
			usleep(1000 * IDLE_SLEEP_MILLISECONDS);
		// TODO: use pthread_cond_wait here instead of half-busy-waiting?
	}
}

// ------------------------- getXXX() handling -------------------------

long AutoRouter::get(int x, int y)
{
	assert(x >= 0 && x < width);
	assert(y >= 0 && y < height);

	pthread_mutex_lock(&mutex_sync);
	long result = completeField[x][y].data;
	pthread_mutex_unlock(&mutex_sync);
	
	return(result);
}

void AutoRouter::getowners(int x, int y, long& ud_owner, long& lr_owner)
{
	assert(x >= 0 && x < width);
	assert(y >= 0 && y < height);

	pthread_mutex_lock(&mutex_sync);
	ud_owner = completeField[x][y].owner[OWNER_UD];
	lr_owner = completeField[x][y].owner[OWNER_LR];
	pthread_mutex_unlock(&mutex_sync);
}

// ------------------------- sync() handling -------------------------

void AutoRouter::sync()
{
	enqueue(new ARSyncCommand());
}

void ARSyncCommand::execute(AutoRouter *router)
{
	router->thread_sync();
}

void AutoRouter::thread_sync()
{
	int i;
	pthread_mutex_lock(&mutex_sync);
	
	for(i = 0; i < width; i++)
		memcpy(completeField[i], field[i], sizeof(Field)*height);
	_needRedraw = true;
	
	pthread_mutex_unlock(&mutex_sync);
}

bool AutoRouter::needRedraw()
{
	bool result;

	pthread_mutex_lock(&mutex_sync);
	result = _needRedraw;
	_needRedraw = false;
#ifdef AR_DEBUG
	if(result) arts_debug("NEED REDRAW NOW!");
#endif
	pthread_mutex_unlock(&mutex_sync);

	return result;
}

// ------------------------- clear() handling -------------------------

void AutoRouter::clear()
{
	enqueue(new ARClearCommand());
}

bool ARClearCommand::isDestructive()
{
	return true;
}

void ARClearCommand::execute(AutoRouter *router)
{
	router->thread_clear();
}

void AutoRouter::thread_clear()
{
	arts_debug("clear()ing now...");
	int x, y;

	for(x = 0; x < width; x++)
		for(y = 0; y < height; y++)
		{
			field[x][y].data = none;
			field[x][y].penalty = 0;
			field[x][y].owner[0] = -1;
			field[x][y].owner[1] = -1;
		}
}

// ------------------------- set() command handling -------------------------

void AutoRouter::set(int x1, int y1, int x2, int y2, long lt)
{
	enqueue(new ARSetCommand(x1, y1, x2, y2, lt));
}

ARSetCommand::ARSetCommand(int x1, int y1, int x2, int y2, long lt)
	: _x1(x1), _y1(y1), _x2(x2), _y2(y2), _lt(lt)
{
}

void ARSetCommand::execute(AutoRouter *router)
{
	router->thread_set(_x1, _y1, _x2, _y2, _lt);
}

void AutoRouter::thread_set(int x1, int y1, int x2, int y2, long lt)
{
	for(int x = x1; x <= x2; x++)
	{
		for(int y = y1; y <= y2; y++)
		{
			assert(x >= 0 && x < width);
			assert(y >= 0 && y < height);

			if(lt & solid)
			{
				if((y - 1) >= 0)
					field[x][y - 1].penalty += 5;

				if((y - 2) >= 0)
					field[x][y - 2].penalty += 2;

				if((y + 1) < height)
					field[x][y + 1].penalty += 5;

				if((y + 2) < height)
					field[x][y + 2].penalty += 2;
			}
			
			field[x][y].data = lt;
			field[x][y].owner[0] = 0;
			field[x][y].owner[1] = 0;		// don't pass
		}
	}
}


long AutoRouter::connect(int x1, int y1, int x2, int y2, long owner)
{
	if(owner == 0)
		owner = newOwner++;

	enqueue(new ARConnectCommand(x1, y1, x2, y2, owner));

	return owner;
}

ARConnectCommand::ARConnectCommand(int x1, int y1, int x2, int y2, long owner)
{
	_x1 = x1; _y1 = y1; _x2 = x2; _y2 = y2; _owner = owner;
}

void ARConnectCommand::execute(AutoRouter *router)
{
	router->thread_connect(_x1, _y1, _x2, _y2, _owner);
}

void AutoRouter::thread_connect(int x1, int y1, int x2, int y2, long owner)
{
	currentOwner = owner;

#ifdef AR_DEBUG
	arts_debug("-field[x1][y1].owner[0..1] = %ld, %ld",field[x1][y1].owner[0],
													field[x1][y1].owner[1]);
	arts_debug("-field[x2][y2].owner[0..1] = %ld, %ld", field[x2][y2].owner[0],
													field[x2][y2].owner[1]);
#endif

	// clear data(source) & data(dest) first and restore later, since
	// they might be solid
	long sourceFieldData = field[x1][y1].data; field[x1][y1].data = none;
	long destFieldData   = field[x2][y2].data; field[x2][y2].data = none;

	for(int x = 0; x < width; x++)
		for(int y = 0; y < height; y++)
			field[x][y].minCost = COST_INIT;

#ifdef AR_DEBUG
	arts_debug("autorouter: trying to connect %d, %d with %d, %d (owner %ld)",
			   x1, y1, x2, y2, owner);
#endif
	nextPR = 0;

	bestGoalPath.cost = COST_INIT;

	int activelist = 0;
	numQueuedPaths = 0;

	PathInfo path;
	path.x1 = x1;
	path.x2 = x2;
	path.y1 = y1;
	path.y2 = y2;
	path.cost = 0;
	path.depth = 0;
	queuePath(path);

	while(numQueuedPaths)
	{
		while(!pathlist[activelist].size())
			activelist++;
		PathQueue& activePathList =
			pathlist[activelist];

		assert(activePathList.size());
		examinePath(activePathList.first());
		activePathList.pop_front();
		numQueuedPaths--;
	}

	field[x1][y1].data = sourceFieldData;
	field[x2][y2].data = destFieldData;

	if(bestGoalPath.cost != COST_INIT)
	{
		//arts_debug("bestGoalPath.history for this connection is %s", bestGoalPath.history.data());
		//arts_debug("minCost for that was %d", gms);

		const char *walk = bestGoalPath.history.ascii();

		int x = x1;
		int y = y1;

		while(*walk)
		{
			field[x][y].owner[ownerIndex[*walk]] = currentOwner;
			switch(*walk)
			{
			case DIR_DOWN:
				field[x][y++].data |= down;
				field[x][y].data |= up;
				break;
			case DIR_UP:
				field[x][y--].data |= up;
				field[x][y].data |= down;
				break;
			case DIR_LEFT:
				field[x--][y].data |= left;
				field[x][y].data |= right;
				break;
			case DIR_RIGHT:
				field[x++][y].data |= right;
				field[x][y].data |= left;
				break;
			}
			field[x][y].owner[ownerIndex[*walk]] = currentOwner;
			walk++;
		}
	}
	else
	{
#ifdef AR_DEBUG
		arts_debug("!! sorry, this connection is impossible !!");
#endif
	}
}

void AutoRouter::queuePath(const PathInfo &path)
{
	PathInfo newPath = path;

	int targetlist = newPath.cost/5;
	if(targetlist > 1023)
		targetlist = 1023;

	pathlist[targetlist].append(newPath);
	qHeapSort(pathlist[targetlist]);

	numQueuedPaths++;
}

void AutoRouter::examinePath(const PathInfo &path)
{
	const char *walk = path.history.ascii();

// check if we can go here:

	if(path.x1 < 0 || path.x1 >= width)
		return;
	if(path.y1 < 0 || path.y1 >= width)
		return;

	int currentFieldCost = path.cost;
	
	if(path.depth > 0)
	{
		// going over a field where already connections are is bad
		if(field[path.x1][path.y1].data != 0)
			currentFieldCost += PENALTY_OTHERCONNECTION;

		if(directionMask[walk[path.depth - 1]] & field[path.x1][path.y1].data)
		{
			// someone already uses that field... we can continue
			// only if the connection has the same sourceport

			long fieldowner = field[path.x1][path.y1].owner[ownerIndex[walk[path.depth - 1]]];

			if(fieldowner != -1) // used?
			{
				if(fieldowner != currentOwner)
					return;
				
				// oops, the connections are from the same owner -> no
				// penalty needed!
				currentFieldCost -= PENALTY_OTHERCONNECTION;
			}
		}
	}

	// add cityblock distance to costs
	currentFieldCost += abs(path.x1 - path.x2) + abs(path.y1 - path.y2);
	
	// add field penalty to costs
	currentFieldCost += field[path.x1][path.y1].penalty;

	// add corner penalty to costs if path had corner here
	if(path.depth > 2)
		if(walk[path.depth - 2] != walk[path.depth - 1])
			currentFieldCost += PENALTY_CORNER;
	
	if(currentFieldCost > bestGoalPath.cost)
		return;

	// better path found?
	if(currentFieldCost < field[path.x1][path.y1].minCost)
	{
		field[path.x1][path.y1].minCost = currentFieldCost;
	
		// check if we are where we wanted to be:
		if(path.x1 == path.x2 && path.y1 == path.y2) {
			// goal! success! :-)
			
			if(path.cost < bestGoalPath.cost)
			{
				bestGoalPath = path; // best solution until now
				bestGoalPath.cost = currentFieldCost;
			}
			
			return;
		}

		// not at the goal yet, search next place to go; take some
		// pseudo random direction order (this method improves search
		// speed)

		PathInfo newPath = path;
		newPath.depth++;
		newPath.cost = currentFieldCost;
		
		for(int i = 0; i < 4; i++)
		{
			if(nextPR >= PRSIZE)
				nextPR = 0;
		
			switch(pseudoRandomDir[nextPR++])
			{
			case DIR_LEFT:
				newPath.x1--;
				newPath.history = path.history + (char)DIR_LEFT;
				queuePath(newPath);
				newPath.x1++;
				break;
				
			case DIR_RIGHT:
				newPath.x1++;
				newPath.history = path.history + (char)DIR_RIGHT;
				queuePath(newPath);
				newPath.x1--;
				break;
				
			case DIR_UP:
				newPath.y1--;
				newPath.history = path.history + (char)DIR_UP;
				queuePath(newPath);
				newPath.y1++;
				break;
				
			case DIR_DOWN:
				newPath.y1++;
				newPath.history = path.history + (char)DIR_DOWN;
				queuePath(newPath);
				newPath.y1--;
				break;
			}
		}
	}
}