text: # ------------------------------------------------------
  # Last Update: modified by Valminder Jan 15, 2014.
  # ------------------------------------------------------
  #
  # TP Treasure Generator
  #
  # Modified by Valminder, January 2014
  #     Added new variables to link to AddComma
  #     Added link to: 
  #          AddComma.Start
  #
  
  /Overriderolls 1
  
  %encLvl%,0
  %coinsCP%,0
  %coinsSP%,0
  %coinsGP%,0
  %coinsPP%,0
  %itemList%,0
  %temp%,0
  %ValueEnc%,0
  %Val1%,0
  
  %cont%,0
  %ctr%,0
  
  %item%,0
  %value%,0
  %weight%,0
  %aura%,0
  %desc%,0
  %appraiseDC%,0
  
  %coinTotal%,0
  %magicPwr%,0
  %miscItemTargetTotal%,0
  %miscItemActualTotal%,0
  %hoardTotal%,0
  
  %limitLower%,0
  %limitUpper%,0
  
  %idx%,0
  %numItems%,0
  %itemID%,1
  %listFirst%,0
  
  %title%,0
  %deviation%,0
  %maxTries%,0
  %iterations%,1
  %unsuccessful%,1
  %match%,0
  %randomCharges%,0
  %splitCoins%,0
  %coinsToSplit%,0
  %splitType%,0
  %numAppraiseValues%,0
  %pctMiscItems%,0
  %hoardType%,0
  %dispVal%,0
  %showInst%,0
  
  # ----------------------------------------------------------------------
  
  # @showInst,1,Show Instructions?,Yes,No
  @showInst,2,Show Instructions?,Yes,No
  @title,Treasure Trove,Enter a name for this hoard:
  # @encLvl,1,Enter Encounter Level:
  @encLvl,15,Enter Encounter Level: (max 20)
  @hoardType,1,What type of hoard is this?,Personal,Lair
  @match,1,Match average value (within a percentage you define)?,No,Yes
  @deviation,15,What percent deviation from average value is permissible?
  @randomCharges,1,Random number of charges for wands and staves?,No,Yes
  # @splitCoins,1,Divide coins up?,No,Yes
  @splitCoins,2,Divide coins up?,No,Yes
  @pctMiscItems,5,What percentage of coins should be converted to items?
  @numAppraiseValues,3,Number of failed appraisal values to list:
  @maxTries,100,How many tries before giving up?
  @listFirst,1,List Order:,DM then Player,Player then DM
  
  # ----------------------------------------------------------------------
  
  :Start
  1,{If~{Version~}<5?This table requires version 5 or higher of TableSmith./[RealStart]}
  
  :RealStart
  1,{Iif~%showInst%=1?[Instructions]:[Generate]}
  
  :Instructions
  1,<b>Treasure Generator v4</b>
  _<p>
  _Welcome to the Treasure Generator! This table uses the Treasure Generation rules in the
  _ <i>DMG</i> as well as other information to generate a treasure hoard for encounters.
  _</p>
  _<p>
  _The parameters for this table are detailed below. If you wish to customize the table for your campaign, 
  _ you may edit the table itself (accessible by the "open book" button to the left, found between the d6 
  _ and the arrow-wrapped globe, or "Edit Table" on the menu bar above).
  _<ul>
  _<li><b>Enter a name for this hoard:</b> This is optional. The hoard name is printed out for the DM's list
  _ and the player's list. This allows you to stay in sync if you print out the hoard you've generated.</li>
  _<li><b>Enter Encounter Level:</b> Select the level of the encounter. Valid values are from 1 to 20.</li>
  _<li><b>Type of Hoard:</b> This determines if the treasure comes from an individual ("Personal") or is
  _located in a lair of some time.</li>
  _<li><b>Match Average Value:</b> Each encounter level (EL) has an average value, though random rolling on
  _ the treasure tables could give you a hoard whose total value is much higher or lower than the average. 
  _ Selecting "Yes" for this parameter means the generated results will match the average value for the EL,
  _ within the deviation you provide below.</li>
  _<li><b>Percentage Deviation:</b> This sets a range for the total value of a treasure hoard based on the
  _ average value for the EL. The percentage deviation puts an upper and lower limit on the total of the
  _ generated horde. For instance, if this value is "10" (10&#37;), and the average value for an encounter is 900,
  _ then the result can be anywhere from 810-990 (900-10&#37;, 900+10&#37;).</li>
  _<li><b>Random number of charges:</b> Selecting "No" means when wands or staves are generated they will be
  _ fully charged (50 charges). "Yes" means they will have a random number of charges, up to 50.</li>
  _<li><b>Divide Coins Up:</b> In the standard tables whenever coins result they are all of one type. Saying 
  _ "Yes" to this parameter will divide up the generated coins into copper, silver, gold, and sometimes platinum.</li>
  _<li><b>Percentage to Convert to Mundane:</b> Rather than having hundreds, or thousands, or even 
  _ tens of thousands of coins, you may find it more realistic to have some of the treasure be in the form 
  _ of mundane items - trade goods, artwork, furniture, etc. With this option, you can convert a portion of 
  _ the treasure you generate into mundane goods.</li>
  _<li><b>Number of failed appraisal values to list:</b> For items that may be appraised by characters with the
  _ "Appraise" skill, you may generate values to use when the skill check fails.
  _<li><b>How Many Tries:</b> This is how many times the table should generate a hoard that doesn't
  _ fall within the deviation above before giving up. This is ignored if "Match Average Value" is "No".</li>
  _<li><b>List Order:</b> Two lists are generated by this table, one for the DM (which lists values, Appraisal
  _ DCs, what magic items <i>actually</i> are, etc.) and one for the players. This parameter allows you to
  _ specify which is shown first.</li>
  _</ul>
  _</p>
  
  :Generate
  1,|limitLower={Calc~[ValuePerEnc=%encLvl%]-{Calc~[ValuePerEnc=%encLvl%]*{Calc~%deviation%/100}}}|
  _|limitUpper={Calc~[ValuePerEnc=%encLvl%]+{Calc~[ValuePerEnc=%encLvl%]*{Calc~%deviation%/100}}}|
  _{While~%iterations%<=%maxTries%,{Status~Attempt #%iterations% of %maxTries%}[GenTreasure]|iterations+1|}
  _{Iif~%unsuccessful%=1?Unable to generate hoard within specified parameters.:[FinishTreasure][DisplayTreasure=%listFirst%]}{Status~}
  
  :GenTreasure
  1,|itemList=||coinsPP=0||coinsGP=0||coinsSP=0||coinsCP=0|
  _{DSCreate~itemList,id,0,name,xxx,value,0,weight,0,desc,xxx,magic,0,aura,xxx,appraise,0,appraiseDC,12}
  _[GenCoins%encLvl%]
  _{If~%pctMiscItems%>0?[GenOtherMundane]}
  _{If~%splitCoins%=2?[SplitCoins]}
  _[GenGoods%encLvl%]
  _[GenItems%encLvl%]
  _|coinTotal={Calc~(%coinsPP%*10)+(%coinsGP%)+(%coinsSP%/10)+(%coinsCP%/100)}|
  _|hoardTotal={Calc~{DSCalc~itemList,sum,value}+%coinTotal%}|
  _{If~%match%=2?[CheckTotal]/|iterations={Calc~%maxTries%+1}||unsuccessful=0|}
  
  :CheckTotal
  1,{If~{And~%hoardTotal%>=%limitLower%,%hoardTotal%<=%limitUpper%}=1?|iterations={Calc~%maxTries%+1}||unsuccessful=0|}
  
  :FinishTreasure
  1,|itemList={CommaReplace~1,XXXcommaXXX,%itemList%}|
  _{DSSort~itemList,desc,0}
  _|idx=1||numItems={DSCount~itemList}|
  _{Loop~%numItems%,{DSSet~itemList,%idx%,id,%idx%}}
  
  :DisplayTreasure
  1,{Bold~%title%} - [DMList]{CR~}[PageBreak]{Line~center,100}{Bold~%title%} - [PlayerList]
  2,[PlayerList]{CR~}[PageBreak]{Line~center,100}[DMList]
  
  :PageBreak
  1,<br style="page-break-after:always;" />
  
  :PlayerList
  1,{Bold~Players List}{CR~}{CR~}
  _[DisplayCoins]
  _{CR~}{Bold~{Italic~Items}}{CR~}
  _|idx=1|
  _{Loop~%numItems%,%idx%: {DSGet~itemList,%idx%,desc} ({DSGet~itemList,%idx%,weight} lbs.)|idx+1|{CR~}}
  
  :DMList
  1,|ValueEnc=[ValuePerEnc=%encLvl%]|
  _{Bold~DM Item List (EL: %encLvl%, Average: [AddComma.Start(%ValueEnc%)])}{CR~}
  _{CR~}{Italic~Total Value: [AddComma.Start(%hoardTotal%)] gp}{CR~}{CR~}
  _[DisplayCoins]
  _{CR~}{Bold~{Italic~Items}}{CR~}
  _|idx=1|
  _{Loop~%numItems%,%idx%: {Iif~{DSGet~itemList,%idx%,magic}=1?{Italic~[DisplayDMItem] (Aura: {DSGet~itemList,%idx%,aura})}:[DisplayDMItem]}|idx+1|
  _{CR~}}
  
  :DisplayCoins
  1,{If~{And~%splitCoins%=1,{Calc~%coinsCP%+%coinsSP%+%coinsGP%+%coinsPP%}!={Trunc~{Calc~%coinsCP%+%coinsSP%+%coinsGP%+%coinsPP%}}}=1?[ConvertCoinFractions]}
  _{Bold~{Italic~Coins}}{CR~}
  _{If~%coinsCP%!=0?[AddComma.Start(%coinsCP%)] cp{CR~}}
  _{If~%coinsSP%!=0?[AddComma.Start(%coinsSP%)] sp{CR~}}
  _{If~%coinsGP%!=0?[AddComma.Start(%coinsGP%)] gp{CR~}}
  _{If~%coinsPP%!=0?[AddComma.Start(%coinsPP%)] pp{CR~}}
  _{If~{Calc~%coinsCP%+%coinsSP%+%coinsGP%+%coinsPP%}=0?None{CR~}}
  
  :ConvertCoinFractions
  1,{If~%coinsCP%>0?|splitType=1|}
  _{If~%coinsSP%>0?|splitType=2|}
  _{If~%coinsGP%>0?|splitType=3|}
  _{If~%coinsPP%>0?|splitType=4|}
  _[ConvertCoins=%splitType%]
  
  :ConvertCoins
  1,
  2,|coinsCP={Round~0,{Calc~10*(%coinsSP%-{Trunc~%coinsSP%})}}||coinsSP={Trunc~%coinsSP%}|
  3,|coinsSP={Round~1,{Calc~10*(%coinsGP%-{Trunc~%coinsGP%})}}||coinsGP={Trunc~%coinsGP%}|[ConvertCoins=2]
  4,|coinsGP={Round~2,{Calc~10*(%coinsPP%-{Trunc~%coinsPP%})}}||coinsPP={Trunc~%coinsPP%}|[ConvertCoins=3]
  
  :DisplayDMItem
  1,{DSGet~itemList,%idx%,name}
  _|value={DSGet~itemList,%idx%,value}| ([DisplayValue] )
  #_|value={DSGet~itemList,%idx%,value}| ( %value% gp )
  _{If~{DSGet~itemList,%idx%,appraise}=1? (Appraise DC: {DSGet~itemList,%idx%,appraiseDC}
  _, fail:{Loop~%numAppraiseValues%, {Round~0,{Calc~{Dice~2d6+3}*0.1*%value%}}})}
  #_, fail:{Loop~%numAppraiseValues%, |Val1={Round~0,{Calc~{Dice~2d6+3}*0.1*%value%}}|[AddComma.Start(%Val1%)]})}
  
  :DisplayValue
  1,|dispVal={Calc~%value%*100}|
  _{If~%dispVal%>=100? |Val1={Trunc~{Calc~%dispVal%/100}}|[AddComma.Start(%Val1%)] gp|dispVal={Mod~%dispVal%,100}|}
  _{If~%dispVal%>=10? {Trunc~{Calc~%dispVal%/10}} sp|dispVal={Mod~%dispVal%,10}|}
  _{If~%dispVal%>0? %dispVal% cp}
  _{If~%value%=0? 0 gp}
  
  # ----------------------------------------------------------------------
  
  # Distribution is as such:
  # 
  #   Copper is primary:		65% cp / 30% sp / 5% gp  / 0% pp
  #   Silver is primary:		10% cp / 65% sp / 25% gp / 0% pp
  #   Gold is primary:		5% cp  / 20% sp / 65% gp / 10% pp
  #   Platinum is primary:	1% cp  / 12% sp / 22% gp / 65% pp
  #
  :SplitCoins
  1,{If~%coinsCP%>0?|splitType=1|}
  _{If~%coinsSP%>0?|splitType=2|}
  _{If~%coinsGP%>0?|splitType=3|}
  _{If~%coinsPP%>0?|splitType=4|}
  _|coinsToSplit={Calc~%coinsCP%+{Calc~%coinsSP%*10}+{Calc~%coinsGP%*100}+{Calc~%coinsPP%*1000}}|
  _[SplitUp=%splitType%]
  _|coinsCP={Calc~%coinsToSplit%-(%coinsPP%*1000)-(%coinsGP%*100)-(%coinsSP%*10)}|
  
  :SplitUp
  1,|coinsPP={Trunc~{Calc~(%coinsToSplit%*0.00)/1000}}||coinsGP={Trunc~{Calc~(%coinsToSplit%*0.05)/100}}||coinsSP={Trunc~{Calc~(%coinsToSplit%*0.30)/10}}|
  2,|coinsPP={Trunc~{Calc~(%coinsToSplit%*0.00)/1000}}||coinsGP={Trunc~{Calc~(%coinsToSplit%*0.25)/100}}||coinsSP={Trunc~{Calc~(%coinsToSplit%*0.65)/10}}|
  3,|coinsPP={Trunc~{Calc~(%coinsToSplit%*0.10)/1000}}||coinsGP={Trunc~{Calc~(%coinsToSplit%*0.65)/100}}||coinsSP={Trunc~{Calc~(%coinsToSplit%*0.20)/10}}|
  4,|coinsPP={Trunc~{Calc~(%coinsToSplit%*0.65)/1000}}||coinsGP={Trunc~{Calc~(%coinsToSplit%*0.22)/100}}||coinsSP={Trunc~{Calc~(%coinsToSplit%*0.12)/10}}|
  
  # ----------------------------------------------------------------------
  
  ;GenOtherMundane
  1,{If~%coinsCP%>0?|miscItemTargetTotal={Calc~(%coinsCP%/100)}|}
  _{If~%coinsSP%>0?|miscItemTargetTotal={Calc~(%coinsSP%/100)}|}
  _{If~%coinsGP%>0?|miscItemTargetTotal=%coinsGP%|}
  _{If~%coinsPP%>0?|miscItemTargetTotal={Calc~(%coinsPP%*10)}|}
  _|miscItemTargetTotal*{Round~2,{Calc~%pctMiscItems%/100}}|
  _|cont=1||ctr=0|
  _{While~%cont%,[GenMiscItem]}
  _[AdjustCoinage]
  
  ;GenMiscItem
  1,|temp=[MiscItem{Param~hoardType,%hoardType%}]|{Split~temp,"!",item,value,weight,appraiseDC}
  _{Iif~%value%>=%miscItemTargetTotal%?|ctr+1|:
  	_|miscItemTargetTotal-%value%||item={CommaReplace~0,XXXcommaXXX,%item%}|
  	_{DSSet~itemList,{DSAdd~itemList},name,%item%,value,%value%,desc,%item%,weight,%weight%,appraise,1,appraiseDC,%appraiseDC%}
  	_|miscItemActualTotal+%value%|
  	_|ctr=0|
  _}
  _{If~%miscItemTargetTotal%<=0?|cont=0|}
  _{If~%ctr%=5?|cont=0|}
  
  :AdjustCoinage
  1,{If~%coinsCP%>0?|coinsCP-{Calc~%miscItemActualTotal%*100}|}
  _{If~%coinsSP%>0?|coinsSP-{Calc~%miscItemActualTotal%*10}|}
  _{If~%coinsGP%>0?|coinsGP-%miscItemActualTotal%|}
  _{If~%coinsPP%>0?|coinsPP-{Calc~%miscItemActualTotal%/10}|}
  
  ;MiscItemPersonal
  5,[TP Clothing.Start(2)]
  3,[TP Miscellaneous.Start(2)]
  2,[TP Musical Instrument.Start(1,2)]
  1,[TP Figurine.Start(2)]
  
  ;MiscItemLair
  10,[MiscItemPersonal]
  5,[TP Trade Goods.Start(2)]
  2,[TP Musical Instrument.Start(2,2)]
  1,[TP Furniture.Start(2)]
  1,[TP Statue.Start(2)]
  
  # ----------------------------------------------------------------------
  
  :GenCoins1
  1-14,
  15-29,|coinsCP={Dice~1d6*1000}|
  30-52,|coinsSP={Dice~1d8*100}|
  53-95,|coinsGP={Dice~2d8*10}|
  96-100,|coinsPP={Dice~1d4*10}|
  
  :GenGoods1
  1-90,
  91-95,[GenGems]
  96-100,[GenArt]
  
  :GenItems1
  1-71,
  72-95,[GenMundane]
  96-100,|magicPwr=minor|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins2
  1-13,
  14-23,|coinsCP={Dice~1d10*1000}|
  24-43,|coinsSP={Dice~2d10*100}|
  44-95,|coinsGP={Dice~4d10*10}|
  96-100,|coinsPP={Dice~2d8*10}|
  
  :GenGoods2
  1-81,
  82-95,{Loop~{Dice~1d3},[GenGems]}
  96-100,{Loop~{Dice~1d3},[GenArt]}
  
  :GenItems2
  1-49,
  50-85,[GenMundane]
  86-100,|magicPwr=minor|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins3
  1-11,
  12-21,|coinsCP={Dice~2d10*1000}|
  22-41,|coinsSP={Dice~4d8*100}|
  42-95,|coinsGP={Dice~1d4*100}|
  96-100,|coinsPP={Dice~1d10*10}|
  
  :GenGoods3
  1-77,
  78-95,{Loop~{Dice~1d3},[GenGems]}
  96-100,{Loop~{Dice~1d3},[GenArt]}
  
  :GenItems3
  1-49,
  50-79,{Loop~{Dice~1d3}, [GenMundane]}
  80-100,|magicPwr=minor|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins4
  1-11,
  12-21,|coinsCP={Dice~3d10*1000}|
  22-41,|coinsSP={Dice~4d12*1000}|
  42-95,|coinsGP={Dice~1d6*100}|
  96-100,|coinsPP={Dice~1d8*10}|
  
  :GenGoods4
  1-70,
  71-95,{Loop~{Dice~1d4},[GenGems]}
  96-100,{Loop~{Dice~1d3},[GenArt]}
  
  :GenItems4
  1-42,
  43-62,{Loop~{Dice~1d4}, [GenMundane]}
  63-100,|magicPwr=minor|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins5
  1-10,
  11-19,|coinsCP={Dice~1d4*10000}|
  20-38,|coinsSP={Dice~1d6*1000}|
  39-95,|coinsGP={Dice~1d8*100}|
  96-100,|coinsPP={Dice~1d10*10}|
  
  :GenGoods5
  1-60,
  61-95,{Loop~{Dice~1d4},[GenGems]}
  96-100,{Loop~{Dice~1d4},[GenArt]}
  
  :GenItems5
  1-57,
  58-67,{Loop~{Dice~1d4}, [GenMundane]}
  68-100,|magicPwr=minor|{Loop~{Dice~1d3},[GenMagic]}
  
  # ----------------------------------------------------------------------
  
  :GenCoins6
  1-10,
  11-18,|coinsCP={Dice~1d6*10000}|
  19-37,|coinsSP={Dice~1d8*1000}|
  38-95,|coinsGP={Dice~1d10*100}|
  96-100,|coinsPP={Dice~1d12*10}|
  
  :GenGoods6
  1-56,
  57-92,{Loop~{Dice~1d4},[GenGems]}
  93-100,{Loop~{Dice~1d4},[GenArt]}
  
  :GenItems6
  1-54,
  55-59,{Loop~{Dice~1d4},[GenMundane]}
  60-99,|magicPwr=minor|{Loop~{Dice~1d3},[GenMagic]}
  100,|magicPwr=medium|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins7
  1-11,
  12-18,|coinsCP={Dice~1d10*10000}|
  19-35,|coinsSP={Dice~1d12*1000}|
  36-93,|coinsGP={Dice~2d6*100}|
  94-100,|coinsPP={Dice~3d4*10}|
  
  :GenGoods7
  1-48,
  49-88,{Loop~{Dice~1d4},[GenGems]}
  89-100,{Loop~{Dice~1d4},[GenArt]}
  
  :GenItems7
  1-51,
  52-97,|magicPwr=minor|{Loop~{Dice~1d3},[GenMagic]}
  98-100,|magicPwr=medium|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins8
  1-10,
  11-15,|coinsCP={Dice~1d12*10000}|
  16-29,|coinsSP={Dice~2d6*1000}|
  30-87,|coinsGP={Dice~2d8*100}|
  88-100,|coinsPP={Dice~3d6*10}|
  
  :GenGoods8
  1-45,
  46-85,{Loop~{Dice~1d6},[GenGems]}
  86-100,{Loop~{Dice~1d4},[GenArt]}
  
  :GenItems8
  1-48,
  49-96,|magicPwr=minor|{Loop~{Dice~1d4},[GenMagic]}
  97-100,|magicPwr=medium|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins9
  1-10,
  11-15,|coinsCP={Dice~2d6*10000}|
  16-29,|coinsSP={Dice~2d8*1000}|
  30-85,|coinsGP={Dice~5d4*100}|
  86-100,|coinsPP={Dice~2d12*10}|
  
  :GenGoods9
  1-40,
  41-80,{Loop~{Dice~1d8},[GenGems]}
  81-100,{Loop~{Dice~1d4},[GenArt]}
  
  :GenItems9
  1-43,
  44-91,|magicPwr=minor|{Loop~{Dice~1d4},[GenMagic]}
  92-100,|magicPwr=medium|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins10
  1-10,
  11-24,|coinsSP={Dice~2d10*1000}|
  25-79,|coinsGP={Dice~6d4*100}|
  80-100,|coinsPP={Dice~5d6*10}|
  
  :GenGoods10
  1-35,
  36-79,{Loop~{Dice~1d8},[GenGems]}
  80-100,{Loop~{Dice~1d6},[GenArt]}
  
  :GenItems10
  1-40,
  41-88,|magicPwr=minor|{Loop~{Dice~1d4},[GenMagic]}
  89-99,|magicPwr=medium|[GenMagic]
  100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins11
  1-8,
  9-14,|coinsSP={Dice~3d10*1000}|
  15-75,|coinsGP={Dice~4d8*100}|
  76-100,|coinsPP={Dice~4d10*10}|
  
  :GenGoods11
  1-24,
  25-74,{Loop~{Dice~1d10},[GenGems]}
  75-100,{Loop~{Dice~1d6},[GenArt]}
  
  :GenItems11
  1-31,
  32-84,|magicPwr=minor|{Loop~{Dice~1d4},[GenMagic]}
  85-98,|magicPwr=medium|[GenMagic]
  99-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins12
  1-8,
  9-14,|coinsSP={Dice~3d12*1000}|
  15-75,|coinsGP={Dice~1d4*1000}|
  76-100,|coinsPP={Dice~1d4*100}|
  
  :GenGoods12
  1-17,
  18-70,{Loop~{Dice~1d10},[GenGems]}
  71-100,{Loop~{Dice~1d8},[GenArt]}
  
  :GenItems12
  1-27,
  28-82,|magicPwr=minor|{Loop~{Dice~1d6},[GenMagic]}
  83-97,|magicPwr=medium|[GenMagic]
  98-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins13
  1-8,
  9-75,|coinsGP={Dice~1d4*1000}|
  76-100,|coinsPP={Dice~1d10*100}|
  
  :GenGoods13
  1-11,
  12-66,{Loop~{Dice~1d12},[GenGems]}
  67-100,{Loop~{Dice~1d10},[GenArt]}
  
  :GenItems13
  1-19,
  20-73,|magicPwr=minor|{Loop~{Dice~1d6},[GenMagic]}
  74-95,|magicPwr=medium|[GenMagic]
  96-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins14
  1-8,
  9-75,|coinsGP={Dice~1d6*1000}|
  76-100,|coinsPP={Dice~1d12*100}|
  
  :GenGoods14
  1-11,
  12-66,{Loop~{Dice~2d8},[GenGems]}
  67-100,{Loop~{Dice~2d6},[GenArt]}
  
  :GenItems14
  1-19,
  20-58,|magicPwr=minor|{Loop~{Dice~1d6},[GenMagic]}
  59-92,|magicPwr=medium|[GenMagic]
  93-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins15
  1-3,
  4-74,|coinsGP={Dice~1d8*1000}|
  75-100,|coinsPP={Dice~3d4*100}|
  
  :GenGoods15
  1-9,
  10-65,{Loop~{Dice~2d10},[GenGems]}
  66-100,{Loop~{Dice~2d8},[GenArt]}
  
  :GenItems15
  1-11,
  12-46,|magicPwr=minor|{Loop~{Dice~1d10},[GenMagic]}
  47-90,|magicPwr=medium|[GenMagic]
  91-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins16
  1-3,
  4-74,|coinsGP={Dice~1d12*1000}|
  75-100,|coinsPP={Dice~3d4*100}|
  
  :GenGoods16
  1-7,
  8-64,{Loop~{Dice~4d6},[GenGems]}
  65-100,{Loop~{Dice~2d10},[GenArt]}
  
  :GenItems16
  1-40,
  41-46,|magicPwr=minor|{Loop~{Dice~1d10},[GenMagic]}
  47-90,|magicPwr=medium|{Loop~{Dice~1d3},[GenMagic]}
  91-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins17
  1-3,
  4-68,|coinsGP={Dice~3d4*1000}|
  69-100,|coinsPP={Dice~2d10*100}|
  
  :GenGoods17
  1-4,
  5-63,{Loop~{Dice~4d8},[GenGems]}
  64-100,{Loop~{Dice~3d8},[GenArt]}
  
  :GenItems17
  1-33,
  34-83,|magicPwr=medium|{Loop~{Dice~1d3},[GenMagic]}
  84-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins18
  1-2,
  3-65,|coinsGP={Dice~3d6*1000}|
  66-100,|coinsPP={Dice~5d4*100}|
  
  :GenGoods18
  1-4,
  5-54,{Loop~{Dice~3d12},[GenGems]}
  55-100,{Loop~{Dice~3d10},[GenArt]}
  
  :GenItems18
  1-24,
  25-80,|magicPwr=medium|{Loop~{Dice~1d4},[GenMagic]}
  81-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins19
  1-2,
  3-65,|coinsGP={Dice~3d8*1000}|
  66-100,|coinsPP={Dice~3d10*100}|
  
  :GenGoods19
  1-3,
  4-50,{Loop~{Dice~6d6},[GenGems]}
  51-100,{Loop~{Dice~6d6},[GenArt]}
  
  :GenItems19
  1-4,
  5-70,|magicPwr=medium|{Loop~{Dice~1d4},[GenMagic]}
  71-100,|magicPwr=major|[GenMagic]
  
  # ----------------------------------------------------------------------
  
  :GenCoins20
  1-2,
  3-65,|coinsGP={Dice~4d8*1000}|
  66-100,|coinsPP={Dice~4d10*100}|
  
  :GenGoods20
  1-2,
  3-38,{Loop~{Dice~4d10},[GenGems]}
  39-100,{Loop~{Dice~7d6},[GenArt]}
  
  :GenItems20
  1-25,
  26-65,|magicPwr=medium|{Loop~{Dice~1d4},[GenMagic]}
  66-100,|magicPwr=major|{Loop~{Dice~1d3},[GenMagic]}
  
  # ----------------------------------------------------------------------
  
  ;GenGems
  1,|temp=[TP Gems.Start(2)]|{Split~temp,"!",item,value,appraiseDC}
  _|item={CommaReplace~0,XXXcommaXXX,%item%}|
  _{DSSet~itemList,{DSAdd~itemList},name,%item%,value,%value%,desc,%item%,appraiseDC,%appraiseDC%,appraise,1}
  
  ;GenArt
  1,|temp=[TP Art.Start]|{Split~temp,"!",item,weight,value,appraiseDC}
  _|item={CommaReplace~0,XXXcommaXXX,%item%}|
  _{DSSet~itemList,{DSAdd~itemList},name,%item%,value,%value%,weight,%weight%,desc,%item%,appraiseDC,%appraiseDC%,appraise,1}
  
  ;GenMundane
  1,|temp=[TP Mundane.Start]|{Split~temp,"!",item,value,weight,desc}
  _|item={CommaReplace~0,XXXcommaXXX,%item%}|
  _|desc={CommaReplace~0,XXXcommaXXX,%desc%}|
  _{DSSet~itemList,{DSAdd~itemList},name,%item%,value,%value%,desc,%item%,appraise,1}
  
  :GenMagic
  1,|temp=[%magicPwr%Magic]|{Split~temp,"!",item,value,weight,aura,desc}
  _|item={CommaReplace~0,XXXcommaXXX,%item%}|
  _|aura={CommaReplace~0,XXXcommaXXX,%aura%}|
  _|desc={CommaReplace~0,XXXcommaXXX,%desc%}|
  _{DSSet~itemList,{DSAdd~itemList},name,%item%,value,%value%,desc,%desc%,weight,%weight%,aura,%aura%,magic,1}
  
  # Return format: name!value!weight!aura!desc
  ;minorMagic
  4,[TP Magic Armor.Start(,,,2,)]
  5,[TP Magic Weapons.Start(,,,2,)]
  35,[TP Potions.Start(,,,2,)]
  2,[TP Magic Rings.Start(,,,2,)]
  35,[TP Scrolls.Start(,,,,2,)]
  10,[TP Wands.Start(,,,%randomCharges%,2,)]
  9,[TP Wondrous Items.Start(,,,2,)]
  
  ;mediumMagic
  10,[TP Magic Armor.Start(,,,2,)]
  10,[TP Magic Weapons.Start(,,,2,)]
  10,[TP Potions.Start(,,,2,)]
  10,[TP Magic Rings.Start(,,,2,)]
  10,[TP Rods.Start(,,,2,)]
  15,[TP Scrolls.Start(,,,,2,)]
  3,[TP Staves.Start(,,,%randomCharges%,2,)]
  15,[TP Wands.Start(,,,%randomCharges%,2,)]
  17,[TP Wondrous Items.Start(,,,2,)]
  
  ;majorMagic
  10,[TP Magic Armor.Start(,,,2,)]
  10,[TP Magic Weapons.Start(,,,2,)]
  5,[TP Potions.Start(,,,2,)]
  10,[TP Magic Rings.Start(,,,2,)]
  10,[TP Rods.Start(,,,2,)]
  10,[TP Scrolls.Start(,,,,2,)]
  20,[TP Staves.Start(,,,%randomCharges%,2,)]
  5,[TP Wands.Start(,,,%randomCharges%,2,)]
  20,[TP Wondrous Items.Start(,,,2,)]
  
  :ValuePerEnc
  1,300
  2,600
  3,900
  4,1200
  5,1600
  6,2000
  7,2600
  8,3400
  9,4500
  10,5800
  11,7500
  12,9800
  13,13000
  14,17000
  15,22000
  16,28000
  17,36000
  18,47000
  19,61000
  20,80000