Code archives/Miscellaneous/Cellular Model

This code has been declared by its author to be Public Domain code.

Download source code

Cellular Model by superStruct2009
This is a program that was originally designed for my biology teacher. It creates a specified number of cells, each a different species, and goes on to run its life based on normal cell criteria. It looks at the age of the cell, the number of children it can have and how often. To control the population, there is a random event that kills a number of cells based upon their strength and luck. REMEMBER! When putting in a number of farms, the number must be a perfect square.
Graphics3D 600,612,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
AppTitle "CELLULAR MODEL"

.init

Include "lib.bb"

Color 255,255,255

farm_width = width/Sqr(farms)
farm_height = height/Sqr(farms)

Locate 0,0

farms = Input("NUMBER OF FARMS: ")
num = Input("NUMBER OF CELLS: ")

If num > 35000
	RuntimeError "MAX NUMBER OF CELLS EXCEEDED"
EndIf

MultiCell(num)

farm.farm = New farm
farm\x = 0
farm\y = 12
farm\supply = 100
farm\regen_r = 50
farm\regen = 10
farm\width = width/Sqr(farms)
farm\height = height/Sqr(farms)

y# = 12

For i = 1 To farms - 1
	farm.farm = New farm
	If x# >= width - width/Ceil(Sqr(farms))
		y# = y# + height/Ceil(Sqr(farms))
		x# = 0
	Else
		x# = x# + width/Ceil(Sqr(farms))
	EndIf
	farm\x = x
	farm\y = y
	farm\supply = 100
	farm\regen_r = 50
	farm\regen = 10
	farm\width = width/Sqr(farms)
	farm\height = height/Sqr(farms)
Next

Cls

While Not KeyDown(1)
	Cls
	
	Text 0,0,"GENERATION: " + generation
	
	If KeyHit(88)
		If shown = True
			shown = False
		ElseIf shown = False
			shown = True
		EndIf
	EndIf
	
	If KeyDown(19)
		For cell.cell = Each cell
			Delete cell
		Next
		Goto init
	EndIf
	
	population = 0
	food = 0
	
	For cell.cell = Each cell
		For farm.farm = Each farm
			If RectsOverlap(farm\x,farm\y,farm\width,farm\height,cell\x,cell\y,10,10)
				If cell\hunger > 0 And farm\supply > 0
					farm\supply = farm\supply - 1
					cell\hunger = cell\hunger - 1
				EndIf
			EndIf
		Next
		
		If generation = event_next
			event = 1
			If cell\luck > event_power Or cell\strength > event_power
				cell\live = 1
			Else
				cell\live = 0
			EndIf
		EndIf
		
		temp_x = cell\x
		temp_y = cell\y			
		
		cell\age = generation - cell\birth
		cell\counter = cell\counter + 1
		cell\hun_cn = cell\hun_cn + 1
		
		If cell\hun_cn - cell\hunger_r = 0
			cell\hun_cn = 0
			cell\hunger = cell\hunger + 1
		EndIf
				
		If cell\age = cell\longevity
			cell\live = 0
			cells(cell\x/10,cell\y/10) = 0
		EndIf
		
		If cell\hunger > 10
			cell\check = cell\check + 1
			If cell\check > 500
				cell\live = 0
			EndIf
		EndIf			
		
		If cell\counter - cell\reproduction = 0 And cell\live = 1 And cell\hunger = 0
			cell\counter = 0
			temp_x = cell\x
			temp_y = cell\y
			temp_lon = cell\longevity
			temp_rep = cell\reproduction
			temp_r = cell\r
			temp_g = cell\g
			temp_b = cell\b
			temp_mutation_r = cell\mutation_r
			temp_strength = cell\strength
			temp_hun_r = cell\hunger_r
			For i = 1 To cell\child
				If cell\mutation = Rand(1,cell\mutation_r)
					Mutation(temp_x,temp_y,temp_lon,temp_rep,temp_mutation_r,temp_strength,temp_hun_r)
				Else
					NewCell(temp_x,temp_y,temp_r,temp_g,temp_b,temp_lon,temp_rep,temp_mutation_r,temp_strength,temp_hun_r)
				EndIf
			Next
		EndIf
		
		If cell\live = 1
			If cell\x + 10 > width
				cell\x = width - 10
			ElseIf cell\x < 0
				cell\x = 0
			EndIf
			If cell\y + 10 > height
				cell\y = height - 10
			ElseIf cell\y < 12
				cell\y = 12
			EndIf
			
			population = population + 1
			
			Color cell\r,cell\g,cell\b
			Oval cell\x,cell\y,4,4,0
		Else
			Delete cell
		EndIf
	Next
	
	If event = 1
		event = 0
		event_next = Rand(2000,10000) + generation
		event_power = Rand(-3,10)
	EndIf
	
	generation = generation + 1
	
	Color 255,255,255
	Rect 0,12,height,width,0
	
	For farm.farm = Each farm
		If shown = True And farm\supply > 0
			Rect farm\x,farm\y,farm\width,farm\height,0
		EndIf
		If (generation Mod farm\regen) = 0 And farm\supply + farm\regen< 100
			farm\supply = farm\supply + farm\regen
		EndIf
		food = food + farm\supply
	Next
	
	Flip
Wend

Comments

Nate the Great2009
cool, I dont get exactly what its doing but it does look interesting....


Guy Fawkes2009
think of it as the inside of a body, dude. its like the "nanobots" of our body. it works like our cells do.


superStruct2009
It's actually meant to replicate the basis of how cells live. Evolution and natural selection.


superStruct2009
Here is the library file you will undoubtedly need.
Global generation = 0,population = 0
Global event,event_counter,event_next,event_power
Global mouse_btn = False

Global height# = 600
Global width# = 600
Global farm_height
Global farm_width
Global regen = 1000
Global food

Global area = height*width

Global farms

Type cell
	Field x,y
	Field age,birth,longevity,live
	Field reproduction,child,counter
	Field r,g,b
	Field hunger,check,hunger_r,hun_cn
	Field mutation,mutation_r
	Field luck,strength
	Field species$
End Type

Type farm
	Field x,y
	Field height,width
	Field supply
	Field regen,regen_r
End Type

Dim cells(800/10,(GraphicsHeight()/10))

event_next = Rand(2000,10000)
event_power = Rand(-3,10)

Function Mutation(temp_x,temp_y,temp_lon,temp_rep,temp_mutation_r,temp_hun_r,temp_strength)	
	temp_x2 = temp_x + 10*Rand(-1,1)
	temp_y2 = temp_y + 10*Rand(-1,1)
	If temp_x2 < 0
		temp_x2 = 0
	ElseIf temp_x2 > 800 - 10
		temp_x2 = 800 - 10
	EndIf
	If temp_y2 < 12
		temp_y2 = 12
	ElseIf temp_y2 > GraphicsHeight() - 10
		temp_y2 = GraphicsHeight() - 10
	EndIf
	If cells(temp_x2/10,temp_y2/10) = 0
		cells(temp_x2/10,temp_y2/10) = 1
		gene_1 = Rand(0,1)
		gene_2 = Rand(0,1)
		gene_3 = Rand(0,1)
		gene_4 = Rand(0,1)
		gene_5 = Rand(0,1)
		cell.cell = New cell
		cell\x = temp_x2
		cell\y = temp_y2
		cell\live = 1
		cell\r = Rand(100,255)
		cell\g = Rand(100,255)
		cell\b = Rand(100,255)
		cell\birth = generation
		cell\luck = Rand(0,10)
		cell\child = Rand(0,9)
		cell\mutation_r = temp_mutation_r
		cell\longevity = temp_lone
		cell\strength = temp_strength
		cell\reproduction = temp_rep
		cell\hunger_r = temp_hun_r
		If gene_1 = 1
			cell\mutation_r = Rand(1,1000)
		EndIf
		If gene_2 = 1
			cell\longevity = Rand(50,500)
		EndIf
		If gene_3 = 1
			cell\reproduction = Rand(150,500)
		EndIf
		If gene_4 = 1
			cell\strength = Rand(0,10)
		EndIf
		If gene_5 = 1
			cell\hunger_r = Rand(25,50)
		EndIf
		cell\mutation = Rand(10,cell\mutation_r)
	EndIf
End Function
	
Function NewCell(temp_x,temp_y,temp_r,temp_g,temp_b,temp_lon,temp_rep,temp_mutation_r,temp_strength,temp_hun_r)
	temp_x2 = temp_x + 10*Rand(-1,1)
	temp_y2 = temp_y + 10*Rand(-1,1)
	If temp_x2 < 0
		temp_x2 = temp_x2*-1
	ElseIf temp_x2 > width - 10
		temp_x2 = width - 10
	EndIf
	If temp_y2 < 12
		temp_y2 = 12
	ElseIf temp_y2 > height - 10
		temp_y2 = height - 10
	EndIf
	If cells((temp_x2)/10,(temp_y2)/10) = 0	
		cells(temp_x2/10,temp_y2/10) = 1
		cell.cell = New cell
		cell\live = 1
		cell\luck = Rand(0,10)
		cell\r = temp_r
		cell\g = temp_g
		cell\b = temp_b
		cell\x = temp_x + 10*Rand(-1,1)
		cell\y = temp_y + 10*Rand(-1,1)
		cell\birth = generation
		cell\hunger_r = Rand(50,75)
		cell\longevity = temp_lon
		cell\reproduction = temp_rep
		cell\child = Rand(0,9)
		cell\mutation_r = temp_mutation_r
		cell\mutation = Rand(1,cell\mutation_r)
		cell\strength = temp_strength
	EndIf
End Function

Function MultiCell(num)
	For i = 1 To num
		cell.cell = New cell
		Repeat
			Repeat
				temp_x = Rand(800)
			Until (temp_x Mod 10) = 0
			Repeat
				temp_y = Rand(GraphicsHeight())
			Until (temp_y Mod 10) = 0 And temp_y > 20
		Until cells((temp_x)/10,(temp_y)/10) = 0
		cell\y = temp_y
		cell\x = temp_x
		cell\age = 0
		cell\luck = Rand(0,10)
		cell\strength = Rand(0,10)
		cell\r = Rand(100,255)
		cell\g = Rand(100,255)
		cell\b = Rand(100,255)
		cell\birth = 0
		cell\longevity = Rand(50,500)
		cell\live = 1
		cell\hunger_r = Rand(25,50)
		cell\reproduction = Rand(150,500)
		cell\child = Rand(0,9)
		cell\mutation_r = Rand(1,1000)
		cell\mutation = Rand(1,cell\mutation_r)
	Next
End Function



Code Archives Forum