Page 1 of 1

Random script example : Civilization's v0.1 farms

Posted: Wed Jun 06, 2012 1:48 am
by Mit
So heres the sorta stuff you can get up to in script these days. This scipt runs the whole farming process on the current chapter, with stages of production and osd and everything.

Code: Select all

//---------------------------------------------------
//  Farms.mit
//
// [Config]

$kFarm1BuildingNum = 9
$kFarm2BuildingNum = 10

//----------------------------------------------------
//
// BuildingVars :
//
//   1 - records number of accesses of the building
//   2 - Current crop stage
//   3 - Number of wheat left from last harvest
//   4 - Number of seeds left from last harvest
//   5 - Current active field background model
//
// BuildingTimeVar :
//
//	1 - Time planted

$kFarmStageFallow = 0
$kFarmStageTurned = 1
$kFarmStageGoneOver = 2
$kFarmStageHarvested = 3
	// Lower stages are considered 'active' ones (ie the FarmCropsUpdate is called daily)
$kFarmStageSeedsPlanted = 10
$kFarmStageSeedsGrowing = 11
$kFarmStagePlantsGrowing = 12
$kFarmStagePlantsGrown = 13
$kFarmStageReadyForHarvest = 14

	// Times for all the farm events (in minutes)
$kFarmTimeSeedsToSeedlings = 60			// (1 hour)
$kFarmTimeSeedlingsToPlants = 1200		// (20 hours)
$kFarmTimePlantsToTallPlants = 2400		// (40 hours)
$kFarmTimeReadyToHarvest = 2760			// (46 hours)
$kFarmTimeGoneOver = 3000				// (50 hours)

Function	FarmCropsUpdate( )
{
	$numSecondsSincePlanting = $gServerTime - $gBuildingTimeVar[1]
	if ( $numSecondsSincePlanting < 0 )
	{
		$numSecondsSincePlanting = 0
	}
	$numMinutesSincePlanting = $numSecondsSincePlanting / 60

	// Has planted seeds
	if ( $gBuildingVar[2] == $kFarmStageSeedsPlanted )
	{
		if ( $numMinutesSincePlanting >= $kFarmTimeSeedsToSeedlings )
		{
			$ret = FarmSetFieldTexture( 6 )
			$gBuildingVar[2] = $kFarmStageSeedsGrowing
		}
	}
	else if ( $gBuildingVar[2] == $kFarmStageSeedsGrowing )		
	{
		if ( $numMinutesSincePlanting >= $kFarmTimeSeedlingsToPlants )
		{
			// Add background model for short crops
			$ret = FarmSetFieldModel(1)
			$gBuildingVar[2] = $kFarmStagePlantsGrowing
		}
	}
	else if ( $gBuildingVar[2] == $kFarmStagePlantsGrowing )	
	{
		if ( $numMinutesSincePlanting >= $kFarmTimePlantsToTallPlants )
		{
			//  Add background model for tall crops
			$ret = FarmSetFieldModel(2)
			$gBuildingVar[2] = $kFarmStagePlantsGrown
		}
	}
	else if ( $gBuildingVar[2] == $kFarmStagePlantsGrown )	
	{
		if ( $numMinutesSincePlanting >= $kFarmTimeReadyToHarvest )
		{
			$ret = FarmSetFieldTexture( 5 )
			// Four hours after being ready for harvest, crops decay
			$gBuildingVar[2] = $kFarmStageReadyForHarvest
		}
	}
	else if ( $gBuildingVar[2] == $kFarmStageReadyForHarvest )	
	{
		if ( $numMinutesSincePlanting >= $kFarmTimeGoneOver )
		{
			$ret = FarmSetFieldModel(0)
			$ret = FarmSetFieldTexture( 5 )
			// Four hours after being ready for harvest, crops decay
			$gBuildingVar[2] = $kFarmStageGoneOver
		}
	}	

}

Event( "BuildingProductionRun", "$kFarm1BuildingNum" )
{
	// If theres growing going on
	if ( $gBuildingVar[2] >= $kFarmStageSeedsPlanted )
	{
		$ret = FarmCropsUpdate()
	}
}

Function FarmSetFieldModel( $modelNum )
{
	$tileX1 = $gBuildingX
	$tileY1 = $gBuildingY
	$tileX2 = $tileX1
	$tileY2 = $tileY1 - 1

	$currentModel = $gBuildingVar[5]
	if ( $currentModel != 0 )
	{
		*removebackgroundmodel $currentModel,$tileX1,$tileY1
		*removebackgroundmodel $currentModel,$tileX2,$tileY2
		$gBuildingVar[5] = 0
	}

	if ( $modelNum == 1 )
	{
		*addbackgroundmodel 2,$tileX1,$tileY1,0		
		*addbackgroundmodel 2,$tileX2,$tileY2,0		
		$gBuildingVar[5] = 2
	}
	else if ( $modelNum == 2 )
	{
		*addbackgroundmodel 3,$tileX1,$tileY1,0		
		*addbackgroundmodel 3,$tileX2,$tileY2,0		
		$gBuildingVar[5] = 3
	}
}

Function FarmSetFieldTexture( $textureNum )
{
	$tileX1 = $gBuildingX
	$tileY1 = $gBuildingY
	$tileX2 = $tileX1
	$tileY2 = $tileY1 - 1
	*settile $textureNum,$tileX1,$tileY1,$tileX2,$tileY2
}

Function FarmTaskComplete( $taskNum )
{
	if ( $taskNum == 4 )			// Digging over
	{
		$gBuildingVar[2] = $kFarmStageTurned
		$ret = FarmSetFieldTexture( 5 )
	}
	else if ( $taskNum == 5 )		// Planting seed
	{
		*changeinventory %PLAYER% -1 Wheat Seed
		$gBuildingVar[2] = $kFarmStageSeedsPlanted
		$gBuildingTimeVar[1] = $gServerTime
	}
	else if ( $taskNum == 6 )		// Harvesting
	{
		$gBuildingVar[2] = $kFarmStageHarvested
		$gBuildingTimeVar[1] = $gServerTime

		$ret = FarmSetFieldTexture( 0 )
		$ret = FarmSetFieldModel( 0 )
		$cropRand = sysRand(4)
		$gBuildingVar[3] = 2 + $cropRand
		$seedRand = sysRand(2)
		$gBuildingVar[4] = 1 + $seedRand
	}
}


Event( "OSDSelect", "FarmOSD:Discard" )
{
	$gBuildingVar[2] = $kFarmStageFallow
	$gBuildingVar[3] = 0
	$gBuildingVar[4] = 0
	$ret = FarmDisplayMainOSD()
}


Event( "OSDSelect", "FarmOSD:TakeWheat" )
{
	if ( $gBuildingVar[3] > 0 )
	{
		$amountAdded = sysAddToInventory( "Wheat" )
		if ( $amountAdded > 0 )
		{
			$gBuildingVar[3] = $gBuildingVar[3] - 1
		}
		else
		{
			*msg %PLAYER% You can't carry anymore wheat right now
		}

		// If we've now emptied the farm
		if ( $gBuildingVar[3] == 0 )
		{
			if ( $gBuildingVar[4] == 0 )
			{
				$gBuildingVar[2] = $kFarmStageFallow
			}
		}
		$ret = FarmDisplayMainOSD()
	}
}

Event( "OSDSelect", "FarmOSD:TakeSeeds" )
{
	if ( $gBuildingVar[4] > 0 )
	{
		$amountAdded = sysAddToInventory( "Wheat seed" )
		if ( $amountAdded > 0 )
		{
			$gBuildingVar[4] = $gBuildingVar[4] - 1
		}
		else
		{
			*msg %PLAYER% You can't carry anymore seed right now
		}

		// If we've now emptied the farm
		if ( $gBuildingVar[4] == 0 )
		{
			if ( $gBuildingVar[3] == 0 )
			{
				$gBuildingVar[2] = $kFarmStageFallow
			}
		}
		$ret = FarmDisplayMainOSD()
	}
}


Event( "OSDSelect", "FarmOSD:Harvest" )
{
	*dobuildingtask %PLAYER% $gBuildingAccessNum 6 Harvesting crops
}

Event( "OSDSelect", "FarmOSD:Plant" )
{
	$numSeeds = sysPlayerInventory( "Wheat Seed" )
	if ( $numSeeds > 0 )
	{
		*dobuildingtask %PLAYER% $gBuildingAccessNum 5 Planting seeds
	}
	else
	{
		*msg %PLAYER% You need seeds to plant
	}
}


Event( "OSDSelect", "FarmOSD:Dig" )
{
	*dobuildingtask %PLAYER% $gBuildingAccessNum 4 Doing a bit of digging

}



Function	ShowFarmState()
{
	$iconX = 10;
	$iconY = 0;
	$textX = $iconX + 270
	$textY = $iconY + 20
	$text = "Unknown state"

	if ( $gBuildingVar[2] = $kFarmStageFallow )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmfallow.jpg") 
		$text = "Field State: Fallow"
	}
	else if ( $gBuildingVar[2] = $kFarmStageTurned )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmturned.jpg") 
		$text = "Field State: Turned"
	}
	else if ( $gBuildingVar[2] = $kFarmStageSeedsPlanted )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmplanted.jpg") 
		$text = "Field State: Planted"
	}
	else if ( $gBuildingVar[2] = $kFarmStageSeedsGrowing )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmgrowingwheat.jpg") 
		$text = "Field State: Growing"
	}
	else if ( $gBuildingVar[2] = $kFarmStagePlantsGrowing )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmgrowingwheat.jpg") 
		$text = "Field State: Growing"
	}
	else if ( $gBuildingVar[2] = $kFarmStagePlantsGrown )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmgrowingwheat.jpg") 
		$text = "Field State: Growing"
	}
	else if ( $gBuildingVar[2] = $kFarmStageReadyForHarvest )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmreadywheat.jpg") 
		$text = "Field State: Ready for Harvest"
	}
	else if ( $gBuildingVar[2] = $kFarmStageGoneOver )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmfallow.jpg") 
		$text = "Field State: Crops withered"
	}
	else if ( $gBuildingVar[2] = $kFarmStageHarvested )
	{
		osdaddat(OSDIMAGE, $iconX, $iconY, 256, 256, "", "http://gameislands.net/gamecontent/civilization/menus/farmwheatharvested.jpg") 
		$text = "Field State: Harvested"
	}
	osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", $text ) 
}

Function	ShowFarmAction()
{
	$textX = 280
	$textY = 50

	if ( $gBuildingVar[2] = $kFarmStageFallow )
	{
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "You will need to dig over the field before planting" ) 
		$textY = $textY + 30
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "This will take a little while to do" ) 
		$textY = $textY + 15

		$buttonX = $textX + 30
		$buttonY = $textY + 40
		osdaddat(OSDEXITBUTTON, $buttonX, $buttonY, 220, 40, "Dig", "Dig it") 
	}
	else if ( $gBuildingVar[2] = $kFarmStageTurned )
	{
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "Now the field is well dug over, next stage is to plant the seeds" ) 
		$textY = $textY + 40
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "After planting, the wheat will take about 46 (real-time) hours to grow. You'll have to return within 4 hours of it ripening not to lose your crop" ) 
		$textY = $textY + 40

		$buttonX = $textX + 30
		$buttonY = $textY + 40
		osdaddat(OSDEXITBUTTON, $buttonX, $buttonY, 220, 40, "Plant", "Plant Seeds") 
	}
	else if ( $gBuildingVar[2] = $kFarmStageReadyForHarvest )
	{
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "Harvest!" ) 
		$textY = $textY + 30

		$buttonX = $textX + 30
		$buttonY = $textY + 40
		osdaddat(OSDEXITBUTTON, $buttonX, $buttonY, 220, 40, "Harvest", "Harvest crops") 
	}
	else if ( $gBuildingVar[2] = $kFarmStageGoneOver )
	{
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "Wheat takes about 2 (realtime) days to grow, then there is a 4 hour window to harvest." ) 
		$textY = $textY + 30	
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "If you leave it too long the crops wither and die" ) 
		$textY = $textY + 30
		$gBuildingVar[2] = 0
		$buttonX = $textX + 30
		$buttonY = $textY + 40
		osdaddat(OSDEXITBUTTON, $buttonX, $buttonY, 220, 40, "", "Oops, im a bad farmer") 
	}
	else if ( $gBuildingVar[2] = $kFarmStageHarvested )
	{
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "Your crops have been harvested, now you need to take them away to clear the field for another season." ) 
		$textY = $textY + 30	
		$buttonX = $textX + 30
		$buttonY = $textY + 20
		osdaddat(OSDBUTTON, $buttonX, $buttonY, 220, 40, "TakeWheat", "Take Wheat ($gBuildingVar[3])") 
		$buttonY = $buttonY + 45
		osdaddat(OSDBUTTON, $buttonX, $buttonY, 220, 40, "TakeSeeds", "Take Seeds ($gBuildingVar[4])") 
		$buttonY = $buttonY + 45
		osdaddat(OSDBUTTON, $buttonX, $buttonY, 220, 40, "Discard", "Discard remaining crops") 
		$buttonY = $buttonY + 45

	}
	else
	{
		osdaddat(OSDTEXT, $textX, $textY, 0, 0, "", "Now you'll just have to wait for nature to take its course" ) 
		$textY = $textY + 30
	}
}

Function FarmDisplayMainOSD( )
{
	osdcreate(OSDBUILDING,"FarmOSD", "" ) 

	$ret = ShowFarmState()
	$ret = ShowFarmAction()

	osdaddat(OSDEXITBUTTON, 195, 302, 200, 20, "", "Exit") 
	osdactivate() 
}


//----------------------------------------------------------
// FarmAccessInit - Custom Event
//  BuildingAccess Entry point
//
//----------------------------------------------------------
Event( "Custom", "FarmAccessInit" )
{
	if ( $gBuildingOwnerID == $gPlayerID )
	{
		$ret = FarmDisplayMainOSD()
		$gBuildingVar[1] = $gBuildingVar[1] + 1
	}
	else
	{
		osdcreate(OSDBUILDING,"FarmOSD", "" ) 
		osdadditem(OSDTEXT, "", "") 
		osdadditem(OSDTEXT, "", "") 
		osdadditem(OSDTEXT, "", "Farm owners can grow seeds and harvest grain") 
		osdadditem(OSDTEXT, "", "") 
		osdadditem(OSDTEXT, "", "") 
		osdadditem(OSDEXITBUTTON, "Option1", "Ok") 
		osdactivate() 
	}

}


Event( "Custom", "FarmDemolished" )
{
	$ret = FarmSetFieldTexture( 0 )
	$ret = FarmSetFieldModel( 0 )
}


//------ Access script for Farms
Event( "AccessBuilding", "$kFarm1BuildingNum" )
{
   CustomEvent( "FarmAccessInit" )
}
Event( "AccessBuilding", "$kFarm2BuildingNum" )
{
   CustomEvent( "FarmAccessInit" )
}

Event( "BuildingDemolished", "$kFarm1BuildingNum" )
{
   CustomEvent( "FarmDemolished" )
}
Event( "BuildingDemolished", "$kFarm2BuildingNum" )
{
   CustomEvent( "FarmDemolished" )
}

Posted: Wed Jun 06, 2012 8:25 am
by Omni
It's good for you to drop this info here, many of us are already constructing worlds and this new stuff we want to get our hands on.

Just waiting for you to bring the servers so we can begin testing everything out! :) Since I'm not skillful at all with scripting (have no idea) this will help a lot. :)

P.S: Perhaps you shouldn't hesitate in dropping good amounts of script info in the World Owner Manual.

Posted: Wed Jun 06, 2012 11:51 am
by morbydvisns
Image

Posted: Wed Jun 06, 2012 11:53 am
by Mit
Now now, that doesn't count! Everyone knows that putting a code snippet block around anything automatically disqualifies it from being a wall of text :)

Posted: Wed Jun 06, 2012 9:37 pm
by morbydvisns
I see whats going on in there, but one thing... I see $ret being set a bit but not used anywhere... whats that there for?


Also, if im reading that right, do each building per each player have irs own set of building vars. Like, I, as a player have my own set of building records and each building have it's own set of buildingvars that are referred to during that particular instance?

Posted: Sat Jun 16, 2012 10:05 am
by zaroba
looking threw it, i'm thinking $ret is a script var for Return.
It's sending the script parser to the Function further down in the script

for example: $ret = FarmSetFieldModel(0)
and a bit after it: Function FarmSetFieldModel( $modelNum )

Posted: Sat Jun 16, 2012 1:01 pm
by morbydvisns
Yea its just the way the parser calls the function. I havnt tried it yet but i assume that $ret var could be $anything and it still do what it should.