source.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. //important variables
  2. let important = {
  3. actions: 10,
  4. colEnd: 4,
  5. gameOver: false,
  6. year: 100,
  7. defense: 0,
  8. population: 8,
  9. maxPop: 8,
  10. nation : 1,
  11. currentSec : 0,
  12. pause: false,
  13. menuOpen: false,
  14. pauseButton: false,
  15. timeSpeed : 1,
  16. timeBase : 15,
  17. pauseTime : 0,
  18. resume : 1,
  19. count : 0
  20. }
  21. const START_YEAR = 100
  22. /*var start = new Date().getTime();
  23. var elapsed = (new Date().getTime() - start) / 1000;
  24. if (elapsed >= 5) {
  25. endTurn()
  26. }*/
  27. window.onload = function() {
  28. /*var sec = Math.floor(Date.now()/1000)
  29. if (sec != important.currentSec) {
  30. important.currentSec = sec
  31. console.log(important.currentSec)
  32. }*/
  33. //setInterval(endTurn, 10000)
  34. setInterval(timeCount, 1000)
  35. }
  36. var time = 10
  37. function timeCount() {
  38. gatheringTimer()
  39. switch(important.timeSpeed) {
  40. case 1 : important.timeBase = 15
  41. break
  42. case 2 : important.timeBase = 7
  43. break
  44. case 3 : important.timeBase = 4
  45. break
  46. case 0 : important.timeBase = -1
  47. time = -1
  48. break
  49. }
  50. important.timeSpeed == 0 ? important.count = important.count : important.count++
  51. time--
  52. if (time == 0) {
  53. endTurn()
  54. time = important.timeBase
  55. }
  56. console.log(important.timeBase + ' || ' + time + ' || ' + important.count)
  57. }
  58. function gatheringTimer(){
  59. var fTime = tStart.food + gatherTime.food - important.count
  60. var waTime = tStart.water + gatherTime.water - important.count
  61. var woTime = tStart.wood + gatherTime.wood - important.count
  62. var sTime = tStart.stone + gatherTime.stone - important.count;
  63. fTime < 0 ? fTime = 0 : fTime = fTime
  64. waTime < 0 ? waTime = 0 : waTime = waTime
  65. woTime < 0 ? woTime = 0 : woTime = woTime
  66. sTime < 0 ? sTime = 0 : sTime = sTime
  67. document.getElementById('fTimer').innerHTML = '<i>'+ fTime + '</i>'
  68. document.getElementById('waTimer').innerHTML = '<i>' + waTime + '</i>'
  69. document.getElementById('woTimer').innerHTML = '<i>' + woTime + '</i>'
  70. document.getElementById('sTimer').innerHTML = '<i>' + sTime + '</i>'
  71. }
  72. function speedChange(speed) {
  73. important.timeSpeed = speed
  74. switch(important.timeSpeed) {
  75. case 1 : important.timeBase = 15
  76. break
  77. case 2 : important.timeBase = 7
  78. break
  79. case 3 : important.timeBase = 4
  80. break
  81. case 0 : important.timeBase = -1
  82. important.pauseTime = time
  83. time = -1
  84. break
  85. }
  86. if (time > important.timeBase) {
  87. time = important.timeBase
  88. } else if (time < 0) {
  89. time = important.pauseTime
  90. }
  91. }
  92. function pauseButt() {
  93. if (!important.pause) {
  94. important.pause = true
  95. important.pauseButton = true
  96. } else if (important.pauseButton && important.pause && menuOpen) {
  97. important.pauseButton = false
  98. } else if (important.pauseButton && important.pause && !menuOpen) {
  99. important.pauseButton = false
  100. important.pause = false
  101. }
  102. }
  103. // functions for gathering actions
  104. let res = {
  105. food: 10,
  106. water: 10,
  107. wood: 15,
  108. stone: 0
  109. }
  110. let rates = {
  111. foodRate: 8,
  112. waterRate: 10,
  113. woodRate: 3,
  114. stoneRate: 3
  115. }
  116. var tStart = {
  117. food : -3,
  118. water : -3,
  119. wood : -3,
  120. stone : -5
  121. }
  122. var gatherTime = {
  123. food: 3,
  124. water : 3,
  125. wood : 5,
  126. stone : 7
  127. }
  128. function moreFood() {
  129. if(important.count > tStart.food + gatherTime.food){
  130. res.food+=rates.foodRate
  131. //important.actions--
  132. console.log(res.food)
  133. gatheringTimer()
  134. tStart.food = important.count
  135. }
  136. document.getElementById('food').innerHTML = res.food
  137. }
  138. function moreWater() {
  139. if(important.count > tStart.water + gatherTime.water){
  140. res.water+=rates.waterRate
  141. //important.actions--
  142. console.log(res.water)
  143. gatheringTimer()
  144. tStart.water = important.count
  145. }
  146. document.getElementById('water').innerHTML = res.water
  147. }
  148. function moreWood() {
  149. if(important.count > tStart.wood + gatherTime.wood){
  150. res.wood += rates.woodRate
  151. //important.actions--
  152. gatheringTimer()
  153. tStart.wood = important.count
  154. }
  155. document.getElementById('wood').innerHTML = res.wood
  156. }
  157. function moreStone() {
  158. if (important.count > tStart.stone + gatherTime.stone && builds.quarry > 0 && rates.stoneRate > 0) {
  159. res.stone += rates.stoneRate
  160. //important.actions--
  161. tStart.stone = important.count
  162. gatheringTimer()
  163. } else if (builds.quarry <= 0 && important.count > tStart.stone + gatherTime.stone) {
  164. document.getElementById('warnings').innerHTML = 'You have no quarries!'
  165. } else if (rates.stoneRate == 0 && important.count > tStart.stone + gatherTime.stone) {
  166. document.getElementById('warnings').innerHTML = 'You have no miners!'
  167. }
  168. document.getElementById('stone').innerHTML = res.stone
  169. }
  170. // building functions
  171. let builds = {
  172. houses:2,
  173. farms:1,
  174. logging:0,
  175. wallLevel:0,
  176. quarry:0,
  177. well:0,
  178. armory:0,
  179. tradePost:0
  180. }
  181. let cost = {
  182. homeCost: 50,
  183. farmCost: 40,
  184. logCost: 40,
  185. wallCost: 45,
  186. quarryCost: 45,
  187. wellCost: 20,
  188. armoryCost: 40,
  189. tradeCost:50
  190. }
  191. let workTotal = {
  192. farm: 8,
  193. wood: 0,
  194. guard: 0,
  195. miner: 0,
  196. soldier: 0
  197. }
  198. function buildHouse() {
  199. if (res.wood >= cost.homeCost && important.actions > 0) {
  200. builds.houses++
  201. important.actions--
  202. res.wood -= cost.homeCost
  203. cost.homeCost += Math.floor(cost.homeCost / 10)
  204. document.getElementById('homes').innerHTML = builds.houses
  205. document.getElementById('wood').innerHTML = res.wood
  206. document.getElementById('homeMat').innerHTML = "Required: " + cost.homeCost + " Wood"
  207. important.maxPop += 4
  208. document.getElementById('pop').innerHTML = 'Population: ' + important.population + '/' + important.maxPop
  209. } else if (res.wood < cost.homeCost && important.actions > 0) {
  210. document.getElementById('warnings').innerHTML = 'Not enough wood!'
  211. }
  212. }
  213. function buildFarm() {
  214. if (res.wood >= cost.farmCost && important.actions > 0) {
  215. builds.farms++
  216. important.actions--
  217. res.wood -= cost.farmCost
  218. workTotal.farm += 4
  219. cost.farmCost += Math.floor(cost.farmCost / 10)
  220. document.getElementById('farm').innerHTML = builds.farms
  221. document.getElementById('wood').innerHTML = res.wood
  222. document.getElementById('farmMat').innerHTML = "Required: " + cost.farmCost + " Wood"
  223. document.getElementById('farmerCount').innerHTML = '<i>Farmers: ' + workers.farm + '/' + workTotal.farm + '</i>'
  224. } else if (res.wood < cost.farmCost && important.actions > 0) {
  225. document.getElementById('warnings').innerHTML = 'Not enough wood!'
  226. }
  227. }
  228. function buildLogger() {
  229. if (res.wood >= cost.logCost && important.actions > 0) {
  230. builds.logging++
  231. important.actions--
  232. res.wood -= cost.logCost
  233. cost.logCost += Math.floor(cost.logCost/10)
  234. workTotal.wood += 4
  235. document.getElementById('logs').innerHTML = builds.logging
  236. document.getElementById('wood').innerHTML = res.wood
  237. document.getElementById('logMat').innerHTML = "Required: " + cost.logCost + " Wood"
  238. document.getElementById('loggerCount').innerHTML = '<i>Loggers: ' + workers.logger + '/' + workTotal.wood + '</i>'
  239. }
  240. }
  241. function buildWall() {
  242. if (builds.wallLevel == 0) {
  243. if (res.wood >= cost.wallCost && important.actions > 0) {
  244. console.log('build')
  245. builds.wallLevel++
  246. res.wood -= cost.wallCost
  247. important.defense+= 4
  248. workTotal.guard+=4
  249. important.actions--
  250. document.getElementById('wall').innerHTML = 'Wooden Palisade'
  251. document.getElementById('wood').innerHTML = res.wood
  252. document.getElementById('guardCount').innerHTML = '<i>Guards: ' + workers.guard + '/' + workTotal.guard + '</i>'
  253. document.getElementById('wallMat').innerHTML = 'Required: 32 Stone'
  254. cost.wallCost = 32
  255. }
  256. } else if (builds.wallLevel == 1) {
  257. if (res.stone >= cost.wallCost && important.actions > 0) {
  258. builds.wallLevel++
  259. res.stone -= cost.wallCost
  260. important.defense+= 4
  261. workTotal.guard+=4
  262. important.actions--
  263. document.getElementById('wall').innerHTML = 'Stone Walls'
  264. document.getElementById('stone').innerHTML = res.stone
  265. document.getElementById('guardCount').innerHTML = '<i>Guards: ' + workers.guard + '/' + workTotal.guard + '</i>'
  266. document.getElementById('wallMat').innerHTML = 'Required: N/A'
  267. }
  268. } else { document.getElementById('warnings').innerHTML = 'No further upgrades!' }
  269. }
  270. function buildQuarry() {
  271. if (res.wood >= cost.quarryCost && important.actions > 0){
  272. builds.quarry++
  273. res.wood -= cost.quarryCost
  274. workTotal.miner += 4
  275. important.actions--
  276. cost.quarryCost += Math.floor(cost.quarryCost/4)
  277. document.getElementById('quarry').innerHTML = builds.quarry
  278. document.getElementById('wood').innerHTML = res.wood
  279. document.getElementById('minerCount').innerHTML = '<i>Miners: ' + workers.miner + '/' + workTotal.miner + '</i>'
  280. }
  281. document.getElementById('hiddenStone').style.display = 'table-cell'
  282. }
  283. function buildWell() {
  284. if (res.stone >= cost.wellCost && important.actions > 0) {
  285. builds.well++
  286. rates.waterRate += 5
  287. res.stone -= cost.wellCost
  288. important.actions--
  289. cost.wellCost += Math.floor(cost.wellCost/2)
  290. document.getElementById('well').innerHTML = builds.well
  291. document.getElementById('stone').innerHTML = res.stone
  292. document.getElementById('wellCost').innerHTML = 'Required: ' + cost.wellCost + ' Stone'
  293. }
  294. }
  295. function buildArmory() {
  296. if (res.stone >= cost.armoryCost && important.actions > 0){
  297. builds.armory++
  298. important.actions--
  299. workTotal.soldier+=4;
  300. cost.armoryCost+=Math.floor(cost.armoryCost/3)
  301. document.getElementById('armory').innerHTML = builds.armory
  302. document.getElementById('stone').innerHTML = res.stone
  303. document.getElementById('soldierCount').innerHTML = '<i>Soldiers: ' + workers.soldiers + '/' + workTotal.soldier + '</i>'
  304. document.getElementById('wallMat').innerHTML = 'Required: ' + cost.armoryCost + ' Stone'
  305. document.getElementById('war').style.display = 'block'
  306. }
  307. }
  308. function buildTrade() {
  309. if (res.wood > cost.tradeCost && important.actions > 0 && builds.tradePost == 0) {
  310. builds.tradePost++
  311. res.wood -= cost.tradeCost
  312. important.actions--
  313. document.getElementById('tradePost').innerHTML = builds.tradePost
  314. document.getElementById('tradeMat').innerHTML = 'N/A'
  315. updateResources()
  316. document.getElementById('trade').style.display = 'block'
  317. } else if (builds.tradePost == 1) {
  318. document.getElementById('warnings').innerHTML = 'No more trading posts can be built!'
  319. }
  320. }
  321. //job shiftings
  322. let workers = {
  323. pop: 8,
  324. available: 0,
  325. farm: 8,
  326. logger: 0,
  327. guard:0,
  328. miner: 0,
  329. soldiers: 0
  330. }
  331. function addWorker(add, workerType){
  332. if (workerType == 'farmer') { //FARMERS
  333. if (add) {
  334. if (important.population - workers.pop > 0 && builds.farms > 0 && workers.farm < workTotal.farm) {
  335. workers.pop++
  336. rates.foodRate = workers.farm
  337. workers.farm++
  338. workers.available = important.population - workers.pop
  339. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  340. document.getElementById('farmerCount').innerHTML = '<i>Farmers: ' + workers.farm + '/' + workTotal.farm + '</i>'
  341. document.getElementById('foodGain').innerHTML = 'You are gaining food at a rate of ' + rates.foodRate +' Food per year.'
  342. console.log('food | ' + workers.farm + '/' + workTotal.farm)
  343. } else if (workers.available <=0) { document.getElementById('warnings').innerHTML = 'Not enough available workers!' }
  344. else if (builds.farms == 0) { document.getElementById('warnings').innerHTML = 'There are no farms!' }
  345. else if (workers.farm >= workTotal.wood) { document.getElementById('warnings').innerHTML = 'There\'s no space left in the farms!' }
  346. } else if (!add) {
  347. if (workers.farm > 0) {
  348. workers.pop--
  349. rates.foodRate--
  350. workers.farm--
  351. rates.foodRate = workers.farm
  352. workers.available = important.population - workers.pop
  353. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  354. document.getElementById('farmerCount').innerHTML = '<i>Farmers: ' + workers.farm + '/' + workTotal.farm + '</i>'
  355. document.getElementById('foodGain').innerHTML = 'You are gaining food at a rate of ' + rates.foodRate +' Food per year.'
  356. } else { document.getElementById('warnings').innerHTML = 'There are no more farmers!' }
  357. }
  358. }
  359. if (workerType == 'logger') { //LOGGERS
  360. if (add) {
  361. if (important.population - workers.pop > 0 && builds.logging > 0 && workers.logger < workTotal.wood) {
  362. workers.pop++
  363. rates.woodRate++
  364. workers.logger++
  365. workers.available = important.population - workers.pop
  366. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  367. document.getElementById('loggerCount').innerHTML = '<i>Workers: ' + workers.logger + '/' + workTotal.wood + '</i>'
  368. console.log(rates.woodRate)
  369. console.log('wood | ' + workers.logger + '/' + workTotal.wood)
  370. } else if (workers.available <= 0) { document.getElementById('warnings').innerHTML = 'Not enough available workers!' }
  371. else if (builds.logging == 0) { document.getElementById('warnings').innerHTML = 'There are no logging camps!' }
  372. else if (workers.logger >= workTotal.wood) { document.getElementById('warnings').innerHTML = 'There\'s no space left in the logging camps!' }
  373. } else if (!add) {
  374. if (workers.logger > 0) {
  375. workers.pop--
  376. rates.woodRate--
  377. workers.logger--
  378. workers.available = important.population - workers.pop
  379. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  380. document.getElementById('loggerCount').innerHTML = '<i>Workers: ' + workers.logger + '/' + workTotal.wood + '</i>'
  381. console.log(rates.woodRate)
  382. } else { document.getElementById('warnings').innerHTML = 'There are no more loggers!' }
  383. }
  384. }
  385. if (workerType == 'guard') { //GUARDS
  386. if (add) {
  387. if (important.population - workers.pop > 0 && builds.wallLevel > 0 && workers.guard < workTotal.guard) {
  388. workers.pop++
  389. important.defense++
  390. workers.guard++
  391. workers.available = important.population - workers.pop
  392. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  393. document.getElementById('guardCount').innerHTML = '<i>Guards: ' + workers.guard + '/' + workTotal.guard + '</i>'
  394. } else if (workers.available <= 0) { document.getElementById('warnings').innerHTML = 'Not enough available workers!' }
  395. else if (builds.wallLevel == 0) { document.getElementById('warnings').innerHTML = 'You have no wall!' }
  396. else if (workers.guard >= workTotal.guard) { document.getElementById('warnings').innerHTML = 'There\'s no space left on the wall!' }
  397. } else if (!add) {
  398. if (workers.guard > 0) {
  399. workers.pop--
  400. important.defense--
  401. workers.guard--
  402. workers.available = important.population - workers.pop
  403. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  404. document.getElementById('guardCount').innerHTML = '<i>Guards: ' + workers.guard + '/' + workTotal.guard + '</i>'
  405. console.log(rates.woodRate)
  406. } else { document.getElementById('warnings').innerHTML = 'There are no more guards!' }
  407. }
  408. }
  409. if (workerType == 'miner') {
  410. if (add) {
  411. if (important.population - workers.pop > 0 && builds.quarry > 0 && workers.miner < workTotal.miner) {
  412. workers.pop++
  413. rates.stoneRate++
  414. workers.miner++
  415. workers.available = important.population - workers.pop
  416. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  417. document.getElementById('minerCount').innerHTML = '<i>Miners: ' + workers.miner + '/' + workTotal.miner + '</i>'
  418. } else if (workers.available <= 0) { document.getElementById('warnings').innerHTML = 'Not enough available workers!' }
  419. else if (builds.quarry == 0) { document.getElementById('warnings').innerHTML = 'You have no quarries!' }
  420. else if (workers.guard >= workTotal.guard) { document.getElementById('warnings').innerHTML = 'There\'s no space left in the quarries!' }
  421. } else if (!add) {
  422. if (workers.miner > 0) {
  423. workers.pop--
  424. rates.stoneRate--
  425. workers.miner--
  426. workers.available = important.population - workers.pop
  427. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  428. document.getElementById('minerCount').innerHTML = '<i>Miners: ' + workers.miner + '/' + workTotal.miner + '</i>'
  429. } else { document.getElementById('warnings').innerHTML = 'There are no more miners!' }
  430. }
  431. }
  432. if (workerType == 'soldier') {
  433. if (add) {
  434. if (important.population - workers.pop > 0 && builds.armory > 0 && workers.soldiers < workTotal.soldier) {
  435. workers.pop++
  436. workers.soldiers++
  437. workers.available = important.population - workers.pop
  438. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  439. document.getElementById('soldierCount').innerHTML = '<i>Soldiers: ' + workers.soldiers + '/' + workTotal.soldier + '</i>'
  440. } else if (workers.available <= 0) { document.getElementById('warnings').innerHTML = 'Not enough available workers!' }
  441. else if (builds.quarry == 0) { document.getElementById('warnings').innerHTML = 'You have no armories!' }
  442. else if (workers.guard >= workTotal.guard) { document.getElementById('warnings').innerHTML = 'There\'s no space left in the barracks!' }
  443. } else if (!add) {
  444. if (workers.soldiers > 0) {
  445. workers.pop--
  446. workers.soldiers--
  447. workers.available = important.population - workers.pop
  448. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  449. document.getElementById('soldierCount').innerHTML = '<i>Soldiers: ' + workers.soldiers + '/' + workTotal.soldier + '</i>'
  450. } else { document.getElementById('warnings').innerHTML = 'There are no more soldiers!' }
  451. }
  452. }
  453. }
  454. //COMBAT
  455. function combat(offensive, enemyStr, tribeStr){
  456. if (!offensive) {
  457. if (enemyStr <= tribeStr) {
  458. return true
  459. } else { return false}
  460. } if (offensive) {
  461. let winChance = 0
  462. if (tribeStr >= enemyStr) {
  463. winChance = Math.ceil(Math.random() * 10)
  464. if (winChance >= 3) {
  465. return true
  466. } else { return false }
  467. } else if (tribeStr < enemyStr) {
  468. if (winChance >= 7) {
  469. return true
  470. } else { return false }
  471. }
  472. }
  473. }
  474. function raid() {
  475. otherTribeStr = Math.ceil(Math.random() * (2-10) + 10)
  476. victory = combat(true, otherTribeStr, workers.soldiers)
  477. if (victory) {
  478. resourcesLost = 0
  479. resourceType = ''
  480. resourceTypeGain = Math.floor(Math.random() * 4)
  481. if (resourceTypeGain <=1) {
  482. resourceType = 'Wood'
  483. tempWood = res.wood
  484. res.wood += Math.floor(Math.random() * 50)
  485. resourcesLost = res.wood - tempWood
  486. } else if (resourceTypeGain <= 2) {
  487. resourceType = 'Food'
  488. tempFood = res.food
  489. res.food += Math.floor(Math.random() * 50)
  490. resourcesLost = res.food - tempFood
  491. } else if (resourceTypeGain <= 3) {
  492. resourceType = 'Water'
  493. tempWater = res.water
  494. res.water += Math.floor(Math.random() * 50)
  495. resourcesLost = res.water - tempWater
  496. } else if (resourceTypeGain <= 4) {
  497. resourceType = 'Stone'
  498. tempStone = res.stone
  499. res.stone += Math.floor(Math.random() * 50)
  500. resourcesLost = res.stone - tempStone
  501. }
  502. updateResources()
  503. document.getElementById('warnings').innerHTML = 'You won the battle!<br>Enemy strength: ' + otherTribeStr + ' | Your strength: ' + workers.soldiers + '<br>You gained: ' + resourcesLost + ' ' + resourceType
  504. } else if (!victory){
  505. document.getElementById('warnings').innerHTML = 'You lost the battle!<br>Enemy strength: ' + otherTribeStr + ' | Your strength: ' + workers.soldiers
  506. }
  507. }
  508. let tribes = {
  509. 1 : {name:'Tolsta', str:6},
  510. 2 : {name:'Lavoya', str:12},
  511. 3 : {name:'Alveyit', str: 15}
  512. }
  513. function conquer() {
  514. important.nation++
  515. victory = combat(true, tribes[important.nation].str, workers.soldiers)
  516. if (victory) {
  517. document.getElementById('warnings').innerHTML = 'win'
  518. } else if (!victory) {
  519. document.getElementById('warnings').innerHTML = 'lose'
  520. }
  521. }
  522. //show info box
  523. function showInfo(show, type){
  524. let infoSent = ' '
  525. if (type != undefined) {
  526. switch(type) {
  527. case 'food' :
  528. infoSent = 'Send people to gather food.<br>Food gained: ' + rates.foodRate
  529. break
  530. case 'water' :
  531. infoSent = 'Have people collet water from the river.<br>Water gained: ' + rates.waterRate
  532. break
  533. case 'wood' :
  534. infoSent = 'Send people to gather wood.<br>Wood gained: ' + rates.woodRate
  535. break
  536. case 'stone' :
  537. infoSent = 'Send people to mine stone.<br>Stone gained: ' + rates.stoneRate
  538. break
  539. case 'farm' :
  540. infoSent = 'Build a farm.<br>Increases maximum number of farmers by 4.'
  541. break
  542. case'log' :
  543. infoSent = 'Build a logging camp.<br>Increases maximum number of loggers by 4.'
  544. break
  545. case 'quarry' :
  546. infoSent = 'Build a quarry.<br>Allows stone mining.'
  547. break
  548. case 'wall' :
  549. infoSent = 'Build a wall around the tribe.<br>Increases defense by 4.<br>Increases maximum number of guards by 4.'
  550. break
  551. case 'armory' :
  552. infoSent = 'Build an armory in the tribe.<br>Allows you to go to war.'
  553. break
  554. case 'trade' :
  555. infoSent = 'Build a trading post outside the tribe.<br>Allows you to trade with your neighbors.'
  556. break
  557. case 'well' :
  558. infoSent = 'Build a well in the tribe.<br>Increases water gain rate by 5.'
  559. break
  560. case 'endyr' :
  561. infoSent = 'End the year and go into the next.'
  562. break
  563. case 'home':
  564. infoSent = 'Build a home in the tribe.<br>Population max +4.'
  565. break
  566. case type == 'raid' :
  567. infoSent = 'Send your soldiers out on a raid.'
  568. break
  569. case 'war' :
  570. if (tribes.nation != 4) {
  571. infoSent = 'Send your soliders to conquer a nearby region.<br>Current closest tribe: ' + tribes[important.nation].name
  572. } else {
  573. infoSent = 'Send your soliders to conquer a nearby region.<br>There are no nearby tribes to conquer.'
  574. }
  575. break
  576. }
  577. }
  578. if (show) {
  579. document.getElementById('itemInformation').innerHTML = infoSent
  580. document.getElementById('info').style.display = 'block'
  581. } else if(!show) {
  582. document.getElementById('info').style.display = 'none'
  583. }
  584. }
  585. function showMenu() {
  586. if (document.getElementById('fullMenu').style.display != 'block') {
  587. document.getElementById('fullMenu').style.display = 'block'
  588. important.resume = important.timeSpeed
  589. speedChange(0)
  590. } else if (document.getElementById('fullMenu').style.display == 'block') {
  591. document.getElementById('fullMenu').style.display = 'none'
  592. speedChange(important.resume)
  593. }
  594. }
  595. // end previous turn, start next turn
  596. let waterSurplus = false
  597. let foodSurplus = false
  598. let foodDeficit = false
  599. conquered = false
  600. function updateResources() {
  601. document.getElementById('food').innerHTML = res.food
  602. document.getElementById('water').innerHTML = res.water
  603. document.getElementById('wood').innerHTML = res.wood
  604. document.getElementById('stone').innerHTML = res.stone
  605. builds.quarry >= 1 ? document.getElementById('hiddenStone').style.display = 'table-cell' : document.getElementById('hiddenStone').style.display = 'none'
  606. document.getElementById('pop').innerHTML = 'Population: ' + important.population + '/' + important.maxPop
  607. console.log('Resources updated')
  608. }
  609. function updateAllText() {
  610. document.getElementById('year').innerHTML = 'Year: ' + important.year
  611. console.log(important.year)
  612. document.getElementById('actions').innerHTML = actions
  613. updateResources()
  614. document.getElementById('farm').innerHTML = builds.farms
  615. document.getElementById('wood').innerHTML = res.wood
  616. document.getElementById('farmerCount').innerHTML = '<i>Farmers: ' + workers.farm + '/' + workTotal.farm + '</i>'
  617. document.getElementById('logs').innerHTML = builds.logging
  618. document.getElementById('logMat').innerHTML = "Required: " + cost.logCost + " Wood"
  619. document.getElementById('loggerCount').innerHTML = '<i>Loggers: ' + workers.logger + '/' + workTotal.wood + '</i>'
  620. if (builds.wallLevel == 1) {
  621. document.getElementById('wall').innerHTML = 'Wooden Palisade'
  622. document.getElementById('guardCount').innerHTML = '<i>Guards: ' + workers.guard + '/' + workTotal.guard + '</i>'
  623. document.getElementById('wallMat').innerHTML = 'Required: 32 Stone'
  624. } else if (builds.wallLevel == 2) {
  625. document.getElementById('wall').innerHTML = 'Stone Walls'
  626. document.getElementById('guardCount').innerHTML = '<i>Guards: ' + workers.guard + '/' + workTotal.guard + '</i>'
  627. document.getElementById('wallMat').innerHTML = 'Required: N/A'
  628. }
  629. document.getElementById('quarry').innerHTML = builds.quarry
  630. document.getElementById('minerCount').innerHTML = '<i>Miners: ' + workers.miner + '/' + workTotal.miner + '</i>'
  631. document.getElementById('homes').innerHTML = builds.houses
  632. document.getElementById('homeMat').innerHTML = "Required: " + cost.homeCost + " Wood"
  633. document.getElementById('pop').innerHTML = 'Population: ' + important.population + '/' + important.maxPop
  634. document.getElementById('well').innerHTML = builds.well
  635. document.getElementById('wellCost').innerHTML = 'Required: ' + cost.wellCost + ' Stone'
  636. document.getElementById('armory').innerHTML = builds.armory
  637. document.getElementById('soldierCount').innerHTML = '<i>Soldiers: ' + workers.soldiers + '/' + workTotal.soldier + '</i>'
  638. document.getElementById('wallMat').innerHTML = 'Required: ' + cost.armoryCost + ' Stone'
  639. builds.armory >= 1 ? document.getElementById('war').style.display = 'block' : document.getElementById('war').style.display = 'none'
  640. document.getElementById('tradePost').innerHTML = builds.tradePost
  641. document.getElementById('tradeMat').innerHTML = 'N/A'
  642. builds.tradePost >= 1 ? document.getElementById('trade').style.display = 'block' : document.getElementById('trade').style.display = 'none'
  643. }
  644. function endTurn(){
  645. /*if (important.actions > 0) {
  646. document.getElementById('warnings').innerHTML = 'You still have remaining actions!'
  647. return
  648. }
  649. if (workers.available < 0) {
  650. document.getElementById('warnings').innerHTML = 'You must reassign some workers!'
  651. return
  652. }*/
  653. if (!important.gameOver && !important.pause) {
  654. res.food > 0 ? res.food-=important.population : res.food = 0
  655. res.water > 0 ? res.water-=important.population : res.water = 0
  656. waterSurplus ? res.water += important.population : res.water = res.water
  657. foodSurplus ? res.food += important.population : res.food = res.food
  658. foodDeficit ? res.food -= Math.floor(important.population / 2) : res.food = res.food
  659. document.getElementById('warnings').innerHTML = ' '
  660. if (res.water <= 0 || res.food <= 0) {
  661. important.colEnd--
  662. document.getElementById('warnings').innerHTML = 'Your tribe has ' + important.colEnd + ' years remaining!'
  663. if (res.water < 0) {
  664. res.water = 0
  665. document.getElementById('water').innerHTML = res.water
  666. }
  667. if (res.food < 0) {
  668. res.food = 0
  669. document.getElementById('food').innerHTML = res.food
  670. }
  671. } else if (res.water > 0 && res.food > 0){
  672. important.colEnd = 3
  673. }
  674. important.year++
  675. document.getElementById('year').innerHTML = 'Year: ' + important.year
  676. if (important.maxPop - important.population != 0 && important.population != 1) {
  677. var popAddChance = Math.floor(Math.random() * 100)
  678. if (popAddChance >= 60) {
  679. important.population++
  680. workers.available = important.population - workers.pop
  681. document.getElementById('pop').innerHTML = 'Population: ' + important.population + '/' + important.maxPop
  682. document.getElementById('warnings').innerHTML = 'A child has been born in the tribe!'
  683. document.getElementById('unusedPop').innerHTML = 'Available Workers: ' + workers.available + '/' + important.population
  684. }
  685. }
  686. randEventPick = Math.floor(Math.random() * 100)
  687. //console.log(randEventPick)
  688. if (randEventPick >= 70 && randEventPick <80) {
  689. raiders()
  690. } else if(randEventPick >= 80 && randEventPick < 85) {
  691. badHarvest()
  692. } else if (randEventPick >= 85 && randEventPick <96) {
  693. goodHarvest()
  694. } else if (randEventPick >= 96) {
  695. heavyRains()
  696. } else {
  697. waterSurplus = false
  698. foodSurplus = false
  699. foodDeficit = false
  700. }
  701. }
  702. if (important.colEnd >= 1) { important.actions = 10 }
  703. updateResources()
  704. if (important.colEnd <= 0) {
  705. if (!conquered) {
  706. document.getElementById('warnings').innerHTML = 'Game over!<br>Your tribe lasted ' + (important.year - START_YEAR) + ' years'
  707. important.gameOver = true
  708. } else if(conquered) {
  709. document.getElementById('warnings').innerHTML = 'Your tribe was conquered!<br>Your tribe lasted ' + (important.year - START_YEAR) + ' years'
  710. important.gameOver = true
  711. }
  712. }
  713. }
  714. function heavyRains() {
  715. document.getElementById('warnings').innerHTML = 'The tribe faces heavy rains.'
  716. waterSurplus = true
  717. }
  718. function goodHarvest() {
  719. document.getElementById('warnings').innerHTML = 'The tribe is having a very good harvest!'
  720. foodSurplus = true
  721. }
  722. function badHarvest() {
  723. document.getElementById('warnings').innerHTML = 'Your tribe suffers a bad harvest!'
  724. foodDeficit = true
  725. }
  726. function raiders() {
  727. raiderStr = Math.floor(Math.random() * (8-3) + 3)
  728. victory = combat(false, raiderStr, important.defense)
  729. console.log('attack')
  730. resourcesLost = 0
  731. resourceType = ''
  732. if (victory) {
  733. document.getElementById('warnings').innerHTML = 'Your tribe was attacked, but the raiders were repelled!\nRaider strength: ' + raiderStr + ' | Your strength: ' + important.defense
  734. } else if (!victory) {
  735. popLost = Math.ceil(Math.random() * (important.population/2))
  736. important.population-=popLost
  737. if (important.population <= 1) {
  738. important.population = 0
  739. important.gameOver = true
  740. important.colEnd = 0
  741. updateResources()
  742. conquered = true
  743. endTurn()
  744. }
  745. resourceTypeLost = Math.floor(Math.random() * 4)
  746. if (resourceTypeLost <=1) {
  747. resourceType = 'Wood'
  748. tempWood = res.wood
  749. res.wood -= Math.floor(Math.random() * (res.wood/2))
  750. resourcesLost = tempWood - res.wood
  751. } else if (resourceTypeLost <= 2) {
  752. resourceType = 'Food'
  753. tempFood = res.food
  754. res.food -= Math.floor(Math.random() * (res.food/2))
  755. resourcesLost = tempFood - res.food
  756. } else if (resourceTypeLost <= 3) {
  757. resourceType = 'Water'
  758. tempWater = res.water
  759. res.water -= Math.floor(Math.random() * (res.water/2))
  760. resourcesLost = tempWater - res.water
  761. } else if (resourceTypeLost <= 4) {
  762. resourceType = 'Stone'
  763. tempStone = res.stone
  764. res.stone -= Math.floor(Math.random() * (res.stone/4))
  765. resourcesLost = tempStone - res.stone
  766. }
  767. document.getElementById('warnings').innerHTML = 'Your tribe was attacked, and your defenses fell!<br>Raider strength: ' + raiderStr + ' | Your strength: ' + important.defense + ' <br>Population lost: ' + popLost + ' | Resources lost: ' + resourcesLost + ' ' + resourceType
  768. updateResources()
  769. }
  770. }
  771. //setTimeout(endTurn(), 5000)
  772. //SAVE/LOAD GAME
  773. function saveGame() {
  774. localStorage.setItem('main', JSON.stringify(important));
  775. localStorage.setItem('resources', JSON.stringify(res));
  776. localStorage.setItem('rates', JSON.stringify(rates));
  777. localStorage.setItem('workers', JSON.stringify(workers));
  778. localStorage.setItem('work totals', JSON.stringify(workTotal));
  779. localStorage.setItem('builds', JSON.stringify(builds));
  780. localStorage.setItem('costs', JSON.stringify(cost));
  781. localStorage.setItem('tribes', JSON.stringify(tribes))
  782. console.log('Saved!')
  783. document.getElementById('warnings').innerHTML = 'Saved game!'
  784. }
  785. function loadGame() {
  786. important = JSON.parse(localStorage.getItem('main'))
  787. res = JSON.parse(localStorage.getItem('resources'))
  788. rates = JSON.parse(localStorage.getItem('rates'))
  789. workers = JSON.parse(localStorage.getItem('workers'))
  790. workTotal = JSON.parse(localStorage.getItem('work totals'))
  791. builds = JSON.parse(localStorage.getItem('builds'))
  792. cost = JSON.parse(localStorage.getItem('costs'))
  793. tribes = JSON.parse(localStorage.getItem('tribes'))
  794. console.log('Loaded game!')
  795. document.getElementById('warnings').innerHTML = 'Loaded!'
  796. updateAllText();
  797. }
  798. function devMode() { //FOR EASE OF DEVELOPMENT ONLY
  799. res.wood = 1000000
  800. res.water = 1000000
  801. res.food = 1000000
  802. res.stone = 1000000
  803. updateResources()
  804. document.getElementById('hiddenStone').style.display = 'table-cell' //call thru inspector console
  805. }
  806. //LINKS
  807. //<a target="_blank" href="https://icons8.com/icons/set/play">Play icon</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>