[offtopic] Artificial Life (Simulation & Code)

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: [offtopic] Artificial Life (Simulation & Code)

Post by BasicCoder2 »

Thank you for the post I found it very fascinating.
Last edited by BasicCoder2 on Aug 28, 2022 22:37, edited 1 time in total.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: [offtopic] Artificial Life (Simulation & Code)

Post by grindstone »

Fascinating, indeed!
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: [offtopic] Artificial Life (Simulation & Code)

Post by angros47 »

FreeBasic port:

Code: Select all

const ScreenWidth=800
const ScreenHeight=600
const AtomSize=2
#define Random_(mn, mx) rnd*(mx-mn)+mn

type atom
	as single x, y, vx, vy
	c as long
end type

sub Populate(group() as atom, c as long)
	for i as integer=0 to ubound(group)
		group(i).x=Random_(50, ScreenWidth-50)
		group(i).y=Random_(50, ScreenHeight-50)
		group(i).c=c
	next
end sub

sub Rule(group1() as atom, group2() as atom, G_ as single, radius as single)
	dim g as single= G_/-100
	for i as integer=0 to ubound(group1)
		dim as single fx, fy
		dim byref p1 as atom=group1(i)
		for j as integer=0 to ubound(group2)
			dim byref p2 as atom=group2(j)
			var dx = p1.x - p2.x
			var dy = p1.y - p2.y
			var r = sqr(dx * dx + dy * dy)
			if r<radius andalso r>0 then
				var F=g/r
				fx+=dx*F
				fy+=dy*F
			end if
		next
		p1.vx = (p1.vx + fx) * 0.5
		p1.vy = (p1.vy + fy) * 0.5
		p1.x += p1.vx
		p1.y += p1.vy
		if p1.x>ScreenWidth OrElse p1.x <0 then p1.vx*=-1
		if p1.y>ScreenHeight OrElse p1.y <0 then p1.vy*=-1
	next
end sub

sub Show(group() as atom)
	for i as integer=0 to ubound(group)
		line (group(i).x, group(i).y)- step(AtomSize,AtomSize), group(i).c, bf
	next
end sub

screenres ScreenWidth, ScreenHeight,32,2: screenset 1,0

dim Yellow(200) as atom: Populate Yellow(), rgb(255,255,0)
dim Red(200) as atom: Populate Red(), rgb(255,0,0)
dim Green(200) as atom: Populate Green(), rgb(0,255,0)

do

	rule(green(), green(), 32, 80)
	rule(green(), red(), 17, 80)
	rule(green(), yellow(), -34, 80)
	rule(red(), red(), 10, 80)
	rule(red(), green(), 34, 80)
	rule(yellow(), yellow(), -15, 80)
	rule(yellow(), green(), 20, 80)

	cls

	Show Green()
	Show Red()
	Show Yellow()
	flip 1,0
	sleep 1
loop until multikey(1)
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: [offtopic] Artificial Life (Simulation & Code)

Post by SARG »

@angros47
Thanks, it was in my todo list. :D
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: [offtopic] Artificial Life (Simulation & Code)

Post by BasicCoder2 »

@angros47

Thanks for the code, well done.
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: [offtopic] Artificial Life (Simulation & Code)

Post by SARG »

@angros47
It seems that 2 rule calls are missing : red/yellow and yellow/red.

In OP code 4 colors and 16 calls : each color to the 3 other colors and itself.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: [offtopic] Artificial Life (Simulation & Code)

Post by angros47 »

This is the example I ported:

https://github.com/hunar4321/life_code# ... le-as-this

I don't see missing rules
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: [offtopic] Artificial Life (Simulation & Code)

Post by SARG »

The javascript is only 3 colors and the cpp code 4 colors with complete relations.

I guess to not have a relation can be considered equivalent to zero attraction/repulsion.
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: [offtopic] Artificial Life (Simulation & Code)

Post by Coolman »

for information for SARG. this code is compiled with :

fbc "ArtificialLife00.bas" -gen gcc -Wc -O3 -s gui

but not with gas64 :

fbc "ArtificialLife00.bas" -gen gas64 -w all -s gui

ArtificialLife00.asm: Assembler messages:
ArtificialLife00.asm:178: Error: no such instruction: `found AN ERROR:virtual register=25 no real register corresponding,using KREG_XXX'
ArtificialLife00.asm:180: Error: bad expression
ArtificialLife00.asm:180: Error: junk `X_D' after expression
Failed compilation.
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: [offtopic] Artificial Life (Simulation & Code)

Post by SARG »

@coolman
What code ? angros47's code ?
And what version of fbc ?

No problem with angros47's code and fbc 1.10

Edit : yes issue when compiling with 1.09 and already fixed in 1.10.
If you continue to use 1.09 change SINGLE by DOUBLE, the issue was due to : sqr(dx * dx + dy * dy)

Anyway thanks for the report.
Coolman
Posts: 294
Joined: Nov 05, 2010 15:09

Re: [offtopic] Artificial Life (Simulation & Code)

Post by Coolman »

SARG wrote: Aug 30, 2022 12:06 @coolman
What code ? angros47's code ?
And what version of fbc ?

No problem with angros47's code and fbc 1.10

Edit : yes issue when compiling with 1.09 and already fixed in 1.10.
If you continue to use 1.09 change SINGLE by DOUBLE, the issue was due to : sqr(dx * dx + dy * dy)

Anyway thanks for the report.
Hello, I always use the latest official version of freebasic. for the moment, to my knowledge, it is 1.09. thank you for your answer.
Post Reply