Welcome to the Creatures Wiki! Log in and join the community.

Full Lifecycle Plant script

From Creatures Wiki
Jump to navigation Jump to search

This is a CAOS tutorial in the form of an in-depth analysis of an agent.


Introduction

Sharmflower sprite images.

This plant script has a full lifecycle and is interactive for creatures. The fruit timer script uses subroutines to check the environment and determine whether it's ok to grow, but the plant itself uses valid logic statements to govern its' timer. It is useful to use the CAOS Tool or another highlighting tool because they can make the CAOS syntax easier to read.

CAOS2PRAY

Monk, ready to use CAOS2PRAY.

CAOS2PRAY is a feature included with RProgrammer's Jagent which allows the coder to not have to code a separate PRAY file in their agents. This chunk of code goes at the top of the COS file, and when it is dragged and dropped onto Monk, compiles the cos file, with its dependant sprite file, into an agent that will be easy for everyone to install and play with. For more details on CAOS2PRAY, see CAOS2PRAY: An Easier Way by Amaikokonut.

**CAOS2PRAY
*# Pray-File "fulllifecycleplant.agents"
*# DS-Name "Full Lifecycle Plant"
*# attach sharmflower.c16
*# attach tutorial_seed.c16
*# attach chwp.wav
*# attach smit.wav
*# desc = "A basic plant that grows, fruits and dies, and creatures can interact with."
*# Agent Animation File = "sharmflower.c16"
*# Agent Animation String = "5"
*# Agent Sprite First Image = 5
*# Agent Animation Gallery = "sharmflower"
*# Web URL = "creatures.wiki"
*# Web Label = "Creatures Wiki" 

Initial installation

Installs 30 fruits in the DS norn meso, using the sprite file tutorial_seed.c16.

inst
reps 30
	new: simp 2 8 1000 "tutorial_seed" 1 0 4900
	attr 195
	bhvr 48
	elas 30
	fric 50
	accg 3
	mvsf rand 748 1550 9547
	velo rand -10 10 rand -15 -20
	tick rand 300 600
	emit 6 0.3
*	dormancy counter - reaches 0 and dies
	setv ov72 rand 10 40
*	max light to sprout
	setv ov80 1
*	max heat to sprout
	setv ov82 1
*	min nutrients to sprout
	setv ov87 0
repe

Fruit timer script

Fruit timer script - first checks there aren't too many fruits nearby, then checks if the fruit is in water, then tests the environment for light, heat and nutrients (and then if there are enough, the plant grows, using the sharmflower.c16 file) but if there aren't enough, the fruit goes dormant and eventually is killed.


scrp 2 8 1000 9
*fruit population check
	lock
	rnge 30
	setv va66 0
	esee 2 8 1000
		addv va66 1
	next
	targ ownr
	doif va66 gt 7
		kill ownr
	endi
	unlk
*	if not being carried or falling
	doif fall eq 0 and carr eq null
		doif rtyp room targ eq 8 or rtyp room targ eq 9
			kill ownr
		endi
*	check environment to test whether can sprout
		gsub envi
*	if don't survive envi test, go dormant
		gsub dorm
	endi
**environment test - grow if successful
	subr envi
*	light check
		doif prop room targ 1 le ov80
			setv va00 1
		endi
*	heat check
		doif prop room targ 2 le ov82
			setv va01 1
		endi
*	nutrient check
		doif prop room targ 4 ge ov87
			setv va02 1
		endi

*	can I grow?	
		doif va00 eq 1 and va01 eq 1 and va02 eq 1
			gsub grow
		endi
	retn
**growing into plant routine
	subr grow
		setv va00 posl
		setv va01 post
		new: simp 2 4 1000 "sharmflower" 9 0 140
		attr 199
		bhvr 43
		elas 0
*OV00 - Plant state (0=growing, 1=fruiting, 2=dying, 3=picked)
		setv ov00 0
*OV99 - an age counter, counts the amount of time spent in a mature state.
		setv ov99 0
		doif tmvt va00 va01 eq 1
			mvto va00 va01
		else
			mvsf va00 va01
		endi
		tick rand 200 1000
		kill ownr
	retn
**dormancy routine
	subr dorm
		doif pose lt 4
*	make it invisible
			attr 16
			over
		endi
*	reduce dormancy counter
		subv ov72 1
*	if been dormant too long, die
		doif ov72 le 0
			doif room targ ne -1
				altr room targ 3 0.1
				altr room targ 4 0.1
				kill ownr
			else
				kill ownr
 			endi
		endi
	retn
endm

Plant timer script

****plant timer
scrp 2 4 1000 9
**OV00 is state
*0=growing
*1=fruiting
*2=dying
*3=picked
*increase age
	addv ov01 1
**growing
	doif ov00 eq 0
		doif pose lt 5
			setv va00 pose
			addv va00 1
			pose va00
		else
*	if in right pose and old enough, fruit
			doif ov01 ge 20
				setv ov00 1
				stop
			endi
		endi
	endi
**fruiting
	doif ov00 eq 1
		doif pose lt 5
			setv va00 pose
			addv va00 1
			pose va00
		else
*	create a fruit
				inst
 				setv va00 posl
				setv va01 post
				new: simp 2 8 1000 "tutorial_seed" 1 0 4900
				attr 195
				bhvr 48
				elas 30
				fric 50
				accg 3
				doif tmvt va00 va01 <> 1
					kill targ
					targ ownr
					setv ov00 2
					stop
				endi
				doif tmvt va00 va01 = 1
					mvto va00 va01
				else
					mvsf va00 va01
				endi
				velo rand -10 10 rand -15 -20
				tick rand 300 600
				emit 6 0.3
*	dormancy counter - reaches 0 and dies
				setv ov72 rand 10 40
*	max light to sprout
				setv ov80 1
*	max heat to sprout
				setv ov82 1
*	min nutrients to sprout
				setv ov87 0
			targ ownr
			setv ov00 2
		endi
	endi
**dying
	doif ov00 eq 2
		doif pose lt 8
			setv va00 pose
			addv va00 1
			pose va00
		else
			altr room targ 3 0.2
			altr room targ 4 0.2
			kill ownr
		endi
	endi


**if you've been picked
	doif ov00 eq 3
*	if not carried or falling
		doif carr eq null and fall eq 0
			altr room targ 3 0.2
			altr room targ 4 0.2
			kill ownr
		endi
	endi
endm

Fruit eat

scrp 2 8 1000 12
	lock
	sndc "chwp"
	stim writ from 78 1
	kill ownr
endm

Fruit fall

scrp 2 8 1000 6
	doif wall = down
		sndc "smit"
	endi
endm

Plant Hit script

This script reuses two pieces of code from previous scripts: it stimulates whoever hit the plant as per the push script, and it uses a bit of code repeated from the timer to make the plant grow down if it is hit when it is more than just a seedling.

scrp 2 4 1000 3
	targ from
	stim writ from 84 1
	targ ownr
*the plant grows DOWN if it is hit.	
	doif pose gt 0
		setv va00 pose
		subv va00 1
		pose va00
	endi
endm

Plant push script

scrp 2 4 1000 1
	targ from
	stim writ from 84 1
	targ ownr
	mesg writ ownr 1000
endm

Plant Pull script

scrp 2 4 1000 2
	stim writ from 84 1
	targ ownr
	mesg writ ownr 1000
endm

Special plant vend script

scrp 2 4 1000 1000
	lock
	rnge 30
	setv va66 0
	esee 2 8 1000
		addv va66 1
	next
	targ ownr
	doif va66 le 4
		setv va00 posl
		setv va01 post
		inst
		new: simp 2 8 1000 "tutorial_seed" 1 0 4900
		attr 195
		bhvr 48
		elas 30
		fric 50
		accg 3
		doif tmvt va00 va01 = 1
			mvto va00 va01
		else
			mvsf va00 va01
		endi
		velo rand -10 10 rand -15 -20
		tick rand 300 600
		emit 6 0.3
	endi
endm

Remove

This simply counts through all the objects in the world with that class number, removes them, and then removes the scripts that belong to that class number. It's important to double-check the class numbers here, and make sure that all SCRPs in the object have a corresponding scrx.

rscr
enum 2 8 1000
	kill targ
next
enum 2 4 1000
	kill targ
next
scrx 2 8 1000 9
scrx 2 4 1000 9
scrx 2 8 1000 12
scrx 2 8 1000 6
scrx 2 4 1000 1
scrx 2 4 1000 2
scrx 2 4 1000 3
scrx 2 4 1000 1000


Whole script

**CAOS2PRAY
*# Pray-File "fulllifecycleplant.agents"
*# DS-Name "Full Lifecycle Plant"
*# attach sharmflower.c16
*# attach tutorial_seed.c16
*# attach chwp.wav
*# attach smit.wav
*# desc = "A basic plant that grows, fruits and dies, and creatures can interact with."
*# Agent Animation File = "sharmflower.c16"
*# Agent Animation String = "5"
*# Agent Sprite First Image = 5
*# Agent Animation Gallery = "sharmflower"
*# Web URL = "creatures.wiki"
*# Web Label = "Creatures Wiki" 
inst
reps 30
	new: simp 2 8 1000 "tutorial_seed" 1 0 4900
	attr 195
	bhvr 48
	elas 30
	fric 50
	accg 3
	mvsf rand 748 1550 9547
	velo rand -10 10 rand -15 -20
	tick rand 300 600
	emit 6 0.3
*	dormancy counter - reaches 0 and dies
	setv ov72 rand 10 40
*	max light to sprout
	setv ov80 1
*	max heat to sprout
	setv ov82 1
*	min nutrients to sprout
	setv ov87 0
repe

scrp 2 8 1000 9
*fruit population check
	lock
	rnge 30
	setv va66 0
	esee 2 8 1000
		addv va66 1
	next
	targ ownr
	doif va66 gt 7
		kill ownr
	endi
	unlk
*	if not being carried or falling
	doif fall eq 0 and carr eq null
		doif rtyp room targ eq 8 or rtyp room targ eq 9
			kill ownr
		endi
*	check environment to test whether can sprout
		gsub envi
*	if don't survive envi test, go dormant
		gsub dorm
	endi

**environment test - grow if successful
	subr envi
*	light check
		doif prop room targ 1 le ov80
			setv va00 1
		endi
*	heat check
		doif prop room targ 2 le ov82
			setv va01 1
		endi
*	nutrient check
		doif prop room targ 4 ge ov87
			setv va02 1
		endi

*	can I grow?	
		doif va00 eq 1 and va01 eq 1 and va02 eq 1
			gsub grow
		endi
	retn

**growing into plant routine
	subr grow
		setv va00 posl
		setv va01 post
		new: simp 2 4 1000 "sharmflower" 9 0 140
		attr 199
		bhvr 43
		elas 0
*OV00 - Plant state: switches between growing UP and growing DOWN
		setv ov00 0
*OV99 - an age counter, counts the amount of time spent in a mature state.
		setv ov99 0
		doif tmvt va00 va01 eq 1
			mvto va00 va01
		else
			mvsf va00 va01
		endi
		tick rand 200 1000
		kill ownr
	retn

**dormancy routine
	subr dorm
		doif pose lt 4
*	make it invisible
			attr 16
			over
		endi
*	reduce dormancy counter
		subv ov72 1
*	if been dormant too long, die
		doif ov72 le 0
			doif room targ ne -1
				altr room targ 3 0.1
				altr room targ 4 0.1
				kill ownr
			else
				kill ownr
			endi
		endi
	retn
endm

****plant timer
scrp 2 4 1000 9
**OV00 is state
*0=growing
*1=fruiting
*2=dying
*3=picked

*increase age
	addv ov01 1

**growing
	doif ov00 eq 0
		doif pose lt 5
			setv va00 pose
			addv va00 1
			pose va00
		else
*	if in right pose and old enough, fruit
			doif ov01 ge 20
				setv ov00 1
				stop
			endi
		endi
	endi

**fruiting
	doif ov00 eq 1
		doif pose lt 5
			setv va00 pose
			addv va00 1
			pose va00
		else
*	create a fruit
				inst
				setv va00 posl
				setv va01 post
				new: simp 2 8 1000 "tutorial_seed" 1 0 4900
				attr 195
				bhvr 48
				elas 30
				fric 50
				accg 3
				doif tmvt va00 va01 <> 1
					kill targ
					targ ownr
					setv ov00 2
					stop
				endi
				doif tmvt va00 va01 = 1
					mvto va00 va01
				else
					mvsf va00 va01
				endi
				velo rand -10 10 rand -15 -20
				tick rand 300 600
				emit 6 0.3
*	dormancy counter - reaches 0 and dies
				setv ov72 rand 10 40
*	max light to sprout
				setv ov80 1
*	max heat to sprout
				setv ov82 1
*	min nutrients to sprout
				setv ov87 0
			targ ownr
			setv ov00 2
		endi
	endi

**dying
	doif ov00 eq 2
		doif pose lt 8
			setv va00 pose
			addv va00 1
			pose va00
		else
			altr room targ 3 0.2
			altr room targ 4 0.2
			kill ownr
		endi
	endi


**if you've been picked
	doif ov00 eq 3
*	if not carried or falling
		doif carr eq null and fall eq 0
			altr room targ 3 0.2
			altr room targ 4 0.2
			kill ownr
		endi
	endi
endm


scrp 2 8 1000 12
	lock
	sndc "chwp"
	stim writ from 78 1
	kill ownr
endm

scrp 2 8 1000 6
	doif wall = down
		sndc "smit"
	endi
endm

*Hit script
scrp 2 4 1000 3
	targ from
	stim writ from 84 1
	targ ownr
*the plant grows DOWN if it is hit.	
	doif pose gt 0
		setv va00 pose
		subv va00 1
		pose va00
	endi
endm

*push script
scrp 2 4 1000 1
	targ from
	stim writ from 84 1
	targ ownr
	mesg writ ownr 1000
endm

*Pull script
scrp 2 4 1000 2
	stim writ from 84 1
	targ ownr
	mesg writ ownr 1000
endm

scrp 2 4 1000 1000
	lock
	rnge 30
	setv va66 0
	esee 2 8 1000
		addv va66 1
	next
	targ ownr
	doif va66 le 4
		setv va00 posl
		setv va01 post
		inst
		new: simp 2 8 1000 "tutorial_seed" 1 0 4900
		attr 195
		bhvr 48
		elas 30
		fric 50
		accg 3
		doif tmvt va00 va01 = 1
			mvto va00 va01
		else
			mvsf va00 va01
		endi
		velo rand -10 10 rand -15 -20
		tick rand 300 600
		emit 6 0.3
	endi
endm


rscr
enum 2 8 1000
	kill targ
next
enum 2 4 1000
	kill targ
next
scrx 2 8 1000 9
scrx 2 4 1000 9
scrx 2 8 1000 12
scrx 2 8 1000 6
scrx 2 4 1000 1
scrx 2 4 1000 2
scrx 2 4 1000 3
scrx 2 4 1000 1000

Moving On

  • Change the genus to be a bad plant (2 5 1000 instead of 2 4 1000) and change all three stimuli to 83, "Danger plant", instead of 84, "Friendly plant".