11import {
22 Microbot , EnergyParticle , HazardZone , SimulationConfig ,
3- BehaviorState , SimulationStats , Vector2D , SpeedField , GravityWell , FluidZone , CatalystZone ,
3+ SimulationStats , SpeedField , GravityWell , FluidZone , CatalystZone ,
44 SectorBiome , EnvironmentalDisaster , PortalNode , ParasiticSpore , MeteorStrike , VoidRift , Season , ResourceType
55} from './types' ;
66import { SpatialGrid } from './SpatialGrid' ;
77import { Quadtree } from './Quadtree' ;
88import { PheromoneGrid } from './pheromones/PheromoneGrid' ;
99import { SpeciationManager } from './genetics/SpeciationManager' ;
10- import { NASBrainManager , NASBrainGenome } from './genetics/NASBrainManager' ;
1110import { SpatialHashGrid } from './SpatialHashGrid' ;
1211import { spatialAudio } from '../audio/SpatialAudioSynth' ;
1312import { calculateSteering } from './steering' ;
@@ -30,8 +29,8 @@ export class MicrobotEngine {
3029 public fluidZones : FluidZone [ ] = [ ] ;
3130 public gravityWells : GravityWell [ ] = [ ] ;
3231 public catalystZones : CatalystZone [ ] = [ ] ;
33- public chemicalGrid : ChemicalGrid ;
34- public pheromoneGrid : PheromoneGrid ;
32+ public chemicalGrid ! : ChemicalGrid ;
33+ public pheromoneGrid ! : PheromoneGrid ;
3534 public meteors : MeteorStrike [ ] = [ ] ;
3635 public voidRifts : VoidRift [ ] = [ ] ;
3736 public portals : PortalNode [ ] = [ ] ;
@@ -298,7 +297,7 @@ export class MicrobotEngine {
298297 this . spatialGrid = new SpatialGrid ( this . width , this . height , 60 ) ;
299298 this . chemicalGrid = new ChemicalGrid ( this . width , this . height , 20 ) ;
300299 this . pheromoneGrid = new PheromoneGrid ( this . width , this . height , 10 ) ;
301- this . spatialHash = new SpatialHashGrid ( this . width , this . height , 80 ) ;
300+ this . spatialHash = new SpatialHashGrid ( 80 ) ;
302301 this . generateBiomes ( ) ;
303302 }
304303
@@ -332,15 +331,15 @@ export class MicrobotEngine {
332331 parentA . offspringCount ++ ;
333332
334333 // Inherit species ID
335- const botSpeciesId = parentA . speciesId || SpeciationManager . getInstance ( ) . getSpeciesId ( ) ;
336- let isHybrid = false ;
337- let fertility = parentA . fertility ?? 1.0 ;
334+
335+
336+
338337
339338 if ( parentB ) {
340339 parentB . offspringCount ++ ;
341- const compat = SpeciationManager . getInstance ( ) . evaluateMatingCompatibility ( parentA , parentB ) ;
342- isHybrid = compat . isHybrid ;
343- fertility = compat . fertilityFactor ;
340+
341+
342+
344343 }
345344
346345 if ( generation > this . generationCount ) {
@@ -351,20 +350,20 @@ export class MicrobotEngine {
351350 const mut = this . config . mutationRate * seasonMutMult ;
352351
353352 // Fix(genetics): strict boundaries and NaN prevention for chromosome values
354- speed = clamp ( sanitizeVector ( parent . speed + ( Math . random ( ) - 0.5 ) * mut * 1.5 ) , 1.0 , 5.0 ) ;
355- turnRate = clamp ( sanitizeVector ( parent . turnRate + ( Math . random ( ) - 0.5 ) * mut * 0.1 ) , 0.04 , 0.3 ) ;
356- visionRadius = clamp ( sanitizeVector ( parent . visionRadius + ( Math . random ( ) - 0.5 ) * mut * 50 ) , 60 , 260 ) ;
357- energyEfficiency = clamp ( sanitizeVector ( parent . energyEfficiency + ( Math . random ( ) - 0.5 ) * mut * 0.4 ) , 0.6 , 2.5 ) ;
358- hue = sanitizeVector ( ( parent . hue + ( Math . random ( ) - 0.5 ) * mut * 80 + 360 ) % 360 ) ;
353+ speed = clamp ( sanitizeVector ( parentA . speed + ( Math . random ( ) - 0.5 ) * mut * 1.5 ) , 1.0 , 5.0 ) ;
354+ turnRate = clamp ( sanitizeVector ( parentA . turnRate + ( Math . random ( ) - 0.5 ) * mut * 0.1 ) , 0.04 , 0.3 ) ;
355+ visionRadius = clamp ( sanitizeVector ( parentA . visionRadius + ( Math . random ( ) - 0.5 ) * mut * 50 ) , 60 , 260 ) ;
356+ energyEfficiency = clamp ( sanitizeVector ( parentA . energyEfficiency + ( Math . random ( ) - 0.5 ) * mut * 0.4 ) , 0.6 , 2.5 ) ;
357+ hue = sanitizeVector ( ( parentA . hue + ( Math . random ( ) - 0.5 ) * mut * 80 + 360 ) % 360 ) ;
359358
360- armorGene = clamp ( ( parent . armorGene || 0 ) + ( Math . random ( ) - 0.5 ) * mut * 0.2 , 0 , 0.8 ) ;
361- inkGlandGene = ( parent . inkGlandGene || 0 ) > 0.5 ?
359+ armorGene = clamp ( ( parentA . armorGene || 0 ) + ( Math . random ( ) - 0.5 ) * mut * 0.2 , 0 , 0.8 ) ;
360+ inkGlandGene = ( parentA . inkGlandGene || 0 ) > 0.5 ?
362361 ( Math . random ( ) < mut * 2 ? 0 : 1 ) :
363362 ( Math . random ( ) < mut * 2 ? 1 : 0 ) ;
364- panicGene = clamp ( ( parent . panicGene || 1.0 ) + ( Math . random ( ) - 0.5 ) * mut * 0.5 , 1.0 , 2.5 ) ;
363+ panicGene = clamp ( ( parentA . panicGene || 1.0 ) + ( Math . random ( ) - 0.5 ) * mut * 0.5 , 1.0 , 2.5 ) ;
365364
366- if ( parent . epigenome ) {
367- inheritedEpigenome = parent . epigenome . map ( marker => {
365+ if ( parentA . epigenome ) {
366+ inheritedEpigenome = parentA . epigenome . map ( ( marker : EpigeneticMarker ) => {
368367 const passedOn = Math . random ( ) < marker . heritability ;
369368 return {
370369 ...marker ,
@@ -379,6 +378,9 @@ export class MicrobotEngine {
379378
380379 const bot : Microbot = {
381380 id,
381+ speciesId : parentA ?. speciesId || SpeciationManager . getInstance ( ) . getSpeciesId ( ) ,
382+ isHybrid : parentA && parentB ? SpeciationManager . getInstance ( ) . evaluateMatingCompatibility ( parentA , parentB ) . isHybrid : false ,
383+ fertility : parentA && parentB ? SpeciationManager . getInstance ( ) . evaluateMatingCompatibility ( parentA , parentB ) . fertilityFactor : ( parentA ?. fertility ?? 1.0 ) ,
382384 x,
383385 y,
384386 vx : Math . cos ( Math . random ( ) * Math . PI * 2 ) * speed ,
@@ -1391,9 +1393,20 @@ export class MicrobotEngine {
13911393
13921394 // Try to find a mate nearby
13931395 let mateFound = false ;
1394- const nearbyBots = this . spatialGrid . queryRadius ( bot . x , bot . y , bot . visionRadius ) ;
1396+ const nearbyBots : Microbot [ ] = [ ] ;
1397+ const radSq = bot . visionRadius * bot . visionRadius ;
1398+ for ( const otherBot of this . microbots ) {
1399+ if ( otherBot . id !== bot . id ) {
1400+ const dx = otherBot . x - bot . x ;
1401+ const dy = otherBot . y - bot . y ;
1402+ if ( dx * dx + dy * dy <= radSq ) {
1403+ nearbyBots . push ( otherBot ) ;
1404+ }
1405+ }
1406+ }
1407+
13951408 for ( const otherBot of nearbyBots ) {
1396- if ( otherBot . id !== bot . id && otherBot . battery >= otherBot . maxBattery * 0.8 ) {
1409+ if ( otherBot . battery >= otherBot . maxBattery * 0.8 ) {
13971410 const compat = SpeciationManager . getInstance ( ) . evaluateMatingCompatibility ( bot , otherBot ) ;
13981411 if ( compat . compatible ) {
13991412 bot . battery -= 45 ;
0 commit comments