Advertisement
ZoriaRPG

HP Change Display, v0.8.1

Aug 19th, 2020
2,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.01 KB | None | 0 0
  1. ///////////////////////////
  2. // RPG-esque HP Display  //
  3. // v0.8.1                //
  4. // 20th August, 2020     //
  5. // For ZC 2.55 Alpha 80+ //
  6. // By: ZoriaRPG          //
  7. ///////////////////////////
  8.  
  9. namespace hphealth
  10. {
  11.     const int TEMPLIFESPAN_SPRITE = 1;
  12.     enum colourtype { cNONE, cWHITE, cGREY, cBLUE, cRED = 0x81 };
  13.    
  14.     const int OLDHP_INDX = 19;
  15.     const int ORIGHP_INDX = 25; //lanmola
  16.     const int OLDHP_DIFF = 20;
  17.     const int SPAWN_CLK = 21;
  18.     const int DRAW_CLK = 22;
  19.     const int INITIALISED = 23;
  20.     const int SPAWNCLKDUR = 60;
  21.     const int HPCLOCKDUR = 10;
  22.     const int CREATEDSPR = 24;
  23.  
  24.     const int DMG_COLOUR = cRED;
  25.     const int DMG_OUTLINE = cWHITE;
  26.     const int HEAL_OUTLINE = cWHITE;
  27.     const int HEAL_COLOUR = cBLUE;
  28.  
  29.     const int DRAW_LAYER = 6;
  30.     const int XOFS = -4;
  31.     const int YOFS = -4;
  32.     const int DUMMY_SPRITE_ID = 255;
  33.     const int DUMMY_SPRITE_OTILE = 214240;
  34.     const int DUMMY_SPRITE_FRAMES = 1;
  35.     const int DUMMY_SPRITE_SPD = 16;
  36.     const int DUMMY_AMOUNT = 30;
  37.    
  38.     void init_dummy_sprite()
  39.     {
  40.         spritedata sd = Game->LoadSpriteData(DUMMY_SPRITE_ID);
  41.         sd->Tile = DUMMY_SPRITE_OTILE;
  42.         sd->Frames = DUMMY_SPRITE_FRAMES;
  43.         sd->Speed = DUMMY_SPRITE_SPD;
  44.     }
  45.        
  46.        
  47.  
  48.     global script test
  49.     {
  50.         void run()
  51.         {
  52.             init_dummy_sprite();
  53.             while(1)
  54.             {
  55.                 for ( int q = Screen->NumNPCs(); q > 0; --q )
  56.                 {
  57.                     npc n = Screen->LoadNPC(q);
  58.                     init_enemy_hp(n);
  59.                     do_npc_hp_clock(n);
  60.                     update_npc_hp(n);
  61.                     draw_npc_hp_change(n);
  62.                 }
  63.                
  64.                 init_hero_hp();
  65.                 do_hero_hp_clock();
  66.                 update_hero_hp();
  67.                 draw_hero_hp_change();
  68.                 Waitdraw();
  69.                
  70.                 Waitframe();
  71.             }
  72.         }
  73.     }
  74.  
  75.  
  76.     void do_npc_hp_clock(npc n)
  77.     {
  78.         ++n->Misc[SPAWN_CLK];
  79.     }
  80.  
  81.     void init_enemy_hp(npc n)
  82.     {
  83.        
  84.         unless ( n->Misc[INITIALISED] )
  85.         {
  86.             if ( n->Family == NPCT_LANMOLA )
  87.             {
  88.                 if ( n->Core )
  89.                 {
  90.                    
  91.                    
  92.                     npcdata nd = Game->LoadNPCData(n->ID);
  93.                     n->Misc[OLDHP_INDX] = nd->HP*nd->Attributes[0]+1; //the spawned enemy has +1
  94.                     n->Misc[INITIALISED] = 1;
  95.                     n->Misc[ORIGHP_INDX] = nd->HP*nd->Attributes[0]-(nd->HP-1);  //the engine shuffles HP, and we use this to avoid displaying that shuffle.
  96.                    
  97.                 }
  98.                
  99.             }
  100.            
  101.             else
  102.             {
  103.                 n->Misc[OLDHP_INDX] = n->HP;
  104.                 n->Misc[INITIALISED] = 1;
  105.             }
  106.         }
  107.     }
  108.    
  109.     // Gets the total amount of HP from a lanmola core
  110.     // If used on a semnent, not a core, it returns the UID of the core as a NEGATIVE value.
  111.     int get_lanmola_hp(npc n)
  112.     {
  113.         int totalhp;
  114.                
  115.         if ( n->Core )
  116.         {
  117.             totalhp = n->HP;
  118.             int count;
  119.             // Find its children
  120.             for ( int w = Screen->NumNPCs(); w > 0; --w )
  121.             {
  122.                 npc child = Screen->LoadNPC(w);
  123.                 if ( child->ParentUID == n->UID ) // It is a child of this core
  124.                 {
  125.                     ++count;
  126.                     totalhp += child->HP;
  127.                 }
  128.             }
  129.             return totalhp;
  130.         }
  131.         else
  132.         {
  133.             int uid = n->ParentUID * -1;
  134.             return uid;
  135.         }
  136.        
  137.     }
  138.     void update_npc_hp(npc n)
  139.     {
  140.  
  141.        
  142.         if ( n->Family != NPCT_GLEEOK && n->Misc[DRAW_CLK] == 0 && (n->HitBy[2] || n->Family == NPCT_MOLDORM ) && n->HP != n->Misc[OLDHP_INDX] )
  143.         {
  144.            
  145.             n->Misc[OLDHP_DIFF] = n->HP - n->Misc[OLDHP_INDX];
  146.             n->Misc[DRAW_CLK] = HPCLOCKDUR;
  147.            
  148.         }
  149.         else if ( (n->Family == NPCT_GLEEOK && n->Core) && n->HP != n->Misc[OLDHP_INDX] && n->Misc[DRAW_CLK] == 0 )
  150.         {
  151.             n->Misc[OLDHP_DIFF] = n->HP - n->Misc[OLDHP_INDX];
  152.             n->Misc[DRAW_CLK] = HPCLOCKDUR;
  153.         }
  154.  
  155.         else if ( n->Family == NPCT_LANMOLA && n->Core && n->Misc[DRAW_CLK] == 0 ) ////&& n->HP != n->Misc[OLDHP_INDX]
  156.         {
  157.            
  158.             int totalhp;
  159.                
  160.            
  161.             {
  162.                 totalhp = n->HP;
  163.                 int count;
  164.                 //find its chilren
  165.                 for ( int w = Screen->NumNPCs(); w > 0; --w )
  166.                 {
  167.                     npc child = Screen->LoadNPC(w);
  168.                     if ( child->ParentUID == n->UID ) //it is a child of this core
  169.                     {
  170.                         ++count;
  171.                         totalhp += child->HP;
  172.                     }
  173.                 }
  174.                 if ( totalhp != n->Misc[OLDHP_INDX] )
  175.                 {
  176.                     int dif = totalhp - n->Misc[OLDHP_INDX];
  177.                     if ( dif != (n->Misc[ORIGHP_INDX]*-1) ) //quirk of lanmola spawn
  178.                     {
  179.                         n->Misc[OLDHP_DIFF] = dif;
  180.                         n->Misc[DRAW_CLK] = HPCLOCKDUR;
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.        
  186.     }
  187.    
  188.     lweapon script damagedisplay
  189.     {
  190.         void run()
  191.         {
  192.             int buffer[8];
  193.             int buffer2[8];
  194.             itoa(buffer,this->Misc[DUMMY_AMOUNT]);
  195.             if ( this->Misc[DUMMY_AMOUNT] > 0 )
  196.             {
  197.                 buffer2[0] = '+';
  198.             }
  199.             strcat(buffer2, buffer);
  200.             while(1)
  201.             {
  202.                 Screen->DrawString
  203.                 (
  204.                     DRAW_LAYER,
  205.                     this->X,
  206.                     this->Y,
  207.                     FONT_Z3SMALL,
  208.                     ((this->Misc[DUMMY_AMOUNT]<0) ? DMG_COLOUR : HEAL_COLOUR),
  209.                     -1,
  210.                     0,
  211.                     buffer2,
  212.                     128,
  213.                     SHD_OUTLINED8,
  214.                     ((this->Misc[DUMMY_AMOUNT]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  215.                 );
  216.                 Waitframe();
  217.             }
  218.         }
  219.     }
  220.  
  221.     void draw_npc_hp_change(npc n)
  222.     {
  223.         if (  n->Misc[SPAWN_CLK] < SPAWNCLKDUR ) return;
  224.         if ( n->Family == NPCT_LANMOLA && n->Misc[OLDHP_DIFF] > 0 ) return; //Lanmolas juggle HP so don't show imbalances being offset.
  225.         if ( --n->Misc[DRAW_CLK] > 0 && n->Misc[OLDHP_DIFF] )
  226.         {
  227.            
  228.             if ( TEMPLIFESPAN_SPRITE )
  229.             {
  230.                 int tmpx, tmpy;
  231.                 unless ( n->Misc[CREATEDSPR] )
  232.                 {
  233.                     int prntuid = n->ParentUID;
  234.                     npc prnt = NULL;
  235.                     if (n->Family == NPCT_MOLDORM )
  236.                     {
  237.                         //find the core
  238.                         prnt = Screen->LoadNPCByUID(n->ParentUID);
  239.                         tmpx = prnt->X;
  240.                         tmpy = prnt->Y;
  241.                     }
  242.                     if ( n->Family == NPCT_LANMOLA && prnt->isValid() )
  243.                     {
  244.                         if ( n->Core )
  245.                         {
  246.                             prnt = n;
  247.                         }
  248.                         else prnt = Screen->LoadNPCByUID(n->ParentUID);
  249.                         tmpx = prnt->X;
  250.                         tmpy = prnt->Y;
  251.                     }
  252.                     else if ( n->Family == NPCT_GLEEOK )
  253.                     {
  254.                         prnt = Screen->LoadNPCByUID(n->ParentUID);
  255.                         tmpx = prnt->X-12;
  256.                         tmpy = prnt->Y-4;
  257.                     }
  258.                     else
  259.                     {
  260.                         tmpx = n->X;
  261.                         tmpy = n->Y;
  262.                     }
  263.                    
  264.                     lweapon dummy = Screen->CreateLWeapon(LW_SPARKLE);
  265.                     dummy->X = Clamp(tmpx + XOFS,0,SCREEN_W-8);
  266.                     dummy->Y = Clamp(tmpy + YOFS,0,SCREEN_H-8);
  267.                     dummy->UseSprite(DUMMY_SPRITE_ID);
  268.                     dummy->HitYOffset = -32768;
  269.                     dummy->Misc[DUMMY_AMOUNT] = n->Misc[OLDHP_DIFF];
  270.                     dummy->Script = Game->GetLWeaponScript("damagedisplay");
  271.                     n->Misc[CREATEDSPR] = 1;
  272.                 }
  273.             }
  274.             else
  275.             {
  276.                 int buffer[8];
  277.                 int buffer2[8];
  278.                 itoa(buffer,n->Misc[OLDHP_DIFF]);
  279.                 if ( n->Misc[OLDHP_DIFF] > 0 )
  280.                 {
  281.                     buffer2[0] = '+';
  282.                 }
  283.                 strcat(buffer2, buffer);
  284.                 Screen->DrawString
  285.                 (
  286.                     DRAW_LAYER,
  287.                     n->X + XOFS,
  288.                     n->Y + YOFS,
  289.                     FONT_Z3SMALL,
  290.                     ((n->Misc[OLDHP_DIFF]<0) ? DMG_COLOUR : HEAL_COLOUR),
  291.                     -1,
  292.                     0,
  293.                     buffer2,
  294.                     128,
  295.                     SHD_OUTLINED8,
  296.                     ((n->Misc[OLDHP_DIFF]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  297.                 );
  298.             }
  299.         }
  300.         else
  301.         {
  302.             if ( n->Family == NPCT_LANMOLA )
  303.             {
  304.                 n->Misc[DRAW_CLK] = 0;
  305.                 n->Misc[OLDHP_INDX] = get_lanmola_hp(n);
  306.                 n->Misc[CREATEDSPR] = 0;
  307.             }
  308.             else
  309.             {
  310.                 n->Misc[DRAW_CLK] = 0;
  311.                 n->Misc[OLDHP_INDX] = n->HP;
  312.                 n->Misc[CREATEDSPR] = 0;
  313.             }
  314.         }
  315.     }
  316.     void do_hero_hp_clock()
  317.     {
  318.         ++Hero->Misc[SPAWN_CLK];
  319.     }
  320.  
  321.     void init_hero_hp()
  322.     {
  323.         unless ( Hero->Misc[INITIALISED] )
  324.         {
  325.             Hero->Misc[OLDHP_INDX] = Hero->HP;
  326.             Hero->Misc[INITIALISED] = 1;
  327.         }
  328.     }
  329.     void update_hero_hp()
  330.     {
  331.        
  332.         if ( Hero->Misc[DRAW_CLK] == 0 && Hero->HP != Hero->Misc[OLDHP_INDX] )
  333.         {
  334.             Hero->Misc[OLDHP_DIFF] = Hero->HP - Hero->Misc[OLDHP_INDX];
  335.             Hero->Misc[DRAW_CLK] = HPCLOCKDUR;
  336.         }
  337.     }
  338.  
  339.     void draw_hero_hp_change()
  340.     {
  341.         if ( --Hero->Misc[DRAW_CLK] > 0 && Hero->Misc[OLDHP_DIFF] )
  342.         {
  343.            
  344.             int buffer[8];
  345.             int buffer2[8];
  346.            
  347.             itoa(buffer,Hero->Misc[OLDHP_DIFF]);
  348.             if ( Hero->Misc[OLDHP_DIFF] > 0 )
  349.             {
  350.                 buffer2[0] = '+';
  351.             }
  352.             strcat(buffer2, buffer);
  353.            
  354.             Screen->DrawString
  355.             (
  356.                 DRAW_LAYER,
  357.                 Hero->X + XOFS,
  358.                 Hero->Y + YOFS,
  359.                 FONT_Z3SMALL,
  360.                 ((Hero->Misc[OLDHP_DIFF]<0) ? DMG_COLOUR : HEAL_COLOUR),
  361.                 -1,
  362.                 0,
  363.                 buffer2,
  364.                 128,
  365.                 SHD_OUTLINED8,
  366.                 ((Hero->Misc[OLDHP_DIFF]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  367.             );
  368.         }
  369.         else
  370.         {
  371.             Hero->Misc[DRAW_CLK] = 0;
  372.             Hero->Misc[OLDHP_INDX] = Hero->HP;
  373.         }
  374.     }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement