Simple Archaeology Game
Digs Remaining: 25
Dig Site
Field Journal
No artifacts found yet. Start digging!
Manage Artifact Database
Current Artifacts
Artifact database is empty.
'; return; } sagArtifactDatabase.forEach(a => { const item = document.createElement('div'); item.className = 'sag-db-item'; item.innerHTML = `
${a.name}
`;
sagArtifactDbList.appendChild(item);
});
}
// --- Dashboard / Game Functions ---
function startNewDig() {
sagDigsRemaining = SAG_MAX_DIGS;
sagFoundArtifacts = [];
sagGridData = new Array(SAG_GRID_SIZE).fill(null);
// "Bury" artifacts
if (sagArtifactDatabase.length > 0) {
let artifactsToBury = Math.min(SAG_NUM_ARTIFACTS, sagArtifactDatabase.length);
for (let i = 0; i < artifactsToBury; i++) {
let artifact = sagArtifactDatabase[Math.floor(Math.random() * sagArtifactDatabase.length)];
let index;
// Find an empty spot
do {
index = Math.floor(Math.random() * SAG_GRID_SIZE);
} while (sagGridData[index] !== null);
// Place a *copy* of the artifact
sagGridData[index] = { ...artifact };
}
}
renderGrid();
renderJournal();
updateStats();
}
function renderGrid() {
if (!sagDigSite) return;
sagDigSite.innerHTML = '';
for (let i = 0; i < SAG_GRID_SIZE; i++) {
const square = document.createElement('div');
square.className = 'sag-grid-square';
square.dataset.index = i;
sagDigSite.appendChild(square);
}
}
function renderJournal() {
if (!sagJournalList || !sagNoFindsMsg) return;
sagJournalList.innerHTML = '';
if (sagFoundArtifacts.length === 0) {
sagJournalList.appendChild(sagNoFindsMsg);
sagNoFindsMsg.style.display = 'block';
} else {
sagNoFindsMsg.style.display = 'none';
sagFoundArtifacts.forEach(a => {
const item = document.createElement('div');
item.className = 'sag-journal-item';
item.innerHTML = `
${a.name}
${a.description}
${a.description}
`; sagJournalList.appendChild(item); }); } } function updateStats() { if (!sagGameStats) return; sagGameStats.textContent = `Digs Remaining: ${sagDigsRemaining}`; } function handleGridClick(e) { if (sagDigsRemaining <= 0) { alert("You are out of digs! Start a new dig to continue."); return; } const square = e.target; if (square.matches('.sag-grid-square') && !square.classList.contains('sag-dug')) { const index = parseInt(square.dataset.index, 10); sagDigsRemaining--; square.classList.add('sag-dug'); const foundItem = sagGridData[index]; if (foundItem) { square.classList.add('sag-found'); square.innerHTML = '💎'; // Gem stone emoji sagFoundArtifacts.push(foundItem); renderJournal(); } else { square.textContent = '...'; } updateStats(); if (sagDigsRemaining <= 0) { alert("You are out of digs! Your expedition is complete. Check your journal or start a new dig."); } } } /** * Generates and downloads a PDF of the Field Journal. */ async function downloadPDF() { if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') { console.error("jsPDF or html2canvas library not loaded."); alert("Error: PDF generation libraries not loaded."); return; } const { jsPDF } = window.jspdf; sagPdfBtn.textContent = 'Generating...'; sagPdfBtn.disabled = true; const contentToExport = document.getElementById('sag-pdf-content'); if (!contentToExport) { console.error("PDF content area not found."); sagPdfBtn.textContent = 'Download Journal as PDF'; sagPdfBtn.disabled = false; return; } contentToExport.classList.add('sag-pdf-export-content'); try { const canvas = await html2canvas(contentToExport, { scale: 2, useCORS: true, logging: false, onclone: (doc) => { const list = doc.getElementById('sag-journal-list'); if (list) list.style.maxHeight = 'none'; // Unset max-height } }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const ratio = canvas.width / pdfWidth; const scaledHeight = canvas.height / ratio; let position = 0; let heightLeft = scaledHeight; pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, scaledHeight); heightLeft -= pdfHeight; while (heightLeft > 0) { position = heightLeft - scaledHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 0, position, pdfWidth, scaledHeight); heightLeft -= pdfHeight; } pdf.save('archaeology_journal.pdf'); } catch (error) { console.error("Error during PDF generation:", error); alert("An error occurred while generating the PDF."); } finally { contentToExport.classList.remove('sag-pdf-export-content'); sagPdfBtn.textContent = 'Download Journal as PDF'; sagPdfBtn.disabled = false; } } // --- Tab Navigation Functions --- function openTab(evt, tabName) { if (!sagTabContents.length || !sagTabLinks.length) return; sagTabContents.forEach(tabcontent => { tabcontent.style.display = "none"; tabcontent.classList.remove('sag-active'); }); sagTabLinks.forEach(tablink => { tablink.classList.remove('sag-active'); }); const currentTabContent = document.getElementById(tabName); if (currentTabContent) { currentTabContent.style.display = "block"; currentTabContent.classList.add('sag-active'); } if (evt && evt.currentTarget) { evt.currentTarget.classList.add('sag-active'); } sagCurrentTab = sagTabLinks.indexOf(evt.currentTarget); updateNavButtons(); } function navigateTabs(n) { const newIndex = sagCurrentTab + n; if (newIndex >= 0 && newIndex < sagTabLinks.length) { sagTabLinks[newIndex].click(); } } function updateNavButtons() { if (sagPrevBtn) sagPrevBtn.disabled = (sagCurrentTab === 0); if (sagNextBtn) sagNextBtn.disabled = (sagCurrentTab === sagTabLinks.length - 1); } // --- Attach to Window Scope for onclick Handlers --- window.sagOpenTab = openTab; window.sagNavTab = navigateTabs; })();