Electronics construction

BlitzPlus Forums/BlitzPlus Programming/Electronics construction

Surely some of you here have heard of Wiremod: http://www.wiremod.com/ , an addon for Garry's Mod: http://www.steampowered.com/v/index.php?area=game&AppId=4000 . Well, I purchased the game for interest in wiremod, but my computer is rubbish and I'm currently waiting because I can't stand playing the game without the helpful graphics and tips (which it doesn't show because of low virtual memory. Planning on getting another hard drive soon to fix that) So I decided to write my own program that takes advantage of Logic gates, buttons, and such. It works nicely and I'm hoping to soon implement a graphical editor and a save function, which will have a format like a script so you can edit the area both ways.

Here's the source code. You'll have to change the code to make new circuts and designs, but here it is.
Graphics 640,480,0,2
SetBuffer BackBuffer()

Global popup=CreateImage(300,200)
Global wires=CreateImage(640,480)

Global id=1
Type e
	Field x,y
	Field id,in,in2,out
	Field kind$,desc$
End Type

Function NewE(kind$,desc$,x,y,in=0,out=0,in2=0)
	e.e=New e
	e\kind=kind
	e\x=x:e\y=y
	e\in=in:e\in2=in2
	e\out=out
	e\id=id:id=id+1
	e\desc=desc
	Return e\id
End Function

;;; LIST OF AVAILABE COMPONENTS ;;;
;
;  LED - lights up when it receives true. Doesn't for false.
;note: LED has an output- it will output the same as its input.
;  CONST - stores a constant value and outputs it.
;  LOGIC_NOT - Reverses the input it receives.
;  LOGIC_AND - Only outputs true if both inputs are true.
;  BUTTON_TOGGLE - Click to toggle output.
;note: button will be light gray if on, dark if off.
;  LOGIC_OR - if either input is true then it wil output true.
;  LOGIC_XOR - preforms an exclusive or logic function on its 2 inputs
;  LABEL - Shows some text and nothing more.
;note: use the desc, or description paremeter, to set the shown text.
;  VALUE_LABEL - Shows the value of its input.
;note: This will have an output that is the same as its input.
;  MATH_ADD - Adds two numbers together and outputs the result.
;  MATH_SUBTRACT - Subtracts input 2 from input 1.
;  MATH_MULTIPLY - multiplies its two inputs together.
;  MATH_DIVIDE - divides input 1 by input 2.
;note: if you are attempting to divide by 0, the output will be 0.
;  UPDOWN_INPUT - This is a box with an up and down arrow. Click them to alter the output value.
;  MATH_ABS - Outputs the absolute value of its input.

xorlab=NewE("LABEL","XOR",100,20)
const1=NewE("BUTTON_TOGGLE","val 1",30,30,0)
const2=NewE("BUTTON_TOGGLE","val 2",30,100,0)
andgate=NewE("LOGIC_XOR","XOR logic gate",70,60,const1,0,const2)
result=NewE("LED","Show result",120,60,andgate)

addlab=NewE("LABEL","Addition",100,180)
add1=NewE("UPDOWN_INPUT","val 1",20,200)
add2=NewE("UPDOWN_INPUT","val 2",20,260)
add12=NewE("VALUE_LABEL","val 1",60,210,add1)
addsign=NewE("LABEL","+",60,230)
add22=NewE("VALUE_LABEL","val 2",60,250,add2)
addit=NewE("MATH_ADD","add val 1 + val 2",100,230,add12,0,add22)
sumofadd=NewE("VALUE_LABEL","sum of numbers",150,230,addit)



Global mouse,clearwires,drawpopup
Repeat
Cls

	DrawImage wires,0,0:clearwires=1

	SetBuffer ImageBuffer(popup)
		Cls
	SetBuffer BackBuffer()
	
	If MouseHit(1) mouse=1 Else mouse=0
	
	For e.e=Each e
		nowin=0
		nowin2=0
	
		If e\in>0
			SetBuffer ImageBuffer(wires)
			If clearwires Cls:clearwires=0
			For o.e=Each e
				If o\id=e\in
					Color 128,128,128
					Line e\x,e\y,o\x,o\y
					nowin=o\out
				EndIf
				If o\id=e\in2
					Color 128,128,128
					Line e\x,e\y,o\x,o\y
					nowin2=o\out
				EndIf
			Next
			SetBuffer BackBuffer()
		EndIf
	
		Select Upper(e\kind)
			Case "LED"
				If nowin>0
					Color 255,10,10
				Else
					Color 90,10,10
				EndIf
				Oval e\x-8,e\y-8,16,16
				e\out=nowin
			Case "CONST"
				Color 2,90,86
				Rect e\x-8,e\y-12,16,24
				Color 255,255,255
				Text e\x,e\y,"#",1,1
			Case "LOGIC_NOT"
				Color 250,130,20
				Rect e\x-4,e\y-4,8,8
				If nowin=>1 e\out=1
				If nowing=<0 e\out=0
			Case "LOGIC_AND"
				Color 20,180,30
				Oval e\x-6,e\y-6,12,12
				If nowin>0 And nowin2>0 e\out=Max(nowin,nowin2) Else e\out=0
			Case "BUTTON_TOGGLE"
				If e\out Color 220,220,220 Else Color 160,160,160
				Rect e\x-20,e\y-20,40,40
				If RectsOverlap(MouseX(),MouseY(),1,1,e\x-20,e\y-20,40,40)
					If mouse e\out=Abs(e\out-1)
				EndIf
			Case "LOGIC_OR"
				Color 20,50,250
				Oval e\x-7,e\y-7,14,14
				If nowin Or nowin2 Then e\out=1 Else e\out=0
			Case "LOGIC_XOR"
				Color 170,70,50
				Rect e\x-7,e\y-7,14,14
				If (nowin=>1 And nowin2=<0) Or (nowin2=>1 And nowin=<0) Then e\out=1 Else e\out=0
			Case "LABEL"
				Color 255,255,255
				Rect e\x-StringWidth(e\desc)/2-5,e\y-10,StringWidth(e\desc)+10,20
				Color 0,0,0
				Text e\x,e\y,e\desc,1,1
			Case "VALUE_LABEL"
				Color 255,255,255
				Rect e\x-20,e\y-10,40,20
				Color 0,0,0
				Text e\x,e\y,nowin,1,1
				e\out=nowin	
			Case "MATH_ADD"	
				Color 240,150,150
				Rect e\x-3,e\y-8,6,16
				Rect e\x-8,e\y-3,16,6
				e\out=nowin+nowin2
			Case "MATH_SUBTRACT"	
				Color 100,150,250
				Rect e\x-8,e\y-3,16,6
				e\out=nowin-nowin2
			Case "MATH_MULTIPLY"	
				Color 200,200,50
				Text e\x,e\y,"X",1,1
				e\out=nowin*nowin2
			Case "MATH_DIVIDE"	
				Color 100,100,160
				Rect e\x-8,e\y-2,16,4
				Oval e\x-2,e\y-8,4,4
				Oval e\x-2,e\y+4,4,4
				If nowin2>0 e\out=nowin/nowin2 Else e\out="0"
			Case "UPDOWN_INPUT"
				Color 190,190,190
				Rect e\x-12,e\y-12,24,24
				Color 70,70,70
				Line e\x-12,e\y,e\x+11,e\y
				Line e\x-12,e\y-1,e\x+11,e\y-1
				Line e\x-10,e\y-5,e\x,e\y-10
				Line e\x+9,e\y-5,e\x-1,e\y-10
				Line e\x-10,e\y-5,e\x+9,e\y-5
				Line e\x-10,e\y+4,e\x,e\y+9
				Line e\x+9,e\y+4,e\x-1,e\y+9
				Line e\x-10,e\y+4,e\x+9,e\y+4
				If mouse
					Color 160,160,160
					If RectsOverlap(MouseX(),MouseY(),1,1,e\x-12,e\y-12,24,12)
						e\out=e\out+1
						Rect e\x-12,e\y-12,24,12
					ElseIf RectsOverlap(MouseX(),MouseY(),1,1,e\x-12,e\y,24,12)
						e\out=e\out-1
						Rect e\x-12,e\y,24,12
					EndIf 
				EndIf
			Case "MATH_ABS"
				Color 130,160,200
				Oval e\x-6,e\y-6,12,12
				e\out=Abs(nowin)
		End Select
		
		If RectsOverlap(MouseX(),MouseY(),1,1,e\x-5,e\y-5,10,10)
			SetBuffer ImageBuffer(popup)
			Cls
			Color 255,255,255
			Rect 4,4,280,114
			xx=12:yy=12
			Color 10,10,10
			Text xx,yy,"Component type: "+e\kind
			Text xx,yy+12,"Description: "+e\desc
			Text xx,yy+24,"Component ID: "+e\id
			Text xx,yy+36,"Input 1 ID: "+e\in
			Text xx,yy+48,"Input 2 ID: "+e\in2
			Text xx,yy+60,"Input 1: "+nowin
			Text xx,yy+72,"Input 2: "+nowin2
			Text xx,yy+84,"Outputting: "+e\out
			drawpopup=1
			SetBuffer BackBuffer()
		EndIf
		
	Next
	
	If drawpopup DrawImage popup,MouseX(),MouseY()
	drawpopup=0
	
Flip
If KeyDown(1) End
If Not MouseDown(1) FlushMouse
Forever



Function Max(i0,i1)
	If i0>i1 Return i0
	If i1>i0 Return t1
	Return i0
End Function
Function Min(i0,i1)
	If i0<i1 Return i0
	If i1<i0 Return t1
	Return i0
End Function


If you can come up with some more components and concepts, that'd be great. Mouse over the center of an object to see how it's doing, and left click buttons to use them. I am not aware of any bugs. Enjoy.

Hmm, pretty interesting... You might like to refine this into a quirky, interesting "tool" game for building virtual circuits. In fact, develop it into an "edutainment" game and you might make a few bucks off it.

One thing though: when you want to show off your stuff, you should really put it in the Blitz Showcase on this site, not the B+ forums. Cheers!