if(!worm_init){
    
    WORM_SPACING = 0.75;
    WORM_HEAD_SPACING = 1.5;
    WORM_IS_STUNNABLE = true; // can be stunned with boomerang
    WORM_BASE_HEALTH = 10; // == 2 hits with wooden sword
    WORM_HEAD_HEALTH = 20; // == 4 hits derp
    WORM_DAMAGE = 2; // == 3/4 heart
    WORM_MAX_SPEED = 0.30;
    WORM_MIN_SPEED = 0.15;
    WORM_MAX_LENGTH = 8; // including head

    worm = spawnedEntities.slice(0);
   
    var l = worm.length;
    for(var i = 0; i< l-1 ; i++) {
        worm[i].setMaxHealth(100);
        worm[i].setHealth(100);
        worm[i].setTexture("newmobs/worm_tail.png");
        worm[i].setAttackStrength(WORM_DAMAGE);
    }
    worm[l-1].setMaxHealth(WORM_BASE_HEALTH);
    worm[l-1].setHealth(WORM_BASE_HEALTH);
    worm[l-1].setTexture("newmobs/worm_tail_end.png");
    worm[l-1].setAttackStrength(WORM_DAMAGE);

    worm_dir = new Vec2(1.0,0.0);
    worm_rand = 0;
    worm_turn = 1;
    worm_stunned = 0;
    worm_tail = new Array();
    
    worm_finished = false;
    worm_init = true;
    
} else if (worm_finished && worm.length > 1){ // has tail

    var pos = worm[0].getPosition();
    var speed =(WORM_MAX_SPEED - WORM_MIN_SPEED) * (WORM_MAX_LENGTH - worm.length)/(WORM_MAX_LENGTH - 1) + WORM_MIN_SPEED;
    var space = Math.round(WORM_SPACING/speed);
    var head_space = Math.round(WORM_HEAD_SPACING/speed);

    if(!WORM_IS_STUNNABLE || worm[0].getStunned() <= 0){ // not stunned
    
        worm[0].setStunned(0);
        worm_tail.unshift(pos);
    
        if(worm_tail.length > (worm.length-1) * space + head_space) { // tail array is too long
            worm_tail.length = (worm.length-1) * space + head_space; // no memory leak
        }
        
        if (worm_stunned <= 15) { 
            
            if (worm_rand <= 0) { // time to turn in random direction
                worm_dir.setTheta(worm_dir.getTheta() + Math.random()*180 - 90);
                worm_rand = Math.floor(Math.random()*20) + 30;
                worm_turn = (Math.random()*2) >= 1 ? 1 : -1;
            } else { // normal behaviour
                worm_rand--;
                worm_dir.setTheta(worm_dir.getTheta() + speed * 20 * worm_turn); 
            }

            worm[0].setVelocity(worm_dir.x * speed,worm[0].getVelocity().y, worm_dir.z * speed); 
    
        } else {
            worm_tail[0].x = worm_tail[1].x;
            worm_tail[0].z = worm_tail[1].z;
            worm[0].setPosition(pos.x, pos.y, pos.z);
            worm[0].setVelocity(0,0.5,0);
        }
    
        if(worm_stunned > 0){
            worm_stunned--;
            if(worm_stunned == 0) {
                worm[worm.length - 1].setTexture("newmobs/worm_tail_end.png");
            }
        }
    }
    
    worm[0].setHealth(worm[0].maxHealth);
    worm[0].setHurtTime(0);

    if(worm[worm.length-1].getHealth()<=0) { // a piece of tail died
        worm[worm.length-1].setAttackStrength(0);
        worm[worm.length-1].setStunned(0);
        
        worm_stunned = 25;
        worm[0].setStunned(0);
        sound.playSound3D("sound.rawr",pos.x,pos.y,pos.z);
        worm.pop(); // ATTENTION worm.length changes!
        
        if(worm.length > 1) {
            worm[worm.length -1].setMaxHealth(WORM_BASE_HEALTH);
            worm[worm.length -1].setHealth(WORM_BASE_HEALTH);
            // texture is changed when stunned == 1
        } else {
            worm[0].setMaxHealth(WORM_HEAD_HEALTH);
            worm[0].setHealth(WORM_HEAD_HEALTH);
        }
    } 
    
    for (var i = 1; i < worm.length; i++){
        worm[i].setStunned(0);
        
        if(worm_tail.length < (i-1) * space + head_space){ // tail array not filled yet
            pos = worm_tail[worm_tail.length - 1];
        } else {
            pos = worm_tail[(i-1) * space + head_space - 1];
        }
        
        worm[i].setPosition(pos.x, pos.y, pos.z);
        worm[i].setVelocity(0,0,0);
        
        if(i != worm.length -1 || worm_stunned > 0) {
            worm[i].setHealth(worm[i].maxHealth);
            worm[i].setHurtTime(0);
        } 
    }

} else if(worm_finished){ // only head is left

    if (worm[0].health <= 0){
        worm[0].setAttackStrength(0);
        if(worm_stunned >= 0){
            var pos = worm[0].getPosition();
            sound.playSound3D("sound.die",pos.x,pos.y,pos.z);
            worm_stunned = -1;
        } 
    }
}