2018-02-08 14:41:08 +01:00
|
|
|
# misc.py - module for miscellaneous functions
|
2020-01-21 09:18:19 +01:00
|
|
|
# Copyright (C) 2017-2020 Nguyễn Gia Phong
|
2017-10-19 15:28:56 +02:00
|
|
|
#
|
2018-02-27 14:39:28 +01:00
|
|
|
# This file is part of Brutal Maze.
|
2017-10-19 15:28:56 +02:00
|
|
|
#
|
2018-02-27 14:39:28 +01:00
|
|
|
# Brutal Maze is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Brutal Maze is distributed in the hope that it will be useful,
|
2017-10-19 15:28:56 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-02-27 14:39:28 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2017-10-19 15:28:56 +02:00
|
|
|
#
|
2018-02-27 14:39:28 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with Brutal Maze. If not, see <https://www.gnu.org/licenses/>.
|
2017-10-19 15:28:56 +02:00
|
|
|
|
2018-02-27 14:39:28 +01:00
|
|
|
__doc__ = 'Brutal Maze module for miscellaneous functions'
|
2017-11-02 15:39:06 +01:00
|
|
|
|
2018-08-03 17:50:59 +02:00
|
|
|
from datetime import datetime
|
2018-05-20 15:33:50 +02:00
|
|
|
from itertools import chain
|
2020-09-19 06:12:54 +02:00
|
|
|
from math import cos, degrees, pi, sin
|
2018-08-03 17:50:59 +02:00
|
|
|
from os import path
|
2019-10-09 07:43:22 +02:00
|
|
|
from random import shuffle
|
2017-10-19 15:28:56 +02:00
|
|
|
|
|
|
|
import pygame
|
2020-04-12 11:35:44 +02:00
|
|
|
from palace import Buffer, Source
|
2020-09-19 06:12:54 +02:00
|
|
|
from pygame.gfxdraw import aapolygon, filled_polygon
|
2017-10-19 15:28:56 +02:00
|
|
|
|
2020-04-12 11:35:44 +02:00
|
|
|
from .constants import ADJACENTS, CORNERS, MIDDLE
|
2018-05-20 15:33:50 +02:00
|
|
|
|
2017-11-02 15:39:06 +01:00
|
|
|
|
2017-10-19 15:28:56 +02:00
|
|
|
def randsign():
|
|
|
|
"""Return either -1 or 1 (kind of) randomly."""
|
|
|
|
return (pygame.time.get_ticks() & 1)*2 - 1
|
|
|
|
|
|
|
|
|
|
|
|
def regpoly(n, R, r, x, y):
|
2018-08-03 17:50:59 +02:00
|
|
|
"""Return pointlist of a regular n-gon with circumradius of R,
|
|
|
|
center point I(x, y) and corner A that angle of vector IA is r
|
|
|
|
(in radians).
|
2017-10-19 15:28:56 +02:00
|
|
|
"""
|
|
|
|
r %= pi * 2
|
|
|
|
angles = [r + pi*2*side/n for side in range(n)]
|
|
|
|
return [(x + R*cos(angle), y + R*sin(angle)) for angle in angles]
|
|
|
|
|
|
|
|
|
|
|
|
def fill_aapolygon(surface, points, color):
|
2018-08-03 17:50:59 +02:00
|
|
|
"""Draw a filled polygon with anti-aliased edges onto a surface."""
|
2017-10-19 15:28:56 +02:00
|
|
|
aapolygon(surface, points, color)
|
|
|
|
filled_polygon(surface, points, color)
|
|
|
|
|
|
|
|
|
|
|
|
def sign(n):
|
|
|
|
"""Return the sign of number n."""
|
|
|
|
return -1 if n < 0 else 1 if n else 0
|
|
|
|
|
|
|
|
|
2018-03-05 17:59:02 +01:00
|
|
|
def deg(x):
|
2019-10-09 06:15:55 +02:00
|
|
|
"""Convert angle x from radians to degrees,
|
|
|
|
casted to a nonnegative integer.
|
2018-03-05 17:59:02 +01:00
|
|
|
"""
|
2020-01-21 09:18:19 +01:00
|
|
|
return round((lambda a: a if a > 0 else a + 360)(degrees(x)))
|
2018-03-05 17:59:02 +01:00
|
|
|
|
|
|
|
|
2018-08-03 17:50:59 +02:00
|
|
|
def join(iterable, sep=' ', end='\n'):
|
|
|
|
"""Return a string which is the concatenation of string
|
|
|
|
representations of objects in the iterable, separated by sep.
|
|
|
|
|
|
|
|
end is appended to the resulting string.
|
|
|
|
"""
|
|
|
|
return sep.join(map(str, iterable)) + end
|
|
|
|
|
|
|
|
|
2018-05-20 15:33:50 +02:00
|
|
|
def around(x, y):
|
|
|
|
"""Return grids around the given one in random order."""
|
|
|
|
a = [(x + i, y + j) for i, j in ADJACENTS]
|
|
|
|
shuffle(a)
|
|
|
|
c = [(x + i, y + j) for i, j in CORNERS]
|
|
|
|
shuffle(c)
|
|
|
|
return chain(a, c)
|
|
|
|
|
|
|
|
|
2018-08-03 17:50:59 +02:00
|
|
|
def json_rec(directory):
|
|
|
|
"""Return path to JSON file to be created inside the given directory
|
|
|
|
based on current time local to timezone in ISO 8601 format.
|
|
|
|
"""
|
|
|
|
return path.join(
|
|
|
|
directory, '{}.json'.format(datetime.now().isoformat()[:19]))
|
2020-04-12 11:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def play(sound: str, x: float = MIDDLE, y: float = MIDDLE,
|
2020-04-25 10:19:05 +02:00
|
|
|
gain: float = 1.0) -> Source:
|
2020-04-12 11:35:44 +02:00
|
|
|
"""Play a sound at the given position."""
|
2020-04-12 12:51:12 +02:00
|
|
|
source = Buffer(sound).play()
|
2020-04-12 11:35:44 +02:00
|
|
|
source.spatialize = True
|
2020-04-12 12:51:12 +02:00
|
|
|
source.position = x, -y, 0
|
2020-04-12 11:35:44 +02:00
|
|
|
source.gain = gain
|
2020-04-25 10:19:05 +02:00
|
|
|
return source
|