Aramathea Blacksmith Script

Forum Archive - from http://theuniversal.net
Locked
User avatar
Mit
Staff
Staff
Posts: 3551
Joined: Sun Sep 21, 2003 10:14 pm
Location: Unknown

Aramathea Blacksmith Script

Post by Mit »

Matias requested this, posting here for reference. Its a detailed example of a custom economy building script :

Code: Select all

//----------------------------------------------------
//  Medieval - Blacksmith
// [Config]

$kMedBlacksmithBuildingNum = 45
//----------------------------------------------------
//
// BuildingVars :
//
//   3 - Number of Chisels (229) made here (ever)
//   4 - Number of ploughs (115) made here (ever)
//   5 - Number of pickaxes (117) made here (ever)
//   6 - Iron buy-in limit
//   7 - Steel buy-in limit
//
//   9 - Worker wage per item
//	10 - Wage to be collected by worker
//
//  12 - Number of swords (224) made here (ever)
//  13 - Number of armour (242) made here (ever)
//  
$kChiselIronRequired = 1
$kPloughIronRequired = 5
$kPickaxeIronRequired = 3
$kSwordSteelRequired = 2
$kArmorSteelRequired = 5

$kMaxIronInStorage = 30
$kMaxSteelInStorage = 30

Function	MedBlacksmithShowOwnerEmployeeDisplay( $osdX, $osdY, $wage, $minJobInvest, $currInvest )
{
	$wageText = sysGetPriceText( $wage )
	if ( $gBuildingNumEmployees == 1 )
	{
		$employeeID = sysGetEmployeeID( 0 )
		$name = sysGetPlayerName( $employeeID )
		osdaddat(OSDTEXT, $osdX, $osdY, 500, 20, "", "Currently employing $name for $wageText per item") 
	}
	else if ( $wage == 0 )
	{
		osdaddat(OSDTEXT, $osdX, $osdY, 500, 20, "", "Not currently advertising for staff") 
	}
	else 
	{
		if ( $currInvest > $minJobInvest )
		{
			osdaddat(OSDTEXT, $osdX, $osdY, 500, 20, "", "Job vacancy for a Blacksmith ($wageText per item)") 
		}
		else
		{
			$minInvText = sysGetPriceText( $minJobInvest )
			osdaddat(OSDTEXT, $osdX, $osdY, 500, 20, "", "Requires minimum of $minInvText invested to take on a worker.") 
		}
	}
}



Event( "OSDSelect", "MedSmithOSD:IronAdminApply" )
{
	*setbuyprice $gBuildingNum,Iron,$gParam[1]
	$gBuildingVar[6] = $gParam[2]

	*setbuyprice $gBuildingNum,Steel,$gParam[3]
	$gBuildingVar[7] = $gParam[4]
	$ret = MedBlacksmithDisplayOwnerIronSuppliesOSD()
}


Event( "OSDSelect", "MedSmithOSD:AddIron" )
{
	$hasIron = sysPlayerInventory( "Iron" )
	if ( $hasIron > 0 )
	{
		$ironInStocks = sysAmountInStocks( "Iron" )
		if ( $ironInStocks < $kMaxIronInStorage )
		{
			*grantitem %PLAYER% -1 Iron
			*addtostocks $gBuildingNum,1,Iron
		}
	}
	$ret = MedBlacksmithDisplayOwnerIronSuppliesOSD()
}

Event( "OSDSelect", "MedSmithOSD:AddSteel" )
{
	$hasSteel = sysPlayerInventory( "Steel" )
	if ( $hasSteel > 0 )
	{
		$steelInStocks = sysAmountInStocks( "Steel" )
		if ( $steelInStocks < $kMaxSteelInStorage )
		{
			*grantitem %PLAYER% -1 Steel
			*addtostocks $gBuildingNum,1,Steel
		}
	}
	$ret = MedBlacksmithDisplayOwnerIronSuppliesOSD()
}

Event( "OSDSelect", "MedSmithOSD:WorkerAddIron" )
{
	$hasIron = sysPlayerInventory( "Iron" )
	if ( $hasIron > 0 )
	{
		$ironInStocks = sysAmountInStocks( "Iron" )
		if ( $ironInStocks < $kMaxIronInStorage )
		{
			*grantitem %PLAYER% -1 Iron
			*addtostocks $gBuildingNum,1,Iron
		}
	}
	$ret = MedBlacksmithVisitorOSD()
}

Event( "OSDSelect", "MedSmithOSD:WorkerAddSteel" )
{
	$hasSteel = sysPlayerInventory( "Steel" )
	if ( $hasSteel > 0 )
	{
		$steelInStocks = sysAmountInStocks( "Steel" )
		if ( $steelInStocks < $kMaxSteelInStorage )
		{
			*grantitem %PLAYER% -1 Steel
			*addtostocks $gBuildingNum,1,Steel
		}
	}
	$ret = MedBlacksmithVisitorOSD()
}


Event( "OSDSelect", "MedSmithOSD:Main" )
{
	CustomEvent( "MedBlacksmithAccessInit" )
}

Function		MedBlacksmithDisplayOwnerIronSuppliesOSD( )
{
	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 

	$ironInStocks = sysAmountInStocks( "Iron" )
	$steelInStocks = sysAmountInStocks( "Steel" )
	$ironBuyPrice = sysGetBuyPrice( "Iron" )
	$steelBuyPrice = sysGetBuyPrice( "Steel" )
	$hasIron = sysPlayerInventory( "Iron" )
	$hasSteel = sysPlayerInventory( "Steel" )
	osdaddat(OSDTEXT, 20, 10, 0, 0, "", "$ironInStocks Iron, $steelInStocks Steel currently in storage") 
	if ( $hasIron > 0 )
	{
		if ( $ironInStocks < $kMaxIronInStorage )
		{
			osdaddat(OSDBUTTON, 100, 40, 200, 25, "AddIron", "Add Iron") 
		}
		else
		{
			osdaddat(OSDFADEDBUTTON, 100, 40, 200, 25, "", "Add Iron") 
		}
	}
	else
	{
		osdaddat(OSDFADEDBUTTON, 100, 40, 200, 25, "", "Add Iron") 
	}
	if ( $hasSteel > 0 )
	{
		if ( $steelInStocks < $kMaxSteelInStorage )
		{
			osdaddat(OSDBUTTON, 310, 40, 200, 25, "AddSteel", "Add Steel") 
		}
		else
		{
			osdaddat(OSDFADEDBUTTON, 310, 40, 200, 25, "", "Add Steel") 
		}
	}
	else
	{
		osdaddat(OSDFADEDBUTTON, 310, 40, 200, 25, "", "Add Steel") 
	}

	$lineY = 90
	osdaddat(OSDTEXT, 20, $lineY, 0, 0, "", "Set Iron buy price :") 

	$valLineY = $lineY - 5
	osdaddat(OSDTEXT, 170, $lineY, 0, 0, "", "1 Iron for") 
	osdaddat(OSDCASHVALUE, 240, $valLineY, 100, 25, "ironbuyval", "$ironBuyPrice|0|999") 

	$lineY = $lineY + 30

	if ( $ironBuyPrice == 0 )
	{
		osdaddat(OSDTEXT, 150, $lineY, 0, 0, "", "(Not currently buying Iron)") 
	}
	else
	{
		$ironBuyPriceText = sysGetPriceText( $ironBuyPrice )
		osdaddat(OSDTEXT, 150, $lineY, 0, 0, "", "(Currently buying at 1 Iron for $ironBuyPriceText)") 
	}

	$lineY = 150
	$valLineY = $lineY - 5
	osdaddat(OSDTEXT, 60, $lineY, 0, 0, "", "Stop buying once theres :") 
	$ironInStorageLimit = $gBuildingVar[6]
	osdaddat(OSDVALUE, 240, $valLineY, 100, 25, "ironbuylimit", "$ironInStorageLimit|5|30|5") 
	osdaddat(OSDTEXT, 350, $lineY, 0, 0, "", "Iron in storage") 

	// --------------------- Steel
	$lineY = 190
	osdaddat(OSDTEXT, 110, $lineY, 0, 0, "", "Set Steel buy price :") 

	$valLineY = $lineY - 5
	osdaddat(OSDTEXT, 260, $lineY, 0, 0, "", "1 Steel for") 
	osdaddat(OSDCASHVALUE, 330, $valLineY, 100, 25, "steelbuyval", "$steelBuyPrice|0|999") 

	$lineY = $lineY + 30

	if ( $steelBuyPrice == 0 )
	{
		osdaddat(OSDTEXT, 240, $lineY, 0, 0, "", "(Not currently buying Steel)") 
	}
	else
	{
		$steelBuyPriceText = sysGetPriceText( $steelBuyPrice )
		osdaddat(OSDTEXT, 240, $lineY, 0, 0, "", "(Currently buying at 1 Steel for $steelBuyPriceText)") 
	}

	$lineY = 250
	$valLineY = $lineY - 5
	osdaddat(OSDTEXT, 150, $lineY, 0, 0, "", "Stop buying once theres :") 
	$steelInStorageLimit = $gBuildingVar[7]
	osdaddat(OSDVALUE, 330, $valLineY, 100, 25, "steelbuylimit", "$steelInStorageLimit|5|30|5") 
	osdaddat(OSDTEXT, 440, $lineY, 0, 0, "", "Steel in storage") 

	osdaddat(OSDAPPLYBUTTON, 380, 290, 200, 25, "IronAdminApply|&ironbuyval|&ironbuylimit|&steelbuyval|&steelbuylimit", "Apply Changes") 

	osdaddat(OSDBUTTON, 50, 300, 200, 25, "Main", "Main Page") 
	osdaddat(OSDEXITBUTTON, 50, 330, 200, 20, "", "Close") 
	osdactivate() 
}

Event( "OSDSelect", "MedSmithOSD:IronAdmin" )
{
	$ret = MedBlacksmithDisplayOwnerIronSuppliesOSD()
}

Event( "OSDSelect", "MedSmithOSD:SetPrice" )
{
	$itemNum = $gParam[1]
	$price = $gParam[2]
	$itemName = sysGetItemName( $itemNum )
	*setsellprice $gBuildingNum,$itemName,$price
	$ret = MedBlacksmithOwnerSetPricesOSD()
}

Event( "OSDSelect", "MedSmithOSD:ApplySellPrice" )
{
	$itemNum = $gParam[1]
	$newPrice = $gParam[2]
	$itemName = sysGetItemName( $itemNum )
	*setsellprice $gBuildingNum,$itemName,$newPrice
	$ret = MedBlacksmithOwnerSetPricesOSD()
}

Function		MedBlacksmithShowItemSetPriceOSD( $itemNum )
{
	$itemName = sysGetItemName( $itemNum )
	$sellPrice = sysGetSellPrice( $itemNum )

	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 
	osdaddat(OSDTEXT, 20, 10, 560, 0, "", "Note the building can only make and sell items if you have a worker") 
	$ironRequired = 0
	$steelRequired = 0

	if ( $itemNum == 229 )		// Chisel
	{
		$ironRequired = $kChiselIronRequired
	}
	else if ( $itemNum == 115 )		// Plough
	{
		$ironRequired = $kPloughIronRequired
	}
	else if ( $itemNum == 117 )		// Pickaxe
	{
		$ironRequired = $kPickaxeIronRequired
	}
	else if ( $itemNum == 224 )		// Sword
	{
		$steelRequired = $kSwordSteelRequired
	}
	else if ( $itemNum == 242 )		// Armor
	{
		$steelRequired = $kArmorSteelRequired
	}

	$lineY = 50
	osdaddat(OSDBIGTEXT, 50, $lineY, 0, 0, "", "Set Sell Price for $itemName")
	$lineY = $lineY + 30
	if ( $sellPrice > 0 )
	{
		$priceText = sysGetPriceText( $sellPrice )
		osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Currently selling at $priceText.")
	}
	else
	{
		osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Not currently selling")
	}
	$lineY = $lineY + 50
	osdaddat(OSDCASHVALUE, 250, $lineY, 100, 25, "sellprice", "$sellPrice|0|10000") 
	$lineY = $lineY + 40
	osdaddat(OSDBUTTON, 200, $lineY, 200, 25, "ApplySellPrice|$itemNum|&sellPrice", "Apply") 

	$lineY = $lineY + 40

	if ( $gBuildingWage > 0 )
	{
		$wageText = sysGetPriceText( $gBuildingWage )
		osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Your employee will take $wageText of the price for their wages.")
	}
	else
	{
		osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Don't forget your blacksmith's wages will be taken from this price.")
	}
	$lineY = $lineY + 16
	// TODO - Give description of the amount of resource required to make
	if ( $ironRequired > 0 )
	{
		$ironBuyPrice = sysGetBuyPrice( "Iron" )
		if ( $ironBuyPrice > 0 )
		{
			$ironBuyText = sysGetPriceText( $ironBuyPrice )
			osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Requires $ironRequired Iron (which you currently buy for $ironBuyText) to make.")
		}
		else
		{
			osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Requires $ironRequired Iron to make.")
		}
	}
	else
	{
		$steelBuyPrice = sysGetBuyPrice( "Steel" )
		if ( $steelBuyPrice > 0 )
		{
			$steelBuyText = sysGetPriceText( $steelBuyPrice )
			osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Requires $steelRequired Steel (which you currently buy for $steelBuyText)")
		}
		else
		{
			osdaddat(OSDTEXT, 50, $lineY, 500, 0, "", "Requires $steelRequired Steel to make.")
		}
	}


	osdaddat(OSDBUTTON, 50, 300, 200, 25, "Main", "Main Page") 
	osdaddat(OSDEXITBUTTON, 50, 330, 200, 20, "", "Close") 
	osdactivate() 
}

Event( "OSDSelect", "MedSmithOSD:ChangePrice" )
{
	$itemNum = $gParam[1]

	if ( $itemNum == 242 )
	{
		*msg %PLAYER% You cannot currently make Armor
		$ret = MedBlacksmithOwnerSetPricesOSD( )
	}
	else
	{
		$ret = MedBlacksmithShowItemSetPriceOSD( $itemNum )
	}
}

Function		MedBlacksmithShowSetPriceListItem( $itemNum, $lineY )
{
	$itemName = sysGetItemName( $itemNum )
	$sellPrice = sysGetSellPrice( $itemNum )

	if ( $sellPrice > 0 )
	{
		$priceText = sysGetPriceText( $sellPrice )
		osdaddat(OSDBUTTON, 150, $lineY, 250, 20, "ChangePrice|$itemNum", "$itemName ($priceText)")
	}
	else
	{
		osdaddat(OSDBUTTON, 150, $lineY, 250, 20, "ChangePrice|$itemNum", "$itemName (Not selling)")
	}

}

Function		MedBlacksmithOwnerSetPricesOSD( )
{
	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 
	osdaddat(OSDTEXT, 20, 10, 560, 0, "", "Note the building can only make and sell items if you have a worker") 
	
	osdaddat(OSDSMALLTEXT, 150, 31, 0, 0, "", "Sell Price") 
	$lineY = 50

	$ret = MedBlacksmithShowSetPriceListItem( 229, $lineY )		// chisel
	$lineY = $lineY + 30
	$ret = MedBlacksmithShowSetPriceListItem( 115, $lineY )		// plough
	$lineY = $lineY + 30
	$ret = MedBlacksmithShowSetPriceListItem( 117, $lineY )		// pickaxe
	$lineY = $lineY + 30
	$ret = MedBlacksmithShowSetPriceListItem( 224, $lineY )		// Sword
	$lineY = $lineY + 30
	$ret = MedBlacksmithShowSetPriceListItem( 242, $lineY )		// Armor
	$lineY = $lineY + 30

	osdaddat(OSDBUTTON, 50, 300, 200, 25, "Main", "Main Page") 
	osdaddat(OSDEXITBUTTON, 50, 330, 200, 20, "", "Close") 
	osdactivate() 
}


Event( "OSDSelect", "MedSmithOSD:SetPrices" )
{
	$ret = MedBlacksmithOwnerSetPricesOSD()
}

Event( "OSDSelect", "MedSmithOSD:SetWage" )
{
	*setwage $gBuildingNum $gParam[1]
	$ret = MedBlacksmithOwnerWorkerAdminOSD()
}

Event( "OSDSelect", "MedSmithOSD:Sack" )
{
	if ( $gBuildingNumEmployees > 0 )
	{
		$employeeID = sysGetEmployeeID( 0 )
		$name = sysGetPlayerName( $employeeID )
		*removeemployee $gBuildingNum $name
		
		// Clear the ore that was going to go to the employee
		if ( $gBuildingVar[10] > 0 )
		{
			*grantitem $name $gBuildingVar[10] Gold
			$gBuildingVar[10] = 0
		}
		*msg %PLAYER% You fired your worker
	}
   CustomEvent( "MedBlacksmithAccessInit" )
}

Function		MedBlacksmithOwnerWorkerAdminOSD()
{
	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 
//	osdaddat(OSDIMAGE, 30, 10, 128, 128, "", "http://gameislands.net/gamecontent/civilization/menus/constructfarm1.jpg") 

	$wageText = sysGetPriceText( $gBuildingWage )
	if ( $gBuildingNumEmployees > 0 )
	{
		$employeeID = sysGetEmployeeID( 0 )
		$name = sysGetPlayerName( $employeeID )
		
		osdaddat(OSDTEXT, 160,115,440,0, "", "You currently employ $name at a rate of $wageText per item") 
		osdaddat(OSDBUTTON, 220,140,300,25, "Sack", "Fire Worker") 
	}
	else
	{
		osdaddat(OSDBIGTEXT, 20,100,560,0, "", "Set Wage (cost per item made)") 
		osdaddat(OSDVALUE, 215, 140, 150, 25, "wageval", "$gBuildingWage|1|500" ) 
		osdaddat(OSDBUTTON, 200,180,150,20, "SetWage|&wageval", "Confirm") 
		osdaddat(OSDBUTTON, 400,180,150,20, "SetWage|0", "Not employing") 
	}

	if ( $gBuildingWage > 0 )
	{
		osdaddat(OSDTEXT, 20, 250, 560, 20, "", "Current wage is $wageText per item produced.") 
	}
	else
	{
		osdaddat(OSDTEXT, 20, 250, 560, 20, "", "You currently have no wage set so will not be employing anyone") 
	}

	osdaddat(OSDBUTTON, 20, 300, 200, 25, "Main", "Main Page") 
	osdaddat(OSDEXITBUTTON, 20, 335, 150, 20, "", "Close") 
	osdactivate() 
}

Event( "OSDSelect", "MedSmithOSD:Admin" )
{
	$ret = MedBlacksmithOwnerWorkerAdminOSD()
}




Function		MedBlacksmithOwnerOSD( )
{
	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 

	$ironInStocks = sysAmountInStocks( "Iron" )
	$steelInStocks = sysAmountInStocks( "Steel" )
	$ironBuyPrice = sysGetBuyPrice( "Iron" )
	$steelBuyPrice = sysGetBuyPrice( "Steel" )
	$investText = sysGetPriceText( $gBuildingInvestment )
	osdaddat(OSDTEXT, 20, 10, 560, 0, "", "Current investment : $investText") 

	if ( $ironBuyPrice == 0 )
	{
		osdaddat(OSDTEXT, 20, 30, 560, 0, "", "$ironInStocks Iron currently in storage - Not currently buying") 
	}
	else
	{
		$ironBuyPriceText = sysGetPriceText( $ironBuyPrice )
		osdaddat(OSDTEXT, 20, 30, 560, 0, "", "$ironInStocks Iron currently in storage - Buying at $ironBuyPriceText each (Until $gBuildingVar[6] in storage)") 
	}
	if ( $steelBuyPrice == 0 )
	{
		osdaddat(OSDTEXT, 20, 50, 560, 0, "", "$steelInStocks Steel currently in storage - Not currently buying") 
	}
	else
	{
		$steelBuyPriceText = sysGetPriceText( $steelBuyPrice )
		osdaddat(OSDTEXT, 20, 50, 560, 0, "", "$steelInStocks Steel currently in storage - Buying at $steelBuyPriceText each (Until $gBuildingVar[6] in storage)") 
	}
	osdaddat(OSDBUTTON, 200, 80, 200, 25, "IronAdmin", "Supplies Admin") 


	osdaddat(OSDFADEDTEXT, 20, 140, 560, 20, "", "Made here so far:") 
	$lineY = 165
	osdaddat(OSDTEXT, 100, $lineY, 0, 20, "", "$gBuildingVar[3] Chisels") 
	$lineY = $lineY + 20
	osdaddat(OSDTEXT, 100, $lineY, 0, 20, "", "$gBuildingVar[4] Ploughs") 
	$lineY = $lineY + 20
	osdaddat(OSDTEXT, 100, $lineY, 0, 20, "", "$gBuildingVar[5] Pickaxes") 

	$lineY = 165
	osdaddat(OSDTEXT, 350, $lineY, 0, 20, "", "$gBuildingVar[12] Swords") 
	$lineY = $lineY + 20
	osdaddat(OSDTEXT, 350, $lineY, 0, 20, "", "$gBuildingVar[13] Armour") 
	$lineY = $lineY + 20

	$lineY = $lineY + 30
	osdaddat(OSDBUTTON, 200, $lineY, 200, 25, "SetPrices", "Set Prices") 

	$ret = MedBlacksmithShowOwnerEmployeeDisplay( 50, 300, $gBuildingWage, 0, 100 )

	osdaddat(OSDBUTTON, 200, 320, 200, 25, "Admin", "Worker Admin") 

	osdactivate() 

}

Function	MedBlacksmithTaskComplete( $taskNum )
{
	if ( $taskNum == 1 )		// Chisel
	{
		$itemNum = 229
		$gBuildingVar[3] = $gBuildingVar[3] + 1
	}
	else if ( $taskNum == 2 )	// Plough
	{
		$itemNum = 115
		$gBuildingVar[4] = $gBuildingVar[4] + 1
	}
	else if ( $taskNum == 3 )	// Pickaxe
	{
		$itemNum = 117
		$gBuildingVar[5] = $gBuildingVar[5] + 1
	}
	else if ( $taskNum == 4 )	// Sword
	{
		$itemNum = 224
		$gBuildingVar[12] = $gBuildingVar[12] + 1
	}
	else if ( $taskNum == 5 )		// Armor
	{
		$itemNum = 242
		$gBuildingVar[13] = $gBuildingVar[13] + 1
	}

	$itemName = sysGetItemName( $itemNum )
	*grantitem %PLAYER% 1 $itemName 

}

Event( "OSDSelect", "MedSmithOSD:Make" )
{ 
	$price = sysGetSellPrice( $gParam[1] )
	if ( $gParam[1] == 229 )		// Chisel
	{
		$ironRequired = $kChiselIronRequired
	}
	else if ( $gParam[1] == 115 )		// Plough
	{
		$ironRequired = $kPloughIronRequired
	}
	else if ( $gParam[1] == 117 )		// Pickaxe
	{
		$ironRequired = $kPickaxeIronRequired
	}
	else if ( $gParam[1] == 224 )		// Sword
	{
		$steelRequired = $kSwordSteelRequired
	}
	else if ( $gParam[1] == 242 )		// Armor
	{
		$steelRequired = $kArmorSteelRequired
	}

	if ( $price <= $gPlayerCash )
	{
		// If wage is less than price
		if ( $gBuildingWage < $price )
		{
			if ( $ironRequired > 0 )
			{
				$ironInStocks = sysAmountInStocks( "Iron" )

				if ( $ironRequired <= $ironInStocks )
				{	
					$amountForInvest = $price - $gBuildingWage
					// Add wages
					$gBuildingVar[10] = $gBuildingVar[10] + $gBuildingWage

					// Take Gold etc
					*grantcash %PLAYER% -$price
					*addtoinvestment $gBuildingNum $amountForInvest
					*removefromstocks $gBuildingNum,$ironRequired,Iron

					// TODO - Generify this..
					//  NEED an extra param on the dobuildingtask to let us pass in an item number
					if ( $gParam[1] == 229 )		// Chisel
					{
						*dobuildingtask %PLAYER% $gBuildingAccessNum 13 Waiting for the blacksmith to make your chisel
					}
					else if ( $gParam[1] == 115 )		// Plough
					{
						*dobuildingtask %PLAYER% $gBuildingAccessNum 14 Waiting for the blacksmith to make your plough
					}
					else if ( $gParam[1] == 117 )		// Pickaxe
					{
						*dobuildingtask %PLAYER% $gBuildingAccessNum 15 Waiting for the blacksmith to make your pickaxe
					}
					else
					{
						*msg %PLAYER% Error - unknown blacksmith task
					}
				}
			}
			else if ( $steelRequired > 0 )
			{
				$steelInStocks = sysAmountInStocks( "Steel" )

				if ( $steelRequired <= $steelInStocks )
				{	
					$amountForInvest = $price - $gBuildingWage
					// Add wages
					$gBuildingVar[10] = $gBuildingVar[10] + $gBuildingWage

					// Take Gold etc
					*grantcash %PLAYER% -$price
					*addtoinvestment $gBuildingNum $amountForInvest
					*removefromstocks $gBuildingNum,$steelRequired,Steel

					if ( $gParam[1] == 224 )		// Sword
					{
						*dobuildingtask %PLAYER% $gBuildingAccessNum 16 Waiting for the blacksmith to make your sword
					}
					else if ( $gParam[1] == 242 )		// Armor
					{
						*dobuildingtask %PLAYER% $gBuildingAccessNum 17 Waiting for the blacksmith to make your armor
					}
					else
					{
						*msg %PLAYER% Error - unknown blacksmith task
					}
				}
			}
		}
		else
		{
			// TODO - Error(?) - wages less than sell price
			*msg %PLAYER% Error - invalid blacksmith task
		}
	}

}

//-------------------------------------------------------------
//  Visitor item OSD

Function		MedBlacksmithVisitorShowItemOSD( $lineY, $itemNum, $ironRequired, $steelRequired )
{
	$itemName = sysGetItemName( $itemNum )
	$sellPrice = sysGetSellPrice( $itemNum )

	$textY = $lineY + 5
	osdaddat(OSDBIGTEXT, 50, $lineY, 0, 0, "", "$itemName") 
	if ( $sellPrice > 0 )
	{
		$sellPriceText = sysGetPriceText( $sellPrice )
		osdaddat(OSDTEXT, 130, $textY, 0, 0, "", "for $sellPriceText") 
	
		$bCanMake = 0;

		if ( $ironRequired > 0 )
		{
			$ironInStocks = sysAmountInStocks( "Iron" )
			if ( $ironInStocks >= $ironRequired )
			{
				$bCanMake = 1
			}
		}
		else
		{
			$steelInStocks = sysAmountInStocks( "Steel" )
			if ( $steelInStocks >= $steelRequired )
			{
				$bCanMake = 1
			}

		}

		if ( $bCanMake == 1 )
		{
			if ( $gPlayerCash >= $sellPrice )
			{
				osdaddat(OSDEXITBUTTON, 400, $lineY, 150, 25, "Make|$itemNum", "Make $itemName") 
			}
			else
			{
				osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "(Not enough cash)") 
			}
		}
		else
		{
			if ( $ironRequired > 0 )
			{
				osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "(Not enough iron)") 
			}
			else
			{
				osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "(Not enough steel)") 
			}
		}
	}
	else
	{
		osdaddat(OSDTEXT, 130, $textY, 0, 0, "", "(Not for sale)") 
		osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "Purchase $itemName") 
	}
	$textY = $lineY + 19
	if ( $ironRequired > 0 )
	{
		osdaddat(OSDSMALLTEXT, 70, $textY, 0, 0, "", "Requires $ironRequired Iron in building") 
	}
	else
	{
		osdaddat(OSDSMALLTEXT, 70, $textY, 0, 0, "", "Requires $steelRequired Steel in building") 
	}

}

Event( "OSDSelect", "MedSmithOSD:SellIron" )
{
	$ironBuyPrice = sysGetBuyPrice( "Iron" )
	if ( $ironBuyPrice > 0 )
	{
		$ironInStocks = sysAmountInStocks( "Iron" )
		if ( $ironInStocks < $gBuildingVar[6] )
		{
			if ( $gBuildingInvestment >= $ironBuyPrice )
			{
				$hasIron = sysPlayerInventory("Iron")
				if ( $hasIron >= 1 )
				{
					*grantitem %PLAYER% -1 Iron
					*addtostocks $gBuildingNum,1,Iron
					*removefrominvestment $gBuildingNum $ironBuyPrice
					*grantcash %PLAYER% $ironBuyPrice
				}
			}
		}
	}
	$ret = MedBlacksmithVisitorOSD()
}

Event( "OSDSelect", "MedSmithOSD:Collect" )
{
	$employeeID = sysGetEmployeeID( 0 )
	if ( $employeeID == $gPlayerID )
	{
		if ( $gBuildingVar[10] > 0 )
		{
			*grantcash %PLAYER% $gBuildingVar[10]
			$gBuildingVar[10] = 0
		}
	}
	$ret = MedBlacksmithVisitorOSD()
}

Event( "OSDSelect", "MedSmithOSD:JobApply" )
{
	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 
	$hasSkill = sysPlayerSkillLevel("Blacksmith")

	if ( $hasSkill < 1 )
	{
		osdaddat(OSDTEXT, 10, 160, 580, 20, "", "You need the 'Blacksmith' skill to be able to take a job here.") 
	}
	else 
	{
		$oldJobCount = $gPlayerNumJobs
		*addemployee $gBuildingNum %PLAYER%
		if ( $gPlayerNumJobs > $oldJobCount )
		{
			osdaddat(OSDTEXT, 10, 160, 580, 20, "", "Welcome to your new job.") 
			osdaddat(OSDTEXT, 50, 190, 500, 20, "", "You don't need to do anything to work here - though filling the building with Iron may help keep wages regular - just come back occasionally to collect your pay") 
		}
		else
		{
			osdaddat(OSDTEXT, 10, 160, 580, 20, "", "There was a problem with your application.") 	
			osdaddat(OSDTEXT, 10, 190, 580, 20, "", "Please try again later.") 	
		}
	}
	
	osdaddat(OSDEXITBUTTON, 50, 330, 200, 20, "", "Close") 
	osdactivate() 
}

Event( "OSDSelect", "MedSmithOSD:QuitJob" )
{
	*removeemployee $gBuildingNum %PLAYER%
	*msg %PLAYER% You left your job at the Blacksmith
	$ret = MedBlacksmithVisitorOSD()
}


Function		MedBlacksmithVisitorOSD( )
{
	$numEmployed = $gBuildingNumEmployees;
	$maxEmployed = $gBuildingMaxEmployees;
	$employeeID = sysGetEmployeeID( 0 )
	$ironInStocks = sysAmountInStocks( "Iron" )
	$steelInStocks = sysAmountInStocks( "Steel" )

	osdcreate(OSDBUILDING,"MedSmithOSD", "" ) 

	if ( $numEmployed > 0 )
	{
		if ( $gBuildingInvestment > 0 )
		{
			$investText = sysGetPriceText( $gBuildingInvestment )
		}
		else
		{	
			$investText = "nothing"
		}
		osdaddat(OSDTEXT, 50, 0, 0, 0, "", "The smith will make items for you for a price..") 
		osdaddat(OSDFADEDSMALLTEXT, 90, 15, 0, 0, "", "  (Current stocks : $ironInStocks Iron, $steelInStocks Steel. Investment : $investText)") 

		$ret = MedBlacksmithVisitorShowItemOSD( 40, 229, $kChiselIronRequired, 0 )		// Chisel
		$ret = MedBlacksmithVisitorShowItemOSD( 80, 115, $kPloughIronRequired, 0 )		// Plough
		$ret = MedBlacksmithVisitorShowItemOSD( 120, 117, $kPickaxeIronRequired, 0 )		// Pickaxe
		$ret = MedBlacksmithVisitorShowItemOSD( 160, 224, 0, $kSwordSteelRequired )		// Sword
		$ret = MedBlacksmithVisitorShowItemOSD( 200, 242, 0, $kArmorSteelRequired )		// Armor

		$name = sysGetPlayerName( $employeeID )
		osdaddat(OSDTEXT, 20, 250, 560, 20, "", "Employs: $name (Blacksmith)") 

		if ( $employeeID == $gPlayerID )
		{
			osdaddat(OSDBUTTON, 30, 250, 100, 20, "QuitJob", "Quit Job") 

			if ( $gBuildingVar[10] > 0 )
			{
				osdaddat(OSDBUTTON, 460, 250, 100, 20, "Collect", "Collect pay") 
			}
			else
			{
				osdaddat(OSDFADEDBUTTON, 460, 250, 100, 20, "", "Collect pay") 
			}

			$wageText = sysGetPriceText( $gBuildingVar[10] )
			osdaddat(OSDSMALLTEXT, 460, 275, 100, 20, "", "$wageText") 
		}
	}
	else
	{
		osdaddat(OSDTEXT, 20, 80, 560, 0, "", "The Blacksmith building does not function unless it has an employee") 
		
		if ( $gBuildingWage == 0 )
		{
			osdaddat(OSDTEXT, 20, 170, 560, 20, "", "Not currently seeking an employee") 
		}
		else if ( $numEmployed < $maxEmployed )
		{
			$wageText = sysGetPriceText( $gBuildingWage )
			osdaddat(OSDTEXT, 40, 170, 520, 20, "", "There is currently a job available for a Blacksmith at $wageText per item produced.") 
			if ( $gPlayerNumJobs < $kMaxNumJobs )
			{
				$isBlacksmith = sysPlayerSkillLevel("Blacksmith")
				if ( $isBlacksmith > 0 )
				{
					if ( $gPlayerNumJobs < $kMaxNumJobs )
					{
						osdaddat(OSDBUTTON, 150, 200, 300, 30, "JobApply", "Apply for Job") 			
					}
					else
					{
						osdaddat(OSDFADEDBUTTON, 150, 200, 300, 30, "", "(You already have a job elsewhere)") 			
					}
				}
				else
				{
					osdaddat(OSDFADEDBUTTON, 150, 200, 300, 30, "", "(Blacksmith skill required)") 			
				}
			}
			else
			{
				osdaddat(OSDFADEDBUTTON, 150, 200, 300, 30, "", "(You already have a job)") 
			}	
		}
	}

	$hasShownMessage = 0
	$ironBuyPrice = sysGetBuyPrice( "Iron" )
	$steelBuyPrice = sysGetBuyPrice( "Steel" )
	if ( $ironBuyPrice > 0 )
	{
		if ( $ironInStocks < $gBuildingVar[6] )
		{
			if ( $gBuildingInvestment >= $ironBuyPrice )
			{
				$ironBuyPriceText = sysGetPriceText( $ironBuyPrice )
				if ( $steelBuyPrice > 0 )
				{
					$steelBuyPriceText = sysGetPriceText( $steelBuyPrice )
					osdaddat(OSDTEXT, 40, 290, 520, 20, "", "This building will buy Iron at $ironBuyPriceText and Steel at $steelBuyPriceText") 
				}
				else
				{
					osdaddat(OSDTEXT, 40, 290, 520, 20, "", "This building will buy Iron at $ironBuyPriceText per iron") 
				}
				$hasShownMessage = 1
				$buttonX = 310
				$hasIron = sysPlayerInventory("Iron")
				if ( $hasIron > 0 )
				{
					osdaddat(OSDBUTTON, $buttonX, 310, 130, 25, "SellIron", "Sell Iron") 
				}
				else
				{
					osdaddat(OSDFADEDBUTTON, $buttonX, 310, 130, 25, "", "Sell Iron") 
				}	
			}
		}		
	}

	if ( $steelBuyPrice > 0 )
	{
		if ( $steelInStocks < $gBuildingVar[7] )
		{
			if ( $gBuildingInvestment >= $steelBuyPrice )
			{
				$buttonX = 450
				$hasSteel = sysPlayerInventory("Steel")
				if ( $hasSteel > 0 )
				{
					osdaddat(OSDBUTTON, $buttonX, 310, 140, 25, "SellSteel", "Sell Steel") 
				}
				else
				{
					osdaddat(OSDFADEDBUTTON, $buttonX, 310, 140, 25, "", "Sell Steel") 
				}	
			}
		}
	}

	if ( $employeeID == $gPlayerID )
	{
		if ( $hasShownMessage == 0 )
		{
			osdaddat(OSDTEXT, 20, 290, 560, 0, "", "$ironInStocks Iron, $steelInStocks Steel currently in storage") 
		}

		if ( $ironInStocks < $kMaxIronInStorage )
		{
			$hasIron = sysPlayerInventory("Iron")
			if ( $hasIron > 0 )
			{
				osdaddat(OSDBUTTON, 40, 310, 120, 25, "WorkerAddIron", "Add Iron") 
			}
			else
			{
				osdaddat(OSDFADEDBUTTON, 40, 310, 120, 25, "", "Add Iron") 
			}
		}
		else
		{
			osdaddat(OSDFADEDBUTTON, 40, 310, 120, 25, "", "Add Iron") 
		}

		if ( $steelInStocks < $kMaxSteelInStorage )
		{
			$hasSteel = sysPlayerInventory("Steel")
			if ( $hasSteel > 0 )
			{
				osdaddat(OSDBUTTON, 170, 310, 120, 25, "WorkerAddSteel", "Add Steel") 
			}
			else
			{
				osdaddat(OSDFADEDBUTTON, 170, 310, 120, 25, "", "Add Steel") 
			}
		}
		else
		{
			osdaddat(OSDFADEDBUTTON, 170, 310, 120, 25, "", "Add Steel") 
		}
	}
	else
	{
		if ( $hasShownMessage == 0 )
		{
			osdaddat(OSDTEXT, 20, 290, 560, 0, "", "$ironInStocks Iron, $steelInStocks Steel currently in storage") 
		}
	}

	osdaddat(OSDEXITBUTTON, 10, 340, 150, 20, "", "Close") 
	osdactivate() 
}



Event( "Custom", "MedBlacksmithAccessInit" )
{
	if ( $gBuildingOwnerID == $gPlayerID )
	{
		$ret = MedBlacksmithOwnerOSD()
	}
	else
	{
		$ret = MedBlacksmithVisitorOSD()
	}

}



Event( "AccessBuilding", "$kMedBlacksmithBuildingNum" )
{
   CustomEvent( "MedBlacksmithAccessInit" )
}

Event( "NewBuilding", "$kMedBlacksmithBuildingNum" )
{
	$gBuildingVar[6] = 10
}
Please let me know if you have any questions
User avatar
matias
World Owner
World Owner
Posts: 46
Joined: Wed Apr 24, 2013 7:37 pm

Post by matias »

Thanks men almost save a week of scripting
User avatar
MasterNiCron
Posts: 582
Joined: Wed Feb 17, 2010 10:57 pm
Location: Frost

Post by MasterNiCron »

:shock: holy hell and thats just the blacksmith shop :shock:
User avatar
morbydvisns
Posts: 1889
Joined: Sun Jan 30, 2005 12:51 am

Post by morbydvisns »

Image
User avatar
zaroba
World Owner
World Owner
Posts: 7257
Joined: Fri Oct 10, 2003 11:06 pm
Location: Hereford, PA
Contact:

Post by zaroba »

(posts a 1500 pixel line just to make the topic wider so the script text wont wrap and it is easier to copy/paste)

Image
User avatar
zaroba
World Owner
World Owner
Posts: 7257
Joined: Fri Oct 10, 2003 11:06 pm
Location: Hereford, PA
Contact:

Post by zaroba »

and it still wraps a little. damn.
User avatar
zaroba
World Owner
World Owner
Posts: 7257
Joined: Fri Oct 10, 2003 11:06 pm
Location: Hereford, PA
Contact:

Post by zaroba »

Code: Select all

1	-----	Function      MedBlacksmithVisitorShowItemOSD( $lineY, $itemNum, $ironRequired, $steelRequired )
2	-----	   {
3	-----	   $itemName = sysGetItemName( $itemNum )
4	-----	   $sellPrice = sysGetSellPrice( $itemNum )
5	-----	   $textY = $lineY + 5
6	-----	   osdaddat(OSDBIGTEXT, 50, $lineY, 0, 0, "", "$itemName")
7	-----	   if ( $sellPrice > 0 )
8	-----	      {
9	-----	      $sellPriceText = sysGetPriceText( $sellPrice )
10	-----	      osdaddat(OSDTEXT, 130, $textY, 0, 0, "", "for $sellPriceText")
11	-----	      $bCanMake = 0;
12	-----	      if ( $ironRequired > 0 )
13	-----	         {
14	-----	         $ironInStocks = sysAmountInStocks( "Iron" )
15	-----	         if ( $ironInStocks >= $ironRequired )
16	-----	            {
17	-----	            $bCanMake = 1
18	-----	            }
19	-----	         }
20	-----	      else
21	-----	         {
22	-----	         $steelInStocks = sysAmountInStocks( "Steel" )
23	-----	         if ( $steelInStocks >= $steelRequired )
24	-----	            {
25	-----	            $bCanMake = 1
26	-----	            }
27	-----	         }
28	-----	      if ( $bCanMake == 1 )
29	-----	         {
30	-----	         if ( $gPlayerCash >= $sellPrice )
31	-----	            {
32	-----	            osdaddat(OSDEXITBUTTON, 400, $lineY, 150, 25, "Make|$itemNum", "Make $itemName")
33	-----	            }
34	-----	         else
35	-----	            {
36	-----	            osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "(Not enough cash)")
37	-----	            }
38	-----	         }
39	-----	      else
40	-----	         {
41	-----	         if ( $ironRequired > 0 )
42	-----	            {
43	-----	            osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "(Not enough iron)")
44	-----	            }
45	-----	         else
46	-----	            {
47	-----	            osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "(Not enough steel)")
48	-----	            }
49	-----	         }
50	-----	      }
51	-----	   else
52	-----	      {
53	-----	      osdaddat(OSDTEXT, 130, $textY, 0, 0, "", "(Not for sale)")
54	-----	      osdaddat(OSDFADEDBUTTON, 400, $lineY, 150, 25, "", "Purchase $itemName")
55	-----	      }
56	-----	   $textY = $lineY + 19
57	-----	   if ( $ironRequired > 0 )
58	-----	      {
59	-----	      osdaddat(OSDSMALLTEXT, 70, $textY, 0, 0, "", "Requires $ironRequired Iron in building")
60	-----	      }
61	-----	   else
62	-----	      {
63	-----	      osdaddat(OSDSMALLTEXT, 70, $textY, 0, 0, "", "Requires $steelRequired Steel in building")
64	-----	      }
65	-----	   } 

Got a question about this thing mit, and partially because I'm not experienced with the function stuff yet, but I see alot of the functions in your script looking like the above, with IFs then ELSEs and then more stuff.
If the script parser is going down an event, an IF doesn't apply, it will then go to the ELSE and then stop wont it?

Like in the above. Imagining the path the script parser would take:
it starts at 1, assuming the IF at 7 applies it will go in there, if the IF at 12 does not apply, it will use the ELSE at 20 and then stop, wont it?
if the IF at 7 doesn't apply, would the script engine go in the ELSE at 51 and then stop and not perform the var change at 56 or the following IF or ELSE?

or don't functions work like that?
or has the script engine been changed so that an ELSE is only counted for the previous IF and the parser will keep going until the end? But then, what if there are ELSE IFs? Does this stuff apply to regular events as well?
User avatar
Mit
Staff
Staff
Posts: 3551
Joined: Sun Sep 21, 2003 10:14 pm
Location: Unknown

Post by Mit »

if the IF at 7 doesn't apply, would the script engine go in the ELSE at 51 and then stop and not perform the var change at 56 or the following IF or ELSE?
conditionals ( If/elseif/else ) dont work like that.. the script doesn't stop after executing the bit in the appropriate conditional. In the case quoted, after running through the ELSE block at 51, it would continue and perform the var change at 56 and then go through the following if/else.

Some examples

Code: Select all

if ( $check == 1 )
{
    $CodeA = 1
}
else
{
    $CodeB = 1
}

$CodeC = 1
Here, if $check equals1, then Code A and Code C are set, otherwise Code B and Code C are set. Bit more involved..

Code: Select all

if ( $check == 1 )
{
    if ( $balance == 1 )
    {
        $codeD = 1
     }
    elseif ( $balance == 2 )
    {
        $codeE = 1
    }
    $CodeA = 1
}
else
{
    $CodeB = 1
}

$CodeC = 1
Here, if $check is not equal to 1 then code B and Code C are set as before. If $check is 1 and $balance is 1, CodeA, CodeC and CodeD are set. If $check is 1 and $balance is 2, CodeA, CodeC and CodeE are set. Otherwise if $check is 1 and theres any other value for $balance, only CodeA and CodeC are set.

(This is probably not the best way of explaining things - but the formatting is C-like, the system is much the same as is used in most programming langues so you can probably a better explanation by a bit of googling.)

Functions are a competely separate element and don't affect the flow of anything from where theyre called. Effectively you can consider them just like another * command or any other basic operation, except they happen to call another block of your code. Functions are massively useful for keeping your code clean, maintainable and easy-to-read.
Locked