Spaces:
Running
Running
File size: 51,271 Bytes
8062c1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 |
document.addEventListener('DOMContentLoaded', () => {
// --- Глобальные переменные состояния игры ---
let playerMoney = 150; // Start with slightly more money
let squad = []; // Массив объектов ковбоев в отряде
let availableCowboys = []; // Ковбои, доступные для найма
let shopItems = []; // Предметы в магазине
let availablePlans = []; // Доступные планы ограблений
let currentPlan = null; // Выбранный план
let currentRobberyEvent = null; // Текущее событие в ограблении
let currentRobberyProgress = 0; // Условный прогресс (можно расширить)
let gameState = 'hire'; // Текущая фаза: hire, equip, plan, robbery, results, gameover
// --- Элементы DOM ---
const moneyEl = document.getElementById('money');
const squadStatsEl = document.getElementById('squad-stats');
const equipmentSummaryEl = document.getElementById('equipment-summary');
const mainContentEl = document.getElementById('main-content');
const hireListEl = document.getElementById('hire-list');
const shopListEl = document.getElementById('shop-list');
const squadManageListEl = document.getElementById('squad-manage-list');
const planListEl = document.getElementById('plan-list');
const squadHealthDisplayEl = document.getElementById('squad-health-display');
const eventDescriptionEl = document.getElementById('event-description');
const choicesEl = document.getElementById('choices');
const rollResultDisplayEl = document.getElementById('roll-result-display');
const resultMessageEl = document.getElementById('result-message');
const xpGainedEl = document.getElementById('xp-gained');
const gameOverEl = document.getElementById('game-over');
// --- Кнопки навигации/действий ---
const goToEquipmentBtn = document.getElementById('go-to-equipment-btn');
const goToPlanBtn = document.getElementById('go-to-plan-btn');
const continueGameBtn = document.getElementById('continue-game-btn');
const newGameBtn = document.getElementById('new-game-btn');
const restartGameBtn = document.getElementById('restart-game-btn'); // Из GameOver
// --- Игровые Константы и Настройки ---
const cowboyNames = ["Джед", "Билли", "Сэм", "Клэй", "Дасти", "Хосе", "Уайетт", "Док", "Барт", "Коул"]; // Added more names
const cowboyStats = ["strength", "agility", "marksmanship", "charisma"]; // Сила, Ловкость, Меткость, Харизма
const itemNames = ["Хороший Револьвер", "Винтовка", "Динамит", "Аптечка", "Отмычки", "Бронежилет", "Шляпа Удачи"]; // Added another item
const planNames = ["Ограбление Почтового Вагона", "Нападение на Мосту", "Засада в Каньоне", "Тихое Проникновение"]; // Added another plan
const MAX_LEVEL = 10;
const XP_PER_LEVEL = 100;
const DICE_SIDES = 10; // Используем D10 для проверок
// --- Основные Функции Игры ---
function initGame() {
playerMoney = 300; // Начальные деньги
squad = [];
availableCowboys = [];
shopItems = [];
availablePlans = [];
currentPlan = null;
currentRobberyEvent = null;
currentRobberyProgress = 0;
gameState = 'hire';
gameOverEl.style.display = 'none';
mainContentEl.style.display = 'block';
generateInitialData();
updateStatusBar();
switchPhase('hire'); // Ensure starting phase is rendered correctly
}
function generateInitialData() {
// Генерируем ковбоев для найма
availableCowboys = [];
for (let i = 0; i < 5; i++) {
availableCowboys.push(generateCowboy());
}
// Генерируем предметы для магазина
shopItems = [];
for (let i = 0; i < 4; i++) {
// Ensure variety by removing chosen names temporarily
let tempItemNames = [...itemNames];
const itemName = tempItemNames.splice(Math.floor(Math.random() * tempItemNames.length), 1)[0];
shopItems.push(generateItem(itemName));
}
// Add specific items if desired
if (!shopItems.some(item => item.name === "Аптечка")) {
shopItems.push(generateItem("Аптечка"));
}
if (!shopItems.some(item => item.name === "Динамит")) {
shopItems.push(generateItem("Динамит"));
}
// Генерируем планы
availablePlans = [];
let tempPlanNames = [...planNames];
for (let i = 0; i < 3; i++) {
if (tempPlanNames.length === 0) break; // Avoid errors if not enough names
const planName = tempPlanNames.splice(Math.floor(Math.random() * tempPlanNames.length), 1)[0];
availablePlans.push(generatePlan(planName, i));
}
}
// --- Генерация Случайных Данных ---
function generateCowboy() {
// Generate a 3-character alphanumeric suffix
const randomSuffix = Math.random().toString(36).substring(2, 5);
const name = cowboyNames[Math.floor(Math.random() * cowboyNames.length)] + " " + randomSuffix; // Example: "Уайетт 5xs"
const stats = {};
let totalStatPoints = 10 + Math.floor(Math.random() * 12); // Slightly wider range
cowboyStats.forEach(stat => stats[stat] = 1);
totalStatPoints -= cowboyStats.length;
while (totalStatPoints > 0) {
stats[cowboyStats[Math.floor(Math.random() * cowboyStats.length)]]++;
totalStatPoints--;
}
const level = 1;
const xp = 0;
const maxHealth = 50 + stats.strength * 5 + Math.floor(Math.random() * 15); // Slightly higher health potential
// Adjusted cost calculation
const cost = 20 + Object.values(stats).reduce((a, b) => a + b, 0) * 3 + Math.floor(maxHealth / 8);
return {
id: Date.now() + Math.random(),
name: name,
stats: stats,
health: maxHealth,
maxHealth: maxHealth,
level: level,
xp: xp,
cost: cost,
weapon: null,
equipment: []
};
}
function generateItem(baseItemName = null) {
// If no name provided, pick one randomly
const baseItem = baseItemName || itemNames[Math.floor(Math.random() * itemNames.length)];
let cost = 15 + Math.floor(Math.random() * 50);
let type = "equipment";
let effect = {};
switch (baseItem) {
case "Хороший Револьвер":
effect = { marksmanship_bonus: 2 + Math.floor(Math.random() * 3) }; // 2-4
type = "weapon";
cost += 25;
break;
case "Винтовка":
effect = { marksmanship_bonus: 4 + Math.floor(Math.random() * 4) }; // 4-7
type = "weapon";
cost += 45;
break;
case "Динамит":
effect = { demolition_chance: 0.25 }; // +25%
type = "consumable";
cost += 20;
break;
case "Аптечка":
effect = { health: 25 + Math.floor(Math.random() * 16) }; // 25-40 health
type = "consumable";
cost += 15;
break;
case "Отмычки":
effect = { lockpick_chance: 0.20 }; // +20%
type = "equipment";
cost += 30;
break;
case "Бронежилет":
effect = { damage_reduction: 0.15 }; // 15% reduction
type = "equipment";
cost += 40;
break;
case "Шляпа Удачи":
effect = { charisma_bonus: 1, luck_bonus: 0.05 }; // +1 Charisma, +5% general luck (need to implement luck)
type = "equipment";
cost += 25;
break;
}
return {
id: Date.now() + Math.random(),
name: baseItem,
type: type,
effect: effect,
cost: cost
};
}
function generatePlan(planName, index) {
const difficulty = 40 + Math.floor(Math.random() * 60) + index * 15; // Base difficulty + random + index scaling
const potentialReward = 80 + Math.floor(Math.random() * 120) + difficulty * 2.5; // More varied reward
// Simplified event structure - can be vastly expanded
const events = [
{
description: `Подход к поезду (${planName}). Охрана патрулирует. Ваши действия?`,
choices: [
{ text: "Прокрасться мимо (Ловкость)", requiredStat: "agility", difficulty: difficulty * 0.9 },
{ text: "Создать отвлекающий шум (Харизма)", requiredStat: "charisma", difficulty: difficulty * 1.0 },
{ text: "Устранить охранника тихо (Меткость?)", requiredStat: "marksmanship", difficulty: difficulty * 1.1 }, // Risky use of Marksmanship
],
successReward: { progress: 1 },
failurePenalty: { health: 10 }
},
{
description: "Нужно проникнуть в вагон с ценностями.",
choices: [
{ text: "Взломать замок (Ловкость + Отмычки?)", requiredStat: "agility", difficulty: difficulty * 1.0 },
{ text: "Выбить дверь (Сила)", requiredStat: "strength", difficulty: difficulty * 0.9 },
{ text: "Использовать Динамит? (Сила + Динамит?)", requiredStat: "strength", difficulty: difficulty * 0.7 } // Easier but noisy & uses item
],
successReward: { progress: 1, money: potentialReward * 0.1 }, // Small initial reward
failurePenalty: { health: 15, money: -15 }
},
{
description: "Внутри! Забрать добычу и быстро уходить!",
choices: [
{ text: "Хватать все и бежать (Ловкость)", requiredStat: "agility", difficulty: difficulty * 1.1 },
{ text: "Прикрывать отход (Меткость)", requiredStat: "marksmanship", difficulty: difficulty * 1.0 },
{ text: "Забаррикадировать дверь (Сила)", requiredStat: "strength", difficulty: difficulty * 1.0 }
],
successReward: { money: potentialReward * 0.9, xp: 50 + difficulty / 2, progress: 1 }, // Main reward + scaled XP
failurePenalty: { health: 25, money: -potentialReward * 0.3 }
}
];
return {
id: Date.now() + Math.random(),
name: planName,
description: `Сложность: ~${difficulty}, Награда: ~$${Math.round(potentialReward)}`,
baseDifficulty: difficulty,
potentialReward: Math.round(potentialReward),
events: events
};
}
function getRobberyEvent() {
if (currentPlan && currentPlan.events.length > currentRobberyProgress) {
return currentPlan.events[currentRobberyProgress];
}
return null;
}
// --- Обновление Интерфейса ---
function updateStatusBar() {
moneyEl.textContent = playerMoney;
const totalStats = { strength: 0, agility: 0, marksmanship: 0, charisma: 0 };
let equipmentText = [];
let consumableText = []; // Separate consumables
squad.forEach(cowboy => {
// Base stats
Object.keys(totalStats).forEach(stat => {
totalStats[stat] += cowboy.stats[stat] || 0; // Ensure stat exists
});
// Bonuses from weapon
if (cowboy.weapon) {
equipmentText.push(cowboy.weapon.name);
Object.keys(totalStats).forEach(stat => {
if (cowboy.weapon.effect[`${stat}_bonus`]) {
totalStats[stat] += cowboy.weapon.effect[`${stat}_bonus`];
}
});
}
// Bonuses from equipment and list consumables
cowboy.equipment.forEach(item => {
if (item.type === 'consumable') {
// Count consumables instead of just listing names once
let existing = consumableText.find(c => c.name === item.name);
if (existing) {
existing.count++;
} else {
consumableText.push({ name: item.name, count: 1 });
}
} else {
equipmentText.push(item.name); // Add other equipment names
}
// Apply stat bonuses from all equipment/consumables (if any)
Object.keys(totalStats).forEach(stat => {
if (item.effect && item.effect[`${stat}_bonus`]) {
totalStats[stat] += item.effect[`${stat}_bonus`];
}
});
});
});
// --- Updated line with emojis for total stats ---
squadStatsEl.innerHTML = `💪С ${totalStats.strength} ✨Л ${totalStats.agility} 🎯М ${totalStats.marksmanship} 😊Х ${totalStats.charisma}`;
// --- End of updated line --- (Used for spacing)
// Combine and display equipment/consumables summary
const uniqueEquipment = [...new Set(equipmentText)];
let summary = uniqueEquipment.slice(0, 2).join(', '); // Show max 2 permanent items
if (uniqueEquipment.length > 2) summary += '...';
if (consumableText.length > 0) {
// Format consumables as "Name(count)"
let consumableSummary = consumableText
.map(c => `${c.name}(${c.count})`)
.slice(0, 2) // Show max 2 types of consumables
.join(', ');
if (consumableText.length > 2) consumableSummary += '...';
summary += (summary ? ' / ' : '') + 'Расх: ' + consumableSummary;
}
equipmentSummaryEl.textContent = summary || "Ничего";
}
function updateSquadHealthDisplay() {
if (gameState !== 'robbery') {
squadHealthDisplayEl.innerHTML = '';
squadHealthDisplayEl.style.display = 'none'; // Hide if not in robbery
return;
}
squadHealthDisplayEl.style.display = 'block'; // Show if in robbery
squadHealthDisplayEl.innerHTML = "Здоровье отряда: ";
if (squad.length === 0) {
squadHealthDisplayEl.innerHTML += "Отряд пуст!";
return;
}
squad.forEach(cowboy => {
const healthSpan = document.createElement('span');
healthSpan.classList.add('cowboy-health');
// Calculate percentage for styling, handle division by zero
const healthPercent = cowboy.maxHealth > 0 ? (cowboy.health / cowboy.maxHealth) * 100 : 0;
healthSpan.textContent = `${cowboy.name}: ${cowboy.health}/${cowboy.maxHealth} HP`;
healthSpan.classList.remove('low-health', 'critical-health'); // Reset classes
if (healthPercent <= 20) {
healthSpan.classList.add('critical-health');
} else if (healthPercent <= 50) {
healthSpan.classList.add('low-health');
}
squadHealthDisplayEl.appendChild(healthSpan);
});
}
// --- Рендеринг Фаз Игры ---
function switchPhase(newPhase) {
console.log("Switching phase to:", newPhase);
// Hide all phases
document.querySelectorAll('#main-content > div[id^="phase-"]').forEach(div => div.style.display = 'none');
gameOverEl.style.display = 'none';
mainContentEl.style.display = 'block'; // Ensure main content is visible unless game over
gameState = newPhase;
// Show the target phase
const phaseEl = document.getElementById(`phase-${newPhase}`);
if (phaseEl) {
phaseEl.style.display = 'block';
// Call the corresponding render function
switch (newPhase) {
case 'hire': renderHirePhase(); break;
case 'equip': renderEquipPhase(); break;
case 'plan': renderPlanPhase(); break;
case 'robbery': renderRobberyPhase(); break;
case 'results': /* Handled by endRobbery calling renderResultsPhase */ break;
}
} else if (newPhase === 'gameover') {
gameOverEl.style.display = 'block';
mainContentEl.style.display = 'none'; // Hide main content on game over
} else {
console.error("Unknown phase:", newPhase);
}
updateStatusBar(); // Update status bar on every phase switch
updateSquadHealthDisplay(); // Update health display visibility
}
function renderHirePhase() {
hireListEl.innerHTML = ''; // Clear list
if (availableCowboys.length === 0) {
hireListEl.innerHTML = '<li>Нет доступных ковбоев для найма.</li>';
} else {
availableCowboys.forEach(cowboy => {
const li = document.createElement('li');
// --- Updated line with emojis ---
li.innerHTML = `
<div>
<b>${cowboy.name}</b> (Ур: ${cowboy.level}, Зд: ${cowboy.health}/${cowboy.maxHealth})<br>
Характеристики: 💪С ${cowboy.stats.strength} ✨Л ${cowboy.stats.agility} 🎯М ${cowboy.stats.marksmanship} 😊Х ${cowboy.stats.charisma}<br>
Цена: $${cowboy.cost}
</div>
<button data-cowboy-id="${cowboy.id}" ${playerMoney < cowboy.cost ? 'disabled' : ''}>Нанять</button>
`;
// --- End of updated line ---
li.querySelector('button').addEventListener('click', () => handleHire(cowboy.id));
hireListEl.appendChild(li);
});
}
// Disable "Go to Equipment" if no squad members
goToEquipmentBtn.disabled = squad.length === 0;
}
function renderEquipPhase() {
shopListEl.innerHTML = ''; // Clear shop
if (shopItems.length === 0) {
shopListEl.innerHTML = '<li>Магазин пуст.</li>';
} else {
shopItems.forEach(item => {
const li = document.createElement('li');
let effectDesc = Object.entries(item.effect)
.map(([key, value]) => `${key.replace('_bonus', '').replace('_chance', ' шанс').replace('health','здровье').replace('damage_reduction','сниж. урона')}: ${value * 100 % 1 === 0 && value < 1 && value > 0 ? (value*100)+'%' : value}`) // Format effects nicely
.join(', ');
li.innerHTML = `
<div>
<b>${item.name}</b> (${item.type === 'consumable' ? 'Расходуемый' : item.type === 'weapon' ? 'Оружие' : 'Снаряжение'})<br>
Эффект: ${effectDesc || 'Нет'}<br>
Цена: $${item.cost}
</div>
<button data-item-id="${item.id}" ${playerMoney < item.cost || squad.length === 0 ? 'disabled' : ''}>Купить</button>
`;
li.querySelector('button').addEventListener('click', () => handleBuy(item.id));
shopListEl.appendChild(li);
});
}
// Display squad for equipment management
squadManageListEl.innerHTML = '';
if (squad.length === 0) {
squadManageListEl.innerHTML = '<li>Ваш отряд пуст.</li>';
} else {
squad.forEach(cowboy => {
const li = document.createElement('li');
const equipmentList = cowboy.equipment.map(e => e.name).join(', ') || 'Нет';
li.innerHTML = `
<span><b>${cowboy.name}</b> (Зд: ${cowboy.health}/${cowboy.maxHealth}) Оружие: ${cowboy.weapon ? cowboy.weapon.name : 'Нет'}, Снаряжение: ${equipmentList}</span>
<!-- Basic 'Use Medkit' button -->
${cowboy.equipment.some(e => e.name === 'Аптечка' && cowboy.health < cowboy.maxHealth) ?
`<button class="use-item-btn" data-cowboy-id="${cowboy.id}" data-item-name="Аптечка">Исп. Аптечку</button>` : ''}
`;
// Add event listener for using items if button exists
const useButton = li.querySelector('.use-item-btn');
if (useButton) {
useButton.addEventListener('click', (e) => {
const cowboyId = e.target.getAttribute('data-cowboy-id');
const itemName = e.target.getAttribute('data-item-name');
handleUseItem(cowboyId, itemName);
});
}
squadManageListEl.appendChild(li);
});
}
// Disable "Go to Plan" if no squad members
goToPlanBtn.disabled = squad.length === 0;
}
function handleUseItem(cowboyId, itemName) {
const cowboy = squad.find(c => c.id == cowboyId); // Use == for type flexibility if needed, or === if strict
if (!cowboy) return;
const itemIndex = cowboy.equipment.findIndex(item => item.name === itemName && item.type === 'consumable');
if (itemIndex > -1) {
const item = cowboy.equipment[itemIndex];
let used = false;
if (item.name === "Аптечка" && cowboy.health < cowboy.maxHealth) {
const healAmount = item.effect.health || 25;
const actualHeal = Math.min(healAmount, cowboy.maxHealth - cowboy.health); // Don't overheal
cowboy.health += actualHeal;
used = true;
console.log(`${cowboy.name} использовал ${item.name}, восстановлено ${actualHeal} здоровья.`);
}
// Add other usable items here (e.g., Dynamite outside of combat?)
if (used) {
cowboy.equipment.splice(itemIndex, 1); // Remove used consumable
updateStatusBar();
renderEquipPhase(); // Re-render the equipment phase to show changes
}
}
}
function renderPlanPhase() {
planListEl.innerHTML = ''; // Clear plan list
if (availablePlans.length === 0) {
planListEl.innerHTML = '<li>Нет доступных планов для ограбления.</li>';
} else {
availablePlans.forEach(plan => {
const li = document.createElement('li');
li.innerHTML = `
<div>
<b>${plan.name}</b><br>
Описание: ${plan.description}<br>
</div>
<button data-plan-id="${plan.id}">Выбрать</button>
`;
li.querySelector('button').addEventListener('click', () => handleChoosePlan(plan.id));
planListEl.appendChild(li);
});
}
}
function renderRobberyPhase() {
currentRobberyEvent = getRobberyEvent();
updateSquadHealthDisplay();
rollResultDisplayEl.textContent = '';
if (squad.length === 0 && gameState === 'robbery') {
console.log("Game over triggered from renderRobberyPhase - squad empty");
setTimeout(() => switchPhase('gameover'), 500); // Delay slightly
return;
}
if (!currentRobberyEvent) {
console.log("No more events for this plan.");
// Check if we successfully completed the required progress
if (currentPlan && currentRobberyProgress >= currentPlan.events.length) {
endRobbery(true, `Ограбление "${currentPlan.name}" успешно завершено!`);
} else {
// Didn't finish all steps, might be considered a partial success or failure depending on logic
endRobbery(false, "Ограбление прервано или не завершено.");
}
return;
}
eventDescriptionEl.textContent = currentRobberyEvent.description;
choicesEl.innerHTML = ''; // Clear old choices
currentRobberyEvent.choices.forEach(choice => {
const button = document.createElement('button');
let bonusChanceText = '';
// Check for relevant items
if (choice.text.toLowerCase().includes('динамит') && squadHasItemType('consumable', 'Динамит')) {
bonusChanceText = ' (+Динамит)';
} else if (choice.text.toLowerCase().includes('взломать') && squadHasItemType('equipment', 'Отмычки')) {
bonusChanceText = ' (+Отмычки)';
} else if (choice.requiredStat === 'strength' && squadHasItemType('consumable', 'Динамит') && !bonusChanceText) {
// Generic dynamite bonus for strength if not explicitly mentioned
// bonusChanceText = ' (?+Динамит)'; // Maybe don't show if not obvious
}
button.innerHTML = `
${choice.text}
<span class="stat-requirement">(Проверка: ${choice.requiredStat}, Сложность: ${choice.difficulty})${bonusChanceText}</span>
`;
button.addEventListener('click', () => handleChoice(choice.requiredStat, choice.difficulty, choice));
choicesEl.appendChild(button);
});
}
function renderResultsPhase(success, message, xp) {
resultMessageEl.textContent = message;
xpGainedEl.textContent = xp;
// Ensure buttons are displayed correctly based on success AND if squad survived
const canContinue = success && squad.length > 0;
continueGameBtn.style.display = canContinue ? 'block' : 'none';
newGameBtn.style.display = 'block'; // Always allow starting a new game from results
// Make sure continue button is enabled/disabled correctly
continueGameBtn.disabled = !canContinue;
}
// --- Обработчики Действий ---
function handleHire(cowboyId) {
const cowboy = availableCowboys.find(c => c.id == cowboyId); // Use == for potential string/number mismatch
if (cowboy && playerMoney >= cowboy.cost) {
playerMoney -= cowboy.cost;
squad.push(cowboy);
availableCowboys = availableCowboys.filter(c => c.id != cowboyId);
console.log(`Нанят ${cowboy.name}`);
updateStatusBar();
renderHirePhase(); // Update hire list and buttons
renderEquipPhase(); // Also update squad list in equip phase if visible
} else if (cowboy) {
alert("Недостаточно денег!");
}
}
function handleBuy(itemId) {
const item = shopItems.find(i => i.id == itemId);
if (!item) return;
if (playerMoney < item.cost) {
alert("Недостаточно денег!");
return;
}
if (squad.length === 0) {
alert("Сначала наймите ковбоев, чтобы дать им снаряжение!");
return;
}
// --- More robust item assignment ---
let assigned = false;
if (item.type === 'weapon') {
// Try to give to someone without a weapon first
let targetCowboy = squad.find(c => !c.weapon);
if (targetCowboy) {
targetCowboy.weapon = item;
assigned = true;
console.log(`${item.name} выдан ${targetCowboy.name}`);
} else {
// If everyone has a weapon, replace the first cowboy's (simple logic)
// In a real game, you'd let the player choose or compare stats
squad[0].weapon = item;
assigned = true;
console.log(`${item.name} выдан ${squad[0].name} (заменил старое)`);
}
} else if (item.type === 'equipment' || item.type === 'consumable') {
// Find cowboy with fewest equipment items to distribute somewhat evenly
let targetCowboy = squad.reduce((prev, curr) => {
return (curr.equipment.length < prev.equipment.length) ? curr : prev;
});
targetCowboy.equipment.push(item);
assigned = true;
console.log(`${item.name} добавлен в снаряжение ${targetCowboy.name}`);
}
if (assigned) {
playerMoney -= item.cost;
// Remove *one* instance of the bought item from the shop
const itemIndexInShop = shopItems.findIndex(i => i.id == itemId);
if(itemIndexInShop > -1) {
shopItems.splice(itemIndexInShop, 1);
}
console.log(`Куплен ${item.name}`);
updateStatusBar();
renderEquipPhase(); // Update shop and squad display
} else {
alert("Не удалось назначить предмет."); // Should not happen with current logic if squad exists
}
}
function handleChoosePlan(planId) {
currentPlan = availablePlans.find(p => p.id == planId);
if (currentPlan) {
console.log(`Выбран план: ${currentPlan.name}`);
currentRobberyProgress = 0;
switchPhase('robbery');
}
}
function handleChoice(stat, difficulty, choiceData) {
console.log(`Выбрано действие: ${choiceData.text}, Проверка: ${stat}, Сложность: ${difficulty}`);
performCheck(stat, difficulty, choiceData);
}
function performCheck(stat, difficulty, choiceData) {
if (squad.length === 0) {
rollResultDisplayEl.textContent = "Нет отряда для выполнения действия!";
// Treat as failure?
handleFailure(choiceData, "Отряд пуст!");
return;
}
// Sum the required stat across the squad, including bonuses
let totalStatValue = squad.reduce((sum, cowboy) => {
let effectiveStat = cowboy.stats[stat] || 0;
if (cowboy.weapon && cowboy.weapon.effect && cowboy.weapon.effect[`${stat}_bonus`]) {
effectiveStat += cowboy.weapon.effect[`${stat}_bonus`];
}
cowboy.equipment.forEach(item => {
if (item.effect && item.effect[`${stat}_bonus`]) {
effectiveStat += item.effect[`${stat}_bonus`];
}
});
return sum + effectiveStat;
}, 0);
// Check for item-specific bonuses/effects mentioned in the choice
let bonusChance = 0; // Represents a multiplier bonus, e.g., 0.2 for +20%
let itemUsed = null; // Track if a consumable is used
if (choiceData.text.toLowerCase().includes('динамит') && squadHasItemType('consumable', 'Динамит')) {
const dynamite = findItemInSquad('consumable', 'Динамит'); // Find the item to get its effect
if(dynamite && dynamite.effect.demolition_chance) {
bonusChance += dynamite.effect.demolition_chance;
itemUsed = { type: 'consumable', name: 'Динамит' }; // Mark dynamite for removal
console.log("Используется Динамит! Бонус шанса: +", bonusChance * 100, "%");
}
} else if (choiceData.text.toLowerCase().includes('взломать') && squadHasItemType('equipment', 'Отмычки')) {
const lockpicks = findItemInSquad('equipment', 'Отмычки'); // Find the item
if(lockpicks && lockpicks.effect.lockpick_chance) {
bonusChance += lockpicks.effect.lockpick_chance; // Permanent bonus, item not used up
console.log("Используются Отмычки! Бонус шанса: +", bonusChance * 100, "%");
}
}
// Add luck bonus from items like "Шляпа Удачи" (needs implementation)
// bonusChance += squad.reduce((luck, c) => luck + (c.equipment.find(e => e.name === "Шляпа Удачи")?.effect.luck_bonus || 0), 0);
// Dice roll (D10)
const diceRoll = Math.floor(Math.random() * DICE_SIDES) + 1;
// Core check calculation: (Dice * Stat) compared to Difficulty
// Apply bonus chance multiplicatively to the *score* or additively to the *roll* - let's try multiplying score
const baseScore = diceRoll * totalStatValue;
const finalScore = baseScore * (1 + bonusChance); // Apply bonus multiplier
console.log(`Бросок D${DICE_SIDES}: ${diceRoll}, Суммарная характеристика (${stat}): ${totalStatValue}, Базовый результат: ${baseScore}, Модификатор шанса: ${(1 + bonusChance).toFixed(2)}x, Финальный результат: ${finalScore.toFixed(2)}, Сложность: ${difficulty}`);
rollResultDisplayEl.textContent = `Бросок: ${diceRoll} × ${totalStatValue} (${stat}) × ${(1 + bonusChance).toFixed(2)} (бонус) = ${finalScore.toFixed(2)} / Требуется: ${difficulty}`;
// Consume the item if it was marked
if (itemUsed) {
removeItemFromSquad(itemUsed.type, itemUsed.name); // Remove one instance
}
if (finalScore >= difficulty) {
console.log("Успех!");
handleSuccess(choiceData);
} else {
console.log("Провал!");
handleFailure(choiceData);
}
}
// Helper to check if *any* cowboy has an item
function squadHasItemType(type, name = null) {
return squad.some(cowboy =>
(cowboy.weapon && cowboy.weapon.type === type && (!name || cowboy.weapon.name === name)) ||
cowboy.equipment.some(item => item.type === type && (!name || item.name === name))
);
}
// Helper to find the first instance of an item in the squad (to get its effect details)
function findItemInSquad(type, name) {
for (let cowboy of squad) {
if (cowboy.weapon && cowboy.weapon.type === type && cowboy.weapon.name === name) {
return cowboy.weapon;
}
const item = cowboy.equipment.find(item => item.type === type && item.name === name);
if (item) {
return item;
}
}
return null;
}
// Helper to remove the *first* instance of a consumable item found in the squad
function removeItemFromSquad(type, name) {
for (let cowboy of squad) {
const itemIndex = cowboy.equipment.findIndex(item => item.type === type && item.name === name);
if (itemIndex > -1) {
cowboy.equipment.splice(itemIndex, 1);
console.log(`Предмет ${name} использован и удален у ${cowboy.name}`);
updateStatusBar(); // Update equipment summary
return true; // Item found and removed
}
}
console.log(`Предмет ${name} не найден в отряде для удаления.`);
return false; // Item not found
}
function handleSuccess(choiceData) {
const eventData = currentRobberyEvent;
if (!eventData) return; // Should not happen if called correctly
let message = "Успех! ";
let xpEarned = 0;
// Apply rewards
if (eventData.successReward) {
if (eventData.successReward.money) {
playerMoney += eventData.successReward.money;
message += `Получено $${eventData.successReward.money}. `;
}
if (eventData.successReward.xp) {
xpEarned = eventData.successReward.xp; // Track XP for awarding later
message += `Получено ${xpEarned} опыта. `;
}
if (eventData.successReward.progress) {
currentRobberyProgress += eventData.successReward.progress;
message += `Продвижение по плану. `;
}
// TODO: Add item rewards here
}
// Try to use a medkit automatically if someone is injured (optional QoL)
const injuredCowboy = squad.find(c => c.health < c.maxHealth);
if (injuredCowboy && squadHasItemType('consumable', 'Аптечка')) {
const medkit = findItemInSquad('consumable', 'Аптечка');
if (medkit && removeItemFromSquad('consumable', 'Аптечка')) { // Find and remove it
const healAmount = medkit.effect.health || 25;
const actualHeal = Math.min(healAmount, injuredCowboy.maxHealth - injuredCowboy.health);
injuredCowboy.health += actualHeal;
message += `${injuredCowboy.name} использовал аптечку (+${actualHeal} ЗД). `;
console.log(`${injuredCowboy.name} подлечился.`);
updateSquadHealthDisplay(); // Update health display immediately
}
}
awardXP(xpEarned); // Award XP gained from this step
updateStatusBar();
// Move to the next event or end the robbery
const nextEvent = getRobberyEvent();
rollResultDisplayEl.textContent += ` | ${message}`; // Append outcome to roll result
if (nextEvent && currentRobberyProgress < currentPlan.events.length) { // Check progress hasn't exceeded plan length
setTimeout(() => renderRobberyPhase(), 1500); // Pause before next step
} else {
// If progress >= length, it means the last step was successful
console.log("Reached end of plan events successfully.");
setTimeout(() => endRobbery(true, message + " Ограбление завершено!"), 1500);
}
}
function handleFailure(choiceData, failureReason = "Провал проверки!") {
const eventData = currentRobberyEvent;
if (!eventData) return;
let message = "Провал! " + failureReason + " ";
// Apply penalties
if (eventData.failurePenalty) {
if (eventData.failurePenalty.money) {
const moneyLost = Math.min(playerMoney, Math.abs(eventData.failurePenalty.money));
playerMoney -= moneyLost;
message += `Потеряно $${moneyLost}. `;
}
if (eventData.failurePenalty.health) {
const damage = eventData.failurePenalty.health;
message += `Отряд ранен! `;
let casualties = 0;
// Apply damage, check for deaths immediately
const remainingSquad = [];
squad.forEach(cowboy => {
if (applyDamage(cowboy, Math.ceil(damage / Math.max(1, squad.length)) + Math.floor(Math.random() * 5))) {
remainingSquad.push(cowboy); // Cowboy survived
} else {
casualties++; // Cowboy died
message += `${cowboy.name} погиб! `;
}
});
squad = remainingSquad; // Update squad with survivors
if (squad.length === 0) {
// Game Over scenario
rollResultDisplayEl.textContent += ` | ${message} Весь отряд погиб!`;
console.log("Game over triggered from handleFailure - squad wiped out.");
setTimeout(() => switchPhase('gameover'), 1500);
return; // Stop further processing
}
}
if (eventData.failurePenalty.progress && eventData.failurePenalty.progress < 0) {
currentRobberyProgress = Math.max(0, currentRobberyProgress + eventData.failurePenalty.progress); // Setback
message += `Продвижение отброшено назад. `;
}
// TODO: Add item loss penalty
}
updateStatusBar();
updateSquadHealthDisplay(); // Update health after damage/deaths
// Decide whether to continue or fail the robbery
// Simple: continue to next step even on failure, unless squad wiped out
const nextEvent = getRobberyEvent();
rollResultDisplayEl.textContent += ` | ${message}`;
if (nextEvent && currentRobberyProgress < currentPlan.events.length) { // Check if there are still events left
setTimeout(() => renderRobberyPhase(), 1500);
} else {
// Reached end of events after a failure, or no more events possible
console.log("Reached end of plan events after failure or squad wipeout.");
// Consider this a failed robbery outcome
setTimeout(() => endRobbery(false, message + " Ограбление провалено!"), 1500);
}
}
// Returns true if cowboy survives, false if they die
function applyDamage(cowboy, amount) {
if (!cowboy || amount <= 0) return true; // No damage or invalid cowboy
let damageTaken = amount;
const armor = cowboy.equipment.find(item => item.effect && item.effect.damage_reduction);
if (armor) {
damageTaken = Math.max(1, Math.round(amount * (1 - armor.effect.damage_reduction)));
console.log(`${cowboy.name} получил ${damageTaken} урона (снижено броней с ${amount})`);
} else {
console.log(`${cowboy.name} получил ${damageTaken} урона`);
}
cowboy.health -= damageTaken;
if (cowboy.health <= 0) {
console.log(`${cowboy.name} погиб!`);
return false; // Cowboy died
}
return true; // Cowboy survived
}
function awardXP(amount) {
if (squad.length === 0 || amount <= 0) return;
const xpPerCowboy = Math.floor(amount / squad.length);
if (xpPerCowboy <= 0) return; // Avoid awarding 0 XP
squad.forEach(cowboy => {
if (cowboy.level < MAX_LEVEL) {
cowboy.xp += xpPerCowboy;
console.log(`${cowboy.name} получил ${xpPerCowboy} опыта (Всего: ${cowboy.xp}).`);
// Check for level up
while (cowboy.xp >= XP_PER_LEVEL && cowboy.level < MAX_LEVEL) {
levelUp(cowboy);
}
}
});
// No status bar update needed here unless level up changes stats shown there directly
}
function levelUp(cowboy) {
cowboy.level++;
cowboy.xp -= XP_PER_LEVEL;
console.log(`%c${cowboy.name} достиг Уровня ${cowboy.level}!`, "color: green; font-weight: bold;");
// Improve stats: +1 random mandatory, +1 random optional based on luck/class?
const randomStat1 = cowboyStats[Math.floor(Math.random() * cowboyStats.length)];
cowboy.stats[randomStat1]++;
let levelUpMessage = `+1 ${randomStat1}`;
// Increase health
const healthIncrease = 8 + Math.floor(Math.random() * 8) + Math.floor(cowboy.stats.strength / 2); // 8-15 + strength bonus
cowboy.maxHealth += healthIncrease;
// Heal proportional to level up health gain
cowboy.health = Math.min(cowboy.maxHealth, cowboy.health + healthIncrease);
levelUpMessage += `, +${healthIncrease} Макс. Здоровья`;
console.log(` ${levelUpMessage}`);
updateStatusBar(); // Update stats if they changed
}
function endRobbery(success, finalMessage = "") {
let totalXP = 0;
let finalReward = 0; // Track monetary reward given *at the end*
if (!currentPlan) {
console.error("Ending robbery without a current plan!");
success = false; // Cannot succeed without a plan
finalMessage = finalMessage || "Ошибка: План ограбления не найден.";
} else if (success && currentRobberyProgress >= currentPlan.events.length) {
// Successfully completed ALL events
totalXP = currentPlan.baseDifficulty + Math.floor(currentPlan.potentialReward / 10); // XP for success + reward bonus
finalReward = currentPlan.potentialReward; // Assume full reward if not given incrementally
// Check if reward was already given on last step
const lastEvent = currentPlan.events[currentPlan.events.length - 1];
if(lastEvent && lastEvent.successReward && lastEvent.successReward.money && lastEvent.successReward.money >= finalReward * 0.8) {
// If last step gave most of the reward, don't add it again
console.log("Final reward likely given on last step, not adding full amount again.");
finalReward = 0; // Reset final reward
} else {
playerMoney += finalReward;
finalMessage = finalMessage || `Ограбление "${currentPlan.name}" успешно! Добыча: $${finalReward}`;
}
finalMessage += ` (Опыт: ${totalXP})`;
} else if (success) {
// Escaped early but successfully (partial success)
totalXP = Math.floor((currentPlan.baseDifficulty * currentRobberyProgress / currentPlan.events.length) * 0.8); // XP proportional to progress, slight penalty for leaving early
finalMessage = finalMessage || `Удалось уйти с частью добычи после ${currentRobberyProgress} этапов.`;
finalMessage += ` (Опыт: ${totalXP})`;
} else {
// Failure
totalXP = Math.floor((currentPlan.baseDifficulty * currentRobberyProgress / currentPlan.events.length) * 0.1); // Minimal XP for failure based on progress
finalMessage = finalMessage || "Ограбление провалено!";
finalMessage += ` (Опыт: ${totalXP})`;
}
// Ensure squad exists before awarding XP
if (squad.length > 0) {
awardXP(totalXP);
} else {
// If squad wiped out, ensure success is false
success = false;
finalMessage += " Отряд не выжил.";
}
console.log("Ограбление завершено. Итог:", success ? "Успех" : "Провал", "| Сообщение:", finalMessage, "| Опыт:", totalXP);
currentPlan = null; // Clear current plan after it's finished
currentRobberyProgress = 0;
switchPhase('results');
renderResultsPhase(success, finalMessage, totalXP);
}
// --- Навигация и Перезапуск ---
goToEquipmentBtn.addEventListener('click', () => {
if (squad.length > 0) {
switchPhase('equip');
} else {
alert("Сначала наймите хотя бы одного ковбоя!");
}
});
goToPlanBtn.addEventListener('click', () => {
if (squad.length > 0) {
switchPhase('plan');
} else {
alert("Нужно нанять хотя бы одного ковбоя!");
}
});
continueGameBtn.addEventListener('click', () => {
if (squad.length === 0) {
alert("Нельзя продолжить - отряд пуст! Начните новую игру.");
return;
}
// Heal surviving squad members partially
squad.forEach(cowboy => {
const healAmount = Math.max(10, Math.round(cowboy.maxHealth * 0.4)); // Heal 40% or 10 HP, whichever is more
cowboy.health = Math.min(cowboy.maxHealth, cowboy.health + healAmount);
});
// Generate new shop items and plans
generateInitialData(); // This regenerates *everything* except the squad
switchPhase('equip'); // Go back to the equipment phase
});
newGameBtn.addEventListener('click', initGame);
restartGameBtn.addEventListener('click', initGame); // Button from Game Over screen
// --- Запуск Игры ---
initGame();
}); |