Allow [start, end] times for Night to traverse midnight

Daytime mapping entries like [23, 2] were causing trouble, for which the following fix was implemented...
This commit is contained in:
Raffson 2024-05-20 15:13:21 +02:00
parent 562be59765
commit b07aae3d6e
No known key found for this signature in database
GPG Key ID: B0402B2C9B764D99

View File

@ -4,6 +4,7 @@ import datetime
import logging import logging
import random import random
from dataclasses import dataclass from dataclasses import dataclass
from typing import Tuple
from game.settings import Settings, NightMissions from game.settings import Settings, NightMissions
from game.theater import ConflictTheater, SeasonalConditions from game.theater import ConflictTheater, SeasonalConditions
@ -74,11 +75,25 @@ class Conditions:
# time constrained to that. DaytimeMap enforces that we have only whole hour # time constrained to that. DaytimeMap enforces that we have only whole hour
# ranges for now, so we don't need to worry about accidentally changing the time # ranges for now, so we don't need to worry about accidentally changing the time
# of day by truncating sub-hours. # of day by truncating sub-hours.
time = datetime.time( day, hours = Conditions.random_time_progression(day, time_range)
hour=random.randint(time_range[0].hour, time_range[1].hour) time = datetime.time(hour=hours)
)
return datetime.datetime.combine(day, time) return datetime.datetime.combine(day, time)
@staticmethod
def random_time_progression(
day: datetime.date, time_range: Tuple[datetime.time, datetime.time]
) -> Tuple[datetime.date, int]:
start, end = time_range[0].hour, time_range[1].hour
if start > end:
end += 24
hours = random.randint(start, end)
if hours > 23:
day += datetime.timedelta(days=1.0)
hours %= 24
else:
hours = random.randint(start, end)
return day, hours
@classmethod @classmethod
def generate_weather( def generate_weather(
cls, cls,