From 388c289bb646db0d95e6c4e40bd08e998201268b Mon Sep 17 00:00:00 2001
From: Tor Andersson <tor@ccxvii.net>
Date: Wed, 25 Dec 2024 12:25:26 +0100
Subject: separate flipped generals in stack

---
 play.js | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/play.js b/play.js
index 9a3ac97..1306071 100644
--- a/play.js
+++ b/play.js
@@ -986,10 +986,14 @@ function on_blur_city() {
 
 function on_focus_piece(evt) {
 	let p = evt.target.my_id
-	if (p < 20 && view.troops[p] > 0)
-		ui.status.textContent = piece_log_name[evt.target.my_id] + " (" + view.troops[p] + " troops)"
-	else
-		ui.status.textContent = piece_log_name[evt.target.my_id]
+	let text = piece_log_name[p]
+	if (p < 20 && view.troops[p] > 0) {
+		text += " (" + view.troops[p] + " troops"
+		if (view.oos & (1 << p))
+			text += " \u2013 flipped"
+		text += ")"
+	}
+	ui.status.textContent = text
 }
 
 function on_blur_piece() {
@@ -1023,6 +1027,23 @@ function layout_general_offset(who, here) {
 	return 0
 }
 
+function is_other_general_on_top(who, here) {
+	let other = -1
+	for (let p = 0; p < 20; ++p)
+		if (view.pos[p] === here && who !== p)
+			other = p
+	// no other general!
+	if (other < 0)
+		return false // alone
+	if ((view.supreme & (1 << who)) || view.selected === who)
+		return false // on top
+	if ((view.supreme & (1 << other)) || view.selected === other)
+		return true // on bottom
+	if (piece_rank[who] < piece_rank[other])
+		return false // on top
+	return true // on bottom
+}
+
 function layout_general_offset_elim(g) {
 	let n = 0
 	let p = get_cylinder_power(g)
@@ -1082,6 +1103,11 @@ function layout_general(id, s) {
 		y = data.cities.y[s]
 	}
 
+	if (is_other_general_on_top(id, s)) {
+		if (view.oos & (1 << id))
+			n = -2/3
+	}
+
 	let selected = set_has(view.selected, id)
 
 	let e = ui.pieces[id]
@@ -1091,7 +1117,7 @@ function layout_general(id, s) {
 	e.style.top = (y - 29 - 15 * n) + "px"
 	e.style.zIndex = y + n
 	e.classList.toggle("selected", selected)
-	e.classList.toggle("oos", (view.oos & (1 <<id)) !== 0)
+	e.classList.toggle("oos", (view.oos & (1 << id)) !== 0)
 
 	e = ui.troops[id]
 	if (e.parentElement !== ui.pieces_element)
-- 
cgit v1.2.3