Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 491 lines (490 sloc) 13.2 KB
#!/usr/bin/python2 -OO
from __future__ import division,print_function,unicode_literals
from pyglet import *
from pyglet.gl import *
from socket import *
from math import *
from select import select
from itertools import chain,cycle
from random import getrandbits as ritz
from os.path import isfile
try:range=xrange
except:pass
for a in range(1,16):globals().update(("pi%X%X"%(a,b),pi*a/b) for b in range(1,16))
pi63v=0,pi13,pi23,pi33,pi43,pi53,pi63
GLB3=GLubyte*3
Red,Blu,Gre=RGB=GLB3(255,0,0),GLB3(0,0,255),GLB3(0,255,0)
Blk,Wht,Blk=BWB=((GLB3(0,0,0),GLB3(255,255,255))*2)[:3]
def ptdir(x,y,m):
y=y%pi21-x%pi21
return max(-m,y) if y%pi21>pi else min(m,y)
def Bul(p,x,y,d,v):return Bad([p,x,y,cos(d)*v,sin(d)*v])
def miss(f=True):
global hit,hit2,life,B,E,Ee
if not hit2 and hit==((180,256),(180,256)):
if f:send(b"\xEE")
if not life:print("dead")
Ee=E=0
life-=1
B=B[:]=[]
hit=(N1.x,N1.y),(N2.x,N2.y)
hit2=30
def sexy():
global Ee,sT
usend(b"%c%c%c%c%c"%(sT>>8,sT&255,N1.awds[0]|N1.awds[1]<<1|N1.awds[2]<<2|N1.awds[3]<<3|N1.x>>4&16|N1.y>>3&32|Ee<<6,N1.x&255,N1.y&255))
sT+=1
Ee=0
def atch():
global E,Ee
E+=30
Ee+=1
if E>=360:
send(b"\xE3")
Nut(0)
Ee=E=0
elif Ee==3:sexy()
def Hi():return N1 if N1.y>N2.y else N2
def near(x,y):return min(
chain(
(((a.x-x)**2+(a.y-y)**2,a,a is N1) for a in NN),
(((a[3]-x)**2+(a[4]-y)**2,a,1) for a in N if not a[2]&64)))
#def near(x,y):return min(min(((a.x-x)**2+(a.y-y)**2,a,a is N1) for a in NN),min(((a[3]-x)**2+(a[4]-y)**2,a,1) for a in N if not a[2]&64))
def msg(x,y):
M[y].add(x)
send(b"%c%c"%(0xC0|x,y))
def Nut(x=1):
global N,Nap
Nap=x
x=((Hi().y-Y0)**2+(Hi().x-X0)**2)**.5
if x:N+=[(Hi().x-X0)*12/x,(Hi().y-Y0)*12/x,0,X0,Y0],
def on_key(k,o):
if 65360<k<65365:
N1.awds[k-65361]=o
sexy()
elif k==65307:app.exit()
W=window.Window(360,512,visible=False,vsync=False)
W.on_key_press=lambda k,m:on_key(k,1)
W.on_key_release=lambda k,m:on_key(k,0)
W.idle=W.dispatch_events
glEnable(3042)
X0=Y0=Z0=.5
zT=sT=FC=Nap=T=Tlc=E=Ee=hit2=0
life=3
hit=(180,256),(180,256)
M=[set() for x in range(256)]
Time={}
FF={}
Tl=[]
N=[]
B=[]
Bad=B.append
def F(*x):
global FC
for x in x:
while FC in FF:FC=FC+1&2047
FF[FC]=x
class Node:
def __init__(s,x):
s.x,s.p=(120,0) if x else (240,1)
s.y=256
s.awds=[0,0,0,0]
class Cannon:
die=lambda s:not Fd.add(Cannond(s))
def __init__(s,p,x,y,d,v,t,D=None,V=3):
s.p=p
s.x=x
s.y=y
s.hv=cos(d)*v
s.vv=sin(d)*v
s.bt=t
s.d=d if D is None else D
s.bV=V
def __call__(s):
s.x+=s.hv
s.y+=s.vv
s.d+=ptdir(s.d,atan2(Y0-s.y,X0-s.x),pi1F)
if not(-32<s.x<392 and -32<s.y<544):return send
a=near(s.x,s.y)
if a[0]<400 and a[2]:
if a[1] is N1 and N1.p==s.p:Nut()
return s.die()
ct=cos(T)*16.
st=sin(T)*16.
if not T%s.bt:Bul(s.p,s.x+ct,s.y+st,s.d,s.bV)
s.draw(24.,ct,st)
def draw(s,r,ct,st):
a="v2f",(s.x+ct,s.y+st,s.x-st,s.y+ct,s.x-ct,s.y-st,s.x+st,s.y-ct)
glColor3ubv(RGB[s.p])
graphics.draw(4,7,a)
glColor3ubv(Wht)
glBegin(GL_LINE_LOOP)
for ct in range(0,8,2):glVertex2f(a[1][ct],a[1][ct+1])
glEnd()
a="v2f",(s.x,s.y,s.x+r*cos(s.d-pi16),s.y+r*sin(s.d-pi16),s.x+r*cos(s.d+pi16),s.y+r*sin(s.d+pi16))
glColor3ubv(RGB[s.p])
graphics.draw(3,GL_TRIANGLES,a)
glColor3ubv(Wht)
glBegin(GL_LINE_LOOP)
for s in range(0,6,2):glVertex2f(*a[1][s:s+2])
glEnd()
class Cannond(Cannon):
def __init__(s,z):
for a in "pxyd":setattr(s,a,getattr(z,a))
s.r=16
def __call__(s):
s.r-=1
return not s.r or s.draw(s.r*1.5,cos(T)*s.r,sin(T)*s.r)
class CannonCannon:
def __init__(s,x):
Tl.append(s)
s.x=x
s.y=600
s.h=s.p=0
s.l=84
def __call__(s):
global FC
if s.y>400:s.y-=2
if 0 in M[s.x>>1]:
M[s.x>>1].remove(0)
s.h=45
s.l-=1
if s.l>80:
if not s.h:
a=near(s.x,s.y)
if a[0]<6666 and a[2] and a[1] is not N1:msg(0,s.x>>1)
else:
s.h-=1
if s.h==22:s.p=not s.p
for a in NN:a.y=abs(a.y-(s.h>>2))
if not T&15:F(Cannon(s.p,s.x,s.y,pi32,8,6))
else:
s.l-=2
for a in range(8):Bul(2,s.x,s.y,pi28*a,8)
if not s.l:
FC=0
return send
ct=cos(T)*s.l
st=sin(T)*s.l
a="v2f",(s.x+ct,s.y+st,s.x-st,s.y+ct,s.x-ct,s.y-st,s.x+st,s.y-ct)
glColor4ub(RGB[s.p][0],0,RGB[s.p][2],(655-s.y)*abs(s.h-22)//22)
graphics.draw(4,GL_QUADS,a)
glColor3ubv(Wht)
glBegin(GL_LINE_LOOP)
for s in range(0,8,2):glVertex2f(*a[1][s:s+2])
glEnd()
class Gattle:
die=lambda s:not Fd.add(Gattled(s))
def __init__(s,p,x,y,d,v,t,V=4):
s.p=p
s.x=x
s.y=y
s.hv=cos(d)*v
s.vv=sin(d)*v
s.bt=t
s.bT=s.d=0
s.bV=V
def __call__(s):
s.x+=s.hv
s.y+=s.vv
s.d+=pi19/s.bt
s.bT+=1
if not(-32<s.x<392 and -32<s.y<544):return send
a=near(s.x,s.y)
if a[0]<400 and a[2]:
if a[1] is N1 and N1.p==s.p:Nut()
return s.die()
if not s.bT%s.bt:
Bul(s.p,s.x,s.y,s.d,s.bV)
Bul(s.p,s.x,s.y,s.d+pi23,s.bV)
Bul(s.p,s.x,s.y,s.d-pi23,s.bV)
s.draw(pi16)
def draw(s,r):
a="v2f",(s.x,s.y,s.x+24.*cos(s.d-r),s.y+24.*sin(s.d-r),s.x+24.*cos(s.d+r),s.y+24.*sin(s.d+r),s.x,s.y,s.x+24.*cos(s.d+pi46-r),s.y+24.*sin(s.d+pi46-r),s.x+24.*cos(s.d+pi46+r),s.y+24.*sin(s.d+pi46+r),s.x,s.y,s.x+24.*cos(s.d-pi46-r),s.y+24.*sin(s.d-pi46-r),s.x+24.*cos(s.d-pi46+r),s.y+24.*sin(s.d-pi46+r))
glColor3ubv(RGB[s.p])
graphics.draw(9,GL_TRIANGLES,a)
glColor3ubv(Wht)
glBegin(GL_LINE_LOOP)
for s in range(0,18,2):glVertex2f(*a[1][s:s+2])
glEnd()
class Gattled(Gattle):
def __init__(s,z):
for a in "pxyd":setattr(s,a,getattr(z,a))
s.r=pi16
def __call__(s):
s.r-=pi16/16
return s.r<0 or s.draw(s.r)
class Complement:
def __init__(s,x):
Tl.append(s)
s.p=2
s.x=x
s.y=-96
s.d=pi12
s.l=2
s.h=s.s=0
s.e=1
s.t=666
def __call__(s):
if s.t:s.t-=1
if 1 in M[s.x>>1]:
M[s.x>>1].remove(1)
s.h=30
s.l<<=1
if s.l==16:s.s=3
if s.s<2:
s.y+=-2 if s.s else 16
s.s+=s.y in (444,640)
elif s.s==2:s.d+=pi/(29+ritz(1))
else:
s.e+=1
if s.e==60:return send
a=near(s.x,s.y)
if s.h:s.h-=1
elif a[0]<9999 and a[2] and a[1] is not N1:msg(1,s.x>>1)
glColor3ubv(Gre)
glBegin(GL_LINE_LOOP)
for a in range(s.l):
ca=cos(s.d+pi21*a/s.l)*s.e
sa=sin(s.d+pi21*a/s.l)*s.e
if T&3 and T%7 and s.s<3:Bad([(NN[1-int(Z0)].p if Z0 in (0.,1.) else 2),s.x+ca*99.,s.y+sa*99.,ca*7,sa*7])
elif not s.t:Bad([2,s.x+ca*99.,s.y+sa*99.,ca*7,sa*7])
for a in range(9,99,3):glVertex2f(s.x+ca*a+ritz(3)-4,s.y+sa*a+ritz(3)-4)
glEnd()
class Laser(object):
die=lambda s:not Fd.add(Laserd(s))
def __init__(s,*x):
s.p,s.x,s.y,s.t=x
s.d=iter(cycle(sin(pi19*a) for a in range(18)))
s.l=0
s.V=graphics.vertex_list(1,"v2f/stream","c3B/static")
s.S=0
def __call__(s):
ha=N1.x-s.x
va=N1.y-s.y
h=abs(atan2(va,ha)-s.t)%pi21
v=(ha**2+va**2)**.5
if not Z0 and h<pi12 and v<s.l and sin(h)*v<9 and s.p!=N1.p:miss()
h=cos(s.t)
v=sin(s.t)
s.l+=20 if -16.<s.x+s.l*h<376. and -16.<s.y+s.l*v<528. else 0 if -48.<s.x+s.l*h<408. and -48.<s.y+s.l*v<560. else -20
if s.l<0:return
S=s.S
s.S=3+s.l-s.l%3
if s.S!=S:
s.V.resize(s.S)
for a in range(S,s.S,3):s.V.colors[a*3:a*3+9]=tuple((RGB if a&3 else BWB)[s.p])*3
for sd in s.d:
for a in range(0,s.l,3):
c=cos(a)+sd
ha=s.x+h*a-c*8.
va=s.y+v*a-c*8.
s.V.vertices[a*2:a*2+6]=ha+c*ritz(4),va+c*ritz(4),ha+c*ritz(4),va+c*ritz(4),ha+c*ritz(4),va+c*ritz(4)
return s.V.draw(GL_TRIANGLES)
class Laserd(Laser):
def __init__(s,z):
for a in "pxytdlVS":setattr(s,a,getattr(z,a))
def __call__(s):
s.l-=30
return s.l<0 or super(Laserd,s).__call__()
class LaserC:
die=lambda s:(s.l.die(),Fd.add(LaserCd(s.l)))
def __init__(s,p,x,y,d,v,D=None,a=0):
s.l=Laser(p,x,y,d)
s.p=RGB[p]
s.x=x
s.y=y
s.h=cos(d if D is None else D)*v
s.v=sin(d if D is None else D)*v
s.a=a
def __call__(s):
L=s.l
L.x=s.x=s.x+s.h
L.y=s.y=s.y+s.v
if s.a:L.t+=ptdir(L.t,atan2(Y0-s.y,X0-s.x),pi/s.a)
if not(-32<L.x<392 and -32<L.y<544):
L.die()
return send
a=near(L.x,L.y)
if a[0]<400 and a[2] and a[1] is N1 and L.p==N1.p:
Nut()
return s.die()
L()
s.draw(16.)
def draw(s,st):
ct=cos(T)*st
st*=sin(T)
a="v2f",(s.x+ct,s.y+st,s.x-st,s.y+ct,s.x-ct,s.y-st,s.x+st,s.y-ct)
glColor3ubv(s.p)
graphics.draw(4,GL_QUADS,a)
glColor3ubv(Wht)
glBegin(GL_LINE_LOOP)
for s in range(0,8,2):glVertex2f(*a[1][s:s+2])
glEnd()
class LaserCd(LaserC):
def __init__(s,z):
s.x=z.x
s.y=z.y
s.p=RGB[z.p]
s.r=16
def __call__(s):
s.r-=1
return not s.r or s.draw(s.r)
class Trail:
def __init__(s,*x):s.p,s.x,s.y,s.d,s.v,s.bt=x
def __call__(s):
s.x+=cos(s.d)*s.v
s.y+=sin(s.d)*s.v
s.d+=ptdir(s.d,atan2(Y0-s.y,X0-s.x),pi1F)
a=near(s.x,s.y)
if not T%s.bt:Bad([s.p,s.x,s.y,0.,0.])
return a[0]<666 and a[2] and not(a[1] is N1 and N1.p==s.p)
class Hunt:
def __init__(s,*x):s.p,s.x,s.y,s.d,s.v,s.bt=x
def __call__(s):
s.x+=cos(s.d)*s.v
s.y+=sin(s.d)*s.v
a,b=(N2,N1) if N1.p==s.p else (N1,N2)
s.d+=ptdir(s.d,(a is b)!=Z0 and atan2(a.y-s.y,a.x-s.x),pi1F)-(ptdir(s.d,atan2(b.y-s.y,b.x-s.x),pi18))
a=near(s.x,s.y)
if not T%s.bt:Bad([s.p,s.x,s.y,0.,0.])
return a[0]<400 and a[2] and not(a[1] is N1 and N1.p==s.p)
Fd=set()
udp=socket(AF_INET,SOCK_DGRAM)
udp.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
usend=udp.send
tcp=socket(AF_INET,SOCK_STREAM)
tcp.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
for a in (x for x in chain(("::2000","g","Kikiki","0","U0"),(open("K.txt") if isfile("K.txt") else ()),__import__("sys").argv[1:]) if x not in "#"):
if ":" in a:hopo=(a[:a.find(":")] or hopo[0] if a.find("::")==-1 else ""),int(a[a.rfind(":")+1:] or hopo[1])
elif a[0]=="U" and a[1:].isdigit():U=int(a[1:])
elif a in "brg":N1,N2=NN=(Node(0),Node(1))[::1-((ritz(a=="g") or a=="b")<<1)]
elif a.isdigit():hopo=hopo[0],int(a)
elif isfile(a) and len(a)<64:L=bytes(a)
elif "." in a:hopo=a,hopo[1]
if hopo[0]:
for udp in tcp,udp:udp.connect(hopo)
if U:udp.bind(("",U))
usend(b"")
while not select((tcp,),(),(),.3)[0]:usend(b"")
L=ord(tcp.recv(1))
if L>>7==N1.p:N1,N2=NN=N2,N1
L=tcp.recv(L&63)
tcp.send(b"%c"%isfile(L))
if not isfile(L):
send=__import__("zlib").decompress(tcp.recv(8192))
if (raw_input(send+b"\nExecute %s? yN "%L)[:1] or " ") in "yY":open(L,"w").write(send)
else:
for udp in tcp,udp:udp.bind(hopo)
tcp.listen(1)
tcp=tcp.accept()[0]
udp.connect(udp.recvfrom(0)[1])
tcp.send(b"%c%s"%(N1.p<<7|len(L),L))
if not ord(tcp.recv(1)):tcp.send(__import__("zlib").compress(open(L).read()))
while select((udp,),(),(),.1)[0]:udp.recv(0)
recv=tcp.recv
send=tcp.send
execfile(L)
def draw(t):
global T,Tl,Tlc,X0,Y0,Z0,hit,hit2,E,N,B,Bad,Nap,zT,SYNC
W.clear()
if Tl:
Tlc+=1
Tl=[a for a in Tl if a in FF.values()]
elif T-Tlc in Time:Time[T-Tlc]()
Back(T,1-Z0 if N1.p else Z0)
while select((tcp,),(),(),0)[0]:
try:r=ord(recv(1))
except:raise SystemExit
if r&0xF0==0xA0:
if r&8:
s=((Hi().y-Y0)**2+(Hi().x-X0)**2)**.5
if s:N+=[(Hi().x-X0)*12/s,(Hi().y-Y0)*12/s,64,X0,Y0],
getattr(FF.pop(r&7|ord(recv(1))<<3,None),"die",int)()
elif r&0xF0==0xC0:M[ord(recv(1))].add(r&15)
elif r==0xE3:
E=0
s=((Hi().y-Y0)**2+(Hi().x-X0)**2)**.5
if s:N+=[(Hi().x-X0)*12/s,(Hi().y-Y0)*12/s,64,X0,Y0],
elif r==0xEE:miss(False)
while select((udp,),(),(),0)[0]:
b,c,a,x,y=(ord(x) for x in udp.recv(5))
c|=b<<8
if c!=zT:
E+=(a>>6)*30
if c>zT:
zT=c
N2.awds=a&1,a>>1&1,a>>2&1,a>>3&1
N2.x=x|a<<4&256
N2.y=y|a<<3&256
if hit!=((180,256),(180,256)):hit=(N1.x,N1.y),(N2.x,N2.y)=tuple(((x+12 if x<164 else x-12 if x>196 else 180),(y+12 if y<240 else y-12 if y>272 else 256)) for x,y in hit)
else:
for s in NN:
a=4-(s.awds[2]!=s.awds[0] and s.awds[1]!=s.awds[3])
s.x=max(min(s.x+(s.awds[2]-s.awds[0])*a,352),8)
s.y=max(min(s.y+(s.awds[1]-s.awds[3])*a,504),8)
if hit2:
hit2-=1
if not hit2:Bad=B.append
s=N1.y-N2.y
if s:Z0=max(min(Z0+s/((N2.x-N1.x)**2+s**2)**.5,1.),0.)
X0=N1.x+(N2.x-N1.x)*Z0
Y0=N1.y-s*Z0
glBlendFunc(GL_SRC_ALPHA,1)
graphics.draw(2,GL_LINES,("v2f",(N1.x,N1.y,N2.x,N2.y)),("c3B",tuple(RGB[N1.p])+tuple(RGB[N2.p])))
for s in NN:
glColor3ubv(Blk)
glBegin(GL_TRIANGLE_FAN)
glVertex2i(s.x,s.y)
glColor3ubv(RGB[s.p])
for a in pi63v:glVertex2f(s.x+cos(a+T)*24.,s.y+sin(a+T)*24.)
glEnd()
glColor3ubv(Wht)
glBegin(GL_TRIANGLE_FAN)
glVertex2f(X0,Y0)
glColor4ub(0,0,0,0)
for a in pi63v:glVertex2f(X0+cos(a)*12.,Y0+sin(a)*12.)
glEnd()
s=[]
a=[]
for b in reversed(B):
r=b[0]
x=b[1]=b[1]+b[3]
y=b[2]=b[2]+b[4]
if not(-12.<x<372. and -12.<y<524.) or Z0 in (0.,1.) and r==NN[int(Z0)].p and (x-NN[int(Z0)].x)**2+(y-NN[int(Z0)].y)**2<400. and (Z0==1. or not atch()):B.remove(b)
elif Z0<=.5 and r!=N1.p and (x-X0)**2+(y-Y0)**2<49.:
miss()
break
else:
s+=tuple(RGB[r])*4+tuple(BWB[r])*4
a+=x+7.,y,x,y+7.,x-7.,y,x,y-7.,x+4.,y,x,y+4.,x-4.,y,x,y-4.
glBlendFunc(GL_SRC_ALPHA,0)
if a:graphics.draw(len(a)>>1,GL_QUADS,("v2f",a),("c3B",s))
for r,a in [x for x in ((x,y()) for x,y in FF.items()) if x[1]]:
del FF[r]
if a is not send:
send(b"%c%c"%(0xA0|Nap<<3|r&7,r>>3))
Nap=0
Fd.difference_update([r for r in Fd if r()])
r=0
s=[0,512,0,504,E,504,E,504,0,512,E,512]
for a in N:
a[2]+=1
if not a[2]&63:
r+=1
continue
a[3]+=a[0]
a[4]+=a[1]
for b in range(min(12,a[2]&63)):
x=a[3]-a[0]*b-6.
y=a[4]-a[1]*b-6.
s+=x+12*ritz(2),y+12*ritz(2),x+12*ritz(2),y+12*ritz(2),x+12*ritz(2),y+12*ritz(2)
del N[:r]
glBlendFunc(GL_ONE_MINUS_DST_COLOR,0)
glColor3ubv(Wht)
graphics.draw(len(s)>>1,GL_TRIANGLES,("v2f",s))
T+=1
W.flip()
clock.schedule_interval(draw,1/30)
W.set_visible(True)
recv(send(b" "))
app.run()