MediaWiki:Gadget-spawncalc.js: Difference between revisions

From Pixelrus
Jump to navigation Jump to search
m (Update MediaWiki:Gadget-spawncalc.js)
m (Update MediaWiki:Gadget-spawncalc.js)
Line 3: Line 3:
  *
  *
  * Mounts into <div id="spawn-calculator" data-species="..."> on a species page.
  * Mounts into <div id="spawn-calculator" data-species="..."> on a species page.
* If data-species is empty (the standalone Spawn Calculator page), the gadget
* shows a Pokemon picker so any species can be chosen.
*
  * Features:
  * Features:
  *  1. Pick a situation (biome, time, weather, location type, Y-level) and see
  *  1. Pick a situation (biome, time, weather, location type, Y-level) and see
Line 64: Line 67:
function inList( list, v ) { return list && list.indexOf( v ) !== -1; }
function inList( list, v ) { return list && list.indexOf( v ) !== -1; }


function build( root, data, species ) {
function build( root, data, initialSpecies ) {
var entries = data.entries;
var entries = data.entries;
var speciesEntries = entries.filter( function ( e ) { return e.s === species; } );
var NT = data.times.length;
var NT = data.times.length;
var timeLen = data.times.map( function ( name ) { return TIME_TICKS[ name ] || 1; } );
var timeLen = data.times.map( function ( name ) { return TIME_TICKS[ name ] || 1; } );
// Current species (mutable: the standalone page lets the user change it).
var species = initialSpecies || '';
var speciesEntries = [];
function refreshSpeciesEntries() {
speciesEntries = entries.filter( function ( e ) { return e.s === species; } );
}
refreshSpeciesEntries();
// Distinct species that have standard spawn data, for the picker.
var speciesSet = {};
var speciesList = [];
entries.forEach( function ( e ) {
if ( !speciesSet[ e.s ] ) { speciesSet[ e.s ] = true; speciesList.push( e.s ); }
} );
speciesList.sort( function ( a, b ) { return a.localeCompare( b ); } );


// Pre-bucket entries by method id for the "find best" search.
// Pre-bucket entries by method id for the "find best" search.
Line 186: Line 204:
var bestBtn = el( 'button', { class: 'scalc-btn', type: 'button',
var bestBtn = el( 'button', { class: 'scalc-btn', type: 'button',
text: 'Find best chance' } );
text: 'Find best chance' } );
// Optional species picker (standalone page only).
var speciesField = null, speciesInput = null;
if ( !initialSpecies ) {
var dl = el( 'datalist', { id: 'scalc-species-list' } );
speciesList.forEach( function ( n ) { dl.appendChild( el( 'option', { value: n } ) ); } );
speciesInput = el( 'input', {
class: 'scalc-input scalc-species', list: 'scalc-species-list',
placeholder: 'e.g. Bulbasaur', autocomplete: 'off'
} );
function onSpeciesPick() {
if ( speciesSet[ speciesInput.value ] ) { setSpecies( speciesInput.value ); }
}
speciesInput.addEventListener( 'change', onSpeciesPick );
speciesInput.addEventListener( 'input', onSpeciesPick );
speciesField = el( 'div', { class: 'scalc-field scalc-field-species' }, [
el( 'label', { class: 'scalc-label', text: 'Pokemon' } ), speciesInput, dl
] );
}


// Actual in-game spawn conditions the player can stand in.
// Actual in-game spawn conditions the player can stand in.
var form = el( 'div', { class: 'scalc-form' }, [
var condFields = [];
if ( speciesField ) { condFields.push( speciesField ); }
condFields.push(
field( 'Biome', biomeSel ),
field( 'Biome', biomeSel ),
field( 'Location type', methodSel ),
field( 'Location type', methodSel ),
Line 197: Line 236:
el( 'span', { class: 'scalc-label', text: ' ' } ), bestBtn
el( 'span', { class: 'scalc-label', text: ' ' } ), bestBtn
] )
] )
] );
);
var form = el( 'div', { class: 'scalc-form' }, condFields );


// Meta/analysis controls: not a spawn condition, they only change how the
// Meta/analysis controls: not a spawn condition, they only change how the
Line 235: Line 275:
function recompute() {
function recompute() {
results.innerHTML = '';
results.innerHTML = '';
if ( !species ) {
results.appendChild( el( 'div', { class: 'scalc-result' }, [
el( 'span', { class: 'scalc-note', text:
'Choose a Pokemon above to see its spawn chances.' } )
] ) );
flagBest = false;
warnText = null;
return;
}
if ( !speciesEntries.length ) {
if ( !speciesEntries.length ) {
results.appendChild( el( 'div', { class: 'scalc-result scalc-result-miss' }, [
results.appendChild( el( 'div', { class: 'scalc-result scalc-result-miss' }, [
Line 327: Line 376:
// ── find best: situation (and, for N > 1, best time block) ──────
// ── find best: situation (and, for N > 1, best time block) ──────
function findBest() {
function findBest() {
if ( !species || !speciesEntries.length ) { recompute(); return; }
var s = getInputs();
var s = getInputs();
var N = s.N, strict = s.strict;
var N = s.N, strict = s.strict;
Line 450: Line 500:
if ( se.t && se.t.length ) { timeSel.value = se.t[ 0 ]; }
if ( se.t && se.t.length ) { timeSel.value = se.t[ 0 ]; }
ySel.value = pickY( se );
ySel.value = pickY( se );
}
// Switch to a new species (standalone page): refresh data + form defaults.
function setSpecies( name ) {
species = name;
refreshSpeciesEntries();
setDefaults();
recompute();
}
}


Line 473: Line 531:
if ( !root ) { return; }
if ( !root ) { return; }
var data = window.PixelmonSpawnData;
var data = window.PixelmonSpawnData;
var species = root.getAttribute( 'data-species' );
if ( !data || !data.entries ) {
if ( !data || !data.entries || !species ) {
root.textContent = 'Spawn calculator data failed to load.';
root.textContent = 'Spawn calculator data failed to load.';
return;
return;
Line 480: Line 537:
root.classList.add( 'scalc-ready' );
root.classList.add( 'scalc-ready' );
root.innerHTML = '';
root.innerHTML = '';
build( root, data, species );
build( root, data, root.getAttribute( 'data-species' ) || '' );
}
}



Revision as of 08:35, 18 June 2026

/*
 * Pixelmon spawn-chance calculator gadget.
 *
 * Mounts into <div id="spawn-calculator" data-species="..."> on a species page.
 * If data-species is empty (the standalone Spawn Calculator page), the gadget
 * shows a Pokemon picker so any species can be chosen.
 *
 * Features:
 *   1. Pick a situation (biome, time, weather, location type, Y-level) and see
 *      the chance that a single Better-Spawner selection is this species:
 *        P = sum(rarity of this species' matching spawns)
 *            / sum(rarity of all matching spawns)
 *   2. "Consecutive times": evaluate a block of N consecutive times of day
 *      (cyclic; 1..all). With N > 1 the chance is the per-time chance averaged
 *      over the block, weighting each time by its real in-game length so longer
 *      times (e.g. Night) count more than short ones (e.g. Midnight). Times in
 *      the block where the species does not spawn (0%) are flagged.
 *   3. "Find best chance" searches the situations (and, for N > 1, the best
 *      consecutive time block) giving the highest weighted chance. With "Strict
 *      block" on, only blocks where the species spawns in every time count; if
 *      no full block exists the optimizer reduces N and warns the user.
 *
 * Data (every species' spawn entries) is provided by the companion
 * Gadget-spawncalc-data.js file as window.PixelmonSpawnData.
 *
 * This file is static UI/logic; it is NOT generated. The data file IS.
 */
( function () {
	'use strict';

	// Active in-game duration (ticks out of 24000) of each WorldTime, derived
	// from the Pixelmon WorldTime SEGMENTS partition. Used to weight a block of
	// consecutive times by relative length.
	var TIME_TICKS = {
		DAWN: 1800, MORNING: 7451, DAY: 12001, MIDDAY: 1001,
		AFTERNOON: 6000, DUSK: 1800, NIGHT: 10550, MIDNIGHT: 1001
	};

	function el( tag, attrs, children ) {
		var node = document.createElement( tag );
		attrs = attrs || {};
		Object.keys( attrs ).forEach( function ( k ) {
			if ( k === 'class' ) { node.className = attrs[ k ]; }
			else if ( k === 'text' ) { node.textContent = attrs[ k ]; }
			else { node.setAttribute( k, attrs[ k ] ); }
		} );
		( children || [] ).forEach( function ( c ) {
			node.appendChild( typeof c === 'string' ? document.createTextNode( c ) : c );
		} );
		return node;
	}

	// "minecraft:plains" -> "Plains", "#pixelmon:spawning/mesas" -> "Mesas".
	function prettyBiome( id ) {
		var s = id.charAt( 0 ) === '#' ? id.slice( 1 ) : id;
		if ( s.indexOf( ':' ) !== -1 ) { s = s.split( ':' )[ 1 ]; }
		if ( s.indexOf( '/' ) !== -1 ) { s = s.split( '/' ).pop(); }
		return s.replace( /_/g, ' ' ).replace( /\b\w/g, function ( c ) {
			return c.toUpperCase();
		} );
	}

	function titleCase( s ) {
		return s.charAt( 0 ) + s.slice( 1 ).toLowerCase();
	}

	function inList( list, v ) { return list && list.indexOf( v ) !== -1; }

	function build( root, data, initialSpecies ) {
		var entries = data.entries;
		var NT = data.times.length;
		var timeLen = data.times.map( function ( name ) { return TIME_TICKS[ name ] || 1; } );

		// Current species (mutable: the standalone page lets the user change it).
		var species = initialSpecies || '';
		var speciesEntries = [];
		function refreshSpeciesEntries() {
			speciesEntries = entries.filter( function ( e ) { return e.s === species; } );
		}
		refreshSpeciesEntries();

		// Distinct species that have standard spawn data, for the picker.
		var speciesSet = {};
		var speciesList = [];
		entries.forEach( function ( e ) {
			if ( !speciesSet[ e.s ] ) { speciesSet[ e.s ] = true; speciesList.push( e.s ); }
		} );
		speciesList.sort( function ( a, b ) { return a.localeCompare( b ); } );

		// Pre-bucket entries by method id for the "find best" search.
		var byMethod = {};
		entries.forEach( function ( e ) {
			e.m.forEach( function ( m ) {
				( byMethod[ m ] = byMethod[ m ] || [] ).push( e );
			} );
		} );

		// ── situation match + probability ─────────────────────────────
		function matches( e, B, M, T, W, y ) {
			if ( !inList( e.m, M ) ) { return false; }
			if ( e.b && !inList( e.b, B ) ) { return false; }
			if ( e.t && !inList( e.t, T ) ) { return false; }
			if ( e.w && !inList( e.w, W ) ) { return false; }
			if ( y !== null ) {
				if ( e.y0 != null && y < e.y0 ) { return false; }
				if ( e.y1 != null && y > e.y1 ) { return false; }
			}
			return true;
		}

		function chance( B, M, T, W, y ) {
			var num = 0, den = 0, comp = 0;
			for ( var i = 0; i < entries.length; i++ ) {
				if ( matches( entries[ i ], B, M, T, W, y ) ) {
					den += entries[ i ].r;
					if ( entries[ i ].s === species ) { num += entries[ i ].r; }
					else { comp++; }
				}
			}
			return { num: num, den: den, p: den > 0 ? num / den : 0, competitors: comp };
		}

		// Cyclic block of n time indices starting at `start`.
		function blockTimes( start, n ) {
			n = Math.max( 1, Math.min( NT, n ) );
			var out = [];
			for ( var k = 0; k < n; k++ ) { out.push( ( start + k ) % NT ); }
			return out;
		}

		// Length-weighted average of per-time probabilities over a block.
		function blockWeighted( pByTime, times ) {
			var wsum = 0, acc = 0;
			for ( var i = 0; i < times.length; i++ ) {
				var t = times[ i ];
				wsum += timeLen[ t ];
				acc += timeLen[ t ] * pByTime[ t ];
			}
			return wsum > 0 ? acc / wsum : 0;
		}

		// Default Y inside an entry's range (else 63).
		function pickY( e ) {
			var y = 63;
			if ( e.y0 != null && y < e.y0 ) { y = e.y0; }
			if ( e.y1 != null && y > e.y1 ) { y = e.y1; }
			return y;
		}

		function round2( n ) { return Math.round( n * 100 ) / 100; }

		function fmtPct( p ) {
			if ( !( p > 0 ) ) { return '0%'; }
			var v = p * 100;
			return ( v >= 1 ? v.toFixed( 1 ) : v.toFixed( 2 ) ) + '%';
		}

		// ── controls ──────────────────────────────────────────────────
		var allTimes = data.times.map( function ( _t, i ) { return i; } );
		var allWeathers = data.weathers.map( function ( _w, i ) { return i; } );

		var biomeSel = el( 'select', { class: 'scalc-input' } );
		data.biomes
			.map( function ( id, i ) { return { i: i, name: prettyBiome( id ) }; } )
			.sort( function ( a, b ) { return a.name.localeCompare( b.name ); } )
			.forEach( function ( o ) {
				biomeSel.appendChild( el( 'option', { value: o.i, text: o.name } ) );
			} );

		var methodSel = el( 'select', { class: 'scalc-input' } );
		data.methods.forEach( function ( m, i ) {
			methodSel.appendChild( el( 'option', { value: i, text: m } ) );
		} );

		var timeSel = el( 'select', { class: 'scalc-input' } );
		data.times.forEach( function ( t, i ) {
			timeSel.appendChild( el( 'option', { value: i, text: titleCase( t ) } ) );
		} );

		var weatherSel = el( 'select', { class: 'scalc-input' } );
		data.weathers.forEach( function ( w, i ) {
			weatherSel.appendChild( el( 'option', { value: i, text: titleCase( w ) } ) );
		} );

		var consecSel = el( 'input', {
			type: 'number', min: '1', max: String( NT ), value: '1',
			class: 'scalc-num',
			title: 'Evaluate a block of this many consecutive times of day, starting '
				+ 'at the chosen time. Each time is weighted by its real length, so '
				+ 'longer times (Night) count more than short ones (Midnight). Set to '
				+ NT + ' for any time.'
		} );

		var strictChk = el( 'input', { type: 'checkbox', class: 'scalc-check', checked: 'checked' } );

		var ySel = el( 'input', { type: 'number', min: '-64', max: '320', value: '63', class: 'scalc-num' } );

		function field( label, control ) {
			return el( 'div', { class: 'scalc-field' }, [
				el( 'label', { class: 'scalc-label', text: label } ), control
			] );
		}

		var bestBtn = el( 'button', { class: 'scalc-btn', type: 'button',
			text: 'Find best chance' } );

		// Optional species picker (standalone page only).
		var speciesField = null, speciesInput = null;
		if ( !initialSpecies ) {
			var dl = el( 'datalist', { id: 'scalc-species-list' } );
			speciesList.forEach( function ( n ) { dl.appendChild( el( 'option', { value: n } ) ); } );
			speciesInput = el( 'input', {
				class: 'scalc-input scalc-species', list: 'scalc-species-list',
				placeholder: 'e.g. Bulbasaur', autocomplete: 'off'
			} );
			function onSpeciesPick() {
				if ( speciesSet[ speciesInput.value ] ) { setSpecies( speciesInput.value ); }
			}
			speciesInput.addEventListener( 'change', onSpeciesPick );
			speciesInput.addEventListener( 'input', onSpeciesPick );
			speciesField = el( 'div', { class: 'scalc-field scalc-field-species' }, [
				el( 'label', { class: 'scalc-label', text: 'Pokemon' } ), speciesInput, dl
			] );
		}

		// Actual in-game spawn conditions the player can stand in.
		var condFields = [];
		if ( speciesField ) { condFields.push( speciesField ); }
		condFields.push(
			field( 'Biome', biomeSel ),
			field( 'Location type', methodSel ),
			field( 'Time of day', timeSel ),
			field( 'Weather', weatherSel ),
			field( 'Y-level', ySel ),
			el( 'div', { class: 'scalc-field scalc-field-btn' }, [
				el( 'span', { class: 'scalc-label', text: ' ' } ), bestBtn
			] )
		);
		var form = el( 'div', { class: 'scalc-form' }, condFields );

		// Meta/analysis controls: not a spawn condition, they only change how the
		// chance is aggregated, so they live in their own slim section.
		var strictField = el( 'div', { class: 'scalc-field' }, [
			el( 'label', { class: 'scalc-label', text: 'Strict block' } ),
			el( 'label', { class: 'scalc-check-wrap',
				title: 'When finding the best chance, only accept consecutive-time '
					+ 'blocks where the species spawns in every time. If no full block '
					+ 'exists, the block size is reduced and you are warned.' }, [
				strictChk, el( 'span', { class: 'scalc-check-text', text: 'all times must spawn' } )
			] )
		] );
		var meta = el( 'div', { class: 'scalc-form scalc-meta' }, [
			el( 'span', { class: 'scalc-meta-label', text: 'Analysis' } ),
			field( 'Consecutive times', consecSel ),
			strictField
		] );

		var results = el( 'div', { class: 'scalc-results' } );

		function getInputs() {
			return {
				B: +biomeSel.value,
				M: +methodSel.value,
				T: +timeSel.value,
				W: +weatherSel.value,
				y: ySel.value === '' ? null : +ySel.value,
				N: Math.max( 1, Math.min( NT, parseInt( consecSel.value, 10 ) || 1 ) ),
				strict: strictChk.checked
			};
		}

		var flagBest = false;
		var warnText = null;

		function recompute() {
			results.innerHTML = '';
			if ( !species ) {
				results.appendChild( el( 'div', { class: 'scalc-result' }, [
					el( 'span', { class: 'scalc-note', text:
						'Choose a Pokemon above to see its spawn chances.' } )
				] ) );
				flagBest = false;
				warnText = null;
				return;
			}
			if ( !speciesEntries.length ) {
				results.appendChild( el( 'div', { class: 'scalc-result scalc-result-miss' }, [
					el( 'span', { class: 'scalc-note', text:
						species + ' has no standard overworld spawns (legendary, boss, '
						+ 'fossil, evolution-only or event Pokemon).' } )
				] ) );
				flagBest = false;
				warnText = null;
				return;
			}
			var s = getInputs();
			var times = blockTimes( s.T, s.N );
			var single = times.length === 1;

			var p, detailText;
			var perTime = null;     // per-time breakdown when the block spans >1 time
			var deadCount = 0;      // times in the block with no spawn for this species
			if ( single ) {
				var res = chance( s.B, s.M, times[ 0 ], s.W, s.y );
				p = res.p;
				detailText = p > 0
					? '~1 in ' + Math.round( 1 / p ) + ' spawns - rarity '
						+ round2( res.num ) + ' of ' + round2( res.den ) + ' vs '
						+ res.competitors + ' competing spawn'
						+ ( res.competitors === 1 ? '' : 's' ) + '.'
					: '';
			} else {
				var p8 = [];
				for ( var t = 0; t < NT; t++ ) { p8.push( chance( s.B, s.M, t, s.W, s.y ).p ); }
				p = blockWeighted( p8, times );
				perTime = times.map( function ( ti ) {
					return { name: titleCase( data.times[ ti ] ), p: p8[ ti ] };
				} );
				deadCount = perTime.filter( function ( pt ) { return !( pt.p > 0 ); } ).length;
				detailText = p > 0
					? '~1 in ' + Math.round( 1 / p ) + ' spawns, length-weighted across '
						+ ( s.N >= NT ? 'any time' : s.N + ' consecutive times' )
						+ ' (per-time below).'
					: '';
			}

			var hit = p > 0;
			var pctText = fmtPct( hit ? p : 0 );

			var head = el( 'div', { class: 'scalc-result-head' }, [
				el( 'span', { class: 'scalc-pct', text: pctText } ),
				el( 'span', { class: 'scalc-pctlabel', text:
					'chance a wild spawn here is ' + species
					+ ( flagBest && hit ? ' (best found)' : '' ) } )
			] );
			var detail = hit
				? el( 'span', { class: 'scalc-detail', text: detailText } )
				: el( 'span', { class: 'scalc-note', text:
					'Does not spawn in this situation. Try "Find best chance" or '
					+ 'change the biome / location type / time.' } );

			var children = [];
			if ( warnText ) {
				children.push( el( 'div', { class: 'scalc-warn', text: warnText } ) );
			}
			children.push( head, detail );

			// One compact chip per time in the block, each labelled with its time
			// and that time's own chance; 0% times are flagged so a block is never
			// mistaken for a genuine run of spawning times.
			if ( hit && perTime ) {
				var chips = perTime.map( function ( pt ) {
					var dead = !( pt.p > 0 );
					return el( 'span', { class: 'scalc-chip' + ( dead ? ' scalc-chip-dead' : '' ) }, [
						el( 'span', { class: 'scalc-chip-time', text: pt.name } ),
						el( 'span', { class: 'scalc-chip-pct', text: fmtPct( pt.p ) } )
					] );
				} );
				children.push( el( 'div', { class: 'scalc-breakdown' }, chips ) );
				if ( deadCount > 0 ) {
					children.push( el( 'div', { class: 'scalc-note', text:
						deadCount + ' of ' + perTime.length + ' times have no ' + species
						+ ' spawns here' + ( s.strict ? ''
							: '; enable "Strict block" to make the optimizer avoid them' )
						+ '.' } ) );
				}
			}
			results.appendChild( el( 'div', {
				class: 'scalc-result ' + ( hit ? 'scalc-result-hit' : 'scalc-result-miss' )
				+ ( flagBest && hit ? ' scalc-result-best' : '' )
			}, children ) );
			flagBest = false;
			warnText = null;
		}

		// ── find best: situation (and, for N > 1, best time block) ──────
		function findBest() {
			if ( !species || !speciesEntries.length ) { recompute(); return; }
			var s = getInputs();
			var N = s.N, strict = s.strict;

			// Build every candidate (biome, method, weather, y) situation once,
			// each with its per-time probability array, then search over them.
			var situations = [];
			for ( var i = 0; i < speciesEntries.length; i++ ) {
				var se = speciesEntries[ i ];
				var y = pickY( se );
				var biomes = se.b || data.biomes.map( function ( _b, k ) { return k; } );
				var weathers = se.w || allWeathers;
				for ( var mi = 0; mi < se.m.length; mi++ ) {
					var M = se.m[ mi ];
					var pool = byMethod[ M ] || [];
					for ( var bi = 0; bi < biomes.length; bi++ ) {
						var B = biomes[ bi ];
						var sub = [];
						for ( var pj = 0; pj < pool.length; pj++ ) {
							var e = pool[ pj ];
							if ( e.b && !inList( e.b, B ) ) { continue; }
							if ( e.y0 != null && y < e.y0 ) { continue; }
							if ( e.y1 != null && y > e.y1 ) { continue; }
							sub.push( e );
						}
						for ( var wi = 0; wi < weathers.length; wi++ ) {
							var W = weathers[ wi ];
							var num = [], den = [];
							for ( var z = 0; z < NT; z++ ) { num.push( 0 ); den.push( 0 ); }
							for ( var k = 0; k < sub.length; k++ ) {
								var c = sub[ k ];
								if ( c.w && !inList( c.w, W ) ) { continue; }
								var ct = c.t || allTimes;
								for ( var ti = 0; ti < ct.length; ti++ ) {
									var tt = ct[ ti ];
									den[ tt ] += c.r;
									if ( c.s === species ) { num[ tt ] += c.r; }
								}
							}
							var p8 = [];
							for ( var q = 0; q < NT; q++ ) { p8.push( den[ q ] > 0 ? num[ q ] / den[ q ] : 0 ); }
							situations.push( { B: B, M: M, W: W, y: y, p8: p8 } );
						}
					}
				}
			}

			// Best block of exactly size n. When strictFlag, only accept blocks in
			// which every time spawns this species (no 0% time inside the block).
			function searchAt( n, strictFlag ) {
				var best = null;
				for ( var i = 0; i < situations.length; i++ ) {
					var p8 = situations[ i ].p8;
					for ( var start = 0; start < NT; start++ ) {
						var times = blockTimes( start, n );
						var allSpawn = true;
						for ( var j = 0; j < times.length; j++ ) {
							if ( !( p8[ times[ j ] ] > 0 ) ) { allSpawn = false; break; }
						}
						if ( strictFlag && !allSpawn ) {
							if ( n >= NT ) { break; }
							continue;
						}
						var p = times.length === 1 ? p8[ times[ 0 ] ] : blockWeighted( p8, times );
						if ( p > 0 && ( !best || p > best.p ) ) {
							best = { B: situations[ i ].B, M: situations[ i ].M,
								W: situations[ i ].W, y: situations[ i ].y, start: start, p: p };
						}
						if ( n >= NT ) { break; }
					}
				}
				return best;
			}

			var best = null, effN = N;
			if ( strict ) {
				for ( var n = N; n >= 1; n-- ) {
					best = searchAt( n, true );
					if ( best ) { effN = n; break; }
				}
			} else {
				best = searchAt( N, false );
			}
			if ( !best ) { recompute(); return; }

			biomeSel.value = best.B;
			methodSel.value = best.M;
			weatherSel.value = best.W;
			ySel.value = best.y;
			timeSel.value = best.start;
			if ( strict && effN < N ) {
				consecSel.value = effN;
				warnText = species + ' spawns in at most ' + effN + ' consecutive time'
					+ ( effN === 1 ? '' : 's' ) + ' in any situation, so the block was '
					+ 'reduced from ' + N + ' to ' + effN + '.';
			}
			flagBest = true;
			recompute();
		}

		// Default the form to a representative situation where the species
		// spawns: prefer a common overworld method (Grass/Land/Water/...) and the
		// highest-rarity such entry, so the landing view is not a niche 100%
		// (e.g. a single Curry flavour).
		function setDefaults() {
			if ( !speciesEntries.length ) { return; }
			var common = {};
			[ 'Grass', 'Land', 'Water', 'Surface Water', 'Air', 'Tree Top',
			  'Underground', 'Seafloor' ].forEach( function ( name ) {
				var idx = data.methods.indexOf( name );
				if ( idx !== -1 ) { common[ idx ] = true; }
			} );
			var pick = null;
			speciesEntries.forEach( function ( e ) {
				var isCommon = e.m.some( function ( m ) { return common[ m ]; } );
				var score = ( isCommon ? 1e6 : 0 ) + e.r;
				if ( !pick || score > pick.score ) { pick = { e: e, score: score }; }
			} );
			var se = pick.e;
			var m0 = se.m.filter( function ( m ) { return common[ m ]; } )[ 0 ];
			methodSel.value = ( m0 != null ? m0 : se.m[ 0 ] );
			if ( se.b && se.b.length ) { biomeSel.value = se.b[ 0 ]; }
			if ( se.t && se.t.length ) { timeSel.value = se.t[ 0 ]; }
			ySel.value = pickY( se );
		}

		// Switch to a new species (standalone page): refresh data + form defaults.
		function setSpecies( name ) {
			species = name;
			refreshSpeciesEntries();
			setDefaults();
			recompute();
		}

		[ biomeSel, methodSel, timeSel, weatherSel, ySel, consecSel ].forEach( function ( c ) {
			c.addEventListener( 'change', recompute );
			c.addEventListener( 'input', recompute );
		} );
		bestBtn.addEventListener( 'click', findBest );

		// ── assemble the card (compact: header, controls row, result row) ──
		var header = el( 'div', { class: 'scalc-header' }, [
			el( 'span', { class: 'scalc-header-title', text: 'Spawn Chance Calculator' } )
		] );
		var body = el( 'div', { class: 'scalc-body' }, [ form, meta, results ] );
		root.appendChild( header );
		root.appendChild( body );
		setDefaults();
		recompute();
	}

	function init() {
		var root = document.getElementById( 'spawn-calculator' );
		if ( !root ) { return; }
		var data = window.PixelmonSpawnData;
		if ( !data || !data.entries ) {
			root.textContent = 'Spawn calculator data failed to load.';
			return;
		}
		root.classList.add( 'scalc-ready' );
		root.innerHTML = '';
		build( root, data, root.getAttribute( 'data-species' ) || '' );
	}

	if ( document.readyState !== 'loading' ) { init(); }
	else { document.addEventListener( 'DOMContentLoaded', init ); }
}() );