From 8eb0411bad2ce49f99ce5b21b576104a91e9bc45 Mon Sep 17 00:00:00 2001
From: iainp5 <iain.pearce.ip@gmail.com>
Date: Mon, 9 Dec 2024 07:52:24 +0000
Subject: Check Ceausescu at end round not end event

---
 rules.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 59 insertions(+), 6 deletions(-)

(limited to 'rules.js')

diff --git a/rules.js b/rules.js
index ee22d36..4a4d57f 100644
--- a/rules.js
+++ b/rules.js
@@ -3669,6 +3669,14 @@ function end_round() {
 		return
 	}
 
+	console.log('in end_round, game.persistent_events.includes(C_CEAUSESCU)', game.persistent_events.includes(C_CEAUSESCU))
+	// Check if Ceausescu has been played and if so check end round condition
+	if (game.persistent_events.includes(C_CEAUSESCU)) {
+		console.log('in check ceausescu')
+		if (check_ceausescu())
+			return
+	}
+
 	// Check if the card needs to be discarded.
 	let discard_check = [...game.strategy_removed, ...game.persistent_events]
 	if (!discard_check.includes(game.played_card) && game.played_card > 0) {
@@ -4132,6 +4140,16 @@ function reset_power() {
 	}
 }
 
+function check_ceausescu() {
+	game.return = game.active
+	game.return_state = 'ceausescu_check'
+	console.log('check_ceausescu, game.return', game.return)
+	game.vm_infl_to_do = false
+	game.vm_event = C_CEAUSESCU
+	goto_vm(game.vm_event)
+	return true
+}
+
 function check_tyrant() {
 	if (game.the_tyrant_is_gone > 0 && check_dem_control(game.the_tyrant_is_gone)) {
 		log_summary('+2 VP from C' + C_THE_TYRANT_IS_GONE + '.')
@@ -4519,6 +4537,7 @@ function prompt_event(str) {
 }
 
 function event_prompt(str) {
+	console.log('game.vm.fp', game.vm.fp, 'game.vm.prompt', game.vm.prompt)
 	if (typeof str === "undefined")
 		str = CODE[game.vm.fp][game.vm.prompt][1]
 	if (typeof str === "function")
@@ -4548,7 +4567,7 @@ function vm_return() {
 	}
 
 	// Check if end event state is needed
-	if (game.is_pwr_struggle || game.state === 'vm_tst_6' || game.return_state === 'tiananmen_square_attempt_done') {
+	if (game.is_pwr_struggle || game.state === 'vm_tst_6' || game.return_state === 'tiananmen_square_attempt_done' || game.return_state === 'ceausescu_check') {
 		vm_end_event()
 	}
 	// Check if auto-resolve opponent event
@@ -4564,18 +4583,29 @@ function vm_return() {
 }
 
 function vm_end_event() {
+	console.log('vm_end_event game.return', game.return, 'game.return_state', game.return_state, 'game.vm_infl_to_do', game.vm_infl_to_do)
+	if (game.return_state === 'ceausescu_check') {
+		console.log('1')
+		end_round()
+		return
+	}
 	if (game.return !== game.active) {
 		//next_player()
+		console.log('2')
 		change_player()
 	}
 	if (game.return_state === 'power_struggle') {
+		console.log('3')
 		do_valid_cards()
 	}
 	if (game.return_state && game.return_state !== '') {
+		console.log('4')
 		game.state = game.return_state
 	} else if (game.vm_infl_to_do) {
+		console.log('in game.vm_infl_to_do')
 		game.state = 'resolve_opponent_event'
 	} else {
+		console.log('end round called')
 		end_round()
 	}
 }
@@ -5166,9 +5196,15 @@ function vm_bulgarian_turks_expelled() {
 	vm_next()
 }
 
+function vm_ceausescu_prep() {
+	game.persistent_events.push(C_CEAUSESCU)
+	vm_next()
+}
+
 function vm_ceausescu() {
+	game.persistent_events = game.persistent_events.filter(n => n!== C_CEAUSESCU)
 	let adj_cluj = false
-
+	
 	if (game.demInfl[S_TIMISOARA] > 0) {
 		adj_cluj = true
 	}
@@ -5182,13 +5218,21 @@ function vm_ceausescu() {
 		adj_cluj = true
 	}
 
+	console.log('in vm_ceausescu, adj_cluj', adj_cluj)
 	if (adj_cluj && game.comInfl[S_BUCURESTI] > 0) {
+		if (game.active !== COM) {
+			change_player()
+			game.return = game.active
+			log_event(C_CEAUSESCU)
+		}
 		game.valid_spaces = [S_BUCURESTI]
 		game.vm_available_ops = 1
 		game.remove_opponent_infl = false
+		log(`Democrat has SP adjacent to ${S_CLUJ_NAPOCA}.`)
+		log('Removed SP:')
 		game.state = 'vm_remove_infl'
 	} else {
-		vm_next()
+		vm_return()
 	}
 }
 
@@ -6047,6 +6091,7 @@ function vm_kremlin_coup_elite() {
 states.vm_end_event = {
 	prompt() {
 		prompt_event("Done.")
+		console.log('in vm_end_event')
 		if (game.vm_infl_to_do || game.return_state === 'vm_tst_8') {
 			gen_action('done')
 		} else {
@@ -6250,7 +6295,11 @@ states.vm_remove_infl = {
 			prompt_event(`Remove ${pluralize(game.vm_available_ops, 'opponent SP')}${event_prompt()}.`)
 		}
 		else {
-			prompt_event(`Remove ${pluralize(game.vm_available_ops,'SP')}${event_prompt()}.`)
+			console.log('game.vm_event', game.vm_event)
+			if (game.vm_event === C_CEAUSESCU)
+				prompt_event(`${event_prompt()}.`)
+			else 	
+				prompt_event(`Remove ${pluralize(game.vm_available_ops,'SP')}${event_prompt()}.`)
 		}
 		for (let space_id of game.valid_spaces) {
 			gen_action_space(space_id)
@@ -8784,15 +8833,19 @@ CODE[41] = [ // Ceausescu*
 	[ vm_if, ()=>game.persistent_events.includes(THE_TYRANT_IS_GONE_OCCURRED) ],
 	[ vm_tyrant_block ],
 	[ vm_else ],
+	[ vm_if, ()=>game.persistent_events.includes(C_CEAUSESCU) ],
+	[ vm_prompt, 'you must remove 1 SP from Bucharesti' ],
+	[ vm_ceausescu ],
+	[ vm_return ],
+	[ vm_else ],
 	[ vm_if, ()=>!is_auto_resolve(C_CEAUSESCU) ],
+	[ vm_ceausescu_prep ],
 	[ vm_valid_spaces_country_opp, 'Romania' ],
 	[ vm_prompt, ' from Romania' ],
 	[ vm_remove_opp_infl, 3 ],
 	[ vm_valid_spaces_country_sc, 'Romania' ],
 	[ vm_prompt, 'make a Support Check in Romania' ],
 	[ vm_1_support_check ],
-	[ vm_prompt, ' from Bucharesti' ],
-	[ vm_ceausescu ],
 	[ vm_endif ],
 	[ vm_return ],
 ]
-- 
cgit v1.2.3