跳到正文

剪贴板

剪贴板 bddk2n0x

原文
剪贴板信息

剪贴板信息

收录
更新
JavaScript
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🐍 蛇岛 · 荒岛求生</title>
    <style>
        * { box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', sans-serif;
            background: #1a2a1f;
            color: #d4d4aa;
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
        h1 { text-align: center; color: #b8b84a; text-shadow: 0 0 10px #3f5f3f; }
        .mode-select { text-align: center; margin: 10px 0; }
        .mode-select button {
            padding: 8px 20px; margin: 0 10px; font-size: 1.2rem;
            cursor: pointer; background: #3d5a3d; color: #eee;
            border: 1px solid #7a9a7a; border-radius: 8px;
        }
        .mode-select button.active { background: #5f8a5f; border-color: #b8d4b8; }
        .game-area { display: flex; flex-wrap: wrap; gap: 15px; margin-top: 15px; }
        .player-panel {
            flex: 1 1 45%; background: #2a3a2a; padding: 15px; border-radius: 12px;
            border: 1px solid #4a6a4a; box-shadow: 0 0 15px #0f1f0f; min-width: 280px;
        }
        .player-panel h2 { margin-top: 0; border-bottom: 1px solid #5a7a5a; padding-bottom: 5px; color: #c8d88a; }
        .status-grid {
            display: grid; grid-template-columns: 1fr 1fr;
            gap: 3px 15px; font-size: 0.9rem;
        }
        .status-grid .label { color: #9ab89a; }
        .status-grid .value { color: #f0f0c0; text-align: right; }
        .debuff { color: #ff7a7a; font-weight: bold; }
        .tool-list { display: flex; flex-wrap: wrap; gap: 4px; margin: 5px 0; }
        .tool-tag {
            background: #3a4a2a; padding: 2px 8px; border-radius: 12px;
            font-size: 0.8rem; color: #b0d0b0; border: 1px solid #5a7a4a;
        }
        .action-panel {
            flex: 1 1 100%; background: #1f2f1f; padding: 15px; border-radius: 12px;
            border: 1px solid #3a5a3a;
        }
        .action-panel .phase-title { font-size: 1.4rem; color: #d8d88a; }
        .action-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
        .action-buttons button {
            padding: 6px 14px; background: #3d5a3d; color: #eee;
            border: 1px solid #6a8a6a; border-radius: 6px; cursor: pointer; font-size: 0.9rem;
            transition: 0.15s;
        }
        .action-buttons button:hover:not(:disabled) { background: #5f8a5f; }
        .action-buttons button:disabled { opacity: 0.5; cursor: not-allowed; filter: grayscale(0.6); }
        .action-buttons button.primary { background: #5a7a3a; border-color: #8aaa6a; }
        .action-buttons button.danger { background: #7a3a3a; border-color: #aa6a6a; }
        .log-area {
            background: #0f1a0f; padding: 10px; border-radius: 8px;
            max-height: 150px; overflow-y: auto; font-size: 0.9rem;
            border: 1px solid #2a4a2a; margin-top: 8px;
            white-space: pre-wrap; color: #b8d4b8;
        }
        .log-area .log-entry { border-bottom: 1px dashed #2a4a2a; padding: 2px 0; }
        .craft-menu {
            background: #1a2a1a; padding: 10px; border-radius: 8px;
            border: 1px solid #4a6a4a; margin: 5px 0; display: none;
        }
        .craft-menu.show { display: block; }
        .craft-menu button {
            display: inline-block; margin: 3px 6px 3px 0; padding: 4px 12px;
            background: #3a5a3a; border: 1px solid #5a7a5a; border-radius: 4px;
            color: #ddd; cursor: pointer;
        }
        .craft-menu button:hover { background: #5a7a5a; }
        .night-summary {
            background: #2f2a1a; padding: 8px 12px; border-radius: 6px;
            border-left: 4px solid #b8a84a; margin: 6px 0; color: #e8d88a;
        }
        .dead-tag { color: #ff4a4a; font-weight: bold; margin-left: 10px; }
        /* 模态弹窗样式 */
        .modal-overlay {
            position: fixed;
            top: 0; left: 0; width: 100%; height: 100%;
            background: rgba(0,0,0,0.7);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            animation: fadeIn 0.3s;
        }
        .modal-box {
            background: #2a3a2a;
            padding: 30px 40px;
            border-radius: 16px;
            border: 2px solid #b8a84a;
            box-shadow: 0 0 30px #b8a84a88;
            max-width: 90%;
            text-align: center;
            animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        }
        .modal-box h3 { color: #ff7a7a; margin-top: 0; }
        .modal-box p { color: #d4d4aa; font-size: 1.1rem; }
        .modal-box button {
            padding: 10px 30px; margin: 10px 15px;
            background: #5a7a3a; color: #eee;
            border: 1px solid #8aaa6a; border-radius: 8px;
            cursor: pointer; font-size: 1.1rem;
            transition: 0.2s;
        }
        .modal-box button:hover { background: #7a9a5a; }
        .modal-box button.danger { background: #7a3a3a; border-color: #aa6a6a; }
        .modal-box button.danger:hover { background: #9a5a5a; }
        @keyframes fadeIn {
            from { opacity: 0; } to { opacity: 1; }
        }
        @keyframes popIn {
            0% { transform: scale(0.7); opacity: 0; }
            100% { transform: scale(1); opacity: 1; }
        }
        @media (max-width: 700px) { .player-panel { flex: 1 1 100%; } }
    </style>
</head>
<body>
<h1>🐍 蛇岛 · 荒岛求生</h1>
<div class="mode-select">
    <button id="modeSingle" class="active">单人</button>
    <button id="modeDouble">双人</button>
    <span style="margin-left:20px;color:#8aaa8a;">当前模式: <span id="modeLabel">单人</span></span>
</div>
<div id="gameContainer"></div>
<script>
// ===================================================================
// 完整游戏逻辑 (所有最新机制)
// ===================================================================

function randInt(min,max){ return Math.floor(Math.random()*(max-min+1))+min; }
function randChance(p){ return Math.random()<p; }

class Player {
    constructor(name) {
        this.name = name;
        this.alive = true;
        this.satiety = 5; this.water = 5;
        this.food = 3; this.waterBottles = 3; this.emptyBottles = 0;
        this.medicine = 3; this.expiredMeds = 0;
        this.leaves = 0; this.sticks = 0; this.stones = 0; this.rope = 0;
        this.cloth = 0; this.nails = 0; this.fish = 0; this.coconuts = 0;
        this.coconutJuice = 0;
        this.chests = 0;   // 宝箱数量
        this.keys = 0;     // 钥匙数量
        // 集水器改为多个
        this.waterCollectorTimers = []; // 每个集水器的计时(天数)
        this.hasStoneSpear = false; this.hasPickaxe = false;
        this.hasStoneAxe = false; this.hasStoneSword = false;
        this.hasShoes = false;
        this.hasFishingRod = false; this.hasSlingshot = false;
        this.boatPaddlesCount = 0;
        this.blueprintSpear = false; this.blueprintPickaxe = false;
        this.blueprintAxe = false; this.blueprintSword = false;
        this.blueprintShoes = false; this.blueprintWaterCollector = false;
        this.blueprintNail = false; this.blueprintBoat = false;
        this.blueprintPaddles = false; this.blueprintFishingRod = false;
        this.blueprintSlingshot = false;
        this.houseLevel = 0; this.houseHP = 0;
        this.hasPoison = false; this.hasCold = false;
        this.canActMorning = true;
        this.exp = 0; this.level = 0;
        this.nightSummary = "";
    }
    canCollectStones(){ return this.level>=1; }
    canFish(){ return this.level>=2; }
    getCollectBounds(bMin,bMax){ let b=this.level; return {min:bMin+b, max:bMax+b}; }
    checkDeath(){ return this.satiety<0 || this.water<0; }
    checkWin(){ return this.sticks>=100 && this.rope>=10 && this.emptyBottles>=20 && this.boatPaddlesCount>=2 && this.nails>=20; }
    addExp(gain){
        this.exp += gain;
        while(true){
            let need;
            if(this.level === 0) need = 1000;
            else if(this.level === 1) need = 2000;
            else need = 1000 + this.level * 1000;
            if(this.exp >= need){
                this.exp -= need;
                this.level++;
            } else break;
        }
    }
    // 集水器数量
    get waterCollectorCount() { return this.waterCollectorTimers.length; }
}

let game = {
    mode:'single', players:[], day:1, currentPhase:0,
    phaseNames:['早上','上午','中午','下午','晚上'],
    currentPlayerIndex:0, playerDone:[false,false],
    gameOver:false, gameWin:false, logs:[], showingCraft:false,
    // 标记是否正在处理弹窗,防止重复弹窗
    modalActive: false,
};
function addLog(msg){ game.logs.push(msg); if(game.logs.length>50) game.logs.shift(); }

// ----- 核心操作 (同步) -----
function eat(p){
    if(!p.alive || p.food<=0){ if(!p.alive)addLog(`${p.name}已死亡,无法操作。`); else addLog('没有食物可吃!'); renderDelayed(); return; }
    p.food--; p.satiety=Math.min(5,p.satiety+2);
    addLog(`${p.name} 吃了一份食物,饱食度+2 (${p.satiety})`);
    renderDelayed();
}
function drinkWater(p){
    if(!p.alive || p.waterBottles<=0){ if(!p.alive)addLog(`${p.name}已死亡,无法操作。`); else addLog('没有水可喝!'); renderDelayed(); return; }
    p.waterBottles--; p.water=Math.min(5,p.water+2); p.emptyBottles++;
    addLog(`${p.name} 喝了一瓶水,水分+2,获得空瓶。`);
    renderDelayed();
}
function takeMedicine(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    if(p.medicine<=0){ addLog('没有普通药片!'); renderDelayed(); return; }
    if(!p.hasPoison&&!p.hasCold){ addLog('无中毒或感冒。'); renderDelayed(); return; }
    p.medicine--;
    if(p.hasPoison){ p.hasPoison=false; addLog(`${p.name} 蛇毒已解!`); }
    else if(p.hasCold){ p.hasCold=false; p.canActMorning=true; addLog(`${p.name} 感冒已治愈!`); }
    renderDelayed();
}
function takeExpiredMedicine(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    if(p.expiredMeds<=0){ addLog('没有过期药!'); renderDelayed(); return; }
    if(!p.hasPoison&&!p.hasCold){ addLog('无中毒或感冒。'); renderDelayed(); return; }
    let chance=(p.satiety+p.water)*5; p.expiredMeds--;
    if(randChance(chance/100)){
        if(p.hasPoison){ p.hasPoison=false; addLog(`${p.name} 过期药生效!蛇毒已解!`); }
        else if(p.hasCold){ p.hasCold=false; p.canActMorning=true; addLog(`${p.name} 过期药生效!感冒已治愈!`); }
    } else { addLog(`${p.name} 过期药无效...`); }
    renderDelayed();
}
function drinkCoconutJuice(p){
    if(!p.alive || p.coconutJuice<=0){ if(!p.alive)addLog(`${p.name}已死亡,无法操作。`); else addLog('没有椰汁可喝。'); renderDelayed(); return; }
    p.coconutJuice--; p.satiety=Math.min(5,p.satiety+1); p.water=Math.min(5,p.water+1); p.emptyBottles++;
    addLog(`${p.name} 喝了一瓶椰汁,饱食度+1,水分+1,获得空瓶。`);
    renderDelayed();
}
function openCoconut(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    if(p.coconuts<=0){ addLog('没有椰子。'); renderDelayed(); return; }
    if(p.satiety<1 || p.water<1){ addLog('太虚弱,无法开椰子(需饱食和水分≥1)。'); renderDelayed(); return; }
    p.coconuts--;
    if(p.hasPickaxe){
        addLog(`${p.name} 用石镐打开了椰子。`);
    } else {
        if(p.stones<=0){ addLog('没有石头,无法开椰子。'); p.coconuts++; renderDelayed(); return; }
        p.stones--;
        if(randChance(0.5)){ addLog('石头崩飞了!'); } else { addLog('石头完好。'); p.stones++; }
        addLog(`${p.name} 用石头砸开了椰子。`);
    }
    let juice=2; let bottles=Math.min(p.emptyBottles, juice);
    if(bottles>0){ p.emptyBottles-=bottles; p.coconutJuice+=bottles; juice-=bottles; addLog(`装了 ${bottles} 瓶椰汁。`); }
    if(juice>0){ p.satiety=Math.min(5,p.satiety+juice); p.water=Math.min(5,p.water+juice); addLog(`直接喝掉 ${juice} 份椰汁,饱食度+${juice},水分+${juice}。`); }
    renderDelayed();
}
function eatFish(p){
    if(!p.alive || p.fish<=0){ if(!p.alive)addLog(`${p.name}已死亡,无法操作。`); else addLog('没有鱼。'); renderDelayed(); return; }
    p.fish--; p.satiety=Math.min(5,p.satiety+2);
    addLog(`${p.name} 吃了一条鱼,饱食度+2。`);
    renderDelayed();
}
function eatCoconut(p){
    if(!p.alive || p.coconuts<=0){ if(!p.alive)addLog(`${p.name}已死亡,无法操作。`); else addLog('没有椰子。'); renderDelayed(); return; }
    p.coconuts--; p.satiety=Math.min(5,p.satiety+1); p.water=Math.min(5,p.water+1);
    addLog(`${p.name} 吃了一个椰子,饱食度+1,水分+1。`);
    renderDelayed();
}

// ---- 采集 ----
function collectLeaves(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    let b=p.getCollectBounds(4,9); let minL=b.min,maxL=b.max;
    let bS=p.getCollectBounds(0,2); let minS=bS.min,maxS=bS.max;
    if(p.hasStoneAxe){ minL+=2; maxL+=2; minS+=2; maxS+=2; }
    let leaves=randInt(minL,maxL), sticks=randInt(minS,maxS);
    p.leaves+=leaves; p.sticks+=sticks;
    addLog(`${p.name} 收集树叶 ${leaves} 个,树枝 ${sticks} 个。`);
    let gain=randInt(100,300); if(randChance(0.1)){ gain=randInt(700,800); addLog('暴击!经验暴增!'); }
    p.addExp(gain); addLog(`获得经验 ${gain},等级 ${p.level}`);
    renderDelayed();
}
function collectSticks(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    let b=p.getCollectBounds(4,9); let minS=b.min,maxS=b.max;
    let bL=p.getCollectBounds(0,2); let minL=bL.min,maxL=bL.max;
    if(p.hasStoneAxe){ minS+=2; maxS+=2; minL+=2; maxL+=2; }
    let sticks=randInt(minS,maxS), leaves=randInt(minL,maxL);
    p.sticks+=sticks; p.leaves+=leaves;
    addLog(`${p.name} 收集树枝 ${sticks} 个,树叶 ${leaves} 个。`);
    let gain=randInt(100,300); if(randChance(0.1)){ gain=randInt(700,800); addLog('暴击!经验暴增!'); }
    p.addExp(gain); addLog(`获得经验 ${gain},等级 ${p.level}`);
    renderDelayed();
}
function collectStones(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    if(!p.canCollectStones()){ addLog('等级不足,需要LV1。'); renderDelayed(); return; }
    let b=p.getCollectBounds(3,6); let s=randInt(b.min,b.max);
    p.stones+=s; addLog(`${p.name} 采集石头 ${s} 个。`);
    let gain=randInt(100,300); if(randChance(0.1)){ gain=randInt(700,800); addLog('暴击!经验暴增!'); }
    p.addExp(gain); addLog(`获得经验 ${gain},等级 ${p.level}`);
    renderDelayed();
}
function fish(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    if(!p.canFish()){ addLog('等级不足,需要LV2。'); renderDelayed(); return; }
    let minF=p.hasFishingRod?3:0, maxF=p.hasFishingRod?6:4;
    let b=p.getCollectBounds(minF,maxF); let f=randInt(b.min,b.max);
    p.fish+=f; addLog(`${p.name} 钓到鱼 ${f} 条。`);
    let gain=randInt(100,300); if(randChance(0.1)){ gain=randInt(700,800); addLog('暴击!经验暴增!'); }
    p.addExp(gain); addLog(`获得经验 ${gain},等级 ${p.level}`);
    renderDelayed();
}

// ---- 打野 (概率已降低) ----
function hunt(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    addLog(`${p.name} 选择打野,消耗一整天。`);
    let cost=p.hasShoes?2:3; p.satiety-=cost; p.water-=cost;
    if(p.checkDeath()){ 
        p.alive = false;
        addLog(`${p.name} 因饥饿/干渴而死...`);
        checkAllDead();
        renderDelayed(); 
        return; 
    }
    let baseMin=4, baseMax=6; let num=randInt(baseMin+p.level, baseMax+p.level);
    let items=[]; let pool=['过期药','椰子','石头','水','瓶子','食物','树枝','树叶','绳子','塑料膜'];
    let gotBlueprint=false;
    for(let i=0;i<num;i++){
        // 蓝图概率 15%
        if(!gotBlueprint && randChance(0.15)){
            let avail=[];
            if(!p.blueprintSpear) avail.push('石矛');
            if(!p.blueprintPickaxe) avail.push('石镐');
            if(!p.blueprintAxe) avail.push('石斧');
            if(!p.blueprintSword) avail.push('石剑');
            if(!p.blueprintShoes) avail.push('鞋');
            if(!p.blueprintWaterCollector) avail.push('集水');
            if(!p.blueprintNail) avail.push('铆钉');
            if(!p.blueprintBoat) avail.push('制船');
            if(!p.blueprintPaddles) avail.push('船桨');
            if(!p.blueprintFishingRod) avail.push('鱼竿');
            if(!p.blueprintSlingshot) avail.push('弹弓');
            if(avail.length>0){
                let idx=randInt(0,avail.length-1); let bp=avail[idx];
                items.push('蓝图['+bp+']');
                if(bp==='石矛') p.blueprintSpear=true;
                else if(bp==='石镐') p.blueprintPickaxe=true;
                else if(bp==='石斧') p.blueprintAxe=true;
                else if(bp==='石剑') p.blueprintSword=true;
                else if(bp==='鞋') p.blueprintShoes=true;
                else if(bp==='集水') p.blueprintWaterCollector=true;
                else if(bp==='铆钉') p.blueprintNail=true;
                else if(bp==='制船') p.blueprintBoat=true;
                else if(bp==='船桨') p.blueprintPaddles=true;
                else if(bp==='鱼竿') p.blueprintFishingRod=true;
                else if(bp==='弹弓') p.blueprintSlingshot=true;
                gotBlueprint=true; continue;
            }
        }
        // 宝箱概率 8%
        if(randChance(0.08)){
            p.chests++;
            items.push('宝箱');
        }
        // 钥匙概率 10%
        if(randChance(0.10)){
            p.keys++;
            items.push('宝箱钥匙');
        }
        // 普通物品
        let idx=randInt(0,pool.length-1); let item=pool[idx];
        items.push(item);
        if(item==='过期药') p.expiredMeds++;
        else if(item==='椰子') p.coconuts++;
        else if(item==='石头') p.stones++;
        else if(item==='水'){ p.waterBottles++; p.emptyBottles++; }
        else if(item==='瓶子') p.emptyBottles++;
        else if(item==='食物') p.food++;
        else if(item==='树枝') p.sticks++;
        else if(item==='树叶') p.leaves++;
        else if(item==='绳子') p.rope++;
        else if(item==='塑料膜') p.cloth++;
    }
    addLog(`${p.name} 打野获得: ${items.join(', ')}`);
    renderDelayed();
}

// ---- 开宝箱 (消耗1宝箱+1钥匙,固定3水3食物) ----
function openChest(p){
    if(!p.alive){ addLog(`${p.name}已死亡,无法操作。`); renderDelayed(); return; }
    if(p.chests<=0 || p.keys<=0){
        addLog('宝箱或钥匙不足。');
        renderDelayed();
        return;
    }
    p.chests--;
    p.keys--;
    addLog(`${p.name} 打开了一个宝箱!`);
    // 随机奖励 (蓝图概率15%)
    let num=randInt(4+p.level, 6+p.level);
    let items=[]; let pool=['过期药','椰子','石头','水','瓶子','食物','树枝','树叶','绳子','塑料膜'];
    let gotBlueprint=false;
    for(let i=0;i<num;i++){
        if(!gotBlueprint && randChance(0.15)){
            let avail=[];
            if(!p.blueprintSpear) avail.push('石矛');
            if(!p.blueprintPickaxe) avail.push('石镐');
            if(!p.blueprintAxe) avail.push('石斧');
            if(!p.blueprintSword) avail.push('石剑');
            if(!p.blueprintShoes) avail.push('鞋');
            if(!p.blueprintWaterCollector) avail.push('集水');
            if(!p.blueprintNail) avail.push('铆钉');
            if(!p.blueprintBoat) avail.push('制船');
            if(!p.blueprintPaddles) avail.push('船桨');
            if(!p.blueprintFishingRod) avail.push('鱼竿');
            if(!p.blueprintSlingshot) avail.push('弹弓');
            if(avail.length>0){
                let idx=randInt(0,avail.length-1); let bp=avail[idx];
                items.push('蓝图['+bp+']');
                if(bp==='石矛') p.blueprintSpear=true;
                else if(bp==='石镐') p.blueprintPickaxe=true;
                else if(bp==='石斧') p.blueprintAxe=true;
                else if(bp==='石剑') p.blueprintSword=true;
                else if(bp==='鞋') p.blueprintShoes=true;
                else if(bp==='集水') p.blueprintWaterCollector=true;
                else if(bp==='铆钉') p.blueprintNail=true;
                else if(bp==='制船') p.blueprintBoat=true;
                else if(bp==='船桨') p.blueprintPaddles=true;
                else if(bp==='鱼竿') p.blueprintFishingRod=true;
                else if(bp==='弹弓') p.blueprintSlingshot=true;
                gotBlueprint=true; continue;
            }
        }
        let idx=randInt(0,pool.length-1); let item=pool[idx];
        items.push(item);
        if(item==='过期药') p.expiredMeds++;
        else if(item==='椰子') p.coconuts++;
        else if(item==='石头') p.stones++;
        else if(item==='水'){ p.waterBottles++; p.emptyBottles++; }
        else if(item==='瓶子') p.emptyBottles++;
        else if(item==='食物') p.food++;
        else if(item==='树枝') p.sticks++;
        else if(item==='树叶') p.leaves++;
        else if(item==='绳子') p.rope++;
        else if(item==='塑料膜') p.cloth++;
    }
    addLog(`宝箱中藏有:${items.join(', ')}`);
    // 固定奖励:3水+3食物
    p.waterBottles += 3;
    p.food += 3;
    addLog(`额外获得 3 瓶水和 3 份食物!`);
    renderDelayed();
}

// ---- 制作 ----
function showCraftMenu(p){
    let list=[];
    if(p.blueprintSpear) list.push({id:1, name:'石矛', cost:'3石+2树枝+1绳'});
    if(p.blueprintPickaxe) list.push({id:2, name:'石镐', cost:'3石+1树枝+1绳'});
    if(p.blueprintAxe) list.push({id:3, name:'石斧', cost:'4石+1树枝+1绳'});
    if(p.blueprintSword) list.push({id:4, name:'石剑', cost:'5石+2树枝'});
    if(p.blueprintShoes) list.push({id:5, name:'鞋', cost:'3布+2绳'});
    if(p.blueprintWaterCollector) list.push({id:6, name:'集水器', cost:'1布'}); // 无半天
    if(p.blueprintNail) list.push({id:7, name:'铆钉', cost:'2石'});
    if(p.blueprintPaddles) list.push({id:8, name:'船桨', cost:'6树枝+2绳'});
    if(p.blueprintFishingRod) list.push({id:9, name:'鱼竿', cost:'5树枝+3绳+1铆钉'});
    if(p.blueprintSlingshot) list.push({id:10, name:'弹弓', cost:'2树枝+1绳'});
    if(p.blueprintBoat) list.push({id:11, name:'制船', cost:'100树枝+10绳+20空瓶+2船桨+20铆钉'});
    return list;
}
function craftItem(p, id){
    if(!p.alive){ addLog(`${p.name}已死亡,无法制作。`); renderDelayed(); return; }
    switch(id){
        case 1:
            if(!p.blueprintSpear){ addLog('未解锁。'); break; }
            if(p.stones<3||p.sticks<2||p.rope<1){ addLog('材料不足。'); break; }
            if(p.hasStoneSpear){ addLog('已拥有。'); break; }
            p.stones-=3; p.sticks-=2; p.rope-=1; p.hasStoneSpear=true;
            addLog(`${p.name} 制作石矛成功!`); break;
        case 2:
            if(!p.blueprintPickaxe||p.stones<3||p.sticks<1||p.rope<1||p.hasPickaxe){ addLog('无法制作。'); break; }
            p.stones-=3; p.sticks-=1; p.rope-=1; p.hasPickaxe=true;
            addLog(`${p.name} 制作石镐成功!`); break;
        case 3:
            if(!p.blueprintAxe||p.stones<4||p.sticks<1||p.rope<1||p.hasStoneAxe){ addLog('无法制作。'); break; }
            p.stones-=4; p.sticks-=1; p.rope-=1; p.hasStoneAxe=true;
            addLog(`${p.name} 制作石斧成功!`); break;
        case 4:
            if(!p.blueprintSword||p.stones<5||p.sticks<2||p.hasStoneSword){ addLog('无法制作。'); break; }
            p.stones-=5; p.sticks-=2; p.hasStoneSword=true;
            addLog(`${p.name} 制作石剑成功!`); break;
        case 5:
            if(!p.blueprintShoes||p.cloth<3||p.rope<2||p.hasShoes){ addLog('无法制作。'); break; }
            p.cloth-=3; p.rope-=2; p.hasShoes=true;
            addLog(`${p.name} 制作鞋成功!`); break;
        case 6: { // 集水器 (可建多个,上限2*level)
            if(!p.blueprintWaterCollector){ addLog('未解锁。'); break; }
            if(p.cloth<1){ addLog('材料不足(需要1布)。'); break; }
            let maxCollectors = 2 * p.level;
            if(p.waterCollectorTimers.length >= maxCollectors){
                addLog(`集水器已达上限(${maxCollectors}个)。`);
                break;
            }
            p.cloth -= 1;
            p.waterCollectorTimers.push(0);
            addLog(`${p.name} 集水器建造完成!当前数量:${p.waterCollectorTimers.length}。`);
            break;
        }
        case 7:
            if(!p.blueprintNail||p.stones<2){ addLog('无法制作。'); break; }
            p.stones-=2; p.nails++;
            addLog(`${p.name} 制作一个铆钉,当前:${p.nails}`); break;
        case 8:
            if(!p.blueprintPaddles||p.sticks<6||p.rope<2){ addLog('无法制作。'); break; }
            p.sticks-=6; p.rope-=2; p.boatPaddlesCount++;
            addLog(`${p.name} 制作一个船桨,当前:${p.boatPaddlesCount}`); break;
        case 9:
            if(!p.blueprintFishingRod||p.sticks<5||p.rope<3||p.nails<1||p.hasFishingRod){ addLog('无法制作。'); break; }
            p.sticks-=5; p.rope-=3; p.nails-=1; p.hasFishingRod=true;
            addLog(`${p.name} 制作鱼竿成功!`); break;
        case 10:
            if(!p.blueprintSlingshot||p.sticks<2||p.rope<1||p.hasSlingshot){ addLog('无法制作。'); break; }
            p.sticks-=2; p.rope-=1; p.hasSlingshot=true;
            addLog(`${p.name} 制作弹弓成功!`); break;
        case 11:
            if(!p.blueprintBoat||p.sticks<100||p.rope<10||p.emptyBottles<20||p.boatPaddlesCount<2||p.nails<20){
                addLog('材料不足,无法造船。'); break;
            }
            game.gameWin=true; game.gameOver=true;
            addLog(`${p.name} 造出了船!逃离蛇岛!`);
            break;
        default: addLog('无效选择。');
    }
    renderDelayed();
}

// ---- 房屋 (4级房 10/10/20) ----
function buildHouse(p, targetLevel){
    if(!p.alive){ addLog(`${p.name}已死亡,无法建房。`); renderDelayed(); return false; }
    if(p.houseLevel===4){ addLog('已拥有无限庇护所。'); renderDelayed(); return false; }
    if(targetLevel<=p.houseLevel){ addLog('不能建造低于或等于当前等级的房屋。'); renderDelayed(); return false; }
    let needSticks=0, needLeaves=0, needStones=0;
    if(targetLevel===1){ needSticks=5; needLeaves=5; }
    else if(targetLevel===2){ needSticks=8; needLeaves=8; }
    else if(targetLevel===3){ needSticks=10; needLeaves=10; }
    else if(targetLevel===4){ needSticks=10; needLeaves=10; needStones=20; }
    if(p.houseLevel===1){ needSticks-=5; needLeaves-=5; }
    else if(p.houseLevel===2){ needSticks-=8; needLeaves-=8; }
    else if(p.houseLevel===3){ needSticks-=10; needLeaves-=10; }
    if(needSticks<0) needSticks=0; if(needLeaves<0) needLeaves=0;
    if(p.sticks<needSticks||p.leaves<needLeaves||(targetLevel===4&&p.stones<needStones)){
        addLog(`材料不足!需要树枝 ${needSticks},树叶 ${needLeaves}${targetLevel===4?`,石头 ${needStones}`:''}`);
        renderDelayed(); return false;
    }
    p.sticks-=needSticks; p.leaves-=needLeaves;
    if(targetLevel===4) p.stones-=needStones;
    p.houseLevel=targetLevel;
    if(targetLevel===4){ p.houseHP=9999; addLog(`${p.name} 建成了无限庇护所!`); }
    else { p.houseHP=targetLevel; addLog(`${p.name} 房屋升级到 ${targetLevel} 级!`); }
    renderDelayed();
    return true;
}

// ---- 夜间事件 ----
function nightEventsForPlayer(p){
    if(!p.alive) return;
    let summary='';
    let snakeProb=0.20; if(p.houseLevel>0) snakeProb=0.10; if(p.hasStoneSword&&p.houseLevel>0) snakeProb=0.05;
    if(randChance(snakeProb)){ p.hasPoison=true; summary+='🐍 蛇咬中毒;'; addLog(`${p.name} 被蛇咬,中毒!`); } else { summary+='无蛇;'; }
    let wind=randChance(0.50);
    if(wind && p.houseLevel>0 && p.houseLevel!==4){
        p.houseHP--;
        if(p.houseHP<=0){ summary+='💨 刮风,房屋受损摧毁;'; addLog(`${p.name} 房屋被摧毁(今晚保护仍在)。`); }
        else { summary+='💨 刮风,房屋受损;'; addLog(`${p.name} 房屋受损,剩余血量 ${p.houseHP}`); }
    } else if(wind && p.houseLevel===4){ summary+='💨 刮风,庇护所无损;'; addLog(`${p.name} 无限庇护所抵挡了大风。`); }
    else if(wind && p.houseLevel===0){ summary+='💨 刮风,无房屋;'; addLog(`${p.name} 遭遇大风,没有房屋。`); }
    else { summary+='无风;'; }
    if(wind){
        let coldProb=(p.houseLevel>0)?0.05:0.50;
        if(randChance(coldProb)){ p.hasCold=true; p.canActMorning=false; summary+='🤧 感冒;'; addLog(`${p.name} 感冒了!`); }
        else { summary+='未感冒;'; }
    } else { summary+='不会感冒;'; }
    if(p.houseLevel>0 && p.houseLevel!==4 && p.houseHP<=0){ p.houseLevel=0; p.houseHP=0; summary+='房屋已毁。'; addLog(`${p.name} 房屋彻底损毁。`); }
    p.nightSummary=summary;
}

// ---- 检查是否全部死亡 ----
function checkAllDead(){
    if(game.mode==='double'){
        let allDead = game.players.every(p => !p.alive);
        if(allDead){
            game.gameOver = true;
            game.gameWin = false;
            addLog('所有玩家都死亡了... 游戏结束。');
            renderImmediate();
            return true;
        }
    }
    return false;
}

// ---- 模态弹窗(中毒提醒) ----
function showPoisonModal(p, callback) {
    if (game.modalActive) return;
    game.modalActive = true;
    // 创建遮罩
    const overlay = document.createElement('div');
    overlay.className = 'modal-overlay';
    overlay.id = 'poisonModalOverlay';
    overlay.innerHTML = `
        <div class="modal-box">
            <h3>⚠️ 你已中毒!</h3>
            <p>你身中蛇毒,若不立即服药,可能会死!</p>
            <p>你有 ${p.medicine} 片普通药片,${p.expiredMeds} 片过期药。</p>
            <div>
                <button id="modalTakeMedicine" class="primary">💊 吃药</button>
                <button id="modalIgnore" class="danger">忽略</button>
            </div>
        </div>
    `;
    document.body.appendChild(overlay);
    // 监听按钮点击
    document.getElementById('modalTakeMedicine').addEventListener('click', function() {
        // 吃药逻辑:优先普通药
        if (p.medicine > 0) {
            takeMedicine(p);
        } else if (p.expiredMeds > 0) {
            takeExpiredMedicine(p);
        } else {
            // 理论上不会进入这里,因为弹窗只在有药时弹出
            addLog('没有可用药物!');
        }
        // 移除弹窗
        document.body.removeChild(overlay);
        game.modalActive = false;
        // 不结束早上,让玩家继续操作
        callback(false); // false 表示不结束早上
    });
    document.getElementById('modalIgnore').addEventListener('click', function() {
        document.body.removeChild(overlay);
        game.modalActive = false;
        callback(true); // true 表示结束早上
    });
}

// ---- 渲染与延迟刷新 ----
let renderTimer = null;
function renderDelayed(){
    if(renderTimer) clearTimeout(renderTimer);
    renderTimer = setTimeout(() => {
        renderTimer = null;
        render();
    }, 2000);
}
function renderImmediate(){ if(renderTimer){ clearTimeout(renderTimer); renderTimer=null; } render(); }

function render(){
    const container=document.getElementById('gameContainer');
    if(!container) return;
    if(game.gameOver){
        container.innerHTML=`
            <div style="text-align:center;padding:40px;background:#1f2f1f;border-radius:16px;border:1px solid #4a6a4a;">
                <h2 style="color:${game.gameWin?'#d8d88a':'#ff7a7a'}">${game.gameWin?'🎉 恭喜逃离蛇岛!':'💀 游戏结束'}</h2>
                <p>${game.gameWin?'你成功造出船只,离开了荒岛!':'所有玩家未能存活下来...'}</p>
                <button onclick="resetGame()" style="padding:8px 30px;background:#5a7a3a;border:1px solid #8aaa6a;color:#eee;border-radius:6px;cursor:pointer;font-size:1.1rem;">重新开始</button>
            </div>
        `;
        return;
    }

    let html=`<div class="game-area">`;
    const players=game.players;
    for(let i=0;i<players.length;i++){
        const p=players[i];
        const isActive=(game.mode==='double'&&game.currentPlayerIndex===i)||(game.mode==='single');
        const deadClass = p.alive ? '' : 'dead';
        // 集水器数量
        const collectorCount = p.waterCollectorTimers.length;
        html+=`<div class="player-panel ${deadClass}" style="${isActive?'border-color:#b8d88a;':''}">
            <h2>${p.name} ${isActive?'👤':''} ${!p.alive?'<span class="dead-tag">💀 已死亡</span>':''}</h2>
            <div class="status-grid">
                <span class="label">饱食度</span><span class="value">${p.satiety}/5</span>
                <span class="label">水分</span><span class="value">${p.water}/5</span>
                <span class="label">食物</span><span class="value">${p.food}</span>
                <span class="label">水瓶/空瓶</span><span class="value">${p.waterBottles}/${p.emptyBottles}</span>
                <span class="label">药片(普通)</span><span class="value">${p.medicine}</span>
                <span class="label">过期药</span><span class="value">${p.expiredMeds}</span>
                <span class="label">等级</span><span class="value">LV${p.level} (${p.exp}exp)</span>
                <span class="label">树叶/树枝</span><span class="value">${p.leaves}/${p.sticks}</span>
                <span class="label">石头/绳子</span><span class="value">${p.stones}/${p.rope}</span>
                <span class="label">布/铆钉</span><span class="value">${p.cloth}/${p.nails}</span>
                <span class="label">鱼/椰子</span><span class="value">${p.fish}/${p.coconuts}</span>
                <span class="label">椰汁(瓶)</span><span class="value">${p.coconutJuice}</span>
                <span class="label">宝箱/钥匙</span><span class="value">${p.chests}/${p.keys}</span>
                <span class="label">集水器</span><span class="value">${collectorCount} (上限 ${2*p.level})</span>
                <span class="label">房屋</span><span class="value">${p.houseLevel===0?'无':p.houseLevel===4?'∞':p.houseLevel+'级'}</span>
                <span class="label">房屋血量</span><span class="value">${p.houseLevel===4?'∞':p.houseHP}</span>
            </div>
            <div style="margin:5px 0;">
                ${p.hasPoison?'<span class="debuff">【中毒】</span>':''}
                ${p.hasCold?'<span class="debuff">【感冒】</span>':''}
            </div>
            <div class="tool-list">
                ${p.hasStoneSpear?'<span class="tool-tag">石矛</span>':''}
                ${p.hasPickaxe?'<span class="tool-tag">石镐</span>':''}
                ${p.hasStoneAxe?'<span class="tool-tag">石斧</span>':''}
                ${p.hasStoneSword?'<span class="tool-tag">石剑</span>':''}
                ${p.hasShoes?'<span class="tool-tag">鞋</span>':''}
                ${p.hasFishingRod?'<span class="tool-tag">鱼竿</span>':''}
                ${p.hasSlingshot?'<span class="tool-tag">弹弓</span>':''}
            </div>
            ${p.nightSummary?`<div class="night-summary">${p.nightSummary}</div>`:''}
        </div>`;
    }
    html+=`</div>`;

    html+=`<div class="action-panel">`;
    const phase=game.phaseNames[game.currentPhase];
    const phaseInfo=`第 ${game.day} 天 · ${phase}`;
    html+=`<div class="phase-title">${phaseInfo}</div>`;
    if(game.mode==='double'){
        const p=game.players[game.currentPlayerIndex];
        html+=`<div style="color:#b8d88a;">当前操作: ${p.name} ${p.alive?'':'(已死亡,无法操作)'}</div>`;
    }
    const curP=game.players[game.mode==='double'?game.currentPlayerIndex:0];
    html+=`<div style="font-size:0.9rem;color:#9ab89a;">饱食:${curP.satiety} 水:${curP.water} 食物:${curP.food} 水瓶:${curP.waterBottles}</div>`;

    html+=`<div class="action-buttons">`;
    const phaseIdx=game.currentPhase;
    const disabled = !curP.alive ? 'disabled' : '';
    if(phaseIdx===0){ // 早上
        html+=`<button onclick="doAction('eat')" ${disabled}>吃</button>`;
        html+=`<button onclick="doAction('drink')" ${disabled}>喝</button>`;
        html+=`<button onclick="doAction('medicine')" ${disabled}>吃药</button>`;
        html+=`<button onclick="doAction('expired')" ${disabled}>过期药</button>`;
        html+=`<button onclick="doAction('drinkJuice')" ${disabled}>喝椰汁</button>`;
        html+=`<button onclick="doAction('eatFish')" ${disabled}>吃鱼</button>`;
        html+=`<button onclick="doAction('eatCoconut')" ${disabled}>吃椰子</button>`;
        html+=`<button onclick="doAction('openCoconut')" ${disabled}>开椰子</button>`;
        html+=`<button onclick="doAction('craft')" ${disabled}>制作</button>`;
        html+=`<button onclick="doAction('openChest')" ${disabled}>开宝箱</button>`;
        // 结束早上按钮触发中毒弹窗
        html+=`<button class="primary" onclick="doAction('endMorning')">结束早上</button>`;
    } else if(phaseIdx===2){ // 中午
        html+=`<button onclick="doAction('eat')" ${disabled}>吃</button>`;
        html+=`<button onclick="doAction('drink')" ${disabled}>喝</button>`;
        html+=`<button onclick="doAction('drinkJuice')" ${disabled}>喝椰汁</button>`;
        html+=`<button onclick="doAction('eatFish')" ${disabled}>吃鱼</button>`;
        html+=`<button onclick="doAction('eatCoconut')" ${disabled}>吃椰子</button>`;
        html+=`<button onclick="doAction('openCoconut')" ${disabled}>开椰子</button>`;
        html+=`<button onclick="doAction('craft')" ${disabled}>制作</button>`;
        html+=`<button onclick="doAction('openChest')" ${disabled}>开宝箱</button>`;
        html+=`<button class="primary" onclick="doAction('endPhase')">结束中午</button>`;
    } else if(phaseIdx===4){ // 晚上
        html+=`<button onclick="doAction('eat')" ${disabled}>吃</button>`;
        html+=`<button onclick="doAction('drink')" ${disabled}>喝</button>`;
        html+=`<button onclick="doAction('medicine')" ${disabled}>吃药</button>`;
        html+=`<button onclick="doAction('expired')" ${disabled}>过期药</button>`;
        html+=`<button onclick="doAction('drinkJuice')" ${disabled}>喝椰汁</button>`;
        html+=`<button onclick="doAction('eatFish')" ${disabled}>吃鱼</button>`;
        html+=`<button onclick="doAction('eatCoconut')" ${disabled}>吃椰子</button>`;
        html+=`<button onclick="doAction('openCoconut')" ${disabled}>开椰子</button>`;
        html+=`<button onclick="doAction('craft')" ${disabled}>制作</button>`;
        html+=`<button onclick="doAction('buildHouse')" ${disabled}>建房</button>`;
        html+=`<button onclick="doAction('openChest')" ${disabled}>开宝箱</button>`;
        html+=`<button class="primary" onclick="doAction('endPhase')">结束晚上</button>`;
    } else if(phaseIdx===1){ // 上午
        if(!curP.alive){
            html+=`<span style="color:#ff7a7a;">该玩家已死亡,无法行动</span>`;
        } else if(!curP.hasCold || curP.canActMorning){
            html+=`<button onclick="doAction('collectLeaves')">捡树叶</button>`;
            html+=`<button onclick="doAction('collectSticks')">捡树枝</button>`;
            if(curP.canCollectStones()) html+=`<button onclick="doAction('collectStones')">捡石头</button>`;
            if(curP.canFish()) html+=`<button onclick="doAction('fish')">钓鱼</button>`;
            html+=`<button onclick="doAction('hunt')" style="background:#7a5a3a;">打野</button>`;
            html+=`<button onclick="doAction('craft')">制作</button>`;
        } else {
            html+=`<span style="color:#ff7a7a;">感冒了,上午无法行动</span>`;
        }
        html+=`<button class="primary" onclick="doAction('endPhase')">结束上午</button>`;
    } else if(phaseIdx===3){ // 下午
        if(!curP.alive){
            html+=`<span style="color:#ff7a7a;">该玩家已死亡,无法行动</span>`;
        } else {
            html+=`<button onclick="doAction('collectLeaves')">捡树叶</button>`;
            html+=`<button onclick="doAction('collectSticks')">捡树枝</button>`;
            if(curP.canCollectStones()) html+=`<button onclick="doAction('collectStones')">捡石头</button>`;
            if(curP.canFish()) html+=`<button onclick="doAction('fish')">钓鱼</button>`;
            html+=`<button onclick="doAction('craft')">制作</button>`;
        }
        html+=`<button class="primary" onclick="doAction('endPhase')">结束下午</button>`;
    }
    html+=`</div>`;

    if(game.showingCraft){
        const p=curP;
        const list=showCraftMenu(p);
        html+=`<div class="craft-menu show">`;
        list.forEach(item=>{
            html+=`<button onclick="doAction('craftItem', ${item.id})" ${!p.alive?'disabled':''}>${item.name} (${item.cost})</button>`;
        });
        html+=`<button onclick="doAction('closeCraft')">关闭</button>`;
        html+=`</div>`;
    }

    html+=`<div class="log-area" id="logContainer">`;
    for(let i=Math.max(0,game.logs.length-20); i<game.logs.length; i++){
        html+=`<div class="log-entry">${game.logs[i]}</div>`;
    }
    html+=`</div>`;
    html+=`</div>`;
    container.innerHTML=html;

    const logContainer = document.getElementById('logContainer');
    if(logContainer) logContainer.scrollTop = logContainer.scrollHeight;
}

// ---- 动作分发 ----
function doAction(action, param){
    if(game.gameOver) return;
    const p=game.players[game.mode==='double'?game.currentPlayerIndex:0];

    // 处理早上结束(特殊逻辑:检查中毒弹窗)
    if(action === 'endMorning'){
        // 检查当前玩家是否中毒且有药
        if(p.alive && p.hasPoison && (p.medicine > 0 || p.expiredMeds > 0)){
            // 弹出模态框,回调中决定是否结束早上
            showPoisonModal(p, function(shouldEnd) {
                if(shouldEnd){
                    // 忽略,正常结束早上
                    finishActionPhase();
                    renderImmediate();
                } else {
                    // 吃药后不结束早上,继续早上循环
                    renderImmediate();
                }
            });
            return; // 不继续执行
        } else {
            // 无中毒或无药,直接结束早上
            finishActionPhase();
            renderImmediate();
            return;
        }
    }

    if(action==='endPhase'){
        finishActionPhase();
        renderImmediate();
        return;
    }
    if(action==='closeCraft'){
        game.showingCraft=false;
        renderImmediate();
        return;
    }
    if(action==='craftItem'){
        craftItem(p, param);
        game.showingCraft=false;
        if(game.currentPhase===1||game.currentPhase===3) finishActionPhase();
        return;
    }

    let shouldEndPhase=false;
    switch(action){
        case 'eat': eat(p); break;
        case 'drink': drinkWater(p); break;
        case 'medicine': takeMedicine(p); break;
        case 'expired': takeExpiredMedicine(p); break;
        case 'drinkJuice': drinkCoconutJuice(p); break;
        case 'eatFish': eatFish(p); break;
        case 'eatCoconut': eatCoconut(p); break;
        case 'openCoconut': openCoconut(p); break;
        case 'openChest': openChest(p); break;
        case 'collectLeaves': collectLeaves(p); shouldEndPhase=true; break;
        case 'collectSticks': collectSticks(p); shouldEndPhase=true; break;
        case 'collectStones': collectStones(p); shouldEndPhase=true; break;
        case 'fish': fish(p); shouldEndPhase=true; break;
        case 'hunt':
            hunt(p);
            if(!game.gameOver){
                if(game.mode==='double'){ game.playerDone[0]=true; game.playerDone[1]=true; }
                game.currentPhase=4;
                game.playerDone=[false,false];
                game.currentPlayerIndex=0;
                renderImmediate();
                return;
            }
            break;
        case 'craft':
            game.showingCraft=!game.showingCraft;
            renderImmediate();
            return;
        case 'buildHouse':
            let levels=[];
            if(p.houseLevel<1) levels.push(1);
            if(p.houseLevel<2) levels.push(2);
            if(p.houseLevel<3) levels.push(3);
            if(p.houseLevel<4) levels.push(4);
            if(levels.length===0){ addLog('已最高级。'); renderImmediate(); return; }
            let target=prompt('选择建造等级: '+levels.join(', '));
            if(target===null){ renderImmediate(); return; }
            let t=parseInt(target);
            if(isNaN(t)||!levels.includes(t)){ addLog('无效等级。'); renderImmediate(); return; }
            buildHouse(p, t);
            return;
        default: return;
    }

    if(p.checkDeath()){
        p.alive=false;
        addLog(`${p.name} 死亡...`);
        if(checkAllDead()){
            renderImmediate();
            return;
        }
        if(game.mode==='double'){
            let next = (game.currentPlayerIndex + 1) % 2;
            while(!game.players[next].alive && next !== game.currentPlayerIndex){
                next = (next + 1) % 2;
            }
            if(game.players[next].alive){
                game.currentPlayerIndex = next;
            }
        }
        renderDelayed();
        return;
    }

    if(p.checkWin()){
        game.gameWin=true; game.gameOver=true;
        addLog(`${p.name} 造出船只,逃离蛇岛!`);
        renderImmediate();
        return;
    }

    if(shouldEndPhase && (game.currentPhase===1||game.currentPhase===3)){
        finishActionPhase();
    }
}

// ---- 阶段完成 ----
function finishActionPhase(){
    if(game.mode==='double'){
        game.playerDone[game.currentPlayerIndex]=true;
        let allDone = true;
        for(let i=0;i<game.players.length;i++){
            if(game.players[i].alive && !game.playerDone[i]){
                allDone = false;
                break;
            }
        }
        if(allDone){
            advancePhase();
        } else {
            let next = (game.currentPlayerIndex + 1) % 2;
            while(!game.players[next].alive && next !== game.currentPlayerIndex){
                next = (next + 1) % 2;
            }
            if(game.players[next].alive){
                game.currentPlayerIndex = next;
            } else {
                advancePhase();
            }
        }
    } else {
        advancePhase();
    }
    game.showingCraft=false;
}

// ---- 阶段推进 ----
function advancePhase(){
    game.playerDone=[false,false];
    game.currentPlayerIndex=0;

    let oldPhase=game.currentPhase;
    let newPhase=oldPhase+1;
    if(newPhase>4) newPhase=0;

    if(oldPhase===0){
        for(let p of game.players){
            if(p.alive && p.hasPoison){
                p.alive=false;
                addLog(`${p.name} 因蛇毒发作死亡...`);
            }
        }
        if(checkAllDead()){
            renderImmediate();
            return;
        }
    } else if(oldPhase===1){
        for(let p of game.players){
            if(p.alive){
                p.satiety--; p.water--;
                if(p.checkDeath()){
                    p.alive=false;
                    addLog(`${p.name} 因饥饿/干渴死亡...`);
                }
            }
        }
        if(checkAllDead()){
            renderImmediate();
            return;
        }
        addLog('上午结束,所有玩家饱食度-1,水分-1。');
    } else if(oldPhase===3){
        for(let p of game.players){
            if(p.alive){
                p.satiety--; p.water--;
                if(p.checkDeath()){
                    p.alive=false;
                    addLog(`${p.name} 因饥饿/干渴死亡...`);
                }
            }
        }
        if(checkAllDead()){
            renderImmediate();
            return;
        }
        addLog('下午结束,所有玩家饱食度-1,水分-1。');
    } else if(oldPhase===4){
        for(let p of game.players){
            if(p.alive){
                nightEventsForPlayer(p);
                if(p.checkDeath()){
                    p.alive=false;
                    addLog(`${p.name} 死亡...`);
                }
            }
        }
        if(checkAllDead()){
            renderImmediate();
            return;
        }
        game.day++;
        // 多个集水器独立计时
        for(let p of game.players){
            if(p.alive){
                for(let i=0; i<p.waterCollectorTimers.length; i++){
                    p.waterCollectorTimers[i]++;
                    if(p.waterCollectorTimers[i] >= 3){
                        p.waterBottles++;
                        addLog(`${p.name} 的一个集水器产出一瓶水。`);
                        p.waterCollectorTimers[i] = 0;
                    }
                }
            }
        }
    }

    game.currentPhase=newPhase;
    if(game.mode==='double'){
        let firstAlive = game.players.findIndex(p => p.alive);
        if(firstAlive !== -1) game.currentPlayerIndex = firstAlive;
    }
    if(!game.gameOver) renderImmediate();
}

// ---- 初始化 ----
function initGame(mode){
    game.mode=mode;
    game.day=1;
    game.currentPhase=0;
    game.currentPlayerIndex=0;
    game.playerDone=[false,false];
    game.gameOver=false;
    game.gameWin=false;
    game.logs=[];
    game.showingCraft=false;
    game.modalActive=false;

    if(mode==='single'){
        game.players=[new Player('你')];
    } else {
        game.players=[new Player('玩家1'), new Player('玩家2')];
    }

    document.getElementById('modeSingle').className=(mode==='single')?'active':'';
    document.getElementById('modeDouble').className=(mode==='double')?'active':'';
    document.getElementById('modeLabel').textContent=mode==='single'?'单人':'双人';

    renderImmediate();
    addLog('游戏开始!祝你生存下去。');
}

function resetGame(){ initGame(game.mode); }

// ---- 事件绑定 ----
document.addEventListener('DOMContentLoaded', function(){
    document.getElementById('modeSingle').addEventListener('click', function(){
        if(game.mode!=='single') initGame('single');
    });
    document.getElementById('modeDouble').addEventListener('click', function(){
        if(game.mode!=='double') initGame('double');
    });
    initGame('single');
});
</script>
</body>
</html>