Autor Tema: Decimal dilemma!  (Leído 769 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Breadcaster

  • Newbie
  • *
  • Mensajes: 38
  • Karma: 2
    • Ver Perfil
Decimal dilemma!
« en: Julio 03, 2016, 08:43:37 am »
Hello everyone!

So CyberCrisis is coming along well. There's a few things to be added, like FMV sequences and a few more sound effects, but the bulk of the work is done - now there's just a few pesky bugs left to be fixed. I'd like to thank everyone who's helped so far on the forums, I'll be sure to put a nod to you in the ingame credits. Here's a trailer of the game so far, to show that your assistance has not been in vain! https://vimeo.com/173254063

Now, onto the aforementioned "decimal dilemma". One of the last remaining game bugs is that the health bars do not always accurately depict the health of the units they are floating over, including the player. As this is the only indicator for the players health, this is a pretty major issue.

The reason for this bug, as far as I can tell, is because the system of updating it is so weird - at present, the healthbar(); process is updated by a local variable called "wounded" in the father of the process that called it - which is how processes communicate that they're doing damage to each other in CyberCrisis (via "wounded"). So in the context of the healthbar - "if (father.wounded<>0)" is how the health bar process knows to update itself, taking drawing the red box further to the left of its map. This has an issue when the gameplay gets as frantic as you see in that above trailer there ^^^^ so another solution is required.

Other variables used in the game are health and maxhealth - these should be self explanatory, but essentially maxhealth is the maximum amount of health the player can have, and health is how much health the player currently has. What I thought would be a better idea than using the if (father.wounded<>0) method would be to just update the healthbar(); process every frame, using a calculation between the width of the health bar graphic (50 - 50 pixels wide and 3 pixels high), health and maxhealth - regardless of if the player has taken any damage or not (I would need to somehow adjust this for the enemy units, destructible objects and buildings, as their health variables are private - this could be done with a local variable for health, let's say notplayer_health :p ).

So, the solution I came up with, was firstly to declare some private variables - health_unit and h_unittoremove. health_unit=(50/maxhealth) to come up with the amount of pixels to remove for each health point lost, and h_unittoremove=(health_unit*health), to come up with the total number of pixels to redraw as red (with the red box). Then draw_box(0, 3, h_unittoremove, 3); frame; in the main loop of the process. Now, this works just fine in theory. Maxhealth and health start as the same amount - 300. (you can upgrade the amount of maxhealth you can have later on in the game, through the use of the ingame shop :) )

So, 50/300 (maxhealth) =0.166666666666667 - so 0.166666666666667*300 (health) = 50. Lower the health (the second 300), let's say to halfway - 150 - and you get 0.166666666666667*150 = 25. Perfect. Except, not. Because Bennu does not support floating point maths/decimal points. So what you actually get from 0.166666666666667*300, or 0.166666666666667*150, or 0.166666666666667*anything, is a big fat 0. Doh!

Is there a way around this that anyone can think of? It's a real head scratcher.


Again, thank you so much for everyone's help so far! <3


- Breadcaster

Desconectado butcher

  • Jr. Member
  • **
  • Mensajes: 78
  • Karma: 4
  • No te olvides de poner el where en el delete from
    • Ver Perfil
    • Blog Starblank
Re:Decimal dilemma!
« Respuesta #1 en: Julio 04, 2016, 01:36:27 am »
As far as i know, bennugd supports float vars

Private
  float my_float=3.14159265;

The target var after operation must be float as well

Private
  float my_float=3.14159265;
Private
  float result=0;
result=my_float/3.2;
//Result: result variable returns 0.981719
 


If you want to cast from float to int, simply declare a int var then assign the float to it

Private
  float my_float=3.14159265;
Private
  int converted_int=0;
converted_int=my_float;
//Result: converted_it returns 3
« última modificación: Julio 04, 2016, 01:51:24 am por butcher »

Desconectado Breadcaster

  • Newbie
  • *
  • Mensajes: 38
  • Karma: 2
    • Ver Perfil
Re:Decimal dilemma!
« Respuesta #2 en: Julio 04, 2016, 03:20:20 am »
Hells horses! I had no idea Floats were a possibility in Bennu, wow. Thank you Butcher, I'll give it a shot! Brilliant to have your help on this, very grateful :D I'll give it a shot and see how it works out.

Desconectado SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12457
  • Karma: 367
  • Sexo: Masculino
    • Ver Perfil
Re:Decimal dilemma!
« Respuesta #3 en: Julio 04, 2016, 04:10:29 am »
thanks butcher! I was without understand the Breadcaster's post, because Bennu support float, then I was thinking that my english was very wrong.

sorry for no reply, but I was don't understand that Breadcaster said.
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Desconectado Breadcaster

  • Newbie
  • *
  • Mensajes: 38
  • Karma: 2
    • Ver Perfil
Re:Decimal dilemma!
« Respuesta #4 en: Julio 05, 2016, 08:55:36 am »
@SplinterGU I didn't realise BennuGD supported floats, haha. Though when I try to run the code that I posted with floats instead of ints, it's the same result -

private
float myfloat=0;

*blablabla*

myfloat=50/300;

myfloat comes out as 0, when writing it to screen :/ I don't understand why.

However, I've found a solution to this issue now thanks to Mike Green on the Div-Arena forums who suggested just multiplying everything up and then dividing it down when calculating the final result, which has worked brilliantly :) But I've given you some karma all the same, Butcher, for letting me know about floats!! :D

Conectado panreyes

  • Administrator
  • Hero Member
  • *****
  • Mensajes: 2007
  • Karma: 64
  • Sexo: Masculino
    • Ver Perfil
    • panreyes.com
Re:Decimal dilemma!
« Respuesta #5 en: Julio 05, 2016, 11:03:19 am »
@Breadcaster: Try this: myfloat=(float) 50/300;

Desconectado butcher

  • Jr. Member
  • **
  • Mensajes: 78
  • Karma: 4
  • No te olvides de poner el where en el delete from
    • Ver Perfil
    • Blog Starblank
Re:Decimal dilemma!
« Respuesta #6 en: Julio 05, 2016, 11:24:55 am »
@SplinterGU I didn't realise BennuGD supported floats, haha. Though when I try to run the code that I posted with floats instead of ints, it's the same result -

private
float myfloat=0;

*blablabla*

myfloat=50/300;

myfloat comes out as 0, when writing it to screen :/ I don't understand why.

However, I've found a solution to this issue now thanks to Mike Green on the Div-Arena forums who suggested just multiplying everything up and then dividing it down when calculating the final result, which has worked brilliantly :) But I've given you some karma all the same, Butcher, for letting me know about floats!! :D

The float operation you showed should work. Try casting (float) like suggested panreyes, but theres something weird in your code. Did you added mod_math library, just in case? I can post a tiny .prg with float operation for using it as example, if you need it.

But the div forum gave you a good advice. I think that using float operations for painting pixels is not nice. All the operations you said can be done with integers. And will be faster as float operations use more cpu especially in small devices

Desconectado SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12457
  • Karma: 367
  • Sexo: Masculino
    • Ver Perfil
Re:Decimal dilemma!
« Respuesta #7 en: Julio 05, 2016, 04:36:24 pm »
do this

50.0/300.0
Download Lastest BennuGD Release: http://www.bennugd.org/node/2