|
| 1 | +from threading import Thread |
| 2 | +import threading |
| 3 | +from mrmine.keypress_detector import detect_keypress_nonblocking |
| 4 | +from mrmine.gui import update_GUI, scroll, update_GUI_func, GUI_send, get_save |
| 5 | +import os, time, random, math |
| 6 | +global save_data |
| 7 | +# Make the relay of keys more accessible |
| 8 | +version='v1.4-alpha' |
| 9 | +key_pressed = None |
| 10 | +save_loc='/workspaces/coolmathgames/v1.4-alpha/mrmine/mrmine_save.txt' |
| 11 | +log_loc= '/workspaces/coolmathgames/v1.4-alpha/mrmine/log.txt' |
| 12 | +def write_log(message): |
| 13 | + with open(log_loc, "a") as log_file: |
| 14 | + message1=('Timestamp '+ str(time.time())+': '+ str(message) + "\n") |
| 15 | + log_file.write(message1) |
| 16 | +def m_ms_initialize(): |
| 17 | + write_log('From function \'initialize\': Initializing script...') |
| 18 | +def keypress_listener(): |
| 19 | + global key_pressed |
| 20 | + while True: |
| 21 | + key = detect_keypress_nonblocking() |
| 22 | + if key: |
| 23 | + key_pressed = key.lower() |
| 24 | + if key_pressed == '\x03': # Ctrl+C for interrupt |
| 25 | + raise KeyboardInterrupt |
| 26 | +def tick(): |
| 27 | + global key_pressed |
| 28 | + while True: |
| 29 | + if key_pressed: |
| 30 | + key = key_pressed |
| 31 | + key_pressed = False |
| 32 | + print(f"Key registered: {key}") |
| 33 | + if key == 'up' or key == 'down': |
| 34 | + scroll(key) |
| 35 | + update_GUI() |
| 36 | + elif key in ["k", "q", "c", "s", "h", "u", " ", "p", "r", "e"]: |
| 37 | + update_GUI_func(key) |
| 38 | + else: |
| 39 | + time.sleep(0.1) |
| 40 | + update_GUI() |
| 41 | +def drill_percent(): |
| 42 | + global save_data |
| 43 | + while True: |
| 44 | + drill_data=save_data['DRILL_DATA'] |
| 45 | + time_to_new_depth=save_data['LAYER_HARDNESS']/drill_data[0] |
| 46 | + start_new_depth=time.time() |
| 47 | + end_new_depth=start_new_depth+time_to_new_depth |
| 48 | + while time.time()<=end_new_depth: |
| 49 | + percent=round(((time.time()-start_new_depth)/time_to_new_depth)*100, 2) |
| 50 | + GUI_send(percent, "percent_to_new_layer") |
| 51 | + time.sleep(0.1) |
| 52 | + save_data['DEPTH']+=1 |
| 53 | + save_data['LAYER_HARDNESS']=float((1.57**(1+(save_data['DEPTH']/50)))+23.43) |
| 54 | + write_log(f'From function \'drill_percent\': new layer; new hardness is {save_data['LAYER_HARDNESS']}.') |
| 55 | +def miner_monitor(): |
| 56 | + global save_data |
| 57 | + while True: |
| 58 | + i=1 |
| 59 | + resource_input={ |
| 60 | + 1: 0, |
| 61 | + 2: 0, |
| 62 | + 3: 0, |
| 63 | + 4: 0, |
| 64 | + 5: 0, |
| 65 | + 6: 0, |
| 66 | + 7: 0, |
| 67 | + 8: 0, |
| 68 | + 9: 0, |
| 69 | + 10: 0, |
| 70 | + 11: 0, |
| 71 | + 12: 0, |
| 72 | + 13: 0, |
| 73 | + 14: 0, |
| 74 | + 15: 0 |
| 75 | + } |
| 76 | + while i<=save_data['DEPTH']: |
| 77 | + x100depth=math.ceil(i/100) |
| 78 | + #1-100km: 0.2 coal / sec, 0.07 copper / sec, 0.003 iron / sec (starting values) |
| 79 | + #101-200km: 0.2 copper / sec, 0.07 iron / sec, 0.003 silver / sec (starting values) |
| 80 | + #etc. |
| 81 | + #richness for resource 1 decreases (0.2 --> 0.003) |
| 82 | + #richness for resource 2 increases slightly (0.07 --> 0.125) |
| 83 | + #richness for resource 3 increases (0.003 --> 0.2) |
| 84 | + if x100depth*100<=save_data['DEPTH']: |
| 85 | + resource_input[x100depth]+=(0.2-(0.00197*i))*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*100 |
| 86 | + resource_input[x100depth+1]+=(0.07+(0.00055*i))*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*100 |
| 87 | + resource_input[x100depth+2]+=(0.003+(0.00197*i))*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*100 |
| 88 | + else: |
| 89 | + resource_input[x100depth]+=(0.2-(0.00197*i))*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*(save_data['DEPTH']-((x100depth-1)*100)) |
| 90 | + resource_input[x100depth+1]+=(0.07+(0.00055*i))*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*(save_data['DEPTH']-((x100depth-1)*100)) |
| 91 | + resource_input[x100depth+2]+=(0.003+(0.00197*i))*save_data['MINER_SPEED']*save_data['MINER_EFFICIENCY']*(save_data['DEPTH']-((x100depth-1)*100)) |
| 92 | + i+=100 |
| 93 | + for mineral in resource_input: |
| 94 | + save_data['MINERALS'][mineral-1]+=resource_input[mineral] |
| 95 | + save_data['MINERALS'][mineral-1]=int(round(save_data['MINERALS'][mineral-1], 0)) |
| 96 | + time.sleep(0.1) |
| 97 | +drill_thread=threading.Thread(target=drill_percent) |
| 98 | +miner_thread=threading.Thread(target=miner_monitor) |
| 99 | +def read_save(): |
| 100 | + save_data = { |
| 101 | +'SAVE_NUMBER': 1, |
| 102 | +'END_TIME': 0, |
| 103 | +'STORAGE': 0, |
| 104 | +'UPGRADES': [], |
| 105 | +'MINERALS': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 106 | +'FLUIDS': [0, 0, 0], |
| 107 | +'SCIENTIST_DATA': [], |
| 108 | +'CAVE_DATA': [], |
| 109 | +'CHEST_DATA': [], |
| 110 | +'MONEY': 0, |
| 111 | +'TP_DATA': [], |
| 112 | +'MANAGER': (0.00, 0), |
| 113 | +'ATTACK': (0, 0), |
| 114 | +'MINER_SPEED': 1, |
| 115 | +'MINER_EFFICIENCY': 1, |
| 116 | +'DEPTH': 4, |
| 117 | +'DRILL_DATA': [1, 1], |
| 118 | +'RIG_DATA': [], |
| 119 | +'LAYER_HARDNESS': 1, |
| 120 | +'FORGE_STATUS': 'none', |
| 121 | +'BROADCAST': '', |
| 122 | +'BROADCAST_TYPE': 'none', |
| 123 | +'DATAID': 0, |
| 124 | +'PLANET': 1 |
| 125 | + } |
| 126 | + with open(save_loc, 'r') as file: |
| 127 | + for line in file: |
| 128 | + line = line.strip() |
| 129 | + if '=' in line: # Ensuring there's a key-value split |
| 130 | + key, value = line.split('=', 1) # Split on first '=' to handle complex values |
| 131 | + write_log(f'From function \'read_save\': grabbed set {key, value} from save') |
| 132 | + try: |
| 133 | + # Attempt to evaluate the value for correct data types |
| 134 | + save_data[key] = eval(value) |
| 135 | + write_log(f'From function \'read_save\': writing save data key {key} to data dictionary') |
| 136 | + except Exception as e: |
| 137 | + # If eval fails, it's likely a string or ambiguous format |
| 138 | + write_log(f'From function \'read_save\': eval failed; writing directly to data dictionary') |
| 139 | + write_log(e) |
| 140 | + save_data[key] = value |
| 141 | + return save_data |
| 142 | +def send_data_to_GUI(): |
| 143 | + while True: |
| 144 | + get_save(save_data) |
| 145 | + time.sleep(0.02) |
| 146 | +send_thread=threading.Thread(target=send_data_to_GUI) |
| 147 | +def write_save(save_data): |
| 148 | + success=False |
| 149 | + i=1 |
| 150 | + while i<=5 and not success: |
| 151 | + write_log(f'From function \'write_save\': attempting to write to save file (attempt {i})') |
| 152 | + try: |
| 153 | + with open(save_loc, 'w') as file: |
| 154 | + write_log('From function \'write_save\': opened save file') |
| 155 | + strv='''''' |
| 156 | + for item in save_data: |
| 157 | + write_log('From function \'write_save\': writing item '+str(item)) |
| 158 | + strv += item + '=' + str(save_data[item]) + '\n' |
| 159 | + file.writelines(strv) |
| 160 | + write_log('From function \'write_save\': success') |
| 161 | + success=True |
| 162 | + break |
| 163 | + except Exception as e: |
| 164 | + write_log('From function \'write_save\': failed to write to save file') |
| 165 | + write_log(e) |
| 166 | + i+=1 |
| 167 | + if i>5: |
| 168 | + write_log('From function \'write_save\': aborting write to save file') |
| 169 | +def clear_log(): |
| 170 | + with open(log_loc, "w"): |
| 171 | + write_log('From function \'clear_log\': clearing log file') |
| 172 | +def mrmine_start_game(): |
| 173 | + clear_log() |
| 174 | + global save_data |
| 175 | + os.system('clear') |
| 176 | + try: |
| 177 | + save_data=read_save() |
| 178 | + save_data['DATAID']=str(random.randint(1, 1000000000)) |
| 179 | + write_save(save_data) |
| 180 | + except Exception as e: |
| 181 | + write_log(e) |
| 182 | + print("Error while loading / writing save file.") |
| 183 | + #save_data=None |
| 184 | + #print("Exiting the game...") |
| 185 | + #exit() |
| 186 | + print("Using template data...") |
| 187 | + save_data = { |
| 188 | +'SAVE_NUMBER': 1, |
| 189 | +'END_TIME': 0, |
| 190 | +'STORAGE': 0, |
| 191 | +'UPGRADES': [], |
| 192 | +'MINERALS': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 193 | +'FLUIDS': [0, 0, 0], |
| 194 | +'SCIENTIST_DATA': [], |
| 195 | +'CAVE_DATA': [], |
| 196 | +'CHEST_DATA': [], |
| 197 | +'MONEY': 0, |
| 198 | +'TP_DATA': [], |
| 199 | +'MANAGER': (0.00, 0), |
| 200 | +'ATTACK': (0, 0), |
| 201 | +'MINER_SPEED': 1, |
| 202 | +'MINER_EFFICIENCY': 1, |
| 203 | +'DEPTH': 4, |
| 204 | +'DRILL_DATA': [1, 1], |
| 205 | +'RIG_DATA': [], |
| 206 | +'LAYER_HARDNESS': 1, |
| 207 | +'FORGE_STATUS': 'none', |
| 208 | +'BROADCAST': '', |
| 209 | +'BROADCAST_TYPE': 'none', |
| 210 | +'DATAID': 0, |
| 211 | +'PLANET': 1 |
| 212 | + } |
| 213 | + write_log('From function \'mrmine_start_game\': Verifying save file integrity...') |
| 214 | + if save_data['VERSION']!=version: |
| 215 | + print('Your save file is on the wrong version. Compatibility issues may arise. Please download the newest version of the game or find a compatible save file.') |
| 216 | + write_log('From function \'mrmine_start_game\': Invalid save file version.') |
| 217 | + cont=input('Would you like the game to continue running? [You may corrupt your save or cause fatal errors.] > ') |
| 218 | + yes=['yes', 'y'] |
| 219 | + if cont.lower() in yes: |
| 220 | + print('Continuing with game...') |
| 221 | + write_log('From function \'mrmine_start_game\': Continuing game from use input.') |
| 222 | + time.sleep(0.5) |
| 223 | + else: |
| 224 | + print('Exiting game...') |
| 225 | + write_log('From function \'mrmine_start_game\': Exiting game from use input.') |
| 226 | + else: |
| 227 | + write_log('From function \'mrmine_save_game\': Save file integrity verified.') |
| 228 | + if save_data['BROADCAST_TYPE'] == "message": |
| 229 | + print("Message from devs: "+str(save_data['BROADCAST'])) |
| 230 | + time.sleep(1) |
| 231 | + os.system('clear') |
| 232 | + try: |
| 233 | + listener_thread = Thread(target=keypress_listener) |
| 234 | + listener_thread.daemon = True |
| 235 | + listener_thread.start() |
| 236 | + drill_thread.start() |
| 237 | + miner_thread.start() |
| 238 | + send_thread.start() |
| 239 | + update_GUI() |
| 240 | + while True: |
| 241 | + tick() |
| 242 | + except KeyboardInterrupt: |
| 243 | + print("Game interrupted, exiting...") |
| 244 | + exit() |
0 commit comments