Advertisement
ZoriaRPG

HP Change Display, v0.9

Aug 19th, 2020
1,479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.96 KB | None | 0 0
  1. ///////////////////////////
  2. // RPG-esque HP Display  //
  3. // v0.9                  //
  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.             if ( n->Family == NPCT_MOLDORM )
  101.             {
  102.                 if ( n->Core )
  103.                 {
  104.                    
  105.                     //find a child
  106.                     printf("molorm UID: %f\n", n->UID);
  107.                     npc child = NULL;
  108.                     for ( int w = Screen->NumNPCs(); w > 0; --w )
  109.                     {
  110.                         child = Screen->LoadNPC(w);
  111.                         if ( child->ParentUID == n->UID ) break;
  112.                     }
  113.                     printf("child uid is %d\n", child->UID);
  114.                     printf("child parentuid is %d\n", child->ParentUID);
  115.                     printf("child id is %d\n", child->ID);
  116.                    
  117.                     if ( child->ParentUID )
  118.                     {
  119.                             n->Misc[INITIALISED] = 1;
  120.                             npcdata nd = Game->LoadNPCData(child->ID); //the core has an ID of 0
  121.                             n->Misc[OLDHP_INDX] = nd->HP*nd->Attributes[0]+1; //the spawned enemy has +1
  122.                             n->Misc[ORIGHP_INDX] = nd->HP-1;  //the engine shuffles HP, and we use this to avoid displaying that shuffle.
  123.                     }
  124.                 }
  125.                 //else printf("Moldorm ParentUID: %d\n", n->ParentUID);
  126.                
  127.                 //printf("n->ID: %d\n",n->ID);
  128.                
  129.             }
  130.            
  131.             else
  132.             {
  133.                 n->Misc[OLDHP_INDX] = n->HP;
  134.                 n->Misc[INITIALISED] = 1;
  135.             }
  136.         }
  137.     }
  138.    
  139.     // Gets the total amount of HP from a lanmola core
  140.     // If used on a semnent, not a core, it returns the UID of the core as a NEGATIVE value.
  141.     int get_lanmola_hp(npc n)
  142.     {
  143.         int totalhp;
  144.                
  145.         if ( n->Core )
  146.         {
  147.             totalhp = n->HP;
  148.             int count;
  149.             // Find its children
  150.             for ( int w = Screen->NumNPCs(); w > 0; --w )
  151.             {
  152.                 npc child = Screen->LoadNPC(w);
  153.                 if ( child->ParentUID == n->UID ) // It is a child of this core
  154.                 {
  155.                     ++count;
  156.                     totalhp += child->HP;
  157.                 }
  158.             }
  159.             return totalhp;
  160.         }
  161.         else
  162.         {
  163.             int uid = n->ParentUID * -1;
  164.             return uid;
  165.         }
  166.        
  167.     }
  168.    
  169.     // Gets the total amount of HP from a moldorm core
  170.     // If used on a semnent, not a core, it returns the UID of the core as a NEGATIVE value.
  171.     int get_moldorm_hp(npc n)
  172.     {
  173.         int totalhp;
  174.                
  175.         if ( n->Core )
  176.         {
  177.             totalhp = n->HP;
  178.             int count;
  179.             // Find its children
  180.             for ( int w = Screen->NumNPCs(); w > 0; --w )
  181.             {
  182.                 npc child = Screen->LoadNPC(w);
  183.                 if ( child->ParentUID == n->UID ) // It is a child of this core
  184.                 {
  185.                     ++count;
  186.                     totalhp += child->HP;
  187.                 }
  188.             }
  189.             return totalhp;
  190.         }
  191.         else
  192.         {
  193.             int uid = n->ParentUID * -1;
  194.             return uid;
  195.         }
  196.        
  197.     }
  198.     void update_npc_hp(npc n)
  199.     {
  200.  
  201.        
  202.         if ( n->Family != NPCT_GLEEOK && n->Misc[DRAW_CLK] == 0 && (n->HitBy[2] ) && n->HP != n->Misc[OLDHP_INDX] )
  203.         {
  204.            
  205.             n->Misc[OLDHP_DIFF] = n->HP - n->Misc[OLDHP_INDX];
  206.             n->Misc[DRAW_CLK] = HPCLOCKDUR;
  207.            
  208.         }
  209.         else if ( (n->Family == NPCT_GLEEOK && n->Core) && n->HP != n->Misc[OLDHP_INDX] && n->Misc[DRAW_CLK] == 0 )
  210.         {
  211.             n->Misc[OLDHP_DIFF] = n->HP - n->Misc[OLDHP_INDX];
  212.             n->Misc[DRAW_CLK] = HPCLOCKDUR;
  213.         }
  214.  
  215.         else if ( n->Family == NPCT_MOLDORM && n->Core && n->Misc[DRAW_CLK] == 0 ) ////&& n->HP != n->Misc[OLDHP_INDX]
  216.         {
  217.             int totalhp;
  218.                
  219.            
  220.             {
  221.                 totalhp = n->HP;
  222.                 int count;
  223.                 //find its chilren
  224.                 for ( int w = Screen->NumNPCs(); w > 0; --w )
  225.                 {
  226.                     npc child = Screen->LoadNPC(w);
  227.                     if ( child->ParentUID == n->UID ) //it is a child of this core
  228.                     {
  229.                         ++count;
  230.                         totalhp += child->HP;
  231.                     }
  232.                 }
  233.                 int dif = totalhp - n->Misc[OLDHP_INDX];
  234.                 int shuffle = n->Misc[ORIGHP_INDX] * -1;
  235.                
  236.                
  237.                 //if ( (dif != shuffle ) && (( totalhp < n->Misc[OLDHP_INDX] ) || (n->Misc[SPAWN_CLK] > 100 && ( totalhp != n->Misc[OLDHP_INDX] ))) ) //cannot show positive HP ue to the engine shuffling it
  238.                 if ( ( totalhp < n->Misc[OLDHP_INDX] ) )//|| (n->Misc[SPAWN_CLK] > 100 && ( totalhp != n->Misc[OLDHP_INDX] )) ) //cannot show positive HP ue to the engine shuffling it
  239.                 {
  240.                     n->Misc[OLDHP_DIFF] = dif;
  241.                     n->Misc[DRAW_CLK] = HPCLOCKDUR;
  242.                 }
  243.             }
  244.         }
  245.        
  246.         else if ( n->Family == NPCT_LANMOLA && n->Core && n->Misc[DRAW_CLK] == 0 ) ////&& n->HP != n->Misc[OLDHP_INDX]
  247.         {
  248.            
  249.             int totalhp;
  250.                
  251.            
  252.             {
  253.                 totalhp = n->HP;
  254.                 int count;
  255.                 //find its chilren
  256.                 for ( int w = Screen->NumNPCs(); w > 0; --w )
  257.                 {
  258.                     npc child = Screen->LoadNPC(w);
  259.                     if ( child->ParentUID == n->UID ) //it is a child of this core
  260.                     {
  261.                         ++count;
  262.                         totalhp += child->HP;
  263.                     }
  264.                 }
  265.                 if ( totalhp != n->Misc[OLDHP_INDX] )
  266.                 {
  267.                     int dif = totalhp - n->Misc[OLDHP_INDX];
  268.                     if ( dif != (n->Misc[ORIGHP_INDX]*-1) ) //quirk of lanmola spawn
  269.                     {
  270.                         n->Misc[OLDHP_DIFF] = dif;
  271.                         n->Misc[DRAW_CLK] = HPCLOCKDUR;
  272.                     }
  273.                 }
  274.             }
  275.         }
  276.        
  277.     }
  278.    
  279.     lweapon script damagedisplay
  280.     {
  281.         void run()
  282.         {
  283.             int buffer[8];
  284.             int buffer2[8];
  285.             itoa(buffer,this->Misc[DUMMY_AMOUNT]);
  286.             if ( this->Misc[DUMMY_AMOUNT] > 0 )
  287.             {
  288.                 buffer2[0] = '+';
  289.             }
  290.             strcat(buffer2, buffer);
  291.             bool flip = 1;
  292.             int clk;
  293.             int origx = this->X;
  294.             while(1)
  295.             {
  296.                 Screen->DrawString
  297.                 (
  298.                     DRAW_LAYER,
  299.                     this->X,
  300.                     this->Y,
  301.                     FONT_Z3SMALL,
  302.                     ((this->Misc[DUMMY_AMOUNT]<0) ? DMG_COLOUR : HEAL_COLOUR),
  303.                     -1,
  304.                     0,
  305.                     buffer2,
  306.                     128,
  307.                     SHD_OUTLINED8,
  308.                     ((this->Misc[DUMMY_AMOUNT]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  309.                 );
  310.                 this->Y = Clamp((this->Y - 0.25), 0,SCREEN_H-8);
  311.                 //if ( (clk%2) )
  312.                 //{
  313.                 //  if ( flip )
  314.                 //  {
  315.                 //      this->X += 2;
  316.                 //      flip = 0;
  317.                 //  }
  318.                 //  else this->X -= 2;
  319.                 //}
  320.                 //else this->X = origx;
  321.                 ++clk;
  322.                 Waitframe();
  323.             }
  324.         }
  325.     }
  326.  
  327.     void draw_npc_hp_change(npc n)
  328.     {
  329.         if (  n->Misc[SPAWN_CLK] < SPAWNCLKDUR ) return;
  330.         if ( n->Family == NPCT_LANMOLA && n->Misc[OLDHP_DIFF] > 0 ) return; //Lanmolas juggle HP so don't show imbalances being offset.
  331.         if ( --n->Misc[DRAW_CLK] > 0 && n->Misc[OLDHP_DIFF] )
  332.         {
  333.            
  334.             if ( TEMPLIFESPAN_SPRITE )
  335.             {
  336.                 int tmpx, tmpy;
  337.                 unless ( n->Misc[CREATEDSPR] )
  338.                 {
  339.                     int prntuid = n->ParentUID;
  340.                     npc prnt = NULL;
  341.                     if (n->Family == NPCT_MOLDORM )
  342.                     {
  343.                         //find the core
  344.                         prnt = Screen->LoadNPCByUID(n->ParentUID);
  345.                         tmpx = prnt->X;
  346.                         tmpy = prnt->Y;
  347.                     }
  348.                     if ( n->Family == NPCT_LANMOLA && prnt->isValid() )
  349.                     {
  350.                         if ( n->Core )
  351.                         {
  352.                             prnt = n;
  353.                         }
  354.                         else prnt = Screen->LoadNPCByUID(n->ParentUID);
  355.                         tmpx = prnt->X;
  356.                         tmpy = prnt->Y;
  357.                     }
  358.                     else if ( n->Family == NPCT_GLEEOK )
  359.                     {
  360.                         prnt = Screen->LoadNPCByUID(n->ParentUID);
  361.                         tmpx = prnt->X-12;
  362.                         tmpy = prnt->Y-4;
  363.                     }
  364.                     else
  365.                     {
  366.                         tmpx = n->X;
  367.                         tmpy = n->Y;
  368.                     }
  369.                    
  370.                     lweapon dummy = Screen->CreateLWeapon(LW_SPARKLE);
  371.                     dummy->X = Clamp(tmpx + XOFS,0,SCREEN_W-8);
  372.                     dummy->Y = Clamp(tmpy + YOFS,0,SCREEN_H-8);
  373.                     dummy->UseSprite(DUMMY_SPRITE_ID);
  374.                     dummy->HitYOffset = -32768;
  375.                     dummy->Misc[DUMMY_AMOUNT] = n->Misc[OLDHP_DIFF];
  376.                     dummy->Script = Game->GetLWeaponScript("damagedisplay");
  377.                     n->Misc[CREATEDSPR] = 1;
  378.                 }
  379.             }
  380.             else
  381.             {
  382.                 int buffer[8];
  383.                 int buffer2[8];
  384.                 itoa(buffer,n->Misc[OLDHP_DIFF]);
  385.                 if ( n->Misc[OLDHP_DIFF] > 0 )
  386.                 {
  387.                     buffer2[0] = '+';
  388.                 }
  389.                 strcat(buffer2, buffer);
  390.                 Screen->DrawString
  391.                 (
  392.                     DRAW_LAYER,
  393.                     n->X + XOFS,
  394.                     n->Y + YOFS,
  395.                     FONT_Z3SMALL,
  396.                     ((n->Misc[OLDHP_DIFF]<0) ? DMG_COLOUR : HEAL_COLOUR),
  397.                     -1,
  398.                     0,
  399.                     buffer2,
  400.                     128,
  401.                     SHD_OUTLINED8,
  402.                     ((n->Misc[OLDHP_DIFF]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  403.                 );
  404.             }
  405.         }
  406.         else
  407.         {
  408.             if ( n->Family == NPCT_LANMOLA )
  409.             {
  410.                 n->Misc[DRAW_CLK] = 0;
  411.                 n->Misc[OLDHP_INDX] = get_lanmola_hp(n);
  412.                 n->Misc[CREATEDSPR] = 0;
  413.             }
  414.             else if ( n->Family == NPCT_MOLDORM )
  415.             {
  416.                 n->Misc[DRAW_CLK] = 0;
  417.                 n->Misc[OLDHP_INDX] = get_moldorm_hp(n);
  418.                 n->Misc[CREATEDSPR] = 0;
  419.             }
  420.             else
  421.             {
  422.                 n->Misc[DRAW_CLK] = 0;
  423.                 n->Misc[OLDHP_INDX] = n->HP;
  424.                 n->Misc[CREATEDSPR] = 0;
  425.             }
  426.         }
  427.     }
  428.     void do_hero_hp_clock()
  429.     {
  430.         ++Hero->Misc[SPAWN_CLK];
  431.     }
  432.  
  433.     void init_hero_hp()
  434.     {
  435.         unless ( Hero->Misc[INITIALISED] )
  436.         {
  437.             Hero->Misc[OLDHP_INDX] = Hero->HP;
  438.             Hero->Misc[INITIALISED] = 1;
  439.         }
  440.     }
  441.     void update_hero_hp()
  442.     {
  443.        
  444.         if ( Hero->Misc[DRAW_CLK] == 0 && Hero->HP != Hero->Misc[OLDHP_INDX] )
  445.         {
  446.             Hero->Misc[OLDHP_DIFF] = Hero->HP - Hero->Misc[OLDHP_INDX];
  447.             Hero->Misc[DRAW_CLK] = HPCLOCKDUR;
  448.         }
  449.     }
  450.  
  451.     void draw_hero_hp_change()
  452.     {
  453.         if ( --Hero->Misc[DRAW_CLK] > 0 && Hero->Misc[OLDHP_DIFF] )
  454.         {
  455.            
  456.             int buffer[8];
  457.             int buffer2[8];
  458.            
  459.             itoa(buffer,Hero->Misc[OLDHP_DIFF]);
  460.             if ( Hero->Misc[OLDHP_DIFF] > 0 )
  461.             {
  462.                 buffer2[0] = '+';
  463.             }
  464.             strcat(buffer2, buffer);
  465.            
  466.             Screen->DrawString
  467.             (
  468.                 DRAW_LAYER,
  469.                 Hero->X + XOFS,
  470.                 Hero->Y + YOFS,
  471.                 FONT_Z3SMALL,
  472.                 ((Hero->Misc[OLDHP_DIFF]<0) ? DMG_COLOUR : HEAL_COLOUR),
  473.                 -1,
  474.                 0,
  475.                 buffer2,
  476.                 128,
  477.                 SHD_OUTLINED8,
  478.                 ((Hero->Misc[OLDHP_DIFF]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  479.             );
  480.         }
  481.         else
  482.         {
  483.             Hero->Misc[DRAW_CLK] = 0;
  484.             Hero->Misc[OLDHP_INDX] = Hero->HP;
  485.         }
  486.     }
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement