ZoriaRPG

Random Encounter Template v0.2

Feb 7th, 2018
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////////////////
  2. /// Generic Turn-Based Battle Template /// Rev 0.2 -  7th Febryary, 2018 ///    
  3. /// Requires Zelda Classic 2.54        /// By: ZoriaRPG                  ///  
  4. /// ...Alpha 22 or later.              /// For educational purposes.     ///
  5. ////////////////////////////////////////////////////////////////////////////
  6.  
  7. /////////////////////////////////////////
  8. /// Defines for game engine constants ///
  9. /////////////////////////////////////////
  10.  
  11. //General
  12. const int FFC_INVISIBLE_DATA        = 0;
  13.  
  14. //Screen->D
  15. const int SCREEN_D_BATTLES      = 4;
  16. const int SCREEN_D_BATTLE_TIMER_CHANGED = 1;
  17.  
  18. //Battles[] Array
  19. const int BATTLE_TIMER          = 0;
  20. const int BATTLE_TIMER_MIN      = 1;
  21. const int BATTLE_TIMER_MAX      = 2;
  22. const int BATTLE_TIMER_DOBATTLE     = 3;
  23. const int BATTLE_NUMNPCS        = 4;
  24.    
  25. const int BATTLE_MAX            = 40;
  26.  
  27. //NPC Misc / Attributes
  28. const int NPCM_ATTACKTIMER      = 0;
  29.  
  30. const int NPCA_INT          = 0;
  31. const int NPCA_WIS          = 1;
  32. const int NPCA_MORALE           = 2;
  33. const int NPCA_SPEED            = 3;
  34. const int NPCA_MP           = 4;
  35. const int NPCA_SPELLS           = 5;
  36. const int NPCA_INVENTORY        = 6;
  37. const int NPCA_SPECIAL_STRING       = 7;
  38.  
  39. //Global Array Sizes
  40. const int GAS_NPCDATA           = 512;
  41. const int GAS_MAPDATA           = 512;
  42. const int GAS_ITEMDATA          = 512;
  43. const int GAS_DROPDATA          = 512;
  44. const int GAS_SHOPDATA          = 512;
  45. const int GAS_NPCS          = 256;
  46. const int GAS_ITEMS             = 256;
  47. const int GAS_EWEAPONS          = 256;
  48. const int GAS_LWEAPONS          = 256;
  49. const int GAS_TEXTBUFFER        = 1024;
  50. const int GAS_TYPEBUFFER        = 129;
  51. const int GAS_GLOBALRAM         = 4096;
  52.  
  53. //Global arrays
  54. int Battles[BATTLE_MAX];
  55. int Buffer[GAS_TEXTBUFFER];
  56. int TypingBuffer[GAS_TYPEBUFFER];
  57. int GameRAM[GAS_GLOBALRAM]; //Timers go here.
  58.  
  59. npcdata NData[GAS_NPCDATA];
  60. mapdata MapScr[GAS_MAPDATA];
  61. itemdata IData[GAS_ITEMDATA];
  62. dropsetdata DropData[GAS_DROPDATA];
  63. shopdata ShopData[GAS_SHOPDATA];
  64. ffc GlobalFF[MAX_FFCS];
  65.  
  66. //For on-screen sprited effects:
  67. npc GlobalNPC[GAS_NPCS];
  68. item GlobalItem[GAS_ITEMS];
  69. eweapon GlobalItem[GAS_EWEAPONS];
  70. lweapon GlobalItem[GAS_LWEAPONS];
  71.  
  72.  
  73. ///////////////////////////////
  74. /// Battle System Functions ///
  75. ///////////////////////////////
  76.  
  77. //Disable battles, temporarily.
  78. void NoBattles()
  79. {
  80. }
  81.  
  82. //Sets the minimum and maximum values of a timer.
  83. //These will be parsed as imputs for Rand().
  84. void SetBattleTimerValues(int min, int max)
  85. {
  86.     Battles[BATTLE_TIMER_MIN] = min;
  87.     Battles[BATTLE_TIMER_MAX] = max;
  88. }
  89.  
  90. //Sets the precise value of the ticking timer.
  91. void SetBattleTimerTick(int amt)
  92. {
  93.     Battles[BATTLE_TIMER] = amt;
  94. }
  95.    
  96. //Decrements the battle timer, and determines if there is a battle.
  97. //Call in the active script.
  98. void DecrementBattleTimer()
  99. {
  100.     --Battles[BATTLE_TIMER];
  101.     if ( Battles[BATTLE_TIMER] <= 0 )
  102.     {
  103.         Battles[BATTLE_TIMER] = 214748;
  104.         Battles[BATTLE_TIMER_DOBATTLE] = 1; //this denotes tot he global script that we are runing a random encounter
  105.         RandomEncounter();
  106.     }
  107. }
  108.  
  109.  
  110. //reset values when the battle ends
  111. void BattleOver(npcdata nd, int xp, int cold, int treasure)
  112. {
  113.     bool awarded = AwardXP_Gold_Treasure(xp, gold, nd);
  114.     SetBattleTimerTick( Rand(Battles[BATTLE_TIMER_MIN], Battles[BATTLE_TIMER_MAX]);
  115. }
  116.  
  117. //award stuff after battles
  118. bool AwardXP_Gold_Treasure(int xp, int gold, npcdata special)
  119. {
  120.     ffc f = Screen->LoadFFC(FFC_MENU_AWARDS);
  121.     f->Data = FFC_INVISIBLE_DATA;
  122.    
  123.     f->Script = Game->GetFFCScript("BattleRewards");
  124.     f->InitD[3] = special;
  125.     return true;
  126. }
  127.  
  128. //This ffc displays Tango messages after a battle is over.
  129. ffc script BattleRewards
  130. {
  131.     void run(int xp, int gold, npcdata special)
  132.     {
  133.         //Determine what items an npc drops
  134.         dropsetdata dr = Game->LoadDropSetData(special->DropSet);
  135.         //Do random drop chances based on dropsetdata.
  136.        
  137.         //Tango menus to display this stuff.
  138.        
  139.     }
  140. }
  141.  
  142.  
  143. //Place on a screen to have that screen change the battle timer for an area.
  144. ffc script FFC_Set_Battle_Timer_Values
  145. {
  146.     void run(int min, int max, int reg, int bit, bool repeat)
  147.     {
  148.         if ( reg <= 0 ) { reg = SCREEN_D_BATTLES; }
  149.         if ( bit <= 0 ) { bit = SCREEN_D_BATTLE_TIMER_CHANGED; }
  150.         if ( !repeat )
  151.         {
  152.             if ( !GetScreenDBit(reg, bit) )
  153.             {
  154.                 Battles[BATTLE_TIMER_MIN] = min;
  155.                 Battles[BATTLE_TIMER_MAX] = max;
  156.                 SetScreenDBit(reg, bit, true);
  157.             }
  158.         }
  159.         else
  160.         {
  161.             Battles[BATTLE_TIMER_MIN] = min;
  162.             Battles[BATTLE_TIMER_MAX] = max;
  163.         }
  164.         Quit();
  165.     }
  166. }
  167.  
  168. //Runs a random encounter, based on the timer.
  169. void RandomEncounter()
  170. {
  171.     mapdata m = Game->LoadMapDat(Game->GetCurMap(), Game->GetCurScreen());
  172.     npcdata e = Game->LoadNPCData(m-Enemy[Rand(10]l
  173.     ffc f = Screen->LoadFFC(32);
  174.     f->Script = Game->GetFFCScript("Battle");
  175.     f->InitD[0] = e;
  176.     f->InitD[1] = m; //for drawing backdrops
  177. }
  178.  
  179. //The battle menus run from here, as do all enemy actions.
  180. ffc script Battle
  181. {
  182.    void run(npcdata e, mapdata m)
  183.    {
  184.         bool waiting; int mode; int action;
  185.     while(1)
  186.     {
  187.         // Draw background using 'm'
  188.                 // draw npc using 'e'
  189.             // determine e's attacks using it's attributes;
  190.             // draw menus
  191.             // determine who goes first
  192.             mode = Rand(1); //if 1, enemy goes first
  193.             while(mode)
  194.             {
  195.                 //do enemy actions
  196.                 //Waitframe();
  197.             }
  198.             while(!mode)
  199.             {
  200.                 //player actions
  201.             waiting = true;
  202.             while(waiting)
  203.             {
  204.                 //do_menus();
  205.                 action = BattleMenu();
  206.                     Waitframe();
  207.                 }
  208.            
  209.                 //resolve stuff this round
  210.             Waitframe();
  211.         }
  212.     }
  213. }
  214.  
  215.  
  216. // There are certainly other ways to handle these...
  217. // Vars used by the Tango menu.
  218. int menuCommand;
  219. int menuArg;
  220.  
  221. //Tango Menu Init
  222. void SetUpWindow(int slot, int style, int x, int y, int size)
  223. {
  224.     SetStyleSize(style, size);
  225.     Tango_ClearSlot(slot);
  226.     Tango_SetSlotStyle(slot, style);
  227.     Tango_SetSlotPosition(slot, x, y);
  228. }
  229.  
  230. //Quick way to drop a string into a Tango dialogue box.
  231. void ShowString(int string, int slot, int style, int x, int y)
  232. {
  233.     SetUpWindow(slot, style, x, y, SIZE_LARGE);
  234.     Tango_LoadString(slot, string);
  235.     Tango_ActivateSlot(slot);
  236.     while(Tango_SlotIsActive(slot))  
  237.         Waitframe();
  238. }
  239.  
  240.  
  241. //The Battle Menus called from the 'Battle' FFC.
  242. int BattleMenu() {
  243.  
  244.     int lineBreak[]="@26";
  245.     int line1[]="@choice(1)Attack@tab(40)@choice(2)Spell@26";
  246.     int line2[]="@choice(3)Item@tab(40)@choice(4)Item@domenu(1)@suspend()";
  247.    
  248.    
  249.     SetUpWindow(WINDOW_SLOT_1, WINDOW_STYLE_3, 32, 32, SIZE_LARGE);
  250.         Tango_LoadString(WINDOW_SLOT_1, line1);
  251.     Tango_AppendString(WINDOW_SLOT_1, line2);
  252.         Tango_ActivateSlot(WINDOW_SLOT_1);
  253.  
  254.    
  255.     while(!Tango_MenuIsActive()){
  256.        
  257.         Waitframe();
  258.     }
  259.    
  260.     // Save the state again...
  261.     int slotState[278];
  262.     int menuState[960];
  263.     int cursorPos;
  264.     Tango_SaveSlotState(WINDOW_SLOT_1, slotState);
  265.     Tango_SaveMenuState(menuState);
  266.    
  267.     int done = 0;
  268.     int choice;
  269.     while(true){
  270.    
  271.         while( Tango_MenuIsActive() ) {
  272.             cursorPos=Tango_GetMenuCursorPosition();
  273.             Waitframe();
  274.         }
  275.        
  276.         choice = Tango_GetLastMenuChoice();
  277.         if ( choice == 1 ) { // Item Info
  278.             int text[256];
  279.             Game->GetMessage(msg,text); //Load msg into the buffer.
  280.             ShowString(text, WINDOW_SLOT_2, WINDOW_STYLE_3, 48, 48);
  281.         }
  282.         else if ( choice == 2 ) { //Buy
  283.             done = choice;
  284.             menuArg = choice;
  285.         }
  286.        
  287.         else if ( choice == 3 ) { //Cancel
  288.             done = choice;
  289.         }
  290.        
  291.         else if ( Link->PressEx1 ) {
  292.             done = 1;
  293.         }
  294.         else done = 3;
  295.  
  296.         if ( done ) {
  297.             break;
  298.         //  return choice;
  299.         }
  300.         else {
  301.             Tango_RestoreSlotState(WINDOW_SLOT_1, slotState);
  302.             Tango_RestoreMenuState(menuState);
  303.             Tango_SetMenuCursorPosition(cursorPos);
  304.         }
  305.     }
  306.    
  307.     Tango_ClearSlot(WINDOW_SLOT_1);
  308.     return done;
  309. }
RAW Paste Data