MCPcopy Create free account
hub / github.com/SimHacker/micropolis / tryGo

Method tryGo

MicropolisCore/src/MicropolisEngine/src/traffic.cpp:344–383  ·  view source on GitHub ↗

* Try to drive one tile in a random direction. * @param pos Current position. * @param dirLast Forbidden direction for movement (to prevent reversing). * @return Direction of movement, \c #DIR2_INVALID is returned if not moved. */

Source from the content-addressed store, hash-verified

342 * @return Direction of movement, \c #DIR2_INVALID is returned if not moved.
343 */
344Direction2 Micropolis::tryGo(const Position &pos, Direction2 dirLast)
345{
346 Direction2 directions[4];
347
348 // Find connections from current position.
349 Direction2 dir = DIR2_NORTH;
350 int count = 0;
351 for (int i = 0; i < 4; i++) {
352 if (dir != dirLast && roadTest(getTileFromMap(pos, dir, DIRT))) {
353 // found a road in an allowed direction
354 directions[i] = dir;
355 count++;
356 } else {
357 directions[i] = DIR2_INVALID;
358 }
359
360 dir = rotate90(dir);
361 }
362
363 if (count == 0) { // dead end
364 return DIR2_INVALID;
365 }
366
367 // We have at least one way to go.
368
369 if (count == 1) { // only one solution
370 for (int i = 0; i < 4; i++) {
371 if (directions[i] != DIR2_INVALID) {
372 return directions[i];
373 }
374 }
375 }
376
377 // more than one choice, draw a random number.
378 int i = getRandom16() & 3;
379 while (directions[i] == DIR2_INVALID) {
380 i = (i + 1) & 3;
381 }
382 return directions[i];
383}
384
385
386/**

Callers

nothing calls this directly

Calls 1

rotate90Function · 0.85

Tested by

no test coverage detected