Bizarre, Evil, or Funny Code

Miscellaneous Forums/General Discussion/Bizarre, Evil, or Funny Code

If you've got any code you've written that for some bizarre reason made you laugh, grimace in disgust, or if it just looks plain weird, might as well post it here. Only if it's something you're comfortable posting, obviously, but anywho, these are my contributions:

Evil (It's actually not as painful to work with as it looks, but actually writing it was painful)
    Field filter%[][][]=[[[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]],..
                          [[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR],[GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR]]]


Bizarre/Bizarre
While in And Not out
    out = in.callback( name )
    in = in.pred
Wend
Return out


Off the top of my head:

If not fart then cameralook()


I also have several Monty Python references in some of my older, naffier projects.

Several of my Blitz3D programs have this sort of thing ("Evil"):
Global colorpulse#, dcolor# ;these should be static locals, but Blitz doesn't do static locals
Instead of going neatly inside the function, they had to completely break scope and go outside as main Globals. Having static locals is one of the many things that I love about BlitzMax (using the term "Global" for them within the function is fine: "a rose by any other name would smell as sweet").

Well that would basically describe ALL of my code...

I found this in some *old* B2D code...
Function shoot_card(ypos, fpos, bodge)
	If (Not bodge) Then Return True
	final = False
	fin_pos = 141 + (fpos * 89)
	;dist = 640 - fin_pos
	ang# = ((640 - fin_pos) / 180.0)
	inc = 14
	If ypos = 0
		distance = sint#((player_hand(2, fpos) - fin_pos) / ang) * (80.0 - (10 * fpos))
		rt = -cost#((player_hand(2, fpos) - fin_pos) / ang) * (30.0 - (2 * fpos))	
		update_points(distance, 1, rt, player_hand(2, fpos), 325)
		player_hand(2, fpos) = player_hand(2, fpos) - inc
		If player_hand(2, fpos) <= fin_pos
			player_hand(2, fpos) = fin_pos
			final = True
		EndIf
	ElseIf ypos = 1
		distance = sint#((comp_hand(2, fpos) - fin_pos) / ang) * (80.0 - (10 * fpos))
		rt = -cost#((comp_hand(2, fpos) - fin_pos) / ang) * (30.0 - (2 * fpos))
		update_points(distance, 1, rt, comp_hand(2, fpos), 91)
		comp_hand(2, fpos) = comp_hand(2, fpos) - inc
		If comp_hand(2, fpos) <= fin_pos
			comp_hand(2, fpos) = fin_pos
			final = True
		EndIf
	ElseIf ypos = 2
		distance = sint#((split_hand(2, fpos) - fin_pos) / ang) * (80.0 - (10 * fpos))
		rt = -cost#((split_hand(2, fpos) - fin_pos) / ang) * (30.0 - (2 * fpos))	
		update_points(distance, 1, rt, split_hand(2, fpos), 211)
		split_hand(2, fpos) = split_hand(2, fpos) - inc
		If split_hand(2, fpos) <= fin_pos
			split_hand(2, fpos) = fin_pos
			final = True
		EndIf
	EndIf
	Return final
End Function


here's a little snippet of some old Delphi 3D code I wrote, it's even got comments, not sure how useful they are ... check out the tranform procedure at the bottom hehe.

procedure TMain.FillPolyPoints;
var n: Integer;
begin
    {rotate Normal point}
    x := NX[DrawingPoly];
    y := NY[DrawingPoly];
    z := NZ[DrawingPoly];
    Transform;
    {get Normal Unit vector}
    NormU := round(Sqrt (tx*tx + ty*ty + tz*tz));
    If NormU = 0 then NormU := 1;
    nsx := tx/NormU;
    nsy := ty/NormU;
    nsz := tz/NormU;

    {rotate Line Of Sight point ... 1st point on polygon}
    Offset := E[EdgeOffset[DrawingPoly]];
    x := PX[Offset];
    y := PY[Offset];
    z := PZ[Offset];
    Transform;
    tz := tz + persp;
    {get Line Of Sight Unit vector}
    LOSU := round(Sqrt (tx*tx + ty*ty + tz*tz));
    If LOSU = 0 then LOSU := 1;
    lx := tx/LOSU;
    ly := ty/LOSU;
    lz := tz/LOSU;

  {calc illumination}
  ilum := lx*nsx + ly*nsy + lz*nsz;
  If ilum < 0 then
    DrawIt := True
  else
  begin
     DrawIt := False;
     exit;
  end;

  For n := 0 to DrawingPolyNoEdges do
  begin
    Offset := E[EdgeOffset[DrawingPoly]+n];

    //// starting point
    x := PX[Offset];
    y := PY[Offset];
    z := PZ[Offset];
    Transform;
    // calc perspective ratio
    denom := tz + persp;
    If denom = 0 then
     denom := 1;

    // plot screen point with perspective
    PolyPoints[n].X := CenterX + (tx * persp) div denom ;
    PolyPoints[n].Y := CenterY + (ty * persp) div denom;
  end;

end;

//***************************
procedure TMain.Transform;
begin
    //// x axis ////
    y1 := (y*c1 + z*-s1) div scale;
    z1 := (y*s1 + z*c1) div scale;
    //// y axis ////
    x2 := (x*c2 + z1*s2) div scale;
    tz := (x*-s2 + z1*c2) div scale;
    //// z axis ////
    tx := ( x2*c3 + y1*-s3) div scale;
    ty := ( x2*s3 + y1*c3) div scale;

    ////movement
    tz := tz + Zoom;
    tx := tx + offx;
    ty := ty + offy;
end;


here's another bit:

procedure TMain.CalcNormals;
var
  n : Integer;
  l1x, l1y, l1z: Integer;
  l2x, l2y, l2z: Integer;
begin
  For n := 0 to NumPolys do
  begin
    Offset := EdgeOffset[n];

    l1x := PX[E[Offset+1]] - PX[E[Offset+0]];
    l1y := PY[E[Offset+1]] - PY[E[Offset+0]];
    l1z := PZ[E[Offset+1]] - PZ[E[Offset+0]];
    l2x := PX[E[Offset+2]] - PX[E[Offset+1]];
    l2y := PY[E[Offset+2]] - PY[E[Offset+1]];
    l2z := PZ[E[Offset+2]] - PZ[E[Offset+1]];

    NX[n] := l1y*l2z - l1z*l2y;
    NY[n] := l1z*l2x - l1x*l2z;
    NZ[n] := l1x*l2y - l1y*l2x;
  end;
end;


It all worked well, it was a 3D screensaver.

Here's some pretty hard to follow heap handling code... it doesn't look so bad now, but was nightmare to debug...

Repeat
u = v
If 2*u+1 <= HEAPSIZE
If HEAPKEY(HEAPITEM(u)) >= HEAPKEY(HEAPITEM(2*u)) Then v = 2*u 
If HEAPKEY(HEAPITEM(v)) >= HEAPKEY(HEAPITEM(2*u+1)) Then v = 2*u+1
Else If 2*u <= HEAPSIZE 
If HEAPKEY(HEAPITEM(u)) >= HEAPKEY(HEAPITEM(2*u))Then v = 2*u
End If

If u <> v 
temp = HEAPITEM(u)
HEAPITEM(u) = HEAPITEM(v)
HEAPITEM(v) = temp
Else
Exit 
End If

Forever


And some evil mesh generation code...

	For s=1 To DL_SECTIONS(h)

	;GET VECTOR & NORMALIZE
	If s<DL_SECTIONS(h)
	vx# = DL_X(h,s) - DL_X(h,s+1)
	vy# = DL_Y(h,s) - DL_Y(h,s+1)
	vz# = 0;DL_Z(h,s) - DL_Z(h,s+1)
	hyp# = Sqr(vx*vx + vy*vy + vz*vz)
	nvx# = (-vy / hyp) * DL_WIDTH(h)
	nvy# = (vx / hyp) * DL_WIDTH(h)
	nvz# = 0;(vz / hyp) * DL_WIDTH(h)
	End If

	nx# = DL_X(h,s)
	ny# = DL_Y(h,s)
	nz# = DL_Z(h,s)

	;BUILD VERTICES & ASSIGN UV COORDS
	v#=(1.0/DL_SECTIONS(h)) * s

	verts=verts+1
	VERT[verts] = AddVertex(DL_SURF,nx+nvx,ny+nvy,nz+nvz,0,v)
	DL_VERT1(h,s) = VERT[verts]

	VertexColor(DL_SURF,VERT[verts],red/diver,(cslide*s)/diver,(cslide*s)/diver,0.8-(alphad*s))

	verts=verts+1
	VERT[verts] = AddVertex(DL_SURF,nx-nvx,ny-nvy,nz-nvz,1,v)
	DL_VERT2(h,s) = VERT[verts]

	VertexColor(DL_SURF,VERT[verts],red/diver,(cslide*s)/diver,(cslide*s)/diver,0.8-(alphad*s))

	DL_VERTNX(h,s) = nvx/DL_WIDTH(h)
	DL_VERTNY(h,s) = nvy/DL_WIDTH(h)
	Next


how about, anything in C/C++/C#/Java

HO HO HO HO!

If game_state% = $DEADBEEF Then End


Actually, I've never used that, but I might. :P

Sorry to drag this up again, but I've wrote a beauty statement, which has just given me the chuckles:-

Local fart = loadfart("fart.wav")


Never realised until Blitz threw up an error! :D

Anyway, I thought it was funny!

Dabz

Here's a game in 149 lines written some time ago for Blitz Coder:
Const SCREEN_WIDTH=800,SCREEN_HEIGHT=600,BRAIN_HUMAN=2,BRAIN_CPU=1,MAX_SPEED#=2.5,ACCELERATION#=0.03,STEER#=9,BULLET_SPEED#=2.5,ENEMY_COUNT=20,LAUNCHTIME=200
Graphics(SCREEN_WIDTH,SCREEN_HEIGHT)
SetBuffer(BackBuffer())                    ; /---------- CENTAURI 149 -----------\
SeedRnd(MilliSecs())                       ; |        (C)2003 FOPPYGAMES         |
Global g_gfx_title=LoadImage("Graphics\title.png"),g_gfx_arena=LoadImage("Graphics\toplayer.png"),g_gfx_collisionmap=LoadImage("Graphics\blueprint.png"),g_gfx_egg=LoadImage("Graphics\egg.png"),g_gfx_rocket=LoadImage("Graphics\rocket.png"),g_gfx_ship=LoadAnimImage("Graphics\rotatedship.png",16,16,0,361),g_timer=CreateTimer(60),g_player.FLYER,g_snd_shoot=LoadSound("Sounds\shoot.wav"),g_snd_boom=LoadSound("Sounds\deactivate.wav"),g_snd_explosion=LoadSound("Sounds\explosion.wav"),g_snd_playerBoom=LoadSound("Sounds\playerBoom.wav"),g_score#,g_hiScore
LoopSound(g_snd_explosion)                 ; |                                   |
Type FLYER 
	Field f_owner,f_launching,f_x#,f_y#,f_angle#,f_speed# ;                      |
End Type                                   ; |                                   |
Function FLYER_create.FLYER(p_owner,p_x#,p_y#) 
	flyer.FLYER       = New FLYER          ; |                                   |
	flyer\f_launching = LAUNCHTIME         ; | how to score points:              |
	flyer\f_owner     = p_owner            ; | - fly at higher speeds            |
	flyer\f_x         = p_x                ; | - shoot enemies (10 pts each)     |
	flyer\f_y         = p_y                ; |   (note: each shot costs 1 point) |
	Return(flyer)                          ; \-----------------------------------/
End Function
Function FLYER_controlCPU(p_flyer.FLYER) 
	targetAngle = ATan2(g_player\f_y+Rand(-10,10)-p_flyer\f_y,g_player\f_x+Rand(-10,10)-p_flyer\f_x)
	If (targetAngle < 0) Then targetAngle = targetAngle + 360
	normal = targetAngle - p_flyer\f_angle
	If (normal < 0) Then normal = normal + 360
	If ((normal < 358) And (normal > 180)) Then p_flyer\f_angle = p_flyer\f_angle - STEER/(p_flyer\f_speed+2.0)
	If ((normal > 2) And (normal <= 180)) Then p_flyer\f_angle = p_flyer\f_angle + STEER/(p_flyer\f_speed+2.0)
	p_flyer\f_angle = p_flyer\f_angle Mod 360 + (p_flyer\f_angle<0)*360
	If ((normal > 350) Or (normal < 10)) Then
		If (p_flyer\f_speed < MAX_SPEED) Then p_flyer\f_speed = p_flyer\f_speed + ACCELERATION
		If (Rand(1000)>998) Then BULLET_create(p_flyer)
	Else
		p_flyer\f_speed = p_flyer\f_speed * 0.98
	End If
End Function
Function FLYER_controlHuman(p_flyer.FLYER) 
	If (KeyDown(203)) Then steering# = -STEER/(p_flyer\f_speed+2.0)
	If (KeyDown(205)) Then steering# =  STEER/(p_flyer\f_speed+2.0)
	p_flyer\f_angle = p_flyer\f_angle + steering
	p_flyer\f_angle = p_flyer\f_angle Mod 360 + (p_flyer\f_angle<0)*360
	If ((KeyDown(200)) And (p_flyer\f_speed < MAX_SPEED)) Then p_flyer\f_speed = p_flyer\f_speed + ACCELERATION
	If (KeyDown(208)) Then p_flyer\f_speed = p_flyer\f_speed * 0.98
	If (KeyHit(57)) Then BULLET_create(p_flyer)
End Function
Function FLYER_recreate(p_flyer.FLYER) 
	p_flyer\f_launching = LAUNCHTIME
	p_flyer\f_x         = 10+Rand(SCREEN_WIDTH-20)
	p_flyer\f_y         = 10+Rand(SCREEN_HEIGHT-20)
End Function
Type BULLET 
	Field f_owner,f_x,f_y,f_angle#
End Type
Function BULLET_create(p_owner.FLYER) 
	bullet.BULLET  = New BULLET
	bullet\f_x     = p_owner\f_x + 7
	bullet\f_y     = p_owner\f_y + 7 
	bullet\f_angle = p_owner\f_angle
	bullet\f_owner = p_owner\f_owner
	If ((p_owner\f_owner = BRAIN_HUMAN) And (g_score > 1)) Then g_score = g_score - 1
	PlaySound(g_snd_shoot)
End Function
Repeat 
	FlushKeys()
	DrawBlock(g_gfx_arena,0,0)
	DrawImage(g_gfx_title,SCREEN_WIDTH/2-239,SCREEN_HEIGHT/3-136)
	Text(230,500,"hi-score: "+Str(g_hiScore))
	Flip
	Repeat
		If KeyHit(57) Then Exit
		If KeyHit(1) Then End
	Forever
	g_player.FLYER=FLYER_create(BRAIN_HUMAN,215,224)
	For i = 1 To ENEMY_COUNT
		FLYER_create(BRAIN_CPU,10+Rand(SCREEN_WIDTH-20),10+Rand(SCREEN_HEIGHT-20))
	Next
	FlushKeys()
	continue = True
	explosionChannel = PlaySound(g_snd_explosion)
	g_score = 0
	Color(255,255,255)
	While(((Not(KeyHit(1))) And (continue)))
		For flyer.FLYER = Each FLYER 
		    flyer\f_launching = flyer\f_launching + (flyer\f_launching=0) - 1
			If (flyer\f_launching = 0) Then
				If (flyer\f_owner = BRAIN_CPU) Then
					FLYER_controlCPU(flyer)
				Else
					FLYER_controlHuman(flyer)
				End If
				flyer\f_x = flyer\f_x + Cos(flyer\f_angle)*flyer\f_speed
				flyer\f_y = flyer\f_y + Sin(flyer\f_angle)*flyer\f_speed
				If (ImagesCollide(g_gfx_ship,flyer\f_x,flyer\f_y,flyer\f_angle,g_gfx_collisionmap,0,0,0)) Then
					If (flyer\f_owner = BRAIN_CPU) Then
						FLYER_recreate(flyer)
					Else 
						continue = False
					End If
				Else If (flyer\f_owner = BRAIN_CPU) Then
					If (ImagesCollide(g_gfx_ship,flyer\f_x,flyer\f_y,flyer\f_angle,g_gfx_ship,g_player\f_x,g_player\f_y,g_player\f_angle)) Then continue = False
				End If
			Else If (ImagesCollide(g_gfx_egg,flyer\f_x,flyer\f_y,0,g_gfx_collisionmap,0,0,0)) Then
				FLYER_recreate(flyer)
		    End If
		Next
		For bullet.BULLET = Each BULLET 
			bullet\f_x = bullet\f_x + Cos(bullet\f_angle)*BULLET_SPEED*bullet\f_owner
			bullet\f_y = bullet\f_y + Sin(bullet\f_angle)*BULLET_SPEED*bullet\f_owner
			If (ImagesCollide(g_gfx_rocket,bullet\f_x,bullet\f_y,0,g_gfx_collisionmap,0,0,0)) Then
				Delete(bullet)
			Else
				For flyer.FLYER = Each FLYER
					If (ImagesCollide(g_gfx_rocket,bullet\f_x,bullet\f_y,0,g_gfx_ship,flyer\f_x,flyer\f_y,flyer\f_angle)) Then
						If (flyer\f_owner <> bullet\f_owner) Then
							PlaySound(g_snd_boom)
							Delete(bullet)
							If (flyer\f_owner = BRAIN_CPU) Then
								FLYER_recreate(flyer)
								g_score = g_score + 10
							Else
								continue = False
							End If
							Exit
						End If
					End If
				Next
			End If
		Next
		WaitTimer(g_timer)
		DrawBlock(g_gfx_arena,0,0)
		For flyer.FLYER = Each FLYER 
			If (Not(flyer\f_launching)) Then
				DrawImage(g_gfx_ship,flyer\f_x,flyer\f_y,flyer\f_angle)
			Else If (flyer\f_launching < LAUNCHTIME-1)
				random = (LAUNCHTIME - flyer\f_launching) / 100
				DrawImage(g_gfx_egg,flyer\f_x+Rand(-random,random),flyer\f_y+Rand(-random,random))
			End If
		Next
		For bullet.BULLET = Each BULLET
			DrawBlock(g_gfx_rocket,bullet\f_x,bullet\f_y)
		Next
		g_score = g_score + (g_player\f_speed)/100.0
		Text(10,10,Str(Int(g_score)))
		Flip
    Wend
	PlaySound(g_snd_playerBoom)
	StopChannel(explosionChannel)
	If (g_score > g_hiScore) Then g_hiScore = g_score
	Delay(1000)
	Delete Each FLYER
	Delete Each BULLET
Forever
End

In fact you can put more commands on a single line but there were some rules.

I also have the following "colorful snake effect" (this requires no media):

; colorful snake effect...

Const SCREEN_WIDTH       = 640
Const SCREEN_HEIGHT      = 480

Const SCREEN_WIDTH_HALF  = SCREEN_WIDTH  / 2
Const SCREEN_HEIGHT_HALF = SCREEN_HEIGHT / 2

Graphics(SCREEN_WIDTH,SCREEN_HEIGHT,32,1)
SetBuffer(BackBuffer())
SeedRnd(MilliSecs())

Global lastColor = 512 - 1

; create a palette of 512 colors
Dim g_colors(lastColor,2)
index = 0
For red = 0 To 7
	For green = 0 To 7
		For blue = 0 To 7
			g_colors(index,0) = Floor(red   * (8.0/7.0) * 31.875)
			g_colors(index,1) = Floor(green * (8.0/7.0) * 31.875)
			g_colors(index,2) = Floor(blue  * (8.0/7.0) * 31.875)
			index = index + 1
		Next
	Next
Next

; do the snake effect
break = False
While (Not(KeyHit(1)))
	For c = 0 To 360 Step 6
		Cls
		For index = 0 To lastColor
			Color(g_colors(index,0),g_colors(index,1),g_colors(index,2))
			Line(SCREEN_WIDTH_HALF-lastColor/2+index-Int(Cos(c)*6*Rnd(-4,4)),SCREEN_HEIGHT_HALF+Sin(index+c)*20-Int(Sin(c)*6*Rnd(0,1)),SCREEN_WIDTH_HALF-lastColor/2+index+Int(Cos(c)*6*Rnd(-4,4)),SCREEN_HEIGHT_HALF+Sin(index+c)*10+Int(Sin(c)*6*Rnd(0,1)))
		Next
		Flip
	Next
Wend

End

The evil part is the Line command, I think I just kept adding things at random until I got an effect. (And the magical number 31.875 is also interesting.)

Blitz threw up an error! :D

Of course it did. It clearly should be:
Local fart:<b>Tfart</b> = loadfart("fart.wav")
;)

here's a snippet of some evil code that should work on any noob.

for x = 0 to millisecs()
writepixelfast x,0,rand(),rand(),rand()
next


here's a snippet of some evil code that should work on any noob.
for x = 0 to millisecs()
writepixelfast x,0,rand(),rand(),rand()
next



Understanding that it's supposed to be a joke and all, but it wont 'always' run for some people.....

For those that keep their pc on for lengthy amounts of time, the millisec will flip (go from 4294967295 to -4294967295 ) and thus your For X = 0 to -<whatever> wont work.