mirror of
https://github.com/RYDE-WORK/ballistica.git
synced 2026-01-25 00:13:27 +08:00
tidying and id util functions
This commit is contained in:
parent
f64bfaac35
commit
c8cf5b063f
@ -7,15 +7,15 @@ from __future__ import annotations
|
||||
import weakref
|
||||
from typing import TYPE_CHECKING, TypeVar, overload
|
||||
|
||||
import _ba
|
||||
from ba._messages import DieMessage, DeathType, OutOfBoundsMessage, UNHANDLED
|
||||
from ba._error import print_exception, ActivityNotFoundError
|
||||
import _ba
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any, Optional, Literal
|
||||
import ba
|
||||
|
||||
T = TypeVar('T', bound='Actor')
|
||||
TA = TypeVar('TA', bound='Actor')
|
||||
|
||||
|
||||
class Actor:
|
||||
@ -94,7 +94,7 @@ class Actor:
|
||||
|
||||
return UNHANDLED
|
||||
|
||||
def autoretain(self: T) -> T:
|
||||
def autoretain(self: TA) -> TA:
|
||||
"""Keep this Actor alive without needing to hold a reference to it.
|
||||
|
||||
This keeps the ba.Actor in existence by storing a reference to it
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
<!-- THIS FILE IS AUTO GENERATED; DO NOT EDIT BY HAND -->
|
||||
<h4><em>last updated on 2021-10-09 for Ballistica version 1.6.5 build 20393</em></h4>
|
||||
<h4><em>last updated on 2021-10-14 for Ballistica version 1.6.5 build 20393</em></h4>
|
||||
<p>This page documents the Python classes and functions in the 'ba' module,
|
||||
which are the ones most relevant to modding in Ballistica. If you come across something you feel should be included here or could be better explained, please <a href="mailto:support@froemling.net">let me know</a>. Happy modding!</p>
|
||||
<hr>
|
||||
@ -753,7 +753,7 @@ is a convenient way to access this same functionality.</p>
|
||||
|
||||
</dd>
|
||||
<dt><h4><a name="method_ba_Actor__autoretain">autoretain()</a></dt></h4><dd>
|
||||
<p><span>autoretain(self: T) -> T</span></p>
|
||||
<p><span>autoretain(self: TA) -> TA</span></p>
|
||||
|
||||
<p>Keep this Actor alive without needing to hold a reference to it.</p>
|
||||
|
||||
|
||||
@ -64,7 +64,7 @@ class ErrorResponse(Response):
|
||||
instead results in a local exception being raised.
|
||||
"""
|
||||
error_message: Annotated[str, IOAttrs('m')]
|
||||
error_type: Annotated[ErrorType, IOAttrs('e')]
|
||||
error_type: Annotated[ErrorType, IOAttrs('e')] = ErrorType.OTHER
|
||||
|
||||
|
||||
@ioprepped
|
||||
|
||||
@ -498,3 +498,58 @@ def linearstep(edge0: float, edge1: float, x: float) -> float:
|
||||
Values outside of the range return 0 or 1.
|
||||
"""
|
||||
return max(0.0, min(1.0, (x - edge0) / (edge1 - edge0)))
|
||||
|
||||
|
||||
def _compact_id(num: int, chars: str) -> str:
|
||||
if num < 0:
|
||||
raise ValueError('Negative integers not allowed.')
|
||||
|
||||
# Chars must be in sorted order for sorting to work correctly
|
||||
# on our output.
|
||||
assert ''.join(sorted(list(chars))) == chars
|
||||
|
||||
base = len(chars)
|
||||
out = ''
|
||||
while num:
|
||||
out += chars[num % base]
|
||||
num //= base
|
||||
return out[::-1] or '0'
|
||||
|
||||
|
||||
def human_readable_compact_id(num: int) -> str:
|
||||
"""Given a positive int, return a compact string representation for it.
|
||||
|
||||
Handy for visualizing unique numeric ids using as few as possible chars.
|
||||
This representation uses only lowercase letters and numbers (minus the
|
||||
following letters for readability):
|
||||
's' is excluded due to similarity to '5'.
|
||||
'l' is excluded due to similarity to '1'.
|
||||
'i' is excluded due to similarity to '1'.
|
||||
'o' is excluded due to similarity to '0'.
|
||||
'z' is excluded due to similarity to '2'.
|
||||
|
||||
When reading human input consisting of these IDs, it may be desirable
|
||||
to map the disallowed chars to their corresponding allowed ones
|
||||
('o' -> '0', etc).
|
||||
|
||||
Sort order for these ids is the same as the original numbers.
|
||||
|
||||
If more compactness is desired at the expense of readability,
|
||||
use compact_id() instead.
|
||||
"""
|
||||
return _compact_id(num, '0123456789abcdefghjkmnpqrtuvwxy')
|
||||
|
||||
|
||||
def compact_id(num: int) -> str:
|
||||
"""Given a positive int, return a compact string representation for it.
|
||||
|
||||
Handy for visualizing unique numeric ids using as few as possible chars.
|
||||
This version is more compact than human_readable_compact_id() but less
|
||||
friendly to humans due to using both capital and lowercase letters,
|
||||
both 'O' and '0', etc.
|
||||
|
||||
Sort order for these ids is the same as the original numbers.
|
||||
"""
|
||||
return _compact_id(
|
||||
num, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
'abcdefghijklmnopqrstuvwxyz')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user