Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,7 +4,7 @@ import math
|
|
| 4 |
import random
|
| 5 |
|
| 6 |
# =========================
|
| 7 |
-
# إعدادات ال
|
| 8 |
# =========================
|
| 9 |
BOARD_SIZE = 100
|
| 10 |
SUN_RADIUS = 10
|
|
@@ -17,35 +17,31 @@ class Planet:
|
|
| 17 |
self.x = x
|
| 18 |
self.y = y
|
| 19 |
self.production = production
|
| 20 |
-
#
|
| 21 |
-
self.radius = 1 + math.log(production
|
| 22 |
-
self.ships = random.randint(
|
| 23 |
|
| 24 |
-
# مدار الكوكب
|
| 25 |
self.dist_to_center = math.dist((x, y), (50, 50))
|
|
|
|
| 26 |
self.is_orbiting = self.dist_to_center < 45
|
| 27 |
self.angle = math.atan2(y - 50, x - 50)
|
| 28 |
-
self.angular_vel = random.uniform(0.
|
| 29 |
|
| 30 |
def update(self):
|
| 31 |
-
# دوران الكواكب
|
| 32 |
if self.is_orbiting:
|
| 33 |
self.angle += self.angular_vel
|
| 34 |
self.x = 50 + math.cos(self.angle) * self.dist_to_center
|
| 35 |
self.y = 50 + math.sin(self.angle) * self.dist_to_center
|
| 36 |
-
|
| 37 |
-
# إنتاج السفن (المصنع) - كل دورة يزيد بعدد نقاط الإنتاج
|
| 38 |
if self.owner != -1:
|
| 39 |
self.ships += self.production
|
| 40 |
|
| 41 |
class Fleet:
|
| 42 |
def __init__(self, owner, x, y, angle, ships):
|
| 43 |
self.owner = owner
|
| 44 |
-
self.x = x
|
| 45 |
-
self.y = y
|
| 46 |
self.angle = angle
|
| 47 |
self.ships = ships
|
| 48 |
-
# سرعة
|
| 49 |
log_ratio = math.log(max(1, ships)) / math.log(1000)
|
| 50 |
self.speed = 1.0 + (MAX_SHIP_SPEED - 1.0) * math.pow(min(1.0, log_ratio), 1.5)
|
| 51 |
|
|
@@ -60,112 +56,109 @@ class GameEngine:
|
|
| 60 |
def reset(self):
|
| 61 |
self.planets = []
|
| 62 |
self.fleets = []
|
| 63 |
-
# إنشاء
|
| 64 |
-
for i in range(
|
| 65 |
-
px, py = random.uniform(
|
| 66 |
prod = random.randint(1, 5)
|
|
|
|
| 67 |
coords = [(px, py), (100-px, py), (px, 100-py), (100-px, 100-py)]
|
| 68 |
for idx, (cx, cy) in enumerate(coords):
|
| 69 |
-
owner = idx if i == 0 else -1
|
| 70 |
self.planets.append(Planet(len(self.planets), owner, cx, cy, prod))
|
| 71 |
|
| 72 |
def step(self):
|
| 73 |
for p in self.planets: p.update()
|
| 74 |
-
|
| 75 |
for f in self.fleets[:]:
|
| 76 |
f.update()
|
| 77 |
-
# ☀️ الشمس تقتل السفن
|
| 78 |
if math.dist((f.x, f.y), (50, 50)) < SUN_RADIUS:
|
| 79 |
self.fleets.remove(f)
|
| 80 |
continue
|
| 81 |
-
|
| 82 |
-
# تصادم السفن مع الكواكب (قتال)
|
| 83 |
for p in self.planets:
|
| 84 |
if math.dist((f.x, f.y), (p.x, p.y)) < p.radius:
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if f in self.fleets: self.fleets.remove(f)
|
| 87 |
break
|
| 88 |
-
self.run_ai()
|
| 89 |
-
|
| 90 |
-
def resolve_combat(self, fleet, planet):
|
| 91 |
-
if planet.owner == fleet.owner:
|
| 92 |
-
planet.ships += fleet.ships
|
| 93 |
-
else:
|
| 94 |
-
planet.ships -= fleet.ships
|
| 95 |
-
if planet.ships < 0:
|
| 96 |
-
planet.owner = fleet.owner
|
| 97 |
-
planet.ships = abs(planet.ships)
|
| 98 |
-
|
| 99 |
-
def run_ai(self):
|
| 100 |
-
# ذكاء اصطناعي بسيط يهاجم أقرب كوكب ضعيف
|
| 101 |
-
for p in self.planets:
|
| 102 |
-
if p.owner > 0: # ليس اللاعب البشري
|
| 103 |
-
targets = [target for target in self.planets if target.owner != p.owner]
|
| 104 |
-
if targets and p.ships > 20:
|
| 105 |
-
target = min(targets, key=lambda t: math.dist((p.x, p.y), (t.x, t.y)))
|
| 106 |
-
angle = math.atan2(target.y - p.y, target.x - p.x)
|
| 107 |
-
send_count = int(p.ships * 0.5)
|
| 108 |
-
p.ships -= send_count
|
| 109 |
-
self.fleets.append(Fleet(p.owner, p.x, p.y, angle, send_count))
|
| 110 |
|
| 111 |
game = GameEngine()
|
| 112 |
|
| 113 |
-
def
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
|
|
|
| 117 |
return f"""
|
| 118 |
-
<div style="background:#
|
| 119 |
-
<canvas id="
|
| 120 |
</div>
|
| 121 |
<script>
|
| 122 |
(function() {{
|
| 123 |
-
const
|
| 124 |
-
const ctx =
|
| 125 |
-
const
|
| 126 |
-
const fleets = {f_json};
|
| 127 |
-
const s = 5; // Scale
|
| 128 |
|
| 129 |
ctx.clearRect(0,0,500,500);
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
| 132 |
ctx.beginPath(); ctx.arc(250, 250, {SUN_RADIUS}*s, 0, 7); ctx.fill();
|
| 133 |
|
| 134 |
// Planets
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
ctx.beginPath(); ctx.arc(p.x*s, p.y*s, p.r*s, 0, 7); ctx.fill();
|
| 138 |
-
ctx.fillStyle = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}});
|
| 140 |
|
| 141 |
// Fleets
|
| 142 |
-
|
| 143 |
-
|
|
|
|
| 144 |
ctx.beginPath(); ctx.arc(f.x*s, f.y*s, 2, 0, 7); ctx.fill();
|
| 145 |
}});
|
| 146 |
}})();
|
| 147 |
</script>
|
| 148 |
"""
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
with gr.Row():
|
| 158 |
-
step_btn = gr.Button("Step ⏩")
|
| 159 |
reset_btn = gr.Button("Reset 🔄")
|
| 160 |
|
| 161 |
with gr.Row():
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
demo.launch()
|
|
|
|
| 4 |
import random
|
| 5 |
|
| 6 |
# =========================
|
| 7 |
+
# إعدادات Kaggle الدقيقة
|
| 8 |
# =========================
|
| 9 |
BOARD_SIZE = 100
|
| 10 |
SUN_RADIUS = 10
|
|
|
|
| 17 |
self.x = x
|
| 18 |
self.y = y
|
| 19 |
self.production = production
|
| 20 |
+
# المعادلة الأصلية: 1 + ln(production)
|
| 21 |
+
self.radius = 1 + math.log(production + 1)
|
| 22 |
+
self.ships = 10 if owner != -1 else random.randint(20, 100)
|
| 23 |
|
|
|
|
| 24 |
self.dist_to_center = math.dist((x, y), (50, 50))
|
| 25 |
+
# الكواكب القريبة تدور (مدار < 45)
|
| 26 |
self.is_orbiting = self.dist_to_center < 45
|
| 27 |
self.angle = math.atan2(y - 50, x - 50)
|
| 28 |
+
self.angular_vel = random.uniform(0.02, 0.04)
|
| 29 |
|
| 30 |
def update(self):
|
|
|
|
| 31 |
if self.is_orbiting:
|
| 32 |
self.angle += self.angular_vel
|
| 33 |
self.x = 50 + math.cos(self.angle) * self.dist_to_center
|
| 34 |
self.y = 50 + math.sin(self.angle) * self.dist_to_center
|
|
|
|
|
|
|
| 35 |
if self.owner != -1:
|
| 36 |
self.ships += self.production
|
| 37 |
|
| 38 |
class Fleet:
|
| 39 |
def __init__(self, owner, x, y, angle, ships):
|
| 40 |
self.owner = owner
|
| 41 |
+
self.x, self.y = x, y
|
|
|
|
| 42 |
self.angle = angle
|
| 43 |
self.ships = ships
|
| 44 |
+
# سرعة لوغاريتمية حسب حجم الأسطول
|
| 45 |
log_ratio = math.log(max(1, ships)) / math.log(1000)
|
| 46 |
self.speed = 1.0 + (MAX_SHIP_SPEED - 1.0) * math.pow(min(1.0, log_ratio), 1.5)
|
| 47 |
|
|
|
|
| 56 |
def reset(self):
|
| 57 |
self.planets = []
|
| 58 |
self.fleets = []
|
| 59 |
+
# إنشاء كواكب بتناظر رباعي (4-fold symmetry)
|
| 60 |
+
for i in range(6):
|
| 61 |
+
px, py = random.uniform(15, 40), random.uniform(15, 40)
|
| 62 |
prod = random.randint(1, 5)
|
| 63 |
+
# التناظر الرباعي حول المركز (50, 50)
|
| 64 |
coords = [(px, py), (100-px, py), (px, 100-py), (100-px, 100-py)]
|
| 65 |
for idx, (cx, cy) in enumerate(coords):
|
| 66 |
+
owner = idx if i == 0 else -1 # اللاعب 0 والخصم 1..3
|
| 67 |
self.planets.append(Planet(len(self.planets), owner, cx, cy, prod))
|
| 68 |
|
| 69 |
def step(self):
|
| 70 |
for p in self.planets: p.update()
|
|
|
|
| 71 |
for f in self.fleets[:]:
|
| 72 |
f.update()
|
|
|
|
| 73 |
if math.dist((f.x, f.y), (50, 50)) < SUN_RADIUS:
|
| 74 |
self.fleets.remove(f)
|
| 75 |
continue
|
|
|
|
|
|
|
| 76 |
for p in self.planets:
|
| 77 |
if math.dist((f.x, f.y), (p.x, p.y)) < p.radius:
|
| 78 |
+
if p.owner == f.owner: p.ships += f.ships
|
| 79 |
+
else:
|
| 80 |
+
p.ships -= f.ships
|
| 81 |
+
if p.ships < 0:
|
| 82 |
+
p.owner = f.owner
|
| 83 |
+
p.ships = abs(p.ships)
|
| 84 |
if f in self.fleets: self.fleets.remove(f)
|
| 85 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
game = GameEngine()
|
| 88 |
|
| 89 |
+
def render_html():
|
| 90 |
+
p_data = [{"id":p.id, "x":p.x, "y":p.y, "r":p.radius, "o":p.owner, "s":p.ships} for p in game.planets]
|
| 91 |
+
f_data = [{"x":f.x, "y":f.y, "o":f.owner} for f in game.fleets]
|
| 92 |
|
| 93 |
+
# JavaScript للرسم - تم تحسينه ليعمل فوراً
|
| 94 |
return f"""
|
| 95 |
+
<div style="background:#111; display:flex; justify-content:center; padding:20px;">
|
| 96 |
+
<canvas id="gameCanvas" width="500" height="500" style="background:#000; border:2px solid #444; border-radius:50%; box-shadow: 0 0 20px #000;"></canvas>
|
| 97 |
</div>
|
| 98 |
<script>
|
| 99 |
(function() {{
|
| 100 |
+
const c = document.getElementById('gameCanvas');
|
| 101 |
+
const ctx = c.getContext('2d');
|
| 102 |
+
const s = 5; // Scale 100 to 500
|
|
|
|
|
|
|
| 103 |
|
| 104 |
ctx.clearRect(0,0,500,500);
|
| 105 |
+
|
| 106 |
+
// Draw Sun
|
| 107 |
+
const grad = ctx.createRadialGradient(250,250,5,250,250,50);
|
| 108 |
+
grad.addColorStop(0, "yellow"); grad.addColorStop(1, "rgba(255,165,0,0)");
|
| 109 |
+
ctx.fillStyle = grad;
|
| 110 |
ctx.beginPath(); ctx.arc(250, 250, {SUN_RADIUS}*s, 0, 7); ctx.fill();
|
| 111 |
|
| 112 |
// Planets
|
| 113 |
+
const pData = {json.dumps(p_data)};
|
| 114 |
+
pData.forEach(p => {{
|
| 115 |
+
ctx.fillStyle = p.o == 0 ? "#00a2ff" : (p.o == -1 ? "#555" : "#ff4444");
|
| 116 |
ctx.beginPath(); ctx.arc(p.x*s, p.y*s, p.r*s, 0, 7); ctx.fill();
|
| 117 |
+
ctx.fillStyle = "white"; ctx.font = "bold 10px Arial";
|
| 118 |
+
ctx.fillText(Math.floor(p.s), p.x*s-5, p.y*s+3);
|
| 119 |
+
// ID
|
| 120 |
+
ctx.fillStyle = "#aaa"; ctx.font = "8px Arial";
|
| 121 |
+
ctx.fillText("ID:"+p.id, p.x*s-10, p.y*s-10);
|
| 122 |
}});
|
| 123 |
|
| 124 |
// Fleets
|
| 125 |
+
const fData = {json.dumps(f_data)};
|
| 126 |
+
fData.forEach(f => {{
|
| 127 |
+
ctx.fillStyle = f.o == 0 ? "#00a2ff" : "#ff4444";
|
| 128 |
ctx.beginPath(); ctx.arc(f.x*s, f.y*s, 2, 0, 7); ctx.fill();
|
| 129 |
}});
|
| 130 |
}})();
|
| 131 |
</script>
|
| 132 |
"""
|
| 133 |
|
| 134 |
+
def run_step():
|
| 135 |
+
game.step()
|
| 136 |
+
return render_html()
|
| 137 |
+
|
| 138 |
+
def launch_fleet(sid, angle, amount):
|
| 139 |
+
p = game.planets[int(sid)]
|
| 140 |
+
if p.owner == 0 and p.ships >= amount:
|
| 141 |
+
p.ships -= amount
|
| 142 |
+
game.fleets.append(Fleet(0, p.x, p.y, math.radians(angle), amount))
|
| 143 |
+
return render_html()
|
| 144 |
+
|
| 145 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 146 |
+
gr.Markdown("# 🚀 Orbit Wars - RUI Company Dashboard")
|
| 147 |
+
canvas = gr.HTML(render_html())
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
+
step_btn = gr.Button("Step ⏩", variant="primary")
|
| 151 |
reset_btn = gr.Button("Reset 🔄")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
+
with gr.Column():
|
| 155 |
+
src_id = gr.Number(label="From Planet (ID)", value=0)
|
| 156 |
+
launch_angle = gr.Slider(0, 360, label="Angle 📐")
|
| 157 |
+
launch_num = gr.Number(label="Ships 🚢", value=10)
|
| 158 |
+
launch_btn = gr.Button("Launch! 🚀")
|
| 159 |
+
|
| 160 |
+
step_btn.click(run_step, outputs=canvas)
|
| 161 |
+
reset_btn.click(lambda: (game.reset(), render_html())[1], outputs=canvas)
|
| 162 |
+
launch_btn.click(launch_fleet, [src_id, launch_angle, launch_num], outputs=canvas)
|
| 163 |
|
| 164 |
demo.launch()
|