Advertisement
T3RRYT3RR0R

Batch Character Inventory System for RPG games

Feb 25th, 2021 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 20.78 KB | None | 0 0
  1. ::: AUTHOR: T3RRY Created: 06/03/2021 Version: 1.3 Filename: RPGInventorySystem
  2. :# Purpose:  Provide an easy to use, ready to go Template for creating Batch RPG Games
  3. :# Features:
  4. :# - :Login Function
  5. :#   - remembers last played character; offers single key login for that character.
  6. :#   - flags if character is New or Returning
  7. :# - :Getin Input function to restrict and validate player Input
  8. :# - Example Template of Creating Classes with Starting Inventory { :Knight function }
  9. :# - Multipurpose :Inventory Funtion That Saves Inventory changes Immediately
  10. :# - :ManageInventory Function to allow players to well... manage their inventory.
  11. :# - Menu Macro for easy menus.
  12.  
  13. @Echo off & Goto Prep
  14. =======================================================================:# FUNCTIONS
  15. :# Inventory Arguments and Usage Info:
  16. :# Arg order Mandatory
  17.  
  18. ======================:# ADDING ITEMS TO INVENTORY: Initial Inventory; Loot; Gifts / Rewards; Purchases
  19. :Inventory              <ItemName> <ItemType> <ItemSubType> <ItemWeight> <ItemCost> {Optional:"ModifierType=Value"} {Optional:/F}
  20. ::::::::::::::::::::::::::::::::::::::::::::::::::
  21. :# Optional Arg "/F": Must be last Arg; Flags the Item as free; Does not Subtract ItemCost from Players #Money
  22. :#  Allows Items to be Looted / Gifted / Rewarded that have value stored for use when Selling Items. See Below
  23. ======================:# REMOVING / SELLING ITEMS:
  24. :# Remove item:         <ItemName> /R <ItemSubType>
  25. :#  Optional third Arg is used when selling an Item. May be a Sum, Integer Value or use Array: !#ItemName.Value!
  26. :#  /R - Undefines all array elements containing ItemName in varname or value. Use unique and descriptive Item names.
  27. :::::::::::::::::::::::::::::::::::::::::::::::::::
  28. :# Sell item:           <ItemName> /R <ItemSubType> <ItemSaleValue>
  29. ======================:# EQUIPPING ITEMS:
  30. :# Equip an item   :    <Itemname> /E <ItemSubtype>
  31. ======================:# CHECKPOINTS:
  32. :# Save a Checkpoint:   <LabelName> /L
  33. :::::::::::::::::::::::::::::::::::::::::::::::::::
  34. :# IMPORTANT - RESERVED VARIABLES - The above Parameter names; #%1.Grams #%1.Value #Display.Weight #%3{i} #Weight List? Count
  35. :#  - Arrays:         #%ItemName%.Grams #%ItemName%.Value #Equipped.%ItemSubType%
  36. :#  - Lists:          #Inventory.Types #Inventory.%ItemType% #Inventory.%ItemType%.%ItemSubType%
  37. :# RECOMMENDATION: Use a selection of Category names for ItemTypes that will be Equipped.
  38. :# - Only one of Each ItemSubType may be Equipped at A time.
  39. :#  - It is appropriate that only 1 pair of Boots may be worn, but multiple pieces of different Armor may be equipped:
  40. :#  - Instead of just Defining all armors to the ItemType Armor:
  41. :#  - Define Type Armor and Subtypes: Helm Mantle Gloves Vambraces Pauldrons Boots Pants
  42. :# DEPENDENCIES - Defined in ':Login': #Name Game.Save
  43.  If /I "%~2" == "/L" (
  44.   Set "#Location=%~1"
  45.   Goto :Save
  46.  )
  47.  
  48. :# Removes or Sell ItemName From Inventory;  Removes ItemName From ItemSubtype List
  49.  If /I "%~2" == "/R" (
  50.   Set "Value="
  51.   If not "%~5" == "" Set "Value=%~5"
  52.   For /F "Tokens=1,2* Delims==" %%G in (' Set "#" 2^> nul ^|%__Appdir__%findstr.exe /LIC:"%~1" ')Do (
  53. rem /* Test if Variable is a List Variable; If Not Undefines Variable */
  54.    Set "List?=%%G"
  55.    If not "!List?:~0,10!" == "#Inventory" (
  56.     Set "%%G="
  57.    )Else (
  58. rem /* If Variable is a List Variable; Remove ItemName from ItemSubType List */
  59.     Set "%%G=!%%G: "%~1"=!"
  60. Rem /* If ItemSubType List is Empty; Remove ItemSubtype from ItemType List */
  61.     If "!%%G!" == "" (Set "#Inventory.%~3=!#Inventory.%~3: "%~4"=!")
  62.    )
  63.   )
  64. rem /* If Item SaleValue provided; Increment #Money by Value */
  65.   Set /A "#Money+=!Value!+0"
  66.   Goto :Save
  67.  )
  68.  
  69.  
  70. :# Build Itemtype Arrays and List Using Parameters
  71.  Set "ItemName=%~1"
  72.  Set "ItemType=%~2"
  73.  Set "ItemSubtype=%~3"
  74.  Set "Count=0"
  75. :# Limit ItemTypes to 36 max supported by Menu macro
  76.  If Not "!#Inventory.%ItemType%!" == "" (
  77.   For %%G in (!#Inventory.%ItemType%!)Do Set /A "Count+=1"
  78.   If !Count! GTR 36 (
  79.    Echo(You own too many types of %ItemType%. !ItemName! cannot be Acquired.
  80.    Exit /B 3
  81.   )
  82.  )
  83.  Set "Count=0"
  84. :# Limit Subtypes to 36 max supported by Menu macro
  85.  If Not "!#Inventory.%ItemType%.%ItemSubType%!" == "" (
  86.   For %%G in (!#Inventory.%ItemType%.%ItemSubType%!)Do Set /A "Count+=1"
  87.   If !Count! GTR 36 (
  88.    Echo(You own too many types of %ItemSubtype%. !ItemName! cannot be Acquired.
  89.    Exit /B 3
  90.   )
  91.  )
  92.  If "%~4" == "" (Set "ItemWeight=0")Else (
  93.   Set "ItemWeight=%~4"
  94.   For /F %%G in ('Set /A #MaxWeight-%~4-#weight')Do if %%G LSS 0 (
  95.    Echo/You are carrying too much.
  96.    Exit /B 2
  97.  ))
  98. :# Determine Value of Item and If item is To be Free or Paid For
  99.  Set "Free.Item="
  100.  Echo(%*|%__APPDIR__%findstr.exe /LIC:"/F" > Nul && Set "Free.Item=Y"
  101.  If "%~5"=="" (Set "ItemCost=0")Else (
  102.   Set "ItemCost=%~5"
  103.   If Not Defined Free.Item (
  104.    For /F %%G in ('Set /A #money-%~5')Do if %%G LSS 0 (
  105.     Echo(You cannot afford %ItemName%
  106.     Exit /B 1
  107.    )
  108.   )
  109.  )
  110. :# Define a Modifier To ItemName.modname For Items With Stat Modifiers If supplied
  111.  If not "%~6" == "" If /I not "%~6" == "/F" (
  112.   Set "#%ItemName%.%~6" 2> Nul || (
  113.    Echo(Invalid Argument "[%~6]" In %*
  114.    Echo(Required: "ModifierType=Value"
  115.    Timeout /T 3 /NoBreak
  116.    Pause
  117.    Endlocal & Goto :Eof
  118.   )
  119.  )
  120. :# Define #ItemName Arrays for Grams and Value
  121.  Set "#%ItemName%.Grams=%Itemweight%"
  122.  Set "#%ItemName%.Value=%ItemCost%"
  123.  Echo(!ItemName! added to Inventory.
  124. :# Search savefile for Itemtype; Itemsubtype; and Itemname; Append to relevant list if not present
  125.  %__APPDIR__%findstr.exe /LIC:"!ItemType!" "%Game.Save%" > nul 2> nul || Set "#Inventory.Types=!#Inventory.Types! "!ItemType!""
  126.  %__APPDIR__%findstr.exe /LIC:"!ItemSubType!" "%Game.Save%" > nul 2> nul || Set "#Inventory.%ItemType%=!#Inventory.%ItemType%! "!ItemSubType!""
  127.  %__APPDIR__%findstr.exe /LIC:"!ItemName!" "%Game.Save%" > nul 2> nul || Set "#Inventory.%ItemType%.%ItemSubType%=!#Inventory.%ItemType%.%ItemSubType%! "!ItemName!""
  128.  
  129. =================================================
  130. :Save
  131. :# Remove Item Categories from #Inventory.Types list when all Category sub items are sold / dropped.
  132.  For %%G in (!#Inventory.Types!)Do If "!#Inventory.%%~G!" == "" Set "#Inventory.Types=!#Inventory.Types: "%%~G"=!"
  133.  Set /A "#Def=#Dmg=#weight=0"
  134. :# Updates the below Modifiers of all equipped inventory items by summing all ItemName.%%M definitions
  135.  For %%M in (DEF DMG)Do For /F "Tokens=1,2* Delims==" %%G in ('Set "#Equipped" 2^> nul')Do (
  136.   For /F "Tokens=2 Delims==" %%i in ('Set "#%%H"^|%__APPDIR__%Findstr.exe /LIC:".%%M"')Do (
  137.    Set /A "#%%M+=%%i+0"
  138.   )
  139.  )
  140. :# Updates the current weight of all inventory items by summing all ItemName.Grams definitions
  141.  For /F "Tokens=2 Delims==" %%G in (' Set "#" ^| Findstr /LIC:".Grams"')Do Set /A #weight+=%%G
  142.  Set "#Display.weight=%#weight:~0,-3%.%#weight:~-3%"
  143. :# Enact Save for # prefixed variables
  144. :# Saves Game Variables prefixed by # to Game.Save cmd file to be called on Login
  145.  (For /F "tokens=1,2* Delims==" %%G in ('Set "#"')Do Echo(Set "%%G=%%H") >"%Game.Save%"
  146.  Echo(%#Name%>"%~f0:Last_Char"
  147.  Exit /B 0
  148.  
  149. =================================================
  150. :ManageInventory {Optional:/E /S /D}
  151. :# Control Available Inventory options by Calling ManageInventory with the Options you wish
  152. :# to allow according to the current Gamestate.
  153.  Set "MI.Options="
  154.  If not "%~1" == "" Echo/%*|Findstr /LIC:"E" > Nul && Set "MI.Options=!MI.Options! Equip"
  155.  If not "%~1" == "" Echo/%*|Findstr /LIC:"S" > Nul && Set "MI.Options=!MI.Options! Sell"
  156.  If not "%~1" == "" Echo/%*|Findstr /LIC:"D" > Nul && Set "MI.Options=!MI.Options! Drop"
  157. :# /R - Removes the Sell option from Invetory Action
  158. :# RESERVED VARIABLES - Type; SubType; Action; All Menu Macro Reserved Variables
  159. :# DEPENDENCIES -Functions: Inventory; Save -Macro: Menu -Variables: #Money #Equipped
  160. :#                          Inventory Function Defines Lists used with Each Submenu.
  161. :# This function returns no Errorlevels.
  162. CLS
  163.  Echo(         Credit: !#Money! Damage: !#Dmg! Defence: !#Def! Burden: !#Display.Weight! kg /!#MaxWeight:~0,-3!.!#MaxWeight:~-3! kg.
  164.  Echo( Equipped Items:
  165.  (Set "#Equipped" 2> nul > nul ) || Echo( No items equipped.
  166.  (For /F "Delims=" %%# in ('Set #Equipped 2^> nul') Do For /F "Tokens=1,2 Delims==" %%C in ("%%#")Do (
  167.   Set "[item]=%%C"
  168.   Set "[item]=![item]:*.=!"
  169.   Set "[item]=![item]:.=: !"
  170.   <nul set /P "=> ![item]! = %%D!LF!"
  171.  )) 2> nul
  172.  
  173. :# Choose Action Type or Exit ManageInventory Function; Each Submenu can abort to this Primary Menu
  174.  %Menu% Exit !MI.Options!
  175.  CLS
  176.  Set "Action=!Option!"
  177.  Echo(Select Inventory Category:
  178.  %Menu% Return !#Inventory.Types!
  179.  If "!Option!" == "Return" (Goto :ManageInventory)Else CLS
  180.  Set "Type=!Option!"
  181.  Echo(Select a type of !Type! to !Action!:
  182.  %Menu% Return !#Inventory.%Type%!
  183.  If "!Option!" == "Return" (Goto :ManageInventory)Else CLS
  184.  Set "SubType=%Option%"
  185. :# Apply action type to selected ItemName
  186. :# If Equipping; Handle Two Handed / [One Handed/Off Hand] Equipment Conflict
  187.  Echo(Selecting a !Subtype! from your !Type! Will !Action! it. Return [0] To cancel.
  188.  %Menu% Return !#Inventory.%Type%.%SubType%!
  189.  If Not "!Option!" == "Return" (
  190.   If "!Action!" == "Equip" (
  191.    Set "#Equipped.%Type%.%Subtype%=%Option%"
  192.    If /I "%SubType%" == "Two Handed" (Set "#Equipped.Weapons.One Handed="&Set "#Equipped.Weapons.Off Hand=")
  193.    If /I "%SubType%" == "One Handed" (Set "#Equipped.Weapons.Two Handed=")
  194.    If /I "%SubType%" == "Off Hand" (Set "#Equipped.Weapons.Two Handed=")
  195.    Call :Save
  196.   )
  197.   If "!Action!" == "Sell" (
  198.    Echo(Sell Value: $!#%Option%.Value!. Confirm [Y/N]
  199.    For /F "Delims=" %%O in ('Choice /N /C:YN')Do If %%O==Y Call :Inventory "%Option%" /R "!Type!" "!SubType!" "!#%Option%.Value!"
  200.   )
  201.   If "!Action!" == "Drop" Call :Inventory "%Option%" /R "!Type!" "!SubType!"
  202.  )
  203. Goto :ManageInventory
  204.  
  205. =================================================
  206. :login
  207. :# clear any preexisting definitions
  208.  (For /F "tokens=1,2* Delims==" %%G in ('Set "#"')Do Set "%%G=") 2> nul
  209.  
  210. :# check Alternate data stream 'Last_Char' of this file; offer single key login for last played character.
  211.  more < "%~f0:Last_Char" 2>nul >nul && For /F "Delims=" %%G in ('more ^< "%~f0:Last_Char"')Do (
  212.   Echo(Continue as %%G Y/N
  213.   For /F "Delims=" %%C in ('Choice /N /C:YN')Do if "%%C"=="Y" (
  214.    Set "Game.Save=%TEMP%\%~n0_%%G_Save.cmd"
  215.    CALL "%TEMP%\%~n0_%%G_Save.cmd"
  216.    Exit /B 0
  217.   )
  218.  )
  219.  (For /F "tokens=1,2* Delims==" %%G in ('Set "#"')Do Set "%%G=") 2> nul
  220. :# Login Errorlevels: [2:User quit during Login.] [1:New Character] [0:Existing Character]
  221. :# IMPORTANT - RESERVED VARIABLES:
  222. :# - #Name #Pass Verify
  223. :#   #Name used to create a .cmd file that is called when the user logs back in to the character to reassign
  224. :#   # prefixed variables.
  225. CLS
  226.  Echo(Existing Characters:
  227.  (Dir /B "%TEMP%\%~n0_*_Save.cmd" > Nul 2> nul || Echo(None. )
  228.  For /F "Tokens=2 Delims=_" %%G in (' Dir /B "%TEMP%\%~n0_*_Save.cmd" 2^> nul ')Do Echo(%%G
  229. rem /* example definitions */
  230.  Call :GetIn #Name "[a-zA-Z ]*" /C /S /P:Enter your characters name
  231.  If Not "%Errorlevel%" == "0" Goto :login
  232.  Call :GetIn #Pass "[0-9][0-9][0-9][0-9]" /S /P:Enter your four Digit Pin
  233.  If Not "%Errorlevel%" == "0" Goto :login
  234.  Set "Verify=%#Pass%"
  235.  Set "Game.Save=%TEMP%\%~n0_%#Name%_Save.cmd"
  236. rem /* load saved values */
  237.  If Exist "%Game.Save%" Call "%Game.Save%"
  238.  If /I not "%Verify%" == "%#Pass%" (
  239.   Echo(Invalid Password
  240.   %Menu% Exit Retry
  241. rem /* Flag quit program */
  242.   Goto :Login
  243.  )
  244. rem /* offer delete/ login if exists */
  245.  If Exist "%Game.Save%" (
  246. rem /* Flag Existing Character Errorlevel 0 */
  247.   %Menu% "Remove Character" "Continue"
  248.   If /I "!Option!"=="Continue" Exit /b 0
  249.   %Menu% "Abort" "Proceed"
  250.   If /I "!Option!"=="Abort" Goto :Login
  251.   Del "%Game.Save%"
  252.   Goto :Login
  253.  ) Else (
  254. rem /* Flag New Character Errorlevel 1 */
  255.   Exit /B 1
  256.  )
  257. =====================================================
  258.  
  259. :strlen <resultVar> <stringVar>
  260. (  
  261.     setlocal EnableDelayedExpansion
  262.     (set^ tmp=!%~2!)
  263.     if defined tmp (
  264.         set "len=1"
  265.         for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  266.             if "!tmp:~%%P,1!" NEQ "" (
  267.                 set /a "len+=%%P"
  268.                 set "tmp=!tmp:~%%P!"
  269.             )
  270.         )
  271.     ) ELSE (
  272.         set len=0
  273.     )
  274. )
  275. (
  276.     endlocal
  277.     set "%~1=%len%"
  278.     exit /b
  279. )
  280. =====================================================
  281.  
  282. :GetIn [ReturnVar] ["qouted findstr search pattern"]  [Optional:"/C" - required for patterns with spaces] [Optional "/S" - Suppress input accepted] [/P:Custom Prompt]
  283. :# Input validation function for restricting input to desired character classes or [limited] Regex Patterns.
  284. :# -IMPORTANT - RESERVED VARIABLES: InputParams /C /P /S rVar Pattern Exit.Code
  285. :# - GetIn Errorlevels:
  286. :#  - Errorlevel 3 - User has failed to provide valid input * OR * regex pattern is
  287. :#    invalid * OR * GetIn is a called label in a file that has recieved input
  288. :#    from a pipe that has not been handeled before the call.
  289. :#  - Errorlevel 2 - GetIn is a script that has been invoked by a pipe
  290. :#  - Errorlevel 1 - GetIn has been called without Arg 2 for Regex Pattern
  291. :#  - Errorlevel 0 - Input has been assigned that matches the Regex Pattern
  292. :# GetIn Usage Examples - Description is replaced with Arg 1-ReturnVar:
  293. :#  Call :GetIn Pin "[0-9][0-9][0-9][0-9]" /S /P:Enter a four Digit Pin Number
  294. :#  Call :GetIn Alphanumeric-String "[0-9a-zA-Z]*"
  295. :#  Call :GetIn semicolon-delimited-hexadecimal-String "[0-9a-fA-F][0-9a-fA-F][:][0-9a-fA-F][0-9a-fA-F]"
  296. :#  Call :GetIn AlphaString-with-3-Digit-Numeric-suffix "[a-zA-Z]*[0-9][0-9][0-9]"
  297. :#  Call :GetIn list-of-delimited-words "[a-zA-Z, ]*" /C
  298. :#  Call :GetIn pair-of-delimited-numbers "[0-9]*[, ][0-9]*" /C
  299.  
  300.  SETLOCAL EnableExtensions EnableDelayedExpansion
  301.  Set "/C=" & Set "/P=" & Set "/S="
  302.  Set InputParams=%*
  303.  If Not "!InputParams:/C=!" == "!InputParams!" Set "/C=_"
  304.  If Not "!InputParams:/S=!" == "!InputParams!" Set "/S=_"
  305.  If Not "!InputParams:/P=!" == "!InputParams!" (
  306.   Set "/P=!InputParams:*/P:=!"
  307.   Set "/P=!/P:"=!"
  308.  )
  309. rem [ test if standalone script - if so test if accessed by pipe; reject execution if true ]
  310.  If "%~0" == "%~n0" For %%G in (!cmdcmdline!)Do Echo/%%~G|Findstr.exe /LIC:"/S" > nul 2> nul && (Set "Exit.Code=2" & Goto :Getin.Err)
  311.  
  312. :GetInRetry
  313.  Del /Q "%TEMP%\%~n0_validate.~tmp" 2> nul
  314.  If !Exit.Code! GTR 5 Goto :Getin.Err
  315.  Set "rVar="
  316.  If "%~2" == "" GOTO :Getin.Err
  317.  Setlocal DISABLEdelayedExpansion & rem ::: display occurances of carets [^] in pattern
  318.  Endlocal & Set "Pattern=%~2"
  319.  If Not Defined /P (
  320.   Set /P "rVar=Input Format for %1:!LF!!Pattern!!LF!!LF!Enter %1: "
  321.  )Else  Set /P "rVar=!/P!: "
  322.  If "!rVar!" == "" (Set /A "Exit.Code+=1" & <nul Set /P"= ^! Invalid value.!LF!" & Goto :GetInRetry)
  323.  > "%TEMP%\%~n0_validate.~tmp" (Echo/!rVar!)
  324.  If Defined /C (
  325.   Type "%TEMP%\%!!|%__APPDIR__%findstr.exe /RXC:"%~2" >nul || (Set /A "Exit.Code+=1" & <nul Set /P"= ^! Invalid value. !LF!" & Goto :GetInRetry)
  326.  ) Else (
  327.   Type "%TEMP%\%!!|%__APPDIR__%findstr.exe /RX "^%~2$" >nul || (Set /A "Exit.Code+=1" & <nul Set /P"=- ^! Invalid value. !LF!" & Goto :GetInRetry)
  328.  )
  329.  If not defined /S Echo/%1: [!rVar!] Accepted
  330.  ENDLOCAL & Set "%~1=%rVar%"
  331.  Del /Q "%TEMP%\%~n0_validate.~tmp"
  332.  Exit /B 0
  333. :GetIn.Err
  334. Exit /B %Exit.Code%
  335.  
  336. ===========================:# DEFINE INITIAL INVENTORY - EXAMPLE Only
  337. :Knight Starter Pack
  338. :# Example of defining initial variables for a new character prior to commencing gameplay
  339. :# IMPORTANT - Inventory function is Dependant on #Money #MaxWeight and #Weight variables.
  340.  Set /A "#Money=500,#MaxWeight=25000,#Weight=0"
  341. :# suppress output of Inventory added notifcation by redirecting to nul as desired.
  342.  Call :Inventory "Dagger" Weapons "Off Hand" 250 50 "DMG=5" /F > nul
  343.  Call :Inventory "Buckler" Weapons "Off Hand" 400 40 "DEF=6" /F > nul
  344.  Call :Inventory "Short sword" Weapons "One Handed" 4000 250 "DMG=10" /F > nul
  345.  Call :Inventory "Twin Bladed Katana" Weapons "Two Handed" 5500 350 "DMG=12" /F > nul
  346.  Call :Inventory "Leather Cap" Armor Helm 500 25 "DEF=3" /F > nul
  347.  Call :Inventory "Faded Map" Miscellaneous Maps 20 "DEF=5" /F > nul
  348.  Call :Inventory "Healing Potion" Potions Restorative 10 25 "Heal=30" /F > nul
  349.  Call :Inventory "Old Key" Miscellaneous Keys 10 1 /F > nul
  350.  Call :Inventory "Worn Leather Vambraces" Armor Vambraces 1000 15 "DEF=2" /F > nul
  351.  Call :Inventory "Boots of Luck" Armor Boots 1500 50 "DEF=4" /F > nul
  352. Goto :Eof
  353. ===========================:# END OF FUNCTIONS
  354.  
  355. :Prep
  356. =====================================================:# MACRO DEFINITIONS
  357. ===================================:# MENU macro prep and Definition :::
  358. :# IMPORTANT - RESERVED VARIABLES:
  359. :# - LF \N Game.Save
  360. (Set LF=^
  361.  
  362. %= Linefeed var used for multi-line output in GetIn macro - Do not modify. =%)
  363. (Set \n=^^^
  364.  
  365. %= Newline var \n for multi-line macro definition - Do not modify. =%)
  366.  
  367. :# Enable environment for macro expansion, Arrays and code block variable operations
  368.  Setlocal EnableExtensions EnableDelayedExpansion
  369. ==================================================================
  370. :# Menu macro escaped for Definition with DelayedExpansion Enabled
  371. :# IMPORTANT - RESERVED VARIABLES: Menu CH# CHCS Options Option Opt[i] Option.Output Cholist DIV
  372. :# Key index list Allows 36 menu options. Component of Menu Macro
  373.  Set "ChoList=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  374. rem /* Get console width for dividing line. */
  375.  for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %__APPDIR__%findstr.exe /LIC:"Columns"`) do Set /A "Console_Width=%%W"
  376. :# Build dividing line for menu output.
  377.  Set "DIV="&For /L %%i in (2 1 %Console_Width%)Do Set "DIV=!DIV!-"
  378. :# Menu macro Usage: %Menu% "quoted" "list of" "options"
  379. ==== Set Menu=For %%n in (1 2)Do if %%n==2 (%\n%
  380. %= Output Dividing Line                 =%  Echo(^^!DIV^^!%\n%
  381. %= Reset CH# index value for Opt[#]     =%  Set "CH#=0"%\n%
  382. %= Undefine choice option key list      =%  Set "CHCS="%\n%
  383. %= For Each in list;                    =%  For %%G in (^^!Options^^!)Do (%\n%
  384. %= For Option Index value               =%   For %%i in (^^!CH#^^!)Do (%\n%
  385. %= Build the Choice key list and Opt[#] =%    Set "CHCS=^!CHCS^!^!ChoList:~%%i,1^!"%\n%
  386. %= array using the character at the     =%    Set "Opt[^!ChoList:~%%i,1^!]=%%~G"%\n%
  387. %= current substring index.             =%    Set "option.output=%%~G"%\n%
  388. %!!%    Echo([^^!ChoList:~%%i,1^^!] ^^!Option.output:#=^^!%\n%
  389. %= Increment Opt[#] Index var 'CH#'     =%    Set /A "CH#+=1"%\n%
  390. %= Close CH# loop                       =%   )%\n%
  391. %= Close Options loop                   =%  )%\n%
  392. %= Output Dividing Line                 =%  Echo(^^!DIV^^!%\n%
  393. %= Select option by character index     =%  For /F "Delims=" %%o in ('Choice /N /C:^^!CHCS^^!')Do (%\n%
  394. %= Assign return var 'OPTION' with the  =%   Set "Option=^!Opt[%%o]^!"%\n%
  395. %= value selected from Opt[CH#] array.  =%   If /I "^!Option^!" == "Exit" Exit /B 2%\n%
  396. %= Return to previous script on Exit    =%  )%\n%
  397. %= Capture Macro input - Options List   =% )Else Set Options=
  398. ========================================== ::: End Menu Definition
  399.  
  400. :StartGame
  401. :# Make the Session Local
  402.  Setlocal
  403. ==========================================:# MAIN GAME SCRIPT [ EXAMPLE ]
  404. :# To Create / Continue a Character; Call Login. Errorlevel 0 = Returning Character
  405. :# Modify The reponse to Errorlevel 1 [New Character] As you Wish to facilitate Creation of Character Classes.
  406. :# Suggested: Use the Menu Macro with a list of Character Classes corresponding to functions and Call the selected class Label (IE - Call :Rogue)
  407.  Call :Login
  408.  If Errorlevel 2 Goto :Eof
  409.  If Errorlevel 1 (
  410.   Echo(Select your Class:
  411.   %Menu% Exit "Knight" "Rogue"
  412.   Call :!Option! 2>nul || (
  413.    Echo(The !Option! Class has not been created yet. Use the example of the :Knight function to create your own Classes.
  414.    Timeout /T 3 /NoBreak > Nul
  415.    Pause
  416.    Endlocal
  417.    Goto :StartGame
  418.   )
  419.  )
  420. :# Use Labels as checkpoints by Defining them to the Variable #Location
  421.  If Errorlevel 0 If Defined #Location (Goto :!#Location!)
  422. :# To [S]ell [E]quip or [D]rop Inventory:
  423. :# If the Player is not in a Town / Shop; Use: Call :ManageInventory /ED to Lock out Sale of Items
  424.  Call :ManageInventory /ESD
  425.  
  426. :Home This and below Label locations are examples of using Label Checkpoints
  427.  CLS
  428.  Call :Inventory Home /L
  429. :# Use last selected option to prevent recursive calls.
  430.  If not "!Option!" == "Home" (
  431.   %Menu% Exit "Manage Inventory" "Travel"
  432.  )Else %Menu% Exit "Manage Inventory"
  433.  If "!Option!" == "Manage Inventory" (
  434.   Call :!Option: =! /ESD
  435.   Goto :Home
  436.  )
  437.  If not "!Option!" == "Travel" Goto :Eof
  438.  
  439. :Travel
  440.  CLS
  441.  Call :Inventory Travel /L
  442.  Echo(Choose a Direction:
  443.  %Menu% Exit Home East West North South
  444.  Call :!Option: =! 2> Nul || If not Errorlevel 2 (
  445.   Echo That Direction has not been Scripted yet. You'll have to make your own Adventure.
  446.   Pause
  447.  )
  448.  Goto :Travel
  449.  
  450.  
  451.  
  452.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement