def timestring(timeval: float,
               centi: bool = True,
               timeformat: ba.TimeFormat = TimeFormat.SECONDS,
               suppress_format_warning: bool = False) -> ba.Lstr:
    """Generate a ba.Lstr for displaying a time value.

    Category: General Utility Functions

    Given a time value, returns a ba.Lstr with:
    (hours if > 0 ) : minutes : seconds : (centiseconds if centi=True).

    Time 'timeval' is specified in seconds by default, or 'timeformat' can
    be set to ba.TimeFormat.MILLISECONDS to accept milliseconds instead.

    WARNING: the underlying Lstr value is somewhat large so don't use this
    to rapidly update Node text values for an onscreen timer or you may
    consume significant network bandwidth.  For that purpose you should
    use a 'timedisplay' Node and attribute connections.

    """
    from ba._language import Lstr

    # Temp sanity check while we transition from milliseconds to seconds
    # based time values.
    if __debug__:
        if not suppress_format_warning:
            _ba.time_format_check(timeformat, timeval)

    # We operate on milliseconds internally.
    if timeformat is TimeFormat.SECONDS:
        timeval = int(1000 * timeval)
    elif timeformat is TimeFormat.MILLISECONDS:
        pass
    else:
        raise ValueError(f'invalid timeformat: {timeformat}')
    if not isinstance(timeval, int):
        timeval = int(timeval)
    bits = []
    subs = []
    hval = (timeval // 1000) // (60 * 60)
    if hval != 0:
        bits.append('${H}')
        subs.append(('${H}',
                     Lstr(resource='timeSuffixHoursText',
                          subs=[('${COUNT}', str(hval))])))
    mval = ((timeval // 1000) // 60) % 60
    if mval != 0:
        bits.append('${M}')
        subs.append(('${M}',
                     Lstr(resource='timeSuffixMinutesText',
                          subs=[('${COUNT}', str(mval))])))

    # We add seconds if its non-zero *or* we haven't added anything else.
    if centi:
        # pylint: disable=consider-using-f-string
        sval = (timeval / 1000.0 % 60.0)
        if sval >= 0.005 or not bits:
            bits.append('${S}')
            subs.append(('${S}',
                         Lstr(resource='timeSuffixSecondsText',
                              subs=[('${COUNT}', ('%.2f' % sval))])))
    else:
        sval = (timeval // 1000 % 60)
        if sval != 0 or not bits:
            bits.append('${S}')
            subs.append(('${S}',
                         Lstr(resource='timeSuffixSecondsText',
                              subs=[('${COUNT}', str(sval))])))
    return Lstr(value=' '.join(bits), subs=subs)
def animate(node: ba.Node,
            attr: str,
            keys: dict[float, float],
            loop: bool = False,
            offset: float = 0,
            timetype: ba.TimeType = TimeType.SIM,
            timeformat: ba.TimeFormat = TimeFormat.SECONDS,
            suppress_format_warning: bool = False) -> ba.Node:
    """Animate values on a target ba.Node.

    Category: Gameplay Functions

    Creates an 'animcurve' node with the provided values and time as an input,
    connect it to the provided attribute, and set it to die with the target.
    Key values are provided as time:value dictionary pairs.  Time values are
    relative to the current time. By default, times are specified in seconds,
    but timeformat can also be set to MILLISECONDS to recreate the old behavior
    (prior to ba 1.5) of taking milliseconds. Returns the animcurve node.
    """
    if timetype is TimeType.SIM:
        driver = 'time'
    else:
        raise Exception('FIXME; only SIM timetype is supported currently.')
    items = list(keys.items())
    items.sort()

    # Temp sanity check while we transition from milliseconds to seconds
    # based time values.
    if __debug__:
        if not suppress_format_warning:
            for item in items:
                _ba.time_format_check(timeformat, item[0])

    curve = _ba.newnode('animcurve',
                        owner=node,
                        name='Driving ' + str(node) + ' \'' + attr + '\'')

    if timeformat is TimeFormat.SECONDS:
        mult = 1000
    elif timeformat is TimeFormat.MILLISECONDS:
        mult = 1
    else:
        raise ValueError(f'invalid timeformat value: {timeformat}')

    curve.times = [int(mult * time) for time, val in items]
    curve.offset = _ba.time(timeformat=TimeFormat.MILLISECONDS) + int(
        mult * offset)
    curve.values = [val for time, val in items]
    curve.loop = loop

    # If we're not looping, set a timer to kill this curve
    # after its done its job.
    # FIXME: Even if we are looping we should have a way to die once we
    #  get disconnected.
    if not loop:
        _ba.timer(int(mult * items[-1][0]) + 1000,
                  curve.delete,
                  timeformat=TimeFormat.MILLISECONDS)

    # Do the connects last so all our attrs are in place when we push initial
    # values through.

    # We operate in either activities or sessions..
    try:
        globalsnode = _ba.getactivity().globalsnode
    except ActivityNotFoundError:
        globalsnode = _ba.getsession().sessionglobalsnode

    globalsnode.connectattr(driver, curve, 'in')
    curve.connectattr('out', node, attr)
    return curve
def animate_array(node: ba.Node,
                  attr: str,
                  size: int,
                  keys: dict[float, Sequence[float]],
                  loop: bool = False,
                  offset: float = 0,
                  timetype: ba.TimeType = TimeType.SIM,
                  timeformat: ba.TimeFormat = TimeFormat.SECONDS,
                  suppress_format_warning: bool = False) -> None:
    """Animate an array of values on a target ba.Node.

    Category: Gameplay Functions

    Like ba.animate(), but operates on array attributes.
    """
    # pylint: disable=too-many-locals
    combine = _ba.newnode('combine', owner=node, attrs={'size': size})
    if timetype is TimeType.SIM:
        driver = 'time'
    else:
        raise Exception('FIXME: Only SIM timetype is supported currently.')
    items = list(keys.items())
    items.sort()

    # Temp sanity check while we transition from milliseconds to seconds
    # based time values.
    if __debug__:
        if not suppress_format_warning:
            for item in items:
                # (PyCharm seems to think item is a float, not a tuple)
                _ba.time_format_check(timeformat, item[0])

    if timeformat is TimeFormat.SECONDS:
        mult = 1000
    elif timeformat is TimeFormat.MILLISECONDS:
        mult = 1
    else:
        raise ValueError('invalid timeformat value: "' + str(timeformat) + '"')

    # We operate in either activities or sessions..
    try:
        globalsnode = _ba.getactivity().globalsnode
    except ActivityNotFoundError:
        globalsnode = _ba.getsession().sessionglobalsnode

    for i in range(size):
        curve = _ba.newnode('animcurve',
                            owner=node,
                            name=('Driving ' + str(node) + ' \'' + attr +
                                  '\' member ' + str(i)))
        globalsnode.connectattr(driver, curve, 'in')
        curve.times = [int(mult * time) for time, val in items]
        curve.values = [val[i] for time, val in items]
        curve.offset = _ba.time(timeformat=TimeFormat.MILLISECONDS) + int(
            mult * offset)
        curve.loop = loop
        curve.connectattr('out', combine, 'input' + str(i))

        # If we're not looping, set a timer to kill this
        # curve after its done its job.
        if not loop:
            # (PyCharm seems to think item is a float, not a tuple)
            _ba.timer(int(mult * items[-1][0]) + 1000,
                      curve.delete,
                      timeformat=TimeFormat.MILLISECONDS)
    combine.connectattr('output', node, attr)

    # If we're not looping, set a timer to kill the combine once
    # the job is done.
    # FIXME: Even if we are looping we should have a way to die
    #  once we get disconnected.
    if not loop:
        # (PyCharm seems to think item is a float, not a tuple)
        _ba.timer(int(mult * items[-1][0]) + 1000,
                  combine.delete,
                  timeformat=TimeFormat.MILLISECONDS)