I've spent several hours trying to condense an old function (since it's very clunky and expensive, but it works fine), but I just want to shorten it.
(This is going to be a long post, so bear with me. I'll do my best to explain it in great detail).
Basically, I have a puzzle sequence game which is very dependent on RNG and the player has to input the correct code that is displayed to them, represented as a string (I'll explain that part. It's very simple.)
The puzzle is like a panel with different buttons that can be interacted with. (See image)

Each button has it's own "variable" (Hmm... Perhaps someone might be able to come up with a better solution, but that may mess with how I've got it all set up and how I want it to play out.
Basic defaults for the buttons:
Code: Select all
default la_switch = 0
default lb_switch = 0
default lc_switch = 0
default ld_switch = 0
default la_1 = 0
default la_2 = 0
default la_3 = 0
default la_4 = 0
default lb_1 = 0
default lb_2 = 0
default lb_3 = 0
default lb_4 = 0
default lc_1 = 0
default lc_2 = 0
default lc_3 = 0
default lc_4 = 0
default ld_1 = 0
default ld_2 = 0
default ld_3 = 0
default ld_4 = 0
default le_switch = 0
default lf_switch = 0
default lg_switch = 0
default lh_switch = 0
default le_1 = 0
default le_2 = 0
default le_3 = 0
default le_4 = 0
default lf_1 = 0
default lf_2 = 0
default lf_3 = 0
default lf_4 = 0
default lg_1 = 0
default lg_2 = 0
default lg_3 = 0
default lg_4 = 0
default lh_1 = 0
default lh_2 = 0
default lh_3 = 0
default lh_4 = 0
Code: Select all
init python:
def cmpc_matrix(*mtrx):
vars_matrix = [
(la_switch, lb_switch, lc_switch, ld_switch),
(la_1, la_2, la_3, la_4),
(lb_1, lb_2, lb_3, lb_4),
(lc_1, lc_2, lc_3, lc_4),
(ld_1, ld_2, ld_3, ld_4),
(le_switch, lf_switch, lg_switch, lh_switch),
(le_1, le_2, le_3, le_4),
(lf_1, lf_2, lf_3, lf_4),
(lg_1, lg_2, lg_3, lg_4),
(lh_1, lh_2, lh_3, lh_4)]
return all([vars_matrix[l] == mtrx[l]
for l in range(10)])
Code: Select all
## cmpc_matrix((PANEL 1 TOP[0-3]), (COLUMN 'A' left[0/1]), (COLUMN 'B' left[0/1]), (COLUMN 'C' left[0/1]), (COLUMN 'D' left[0/1]),
## (PANEL 2 TOP[0-3]), (COLUMN 'A' right[0/1]), (COLUMN 'B' right[0/1]), (COLUMN 'C' right[0/1]), (COLUMN 'D' right[0/1]))
cmpc_matrix((0,3,3,0), (0,0,1,1), (0,0,1,1), (1,1,0,0), (1,1,0,0),
(0,0,0,0), (0,1,1,0), (0,1,1,0), (0,1,1,0), (0,1,1,0))
Note: This is a boolean type (from what I've tested).
I thought I'd go ahead and try to add a dictionary for the different variations of the code.
The 'Identifier' and then the 'code' which is actually a 'boolean' type.
The dictionary has a lot more, but just for a demonstration, I've only included a few.
Here it is:
Code: Select all
cmpc_matrix_dict = {
"FH1RJ1": cmpc_matrix((3, 0, 0, 0), (1, 1, 1, 1), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0),
(0, 0, 0, 3), (0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0), (1, 1, 1, 1)),
"CE6CGA": cmpc_matrix((0, 3, 3, 0), (0, 1, 1, 0), (1, 0, 0, 1), (1, 0, 0, 1), (0, 1, 1, 0),
(2, 3, 3, 1), (0, 0, 0, 1), (1, 0, 1, 1), (1, 1, 0, 1), (1, 0, 0, 0)),
"NXUIK8": cmpc_matrix((0, 3, 3, 0), (0, 0, 1, 1), (0, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 0),
(0, 0, 0, 0), (0, 1, 1, 0), (0, 1, 1, 0), (0, 1, 1, 0), (0, 1, 1, 0))
}
# plus many others
I've tried adding a randomiser that will show and expect the correct result by the player.
Code: Select all
default cmpc_list = renpy.random.choice(list(cmpc_matrix_dict.items()))
Code: Select all
init python:
def cmpc_matrix_codes_new():
if list(cmpc_list)[1] is True:
return True
else:
# This is if the code didn't match the required (currently displayed one)
return False
# And then the display one, which works fine since it's supposed to display the required code (or the 'key' from the dictionary above)
def cmpc_serials_new():
for i in cmpc_list:
return str(i)
Here's the old, bulky code (condensed just for demonstration):
Code: Select all
init python:
cmpc_random = renpy.random.randint(1, 32)
def cmpc_matrix_codes_old(): # This one works from an RNG above
# F H 1 R J 1
if cmpc_random == 1:
if cmpc_matrix((3,0,0,0), (1,1,1,1), (0,0,0,0), (0,0,0,0), (0,0,0,0),
(0,0,0,3), (0,0,0,0), (0,0,0,0), (0,0,0,0), (1,1,1,1)):
return True
# C E 6 C G A
elif cmpc_random == 2:
if cmpc_matrix((0,3,3,0), (0,1,1,0), (1,0,0,1), (1,0,0,1), (0,1,1,0),
(2,3,3,1), (0,0,0,1), (1,0,1,1), (1,1,0,1), (1,0,0,0)):
return True
# N X U I K 8
elif cmpc_random == 3:
if cmpc_matrix((0,3,3,0), (0,0,1,1), (0,0,1,1), (1,1,0,0), (1,1,0,0),
(0,0,0,0), (0,1,1,0), (0,1,1,0), (0,1,1,0), (0,1,1,0)):
return True
# And many, many more.
Code: Select all
label code_check: ## CHECK THE CODE
$ renpy.block_rollback()
if cmpc_matrix_codes_new(): # This function is basically suppose to return True to run is block, else return False and skip.
# if cmpc_matrix_codes(cmpc_random): # This is the old function, which works but has a .7 delay.
jump correct
else:
$ cmpc_list = renpy.random.choice(list(cmpc_matrix_dict.items()))
# $ cmpc_random = renpy.random.randint(1, 32) # This is the old RNG
jump incorrect
# etc
If there is anything else you need I'll be more than happy to provide (if I can).
Thanks for taking the time to read this.
All help is appreciated.
EDIT: The new function, for some reason returns a False value (I think), so in the label, when it checks it it just comes back as False.
