File: util.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (58 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download
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
import itertools
import math
import random
import sys
from collections.abc import Iterable, Iterator
from hashlib import md5

error_list = []


def int_hash(s):
    h = md5()
    h.update(s)
    return int(h.hexdigest(), 16)


def seed_rng(seed):
    """
    Seeds the default RNG with the specified seed
    """
    random.seed(seed)
    # The following jumpahead call can be uncommented to work around this issue of the Mersenne Twister PRNG
    # (quoted from wikipedia http://en.wikipedia.org/wiki/Mersenne_twister):
    # "It can take a long time to start generating output that passes randomness tests, if the initial state is highly
    # non-random-- particularly if the initial state has many zeros. A consequence of this is that two instances of the
    # generator, started with initial states that are almost the same, will usually output nearly the same sequence
    # for many iterations, before eventually diverging."
    # random.jumpahead(999999)


def distance(start, end):
    """
    Calculates linear distance between two coordinates.
    """
    x1, y1 = start
    x2, y2 = end
    return math.hypot(float(x1) - float(x2), float(y1) - float(y2))


def report_error(msg):
    """
    Handles error messages.
    """
    error_list.append(msg)
    print(msg, file=sys.stderr)


def unique_product(first: Iterable[int], second: Iterable[int]) -> Iterator[tuple[int, int]]:
    """
    Create all possible unique pairs for two iterables.
    Both iterables should consist of comparable objects.
    Pair is sorted in ascending order.
    """
    return filter(lambda x: x[0] < x[1], itertools.product(first, second))


class MapGenerationError(RuntimeError):
    """Map generation runtime error."""