<commit_before>angular.module('survivalApp')
  .service('CellManagerService', function (ThreeJSRendererService, FoodManagerService, DebugLessService, TileManagerService, $timeout) {//$interval, 
    'use strict';
    var cellManager = this;
    cellManager.cells = [];
    
    
    cellManager.HealthTextureAnimator = function (texture, cell) 
    {	
    	// note: texture passed by reference, will be updated by the update function.
    	texture.wrapS = texture.wrapT = THREE.RepeatWrapping; 
    	texture.repeat.set( 1 , 1/ 15 );

    	this.update = function( milliSec )
    	{
        texture.offset.y = Math.floor(cell.health/100 * 15 ) / 15;
    	};
    }		
    
    
    cellManager.healthSpriteTexture = new THREE.ImageUtils.loadTexture( 'textures/healthSprite.jpg' );
    
    cellManager.cellListener = function (e) {
      var data = {};
      if (e.data !== undefined) {
        data = e.data;
      }
      if (data.cmd !== undefined) {
        switch (data.cmd) {
        case 'echo':
          console.log('echo: ', data.msg);
          break;
        case 'invalidPlacement':
          //positions!
          console.log('invalidPlacement');
      
          break;
        case 'move':
          //positions!
          cellManager.cell(data.cellId).move(data.position, data);
          break;
        default:
          //
          console.log('cellListener', e);
          break;
        }
      }
    };
    
    
    cellManager.createCell = function (options) {

      var settings = options || {};
      var cellId = settings.id || cellManager.cells.length;
      
      var radius = 0.3;
      
      var geometry = new THREE.CubeGeometry(0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10);
      var health = options.health || 100;
      var material = new THREE.MeshPhongMaterial({
        ambient: 0x030303,
        color: 0xdddddd,
        specular: 0x009900,
        shininess: 30
      });
      
      cellManager.cells[cellId] = {
        'id': cellId,
        'health': health,
        'radius':radius,
        'invalidPlacement': settings.invalidPlacement || function (event) {
          // console.log('invalidCell placement: ', this, event);
          this.worker.postMessage({
            cmd:event.cmd,
            message:event.message,
            position:event.position
          });
        }
      };

      var newCell = cellManager.cells[cellId];
      cellManager.cells[cellId].workerBlobText = settings.workerBlobText || '';
      
      if (cellManager.cells[cellId].worker !== undefined) {
        cellManager.cells[cellId].worker.terminate();
      }
      
      cellManager.cells[cellId].workerBlob = new Blob([cellManager.cells[cellId].workerBlobText])

      var blobURL = window.URL.createObjectURL( cellManager.cells[cellId].workerBlob );
      
      cellManager.cells[cellId].worker = new Worker(blobURL);
      
      cellManager.cells[cellId].worker.postMessage({'cmd':'init','cellId':cellId});

      cellManager.cells[cellId].cellListener = cellManager.cellListener; 
      
      cellManager.cells[cellId].worker.addEventListener('message', cellManager.cells[cellId].cellListener);
      
      cellManager.cells[cellId].mesh = new THREE.Mesh(geometry, material); 

    	cellManager.healthSpriteTexture.wrapS = cellManager.healthSpriteTexture.wrapT = THREE.RepeatWrapping; 
    	cellManager.healthSpriteTexture.repeat.set( 10, 10 );
      // HealthTextureAnimator(texture, tilesHoriz, tilesVert, numTiles, tileDispDuration) 
      cellManager.cells[cellId].textureAnimator = new cellManager.HealthTextureAnimator( cellManager.healthSpriteTexture, cellManager.cells[cellId]);
      
      ThreeJSRendererService.onRenderFcts.push(cellManager.cells[cellId].textureAnimator.update);
      
    	var floorMaterial = new THREE.MeshBasicMaterial( { map: cellManager.healthSpriteTexture, side: THREE.DoubleSide } );



      cellManager.cells[cellId].hud = {
        geometry: new THREE.PlaneGeometry( radius, radius / 3.33 ),
        material: new THREE.MeshBasicMaterial({
          map: cellManager.healthSpriteTexture, side: THREE.DoubleSide
        })
      }

      cellManager.cells[cellId].hud.mesh = new THREE.Mesh(cellManager.cells[cellId].hud.geometry, cellManager.cells[cellId].hud.material); 
      
      ThreeJSRendererService.scene.add(cellManager.cells[cellId].hud.mesh);
      ThreeJSRendererService.scene.add(cellManager.cells[cellId].mesh);

      cellManager.cells[cellId].mesh.position = new THREE.Vector3(-0.57, 0.52, 0.7);
      
      cellManager.cells[cellId].hud.mesh.position = new THREE.Vector3(-0.57, 0.52, 0.7);
      
      cellManager.cells[cellId].raycaster = new THREE.Raycaster();
      $timeout(function () {
        cellManager.moveCell(cellId, new THREE.Vector3(-0.57, 0.52, 0.7), {'position':[-0.57, 0.52, 0.7]});
      },1000);
      return cellManager.cells[cellId];
    };
    cellManager.moveCell = function (cellId, position, data) {
      // console.log('cellId,position,data', cellId,position,data);
      /**
       * @ngdoc moveCell
       * @name survival.CellManager:moveCell
       * 
       * @description #Move cell
        Moves the cell, using the cellId or chained from cell(cellId) and a position.
      If the move is invalid it calls the cells.invalidPlacement function, which can be assigned to using the constructor or after the fact
       */
      var cell = cellManager.cells[cellId];
      var newPos = new THREE.Vector3(position[0] || 0, position[1] || 0, position[2] || 0);
      var cellPos = cell.mesh.position;
      
      //we start by making a copy of the cell's position
      var orignialCellPos = new THREE.Vector3(0,0,0).copy(cellPos);

      if (cell.health > 0) {
         cell.health -= 1;
       }else{
         cellPos.copy(orignialCellPos);

         cell.lastMove = 'invalid';
         cell.invalidPlacement({'message':'DeadCell', 'cell':cell});
         return;
       }

      if (TileManagerService.land === undefined) {
        console.log('landNotDefined', landNotDefined);
        cellPos.x = data.position[0]||data.position.x||0;
        cellPos.y = data.position[1]||data.position.y||0;
        cellPos.z = data.position[2]||data.position.z||0;
      }else{
        //wheres the nearest land?
        //Temp move the cell to the new position
        cellPos.x = data.position[0]||data.position.x||0;
        cellPos.y = data.position[1]||data.position.y||0;
        cellPos.z = data.position[2]||data.position.z||0;
        //get the nearest land and water for the new position
        <commit_msg>cleanup<commit_after>angular.module('survivalApp')
  .service('CellManagerService', function (ThreeJSRendererService, FoodManagerService, DebugLessService, TileManagerService, $timeout) {//$interval, 
    'use strict';
    var cellManager = this;
    cellManager.cells = [];
    
    
    cellManager.HealthTextureAnimator = function (texture, cell) 
    {	
    	// note: texture passed by reference, will be updated by the update function.
    	texture.wrapS = texture.wrapT = THREE.RepeatWrapping; 
    	texture.repeat.set( 1 , 1/ 15 );

    	this.update = function( milliSec )
    	{
        texture.offset.y = Math.floor(cell.health/100 * 15 ) / 15;
    	};
    }		
    
    
    cellManager.healthSpriteTexture = new THREE.ImageUtils.loadTexture( 'textures/healthSprite.jpg' );
    
    cellManager.cellListener = function (e) {
      var data = {};
      if (e.data !== undefined) {
        data = e.data;
      }
      if (data.cmd !== undefined) {
        switch (data.cmd) {
        case 'echo':
          console.log('echo: ', data.msg);
          break;
        case 'invalidPlacement':
          //positions!
          console.log('invalidPlacement');
      
          break;
        case 'move':
          //positions!
          cellManager.cell(data.cellId).move(data.position, data);
          break;
        default:
          //
          console.log('cellListener', e);
          break;
        }
      }
    };
    
    
    cellManager.createCell = function (options) {

      var settings = options || {};
      var cellId = settings.id || cellManager.cells.length;
      
      var radius = 0.3;
      
      var geometry = new THREE.CubeGeometry(0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10);
      var health = options.health || 100;
      var material = new THREE.MeshPhongMaterial({
        ambient: 0x030303,
        color: 0xdddddd,
        specular: 0x009900,
        shininess: 30
      });
      
      cellManager.cells[cellId] = {
        'id': cellId,
        'health': health,
        'radius':radius,
        'invalidPlacement': settings.invalidPlacement || function (event) {
          // console.log('invalidCell placement: ', this, event);
          this.worker.postMessage({
            cmd:event.cmd,
            message:event.message,
            position:event.position
          });
        }
      };

      var newCell = cellManager.cells[cellId];
      cellManager.cells[cellId].workerBlobText = settings.workerBlobText || '';
      
      if (cellManager.cells[cellId].worker !== undefined) {
        cellManager.cells[cellId].worker.terminate();
      }
      
      cellManager.cells[cellId].workerBlob = new Blob([cellManager.cells[cellId].workerBlobText])

      var blobURL = window.URL.createObjectURL( cellManager.cells[cellId].workerBlob );
      
      cellManager.cells[cellId].worker = new Worker(blobURL);
      
      cellManager.cells[cellId].worker.postMessage({'cmd':'init','cellId':cellId});

      cellManager.cells[cellId].cellListener = cellManager.cellListener; 
      
      cellManager.cells[cellId].worker.addEventListener('message', cellManager.cells[cellId].cellListener);
      
      cellManager.cells[cellId].mesh = new THREE.Mesh(geometry, material); 

    	cellManager.healthSpriteTexture.wrapS = cellManager.healthSpriteTexture.wrapT = THREE.RepeatWrapping; 
    	cellManager.healthSpriteTexture.repeat.set( 10, 10 );
      // HealthTextureAnimator(texture, tilesHoriz, tilesVert, numTiles, tileDispDuration) 
      cellManager.cells[cellId].textureAnimator = new cellManager.HealthTextureAnimator( cellManager.healthSpriteTexture, cellManager.cells[cellId]);
      
      ThreeJSRendererService.onRenderFcts.push(cellManager.cells[cellId].textureAnimator.update);
      
    	var floorMaterial = new THREE.MeshBasicMaterial( { map: cellManager.healthSpriteTexture, side: THREE.DoubleSide } );



      cellManager.cells[cellId].hud = {
        geometry: new THREE.PlaneGeometry( radius, radius / 3.33 ),
        material: new THREE.MeshBasicMaterial({
          map: cellManager.healthSpriteTexture, side: THREE.DoubleSide
        })
      }

      cellManager.cells[cellId].hud.mesh = new THREE.Mesh(cellManager.cells[cellId].hud.geometry, cellManager.cells[cellId].hud.material); 
      
      ThreeJSRendererService.scene.add(cellManager.cells[cellId].hud.mesh);
      ThreeJSRendererService.scene.add(cellManager.cells[cellId].mesh);

      cellManager.cells[cellId].mesh.position = new THREE.Vector3(-0.57, 0.52, 0.7);
      
      cellManager.cells[cellId].hud.mesh.position = new THREE.Vector3(-0.57, 0.52, 0.7);
      
      cellManager.cells[cellId].raycaster = new THREE.Raycaster();
      $timeout(function () {
        cellManager.moveCell(cellId, new THREE.Vector3(-0.57, 0.52, 0.7), {'position':[-0.57, 0.52, 0.7]});
      },1000);
      return cellManager.cells[cellId];
    };
    cellManager.moveCell = function (cellId, position, data) {
      // console.log('cellId,position,data', cellId,position,data);
      /**
       * @ngdoc moveCell
       * @name survival.CellManager:moveCell
       * 
       * @description #Move cell
        Moves the cell, using the cellId or chained from cell(cellId) and a position.
      If the move is invalid it calls the cells.invalidPlacement function, which can be assigned to using the constructor or after the fact
       */
      var cell = cellManager.cells[cellId];
      var newPos = new THREE.Vector3(position[0] || 0, position[1] || 0, position[2] || 0);
      var cellPos = cell.mesh.position;
      
      //we start by making a copy of the cell's position
      var orignialCellPos = new THREE.Vector3(0,0,0).copy(cellPos);

      if (cell.health > 0) {
         cell.health -= 1;
       }else{
         cellPos.copy(orignialCellPos);

         cell.lastMove = 'invalid';
         cell.invalidPlacement({'message':'DeadCell', 'cell':cell});
         return;
       }

      if (TileManagerService.land === undefined) {
        console.log('landNotDefined');
        cellPos.x = data.position[0]||data.position.x||0;
        cellPos.y = data.position[1]||data.position.y||0;
        cellPos.z = data.position[2]||data.position.z||0;
      }else{
        //wheres the nearest land?
        //Temp move the cell to the new position
        cellPos.x = data.position[0]||data.position.x||0;
        cellPos.y = data.position[1]||data.position.y||0;
        cellPos.z = data.position[2]||data.position.z||0;
        //get the nearest land and water for the new position
        
{'fraction_non_alphanumeric': 0.1268348623853211, 'fraction_numerical': 0.017737003058103974, 'mean_word_length': 2.529681597409606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6928994', 'n_tokens_mistral': 4150, 'n_tokens_neox': 3887, 'n_words': 741}