Alien Boss random doesn't work sometimes when I log in

Hi guys, here’s a boss Kelathos and I made,

It has serverside health and revive delay, he spawns mostly bombs, sometimes explosions, sometimes baddies.

It mostly works real fine, but sometimes when I log in this level, the boss spawn all of its bombs at x,y, ignoring the random.

Any idea why this happens?

// NPC made by Kelathos
// Fixed by Fredi
// Alien Bomber with server side health
// V 1.00

// Spawn
if (created) {
  // Variables
  this.bx = x;
  this.by = y;
  this.hpmax = 1000;
  this.hp = this.hpmax;
  this.sleep = 1;
  this.bombRange = 12; // The maximum range for planting bombs
  this.respawnTime = 120; // Divide by 2 to count as seconds

  // Static
  hide;
  setstring server.AlienBossRespawn,#v(strtofloat(#s(this.respawnTime)));

  // Check for Health on Spawn
  this.hp = strtofloat(#s(server.AlienBossHealth));
  // Activate if it has Health
  if (this.hp > 0) {
    this.sleep = 0;
    show;
  }
  else
  {
   this.sleep = 1;
   setstring server.AlienBossRespawn,#v(strtofloat(#s(this.respawnTime)));
  }
}

// Keep the NPC active, always
if (playerenters) {
  timeout = 0.5;
     if (strtofloat(#s(server.AlienBossHealth)) <=0 && strtofloat(#s(server.AlienBossRespawn))<=0){
      setstring server.AlienBossHealth,#v(this.hpmax);
      this.hp = this.hpmax; //strtofloat(#s(server.AlienBossHealth));
      this.sleep = 0;
      this.bombRange = 12;
      show;
     }
}

// Main Action Loop
if (timeout) {

  // Sleeping
  if (this.sleep == 1) {

    // Check HP
    this.hp = strtofloat(#s(server.AlienBossHealth));

    // Activate if has HP
    if (strtofloat(#s(server.AlienBossHealth)) > 0) {
      this.sleep = 0;
      setstring server.AlienBossRespawn,#v(strtofloat(#s(this.respawnTime)));
      show;
    }

    //Respawn if killed
    if (strtofloat(#s(server.AlienBossHealth)) <=0){

     //Respawn counter decrements
     if (strtofloat(#s(server.AlienBossRespawn))>0){
      this.sleep = 1;
      setstring server.AlienBossRespawn,#v(strtofloat(#s(server.AlienBossRespawn))-1);
     }

     // Boss Respawns
     if (strtofloat(#s(server.AlienBossRespawn))<=0){
      setstring server.AlienBossHealth,#v(this.hpmax);
      this.hp = this.hpmax; //strtofloat(#s(server.AlienBossHealth));
      //setstring server.AlienBossRespawn,#v(strtofloat(#s(this.respawnTime)));
      this.sleep = 0;
      show;
     }
    }// End of Respawn
  }
  // While Awake (sleep == 0)
  else {

    // If Defeated, put to sleep
    if (strtofloat(#s(server.AlienBossHealth)) <= 0) {
      this.hp = 0;
      this.sleep = 1;
      setstring server.AlienBossHealth,0;
      setstring server.AlienBossRespawn,#v(this.respawnTime);
      play ff7victory2.mid;
      hide;
    }
    // Or Fight!
    else {
      show;
      // Special attack each time boss loses 20 hp
      if (strtofloat(#s(server.AlienBossHealth))%20=0){
       this.bx = random(x-this.bombRange,x+this.bombRange);
       this.by = random(y-this.bombRange,y+this.bombRange);
       putexplosion2 3, 3, this.bx,this.by;
      }
      // Each 27 hp
      if(strtofloat(#s(server.AlienBossHealth))%27=0){
       shootfireball random(0,4);

       }
      if(strtofloat(#s(server.AlienBossHealth))%47=0){
       putnewcomp lizardon,x+2,y-4,baddyninja.png,8;
       putnewcomp lizardon,x-2,y-4,baddyninja.png,8;
       }

       //this.bx = random(x-this.bombRange,x+this.bombRange);
       //this.by = random(y-this.bombRange,y+this.bombRange);
       putbomb 1, random(x-this.bombRange,x+this.bombRange),random(y-this.bombRange,y+this.bombRange);

    }
  }
  timeout = 0.5;
}

// How to take damage!
if (washit || wasshot && strtofloat(#s(server.AlienBossHealth))>0) {
  setstring server.AlienBossHealth,#v(strtofloat(#s(server.AlienBossHealth))-playerswordpower/2);
  this.hp = strtofloat(#s(server.AlienBossHealth));
  hearts = strtofloat(#s(server.AlienBossHealth));
  putleaps 2,x,y;
}

// Black rocks and vase hurt 1 hp, other throwables 0.5
if (waspelt && strtofloat(#s(server.AlienBossHealth))>0){
 if (peltwithblackstone || peltwithvase){
  setstring server.AlienBossHealth,#v(strtofloat(#s(server.AlienBossHealth))-1);
  }
  else {
  setstring server.AlienBossHealth,#v(strtofloat(#s(server.AlienBossHealth))-0.5);
  this.hp = strtofloat(#s(server.AlienBossHealth));
  putleaps 2,x,y;
  }
  hearts = strtofloat(#s(server.AlienBossHealth));
}

(I can’t figure out the code tag, please help me on that)

// Spawn
if (created) {
  // Variables
  this.bx = x;
  this.by = y;
  this.hpmax = 1000;
  this.hp = this.hpmax;
  this.sleep = 1;
  this.bombRange = 12; // The maximum range for planting bombs
  this.respawnTime = 120; // Divide by 2 to count as seconds

  // Static
  hide;
  setstring server.AlienBossRespawn,#v(strtofloat(#s(this.respawnTime)));

  // Check for Health on Spawn
  this.hp = strtofloat(#s(server.AlienBossHealth));
  // Activate if it has Health
  if (this.hp > 0) {
    this.sleep = 0;
    show;
  }
  else
  {
   this.sleep = 1;
   setstring server.AlienBossRespawn,#v(strtofloat(#s(this.respawnTime)));
  }
}

// Keep the NPC active, always
if (playerenters) {
  timeout = 0.5;
     if (strtofloat(#s(server.AlienBossHealth)) <=0 && strtofloat(#s(server.AlienBossRespawn))<=0){
      setstring server.AlienBossHealth,#v(this.hpmax);
      this.hp = this.hpmax; //strtofloat(#s(server.AlienBossHealth));
      this.sleep = 0;
      this.bombRange = 12;
      show;
     }
}

I don’t know if it’s the problem but I feel it may well be!
The problem with using if (created){ is it’ll initialise those variables for current player when the npc is first loaded by the server (NOT BY THE PLAYER)
So, the problems that come with this are you’re initialising clientside variables with the intent of having them serverside, this would work if we had a npcserver, but we don’t. This wouldn’t be a problem I feel if you were initialising such things as save vars and/or attributes. For example:

if (created){
  save[0] = 321;
  setcharprop #P1,hat2.png;
}

So, the half assed solution is just to stick all your initialisation code in your playerenters block. If you did want a multiplayer boss, you’ll need such things as save vars, and attributes. I’m pretty sure things like health,x,y,etc… are serverside as well but you can play around with that. Either way you can’t have things like this.hp and expect it to be serverside, it isn’t going to happen.

Thanks!

But for the random thing, I redefined this.bombRange in the playerenters statement, so I don’t understand why it’s still happening.

You should be able to see from this:

if (strtofloat(#s(server.AlienBossHealth)) <=0 && strtofloat(#s(server.AlienBossRespawn))<=0){

That this.bombRange is only intialised when the boss has no health, health is defined on the serverside and will persist. Hence you’ll never re-initialise the variable when the player logs back in. The solution to this problem should be rather obvious.

I just changed that, I’ll tell you if the bug comes again, but I hope not.