Skip to content

Commit

Permalink
Merge pull request #6559 from ThePotatoGuy/zero-length-chunk
Browse files Browse the repository at this point in the history
Skip over zero-sized chunks when advancing filter chunks
  • Loading branch information
jmwall24 authored Oct 27, 2020
2 parents 311ad4d + 6781ddb commit 46e74a6
Showing 1 changed file with 66 additions and 53 deletions.
119 changes: 66 additions & 53 deletions Monika After Story/game/zz_backgrounds.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ init -10 python:

return "\n".join(output)

def adv_chunk(self, sfmn, st_index, run_pp, curr_time, force_co):
def adv_chunk(self, sfmn, st_index, run_pp, curr_time):
"""
Runs advance chunks alg, running progpoints but does NOT actually
set new index. This WILL SET SLICE INDEXES.
Expand All @@ -1294,38 +1294,43 @@ init -10 python:
run_pp - True will run the progpoints, FAlse will not
curr_time - passed to the progpoint. should be current time
as a datetime.time object
force_co - True will force one chunk advancement. False will
not. This is for cases where we are in the same chunk, but
earlier than the current slice. Doing this allows us to
reset the slice index.
RETURNS: new chunk index
"""
# chunk length
c_len = len(self._chunks)

# determine current chunk offsets
# Current Beginning OFFset, Next Beginning OFFset
cb_off, nb_off = self._calc_off(st_index)
curr_chunk = self._chunks[st_index]
found = False

# loop unfil sfmn in range of current chunk
while sfmn < cb_off or nb_off <= sfmn or force_co:
# always set this to false after one iteration
force_co = False
# force stop iteration if something bad happened
iter_stop = 10

# get chunk chunk
curr_chunk = self._chunks[st_index]
# loop until we found the chunk, or if we found it, until the first
# non-zero chunk
while iter_stop > 0 and (not found or len(curr_chunk) < 1):

# determine the next current offset and next index
# determine next chunk index
nxt_index = (st_index + 1) % c_len # next index or 0 if max len

# next offset or 0 if 86400
# next chunk
new_chunk = self._chunks[nxt_index]

# determine next chunk offsets
# next curent chunk offset offset or 0 if 86400
cb_off = nb_off % (store.mas_utils.secInDay())
st_index = (st_index + 1) % c_len # next index or 0 if max len

# now calc next offset
nb_off = cb_off + len(self._chunks[st_index])
# next chunk's offset
nb_off = cb_off + len(new_chunk)

# new chunk is
new_chunk = self._chunks[st_index]
# set found if we found the chunk
if not found:
found = cb_off <= sfmn < nb_off
# once this is set, the next loops will only happen if
# current chunks are less than zero

# lastly run pp if desired
if run_pp:
Expand All @@ -1335,22 +1340,32 @@ init -10 python:
curr_time
)

# always run global after
try:
store.mas_background._gbl_chunk_change(
curr_chunk,
new_chunk,
curr_time
)
except Exception as e:
store.mas_utils.writelog(self._ERR_PP_STR_G.format(
repr(e),
str(curr_chunk),
str(new_chunk),
))

# then finally reset slice index for this chunk
# and global
try:
store.mas_background._gbl_chunk_change(
curr_chunk,
new_chunk,
curr_time
)
except Exception as e:
store.mas_utils.writelog(self._ERR_PP_STR_G.format(
repr(e),
str(curr_chunk),
str(new_chunk),
))

# then finally reset slice index for the chunk we are leaving
curr_chunk.reset_index()

# and set the current chunk to next chunk
curr_chunk = new_chunk
st_index = nxt_index

iter_stop -= 1

if iter_stop < 1:
# this is bad
raise Exception("inf looped here")

return st_index

Expand Down Expand Up @@ -1695,20 +1710,21 @@ init -10 python:
pos_data = self.current_pos()

# are we technically in same chunk but before in time?
# if so, we need to force a chunk move
force_co = (
pos_data[1] <= sfmn < pos_data[2] # in same chunk
and sfmn < (pos_data[1] + pos_data[4]) # earlier than slice
)
# reset the current chunk's slice index then advance
if (
pos_data[1] <= sfmn < pos_data[2] # in same chunk
and sfmn < (pos_data[1] + pos_data[4]) # earlier than slice
):
self._current_chunk().reset_index()

# start by advancing chunks correctly, if needed
self._index = self.adv_chunk(
sfmn,
self._index,
True,
curr_time,
force_co
)
else:
# start by advancing chunks correctly, if needed
self._index = self.adv_chunk(
sfmn,
self._index,
True,
curr_time
)

# now we can start advancing slices
return self._chunks[self._index].progress(
Expand Down Expand Up @@ -1743,19 +1759,15 @@ init -10 python:
self._prev_flt = self.current()

# establish seconds
# Seconds From MidNight
sfmn = store.mas_utils.time2sec(curr_time)

# establish chunk index
boff, eoff = self._calc_off(0)
cindex = 0
while cindex < len(self._chunks)-1 and (sfmn < boff or eoff <= sfmn):
# determine next offsets
cindex += 1
boff, eoff = self._calc_off(cindex)
cindex = self.adv_chunk(sfmn, 0, False, curr_time)

# we should now be in the correct index probably
self._chunks[self._index].reset_index()
self._index = cindex
boff, eoff = self._calc_off(cindex)
self._chunks[cindex].update(sfmn - boff)

# mark that we used update
Expand Down Expand Up @@ -2673,6 +2685,7 @@ init 800 python:

if new_background != mas_current_background:
mas_current_background.exit(new_background, **kwargs)
new_background.update() # NOTE: do not put this in setBackground.
mas_setBackground(new_background, **kwargs)

store.mas_is_indoors = store.mas_background.EXP_TYPE_OUTDOOR not in new_background.ex_props
Expand Down

0 comments on commit 46e74a6

Please sign in to comment.