Advertisement
ZoriaRPG

HP Change Display, v0.7

Aug 19th, 2020
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.42 KB | None | 0 0
  1. ///////////////////////////
  2. // RPG-esque HP Display  //
  3. // v0.7                  //
  4. // 19th 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.                 //draw_floating_damage();
  64.                 init_hero_hp();
  65.                 do_hero_hp_clock();
  66.                 update_hero_hp();
  67.                 draw_hero_hp_change();
  68.                 Waitdraw();
  69.                 //for ( int q = Screen->NumNPCs(); q > 0; --q )
  70.                 //{
  71.                 //  npc n = Screen->LoadNPC(q);
  72.                 //  update_lanmola_hp(n);
  73.                    
  74.                 //}
  75.                 Waitframe();
  76.             }
  77.         }
  78.     }
  79.  
  80.  
  81.     void do_npc_hp_clock(npc n)
  82.     {
  83.         ++n->Misc[SPAWN_CLK];
  84.     }
  85.  
  86.     void init_enemy_hp(npc n)
  87.     {
  88.        
  89.         unless ( n->Misc[INITIALISED] )
  90.         {
  91.             if ( n->Family == NPCT_LANMOLA )
  92.             {
  93.                 if ( n->Core )
  94.                 {
  95.                    
  96.                    
  97.                     npcdata nd = Game->LoadNPCData(n->ID);
  98.                     n->Misc[OLDHP_INDX] = nd->HP*nd->Attributes[0]+1; //the spawned enemy has +1
  99.                     n->Misc[INITIALISED] = 1;
  100.                     n->Misc[ORIGHP_INDX] = nd->HP*nd->Attributes[0]-(nd->HP-1);
  101.                     //printf("n->Misc[ORIGHP_INDX]: %d\n", n->Misc[ORIGHP_INDX]);
  102.                     //printf("Lanmola core hp: %d\n",n->Misc[OLDHP_INDX]);
  103.                 }
  104.                
  105.             }
  106.            
  107.             else
  108.             {
  109.                 n->Misc[OLDHP_INDX] = n->HP;
  110.                 n->Misc[INITIALISED] = 1;
  111.             }
  112.         }
  113.     }
  114.    
  115.    
  116.     int get_lanmola_hp(npc n)
  117.     {
  118.         //if ( n->Misc[DRAW_CLK] ) return;
  119.         if ( n->Family == NPCT_LANMOLA  ) //update every frame because of engine stupidity
  120.         {
  121.                
  122.             int totalhp;
  123.                
  124.             if ( n->Core )
  125.             {
  126.                 totalhp = n->HP;
  127.                 int count;
  128.                 //find its chilren
  129.                 for ( int w = Screen->NumNPCs(); w > 0; --w )
  130.                 {
  131.                     npc child = Screen->LoadNPC(w);
  132.                     if ( child->ParentUID == n->UID ) //it is a child of this core
  133.                     {
  134.                         ++count;
  135.                         totalhp += child->HP;
  136.                     }
  137.                 }
  138.                 return totalhp;
  139.             }
  140.         }
  141.     }
  142.     void update_npc_hp(npc n)
  143.     {
  144.        
  145.        
  146.         {
  147.             if ( n->Family != NPCT_GLEEOK && n->Misc[DRAW_CLK] == 0 && (n->HitBy[2] || n->Family == NPCT_MOLDORM ) && n->HP != n->Misc[OLDHP_INDX] )
  148.             {
  149.                
  150.                 n->Misc[OLDHP_DIFF] = n->HP - n->Misc[OLDHP_INDX];
  151.                 n->Misc[DRAW_CLK] = HPCLOCKDUR;
  152.                
  153.             }
  154.             else if ( (n->Family == NPCT_GLEEOK && n->Core) && n->HP != n->Misc[OLDHP_INDX] && n->Misc[DRAW_CLK] == 0 )
  155.             {
  156.                 n->Misc[OLDHP_DIFF] = n->HP - n->Misc[OLDHP_INDX];
  157.                 n->Misc[DRAW_CLK] = HPCLOCKDUR;
  158.             }
  159.  
  160.             else if ( n->Family == NPCT_LANMOLA && n->Core && n->Misc[DRAW_CLK] == 0 ) ////&& n->HP != n->Misc[OLDHP_INDX]
  161.             {
  162.                
  163.                 int totalhp;
  164.                    
  165.                
  166.                 {
  167.                     totalhp = n->HP;
  168.                     int count;
  169.                     //find its chilren
  170.                     for ( int w = Screen->NumNPCs(); w > 0; --w )
  171.                     {
  172.                         npc child = Screen->LoadNPC(w);
  173.                         if ( child->ParentUID == n->UID ) //it is a child of this core
  174.                         {
  175.                             ++count;
  176.                             totalhp += child->HP;
  177.                         }
  178.                     }
  179.                     //printf("count of segment children is: %d\n", count);
  180.                     //printf("total HP of lanmola: %d\n", totalhp);
  181.                     //printf("n->Misc[SPAWN_CLK]: %d\n", n->Misc[SPAWN_CLK]);
  182.                     //printf("n->Misc[OLDHP_INDX]: %d\n", n->Misc[OLDHP_INDX]);
  183.                     //printf("n->Misc[CFHP_INDX]: %d\n", n->Misc[CFHP_INDX]);
  184.                     if ( totalhp != n->Misc[OLDHP_INDX] )
  185.                     {
  186.                         int dif = totalhp - n->Misc[OLDHP_INDX];
  187.                         //printf("dif is %d\n", dif);
  188.                         if ( dif != (n->Misc[ORIGHP_INDX]*-1) ) //quirk of lanmola spawn
  189.                         {
  190.                             n->Misc[OLDHP_DIFF] = dif;
  191.                             n->Misc[DRAW_CLK] = HPCLOCKDUR;
  192.                         }
  193.                         //n->Misc[OLDHP_INDX] = totalhp;
  194.                     }
  195.                     //else
  196.                     //{
  197.                     //  printf("Equal\n");
  198.                     //  n->Misc[OLDHP_INDX] = totalhp;
  199.                     //  n->Misc[OLDHP_DIFF] = 0;
  200.                     //}
  201.                    
  202.                 }
  203.                    
  204.                
  205.                    
  206.                    
  207.                
  208.                
  209.                
  210.             }
  211.         }
  212.        
  213.     }
  214.    
  215.     /*void draw_floating_damage()
  216.     {
  217.         for ( int q = Screen->NumLWeapons(); q > 0; --q )
  218.         {
  219.             lweapon t = Screen->LoadLWeapon(q);
  220.             if ( t->ID == LW_SPARKLE && t->Misc[IS_DUMMMY_SPR] )
  221.             {
  222.                 t->Script = Game->GetLWeaponScript("damagedisplay");
  223.             }
  224.         }
  225.     }*/
  226.    
  227.     lweapon script damagedisplay
  228.     {
  229.         void run()
  230.         {
  231.             //printf("Starting damagedisplay\n");
  232.             int buffer[8];
  233.             int buffer2[8];
  234.             itoa(buffer,this->Misc[DUMMY_AMOUNT]);
  235.             if ( this->Misc[DUMMY_AMOUNT] > 0 )
  236.             {
  237.                 buffer2[0] = '+';
  238.             }
  239.             strcat(buffer2, buffer);
  240.             while(1)
  241.             {
  242.                 //printf("Drawing damagedisplay\n");
  243.                 Screen->DrawString
  244.                 (
  245.                     DRAW_LAYER,
  246.                     this->X,
  247.                     this->Y,
  248.                     FONT_Z3SMALL,
  249.                     ((this->Misc[DUMMY_AMOUNT]<0) ? DMG_COLOUR : HEAL_COLOUR),
  250.                     -1,
  251.                     0,
  252.                     buffer2,
  253.                     128,
  254.                     SHD_OUTLINED8,
  255.                     ((this->Misc[DUMMY_AMOUNT]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  256.                 );
  257.                 Waitframe();
  258.             }
  259.         }
  260.     }
  261.  
  262.     void draw_npc_hp_change(npc n)
  263.     {
  264.         if (  n->Misc[SPAWN_CLK] < SPAWNCLKDUR ) return;
  265.         if ( n->Family == NPCT_LANMOLA && n->Misc[OLDHP_DIFF] > 0 ) return; //Lanmolas juggle HP so don't show imbalances being offset.
  266.         if ( --n->Misc[DRAW_CLK] > 0 && n->Misc[OLDHP_DIFF] )
  267.         {
  268.             /*int buffer[8];
  269.             int buffer2[8];
  270.             itoa(buffer,n->Misc[OLDHP_DIFF]);
  271.             if ( n->Misc[OLDHP_DIFF] > 0 )
  272.             {
  273.                 buffer2[0] = '+';
  274.             }
  275.             strcat(buffer2, buffer);
  276.             */
  277.             if ( TEMPLIFESPAN_SPRITE )
  278.             {
  279.                 int tmpx, tmpy;
  280.                 unless ( n->Misc[CREATEDSPR] )
  281.                 {
  282.                     int prntuid = n->ParentUID;
  283.                     npc prnt = NULL;
  284.                     if ( prntuid ) prnt = Screen->LoadNPCByUID(n->ParentUID);
  285.                     if (n->Family == NPCT_MOLDORM )
  286.                     {
  287.                         //find the core
  288.                         //npc prnt = Screen->LoadNPCByUID(n->ParentUID);
  289.                         tmpx = prnt->X;
  290.                         tmpy = prnt->Y;
  291.                     }
  292.                     if ( n->Family == NPCT_LANMOLA && prnt->isValid() )
  293.                     {
  294.                         //find the core
  295.                         //npc prnt = Screen->LoadNPCByUID(n->ParentUID);
  296.                         tmpx = prnt->X;
  297.                         tmpy = prnt->Y;
  298.                     }
  299.                     else if ( n->Family == NPCT_GLEEOK )
  300.                     {
  301.                         tmpx = prnt->X-12;
  302.                         tmpy = prnt->Y-4;
  303.                     }
  304.                     else
  305.                     {
  306.                         tmpx = n->X;
  307.                         tmpy = n->Y;
  308.                     }
  309.                    
  310.                     //printf("Creating dummy sprite.\n");
  311.                     lweapon dummy = Screen->CreateLWeapon(LW_SPARKLE);
  312.                     dummy->X = Clamp(tmpx + XOFS,0,SCREEN_W-8);
  313.                     dummy->Y = Clamp(tmpy + YOFS,0,SCREEN_H-8);
  314.                     dummy->UseSprite(DUMMY_SPRITE_ID);
  315.                     dummy->HitYOffset = -32768;
  316.                     dummy->Misc[DUMMY_AMOUNT] = n->Misc[OLDHP_DIFF];
  317.                     dummy->Script = Game->GetLWeaponScript("damagedisplay");
  318.                     //printf("Game->GetLWeaponScript(damagedisplay) is: %d\n", dummy->Script);
  319.                     n->Misc[CREATEDSPR] = 1;
  320.                 }
  321.             }
  322.             else
  323.             {
  324.                 int buffer[8];
  325.                 int buffer2[8];
  326.                 itoa(buffer,n->Misc[OLDHP_DIFF]);
  327.                 if ( n->Misc[OLDHP_DIFF] > 0 )
  328.                 {
  329.                     buffer2[0] = '+';
  330.                 }
  331.                 strcat(buffer2, buffer);
  332.                 Screen->DrawString
  333.                 (
  334.                     DRAW_LAYER,
  335.                     n->X + XOFS,
  336.                     n->Y + YOFS,
  337.                     FONT_Z3SMALL,
  338.                     ((n->Misc[OLDHP_DIFF]<0) ? DMG_COLOUR : HEAL_COLOUR),
  339.                     -1,
  340.                     0,
  341.                     buffer2,
  342.                     128,
  343.                     SHD_OUTLINED8,
  344.                     ((n->Misc[OLDHP_DIFF]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  345.                 );
  346.             }
  347.                
  348.            
  349.         }
  350.         else
  351.         {
  352.             if ( n->Family == NPCT_LANMOLA )
  353.             {
  354.                 n->Misc[DRAW_CLK] = 0;
  355.                 n->Misc[OLDHP_INDX] = get_lanmola_hp(n);
  356.                 n->Misc[CREATEDSPR] = 0;
  357.             }
  358.             else
  359.             {
  360.                 n->Misc[DRAW_CLK] = 0;
  361.                 n->Misc[OLDHP_INDX] = n->HP;
  362.                 n->Misc[CREATEDSPR] = 0;
  363.             }
  364.         }
  365.     }
  366.     void do_hero_hp_clock()
  367.     {
  368.         ++Hero->Misc[SPAWN_CLK];
  369.     }
  370.  
  371.     void init_hero_hp()
  372.     {
  373.         unless ( Hero->Misc[INITIALISED] )
  374.         {
  375.             Hero->Misc[OLDHP_INDX] = Hero->HP;
  376.             Hero->Misc[INITIALISED] = 1;
  377.         }
  378.     }
  379.     void update_hero_hp()
  380.     {
  381.        
  382.         if ( Hero->Misc[DRAW_CLK] == 0 && Hero->HP != Hero->Misc[OLDHP_INDX] )
  383.         {
  384.             Hero->Misc[OLDHP_DIFF] = Hero->HP - Hero->Misc[OLDHP_INDX];
  385.             Hero->Misc[DRAW_CLK] = HPCLOCKDUR;
  386.         }
  387.     }
  388.  
  389.  
  390.  
  391.     void draw_hero_hp_change()
  392.     {
  393.         if ( --Hero->Misc[DRAW_CLK] > 0 && Hero->Misc[OLDHP_DIFF] )
  394.         {
  395.            
  396.             int buffer[8];
  397.             int buffer2[8];
  398.            
  399.             itoa(buffer,Hero->Misc[OLDHP_DIFF]);
  400.             if ( Hero->Misc[OLDHP_DIFF] > 0 )
  401.             {
  402.                 buffer2[0] = '+';
  403.             }
  404.             strcat(buffer2, buffer);
  405.            
  406.             Screen->DrawString
  407.             (
  408.                 DRAW_LAYER,
  409.                 Hero->X + XOFS,
  410.                 Hero->Y + YOFS,
  411.                 FONT_Z3SMALL,
  412.                 ((Hero->Misc[OLDHP_DIFF]<0) ? DMG_COLOUR : HEAL_COLOUR),
  413.                 -1,
  414.                 0,
  415.                 buffer2,
  416.                 128,
  417.                 SHD_OUTLINED8,
  418.                 ((Hero->Misc[OLDHP_DIFF]<0) ? DMG_OUTLINE : HEAL_OUTLINE)
  419.             );
  420.         }
  421.         else
  422.         {
  423.             Hero->Misc[DRAW_CLK] = 0;
  424.             Hero->Misc[OLDHP_INDX] = Hero->HP;
  425.         }
  426.     }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement