#cas try: from random import random, randint, choice pass except: from urandom import random, randint, choice pass from math import pi, cos, sin, floor from polycal3 import * screen_w, screen_h, poly_has_color, font_w, font_h, poly_set_pixel, poly_draw_line, poly_fill_rect, poly_fill_ellipse, poly_draw_string, poly_clean_screen, poly_show_screen, poly_pause, poly_set_buffered = get_infos() # def color2int(r, g, b): # return r | (g * 256) | (b * 65536) def int2color(v): return [v & 255, (v // 256) & 255, (v // 65536) & 255] color_back = 0 color_front = 16777215 sprites=( [16, 16, 1696283181465067017015386769703278171010768577886963022407551036942422377408, 32767, 0], [16, 16, 90406913605304324316311450749470454119920054623686274066983603664472111106016, 255, 8355711], [16, 7, 1142449095824479245067099987647496, 65535, 65280], [16, 8, 15961181869632440322022688966142263820, 16711680, 16711935], [11, 16, 95780240586838128711004227852982791444309999934708792, 16255, 0], [5, 16, 680099750626616230187012, 65280, 8355711], [9, 16, 5153358514263728141002102549763850240, 16776960, 8355711] ) if not(poly_has_color): color_front, color_back = color_back, color_front sprites=( [7, 7, 124606068315420, 0, 0], [9, 7, 8649438164012728952, 0, 16777215], [6, 3, 87218, 0, 0], [7, 6, 1108828926480, 0, 0], [7, 7, 560805023088566, 0, 0], [3, 9, 84541250, 0, 16777215], [7, 7, 72875729937424, 0, 0] ) mwspr, mhspr, zoom, mwc = 0, 0, 1, 1 m_p, m_l, m_k, m_b, m_d, m_a, m_m = 1, 4, 16, 64, 256, 1024, 4096 def xy(s, n, w=0, h=0): a = fix_angle(s * 2 * pi / n) x, y = round(xweb + rweb*cos(a)), round(yweb + rweb*sin(a)) x += [[not(not(w)), -w][x < xweb], -w//2][s * 4 == n or s * 4 == 3 * n] y += [[not(not(h)), -h][y < yweb], -h // 2][s == 0 or s * 2 == n] return x, y def draw_sprite(i_spr, x, y, i_c, z=1): spr = sprites[i_spr] w, h, v, dx, dy = spr[0], spr[1], spr[2], 0, 0 while v > 0: if v & 1: poly_fill_rect(int(x + z*dx),int(y + z*dy), z, z, int2color(spr[3 + i_c])) dy = (dy + 1) % h x += z * (not(dy)) v = v // 2 def draw_connection(s1, s2, c): x1, y1 = xy(s1, dimweb) x2, y2 = xy(s2, dimweb) poly_draw_line(x1, y1, x2, y2, int2color(c)) def draw_connections(s, s0, c): for i in range(s0, dimweb): if web[s][i]: draw_connection(s, i, c) def refresh_platform(i): x, y = xy(i, dimweb, wpf, hpf) poly_fill_rect(x, y, wpf, hpf, int2color(color_back)) pfi = platforms[i] if pfi: for k in range(len(sprites)): if pfi & (3 * 2**(2 * k)): x, y = xy(i, dimweb, sprites[k][0], sprites[k][1]) draw_sprite(k, x, y, (pfi // 2**(2*k + 1)) & 1) else: x, y = xy(i, dimweb, font_w, font_h) poly_draw_string([chr(i + 55), str(i)][i <= 9], x, y, int2color(color_front), int2color(color_back)) def insertinto(l1, l2): for v in l1: if v not in l2: l2.append(v) return l2 def removefrom(l1, l2): for v in l1: try: l2.remove(v) except: pass return l2 def connectPlatforms(s1, s2): global web web[s1][s2], web[s2][s1] = 1, 1 def get_reachable_platforms_from_platforms(l, safe): lv = [] for s in l: for i in range(dimweb): if web[s][i]: if i not in lv and (not(safe) or not (platforms[i] & m_p)): lv.append(i) return lv def cango(s1, s2, safe): lvo1, lvi1, lvo2, lvi2, t_inter, k = [], [s1], [], [s2], 0, 0 while not (t_inter) and len(lvi1) and len(lvi2): lvo1, lvo2 = insertinto(lvo1, lvi1), insertinto(lvo2, lvi2) for v in lvo1: if v in lvo2: return k lvi1, lvi2 = get_reachable_platforms_from_platforms(lvo1, safe), get_reachable_platforms_from_platforms(lvo2, safe) lvi1, lvi2 = removefrom(lvo1, lvi1), removefrom(lvo2, lvi2) k += 1 return 0 def my_bitor(a, b): return ~(~a & ~b) def init_web(d, p_p, p_b): global web, xweb, yweb, rweb, platforms, mwspr, mhspr, zoom, mwc, sprites, screen_w, screen_h for i in range(1, len(sprites)): mwspr, mhspr = max(mwspr, sprites[i][0]), max(mhspr, sprites[i][1]) yweb, l0 = screen_h / 2, list(range(dimweb)) rweb = yweb - max(mhspr, font_h) l0.remove(0) web, xweb, platforms, conn, dconn, i_k = [], rweb + max(mwspr, font_w), [0 for k in range(dimweb)], [0], list(range(1, dimweb)), choice(l0) for j in range(dimweb): web.append([0 for k in range(dimweb)]) while len(dconn): s = dconn[randint(0, len(dconn) - 1)] connectPlatforms(conn[randint(0, len(conn) - 1)], s) dconn.remove(s) conn.append(s) for j in range(dimweb-1): for i in range(j + 1, dimweb): if floor(d + random()): connectPlatforms(i, j) i_d = choice(l0) platforms[i_d] = my_bitor(platforms[i_d], m_d) l1 = list(l0) for v in get_reachable_platforms_from_platforms([0], 0): l1.remove(v) if not(len(l1)): l1 = l0 l2 = list(l1) for v in get_reachable_platforms_from_platforms(get_reachable_platforms_from_platforms([0], 0), 0): try: l2.remove(v) except: pass if not(len(l2)): l2 = l1 i_l = choice(l2) platforms[i_l] = my_bitor(platforms[i_l], m_l) platforms[i_k] = my_bitor(platforms[i_k], m_k) for i in l1: if i != i_k and i != i_d and floor(p_p*dimweb/len(l1) + random()): platforms[i] = my_bitor(platforms[i], m_p) if not cango(0, i_k, 1) or not cango(0, i_d, 1): platforms[i] = platforms[i] & ~m_p if floor(p_b*dimweb/len(l1) + random()): platforms[i] = my_bitor(platforms[i], m_b) for k in range(len(sprites), 0, -1): tzoom = int(min(((screen_w - screen_h) / mwspr) // k, (screen_h * k / mhspr) // len(sprites))) if tzoom >= zoom: zoom, mwc = tzoom, k def parcourir_selon(ia): global dimweb, wpf, hpf, platforms, sprites, screen_w, screen_h t_coeff = min(1, screen_h / 240) dimweb = int(t_coeff * 36) maxcoups = dimweb**2 * 2 poly_set_buffered() init_web(t_coeff * .05, .1, .15) wpf, hpf = max(font_w, mwspr), max(font_h, mhspr) poly_fill_rect(0, 0, screen_w, screen_h, int2color(color_back)) for i in range(dimweb): draw_connections(i, i + 1, color_front) refresh_platform(i) s0, s1, s2, s3, s4, s5, s6, s7 = 0, 0, m_a, 0, 1, -1, 0, 0 pfs0, pfs5 = platforms[s0], 0 while s4 > 0 and (not (s2 & (2 * m_k)) or not (pfs0 & m_d)): if s5 < 0: s5 = 0 else: try: k, k2 = ia(s0, voisines, dimweb, s1, s2) if pfs5 & (2 * m_b): while s0 == s5: s0 = randint(0, dimweb - 1) pfs0, pfs5 = my_bitor(platforms[s0], m_b), pfs5 & ~(3 * m_b) & ~m_m else: if k2: if s2 & m_a: v = platforms[k] if v & m_l: v, s2 = v & ~m_l, my_bitor(s2, 2 * m_l) platforms[k] = my_bitor(v, 2 * m_l) refresh_platform(k) s2 = s2 & ~m_a s2 = my_bitor(s2, 2 * m_a) else: if k in voisines: s0 = k if pfs5 & m_b: pfs5 = my_bitor(pfs5, 2 * m_b) pfs0, pfs5 = platforms[s0], pfs5 & ~m_m s3 += 1 if s3 >= maxcoups: s4 = 0 if pfs0 & m_k: pfs0 = pfs0 & ~m_k s2 = my_bitor(s2, 2 * m_k) if pfs0 & my_bitor(m_p, m_l): s4 = 0 pfs0 = my_bitor(pfs0, 2 * m_m) platforms[s5] = pfs5 except Exception as t_excpt: s4 = -1 print(t_excpt) pfs0 = my_bitor(pfs0, m_m) s1, voisines = pfs0, get_reachable_platforms_from_platforms([s0], 0) platforms[s0] = pfs0 for v in voisines: t = my_bitor(m_p, m_l) t = platforms[v] & my_bitor(t, m_k) s1 = my_bitor(s1, t) for v in get_reachable_platforms_from_platforms(voisines, 0): t = platforms[v] & m_l s1 = my_bitor(s1, t) draw_connections(s5, 0, color_front) draw_connections(s0, 0, 16776960) if cango(s0, s5, 0) == 1: draw_connection(s0, s5, 65280) refresh_platform(s5) refresh_platform(s0) s, sold = my_bitor(s1, s2), my_bitor(s6, s7) for k in range(len(sprites) - 1): if s & 3 != sold & 3: x, y = screen_w - 1 - zoom*mwspr*(1 + k%mwc) + (zoom * (mwspr - sprites[k][0]))//2, (k // mwc)*mhspr*zoom + zoom*(mhspr - sprites[k][1])//2 if s & 3: draw_sprite(k, x, y, (s & 3) // 2, zoom) else: poly_fill_rect(x, y, sprites[k][0] * zoom, sprites[k][1] * zoom, int2color(color_back)) s, sold = s // 4, sold // 4 poly_show_screen() poly_pause() s5, s6, s7, pfs5 = s0, s1, s2, pfs0 v, r = 0, s4 > 0 and s3 < maxcoups if r: print("Gagne ! :-)") else: print("Perdu ! :-(") if pfs0 & m_p: print("Tombe*e puits") v = 1 if pfs0 & m_l: print("Mange*e Leviathan") v = v | 2 if s3 >= maxcoups: print("Energie epuisee") elif s4 < 0: print("Exception interne") v = v | 4 s = "En " + str(s3) + " coup" if s3 > 1: s += "s" s += "." print(s) return r, v, s2, s3