Prototype game engine for Heroes of Might & Magic, featuring a gameplay plot-twist...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

433 lines
18KB

  1. -- Copyright (c) 2024 - Ognjen 'xolatile' Milan Robovic
  2. --
  3. -- GNU General Public Licence (version 3 or later)
  4. pragma ada_2012;
  5. with core, ui, effect, attribute, skill, resource, faction, deity, material, magic, equipment, unit, construction, chad, world;
  6. with ada.strings.unbounded;
  7. use ada.strings.unbounded;
  8. use core;
  9. procedure main is
  10. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  11. type view is (
  12. map_preview_panel,
  13. status_preview_panel,
  14. text_box_panel,
  15. fullscreen
  16. );
  17. ------------------------------------------------------------------------------------------
  18. procedure swap_map_preview_panel;
  19. procedure swap_status_preview_panel;
  20. procedure swap_text_box_panel;
  21. procedure swap_fullscreen;
  22. procedure ui_main_style;
  23. procedure zoom_in;
  24. procedure zoom_out;
  25. ------------------------------------------------------------------------------------------
  26. signal_list : constant array (core.signal_code) of access procedure := (
  27. core.signal_up => world.player_up'access,
  28. core.signal_down => world.player_down'access,
  29. core.signal_left => world.player_left'access,
  30. core.signal_right => world.player_right'access,
  31. core.signal_v => ui_main_style'access,
  32. core.signal_kp_add => zoom_in'access,
  33. core.signal_kp_subtract => zoom_out'access,
  34. core.signal_f1 => world.resource_cheat_1'access,
  35. core.signal_f2 => world.resource_cheat_2'access,
  36. core.signal_f3 => world.resource_cheat_3'access,
  37. core.signal_f4 => world.resource_cheat_4'access,
  38. core.signal_f5 => world.resource_cheat_5'access,
  39. core.signal_f6 => world.resource_cheat_6'access,
  40. core.signal_f7 => world.reveal_map'access,
  41. core.signal_f8 => world.restore_points'access,
  42. others => core.idle_skip'access
  43. );
  44. view_show : array (view) of access procedure := (
  45. swap_map_preview_panel'access,
  46. swap_status_preview_panel'access,
  47. swap_text_box_panel'access,
  48. swap_fullscreen'access
  49. );
  50. view_icon : array (view) of core.sprite := (others => (others => 0));
  51. view_list : array (view) of boolean := (fullscreen => false, others => true);
  52. view_text : array (view) of core.long_string := (
  53. "Toggle map preview panel. ",
  54. "Toggle status preview panel. ",
  55. "Toggle text box panel. ",
  56. "Toggle fullscreen or windowed mode. "
  57. );
  58. game_title : core.sprite;
  59. game_preview : array (world.biome) of core.sprite;
  60. switch : natural := 0;
  61. choose : world.biome := world.grass;
  62. view_source_code : natural := 25;
  63. source_code : array (0 .. 35) of unbounded_string := (
  64. to_unbounded_string (core.folder & "/source/ai.adb"),
  65. to_unbounded_string (core.folder & "/source/ai.ads"),
  66. to_unbounded_string (core.folder & "/source/attribute.adb"),
  67. to_unbounded_string (core.folder & "/source/attribute.ads"),
  68. to_unbounded_string (core.folder & "/source/chad.adb"),
  69. to_unbounded_string (core.folder & "/source/chad.ads"),
  70. to_unbounded_string (core.folder & "/source/construction.adb"),
  71. to_unbounded_string (core.folder & "/source/construction.ads"),
  72. to_unbounded_string (core.folder & "/source/core.adb"),
  73. to_unbounded_string (core.folder & "/source/core.ads"),
  74. to_unbounded_string (core.folder & "/source/deity.adb"),
  75. to_unbounded_string (core.folder & "/source/deity.ads"),
  76. to_unbounded_string (core.folder & "/source/effect.adb"),
  77. to_unbounded_string (core.folder & "/source/effect.ads"),
  78. to_unbounded_string (core.folder & "/source/equipment.adb"),
  79. to_unbounded_string (core.folder & "/source/equipment.ads"),
  80. to_unbounded_string (core.folder & "/source/faction.adb"),
  81. to_unbounded_string (core.folder & "/source/faction.ads"),
  82. to_unbounded_string (core.folder & "/source/magic.adb"),
  83. to_unbounded_string (core.folder & "/source/magic.ads"),
  84. to_unbounded_string (core.folder & "/source/main.adb"),
  85. to_unbounded_string (core.folder & "/source/material.adb"),
  86. to_unbounded_string (core.folder & "/source/material.ads"),
  87. to_unbounded_string (core.folder & "/source/might.adb"),
  88. to_unbounded_string (core.folder & "/source/might.ads"),
  89. to_unbounded_string (core.folder & "/source/ray.ads"),
  90. to_unbounded_string (core.folder & "/source/resource.adb"),
  91. to_unbounded_string (core.folder & "/source/resource.ads"),
  92. to_unbounded_string (core.folder & "/source/skill.adb"),
  93. to_unbounded_string (core.folder & "/source/skill.ads"),
  94. to_unbounded_string (core.folder & "/source/ui.adb"),
  95. to_unbounded_string (core.folder & "/source/ui.ads"),
  96. to_unbounded_string (core.folder & "/source/unit.adb"),
  97. to_unbounded_string (core.folder & "/source/unit.ads"),
  98. to_unbounded_string (core.folder & "/source/world.adb"),
  99. to_unbounded_string (core.folder & "/source/world.ads")
  100. );
  101. side_panel : integer := 0;
  102. preview_width : integer := 0;
  103. preview_height : integer := 0;
  104. text_box_height : integer := 0;
  105. player : chad.information := (
  106. index => chad.ada,
  107. state => core.idle,
  108. x => 0,
  109. y => 0,
  110. health => (30, 40),
  111. mana => (20, 30),
  112. movement => (10, 20),
  113. attributes => attribute.default,
  114. skills => skill.default,
  115. resources => ((29, 480), (11, 240), (23, 240), (13, 240), (17, 240), (7, 240)),
  116. materials => material.default,
  117. equipments => (equipment.chest => equipment.iron_chestplate,
  118. equipment.head => equipment.iron_helmet,
  119. equipment.hands => equipment.iron_gauntlets,
  120. equipment.feet => equipment.iron_greaves,
  121. equipment.main_hand => equipment.iron_sword,
  122. others => equipment.none),
  123. item_count => 0,
  124. items => (others => equipment.none)
  125. );
  126. ------------------------------------------------------------------------------------------
  127. procedure swap_map_preview_panel is begin view_list (map_preview_panel) := not view_list (map_preview_panel); end swap_map_preview_panel;
  128. procedure swap_status_preview_panel is begin view_list (status_preview_panel) := not view_list (status_preview_panel); end swap_status_preview_panel;
  129. procedure swap_text_box_panel is begin view_list (text_box_panel) := not view_list (text_box_panel); end swap_text_box_panel;
  130. procedure swap_fullscreen is begin view_list (fullscreen) := not view_list (fullscreen); end swap_fullscreen;
  131. ------------------------------------------------------------------------------------------
  132. procedure ui_main_style is
  133. begin
  134. ui.active := ui.style'val ((ui.style'pos (ui.active) + 1) mod ui.style_count);
  135. end ui_main_style;
  136. ------------------------------------------------------------------------------------------
  137. procedure zoom_in is begin core.zoom := 2; end zoom_in;
  138. procedure zoom_out is begin core.zoom := 1; end zoom_out;
  139. ------------------------------------------------------------------------------------------
  140. --~procedure main_menu is
  141. --~begin
  142. --~core.draw (game_preview (world.ash), core.center_x (game_preview (world.ash).width * 2), core.center_y (game_preview (world.ash).height * 2), factor => 2);
  143. --~core.draw (game_title, core.center_x (game_title.width * 2), core.center_y (game_title.height * 2), factor => 2);
  144. --~--
  145. --~ui.write ("Main Menu", 0, 0);
  146. --~--
  147. --~ui.draw_check_box (0, 32, view_list (map_preview_panel), "map_preview_panel");
  148. --~ui.draw_check_box (0, 64, view_list (status_preview_panel), "status_preview_panel");
  149. --~ui.draw_check_box (0, 96, view_list (text_box_panel), "text_box_panel");
  150. --~end main_menu;
  151. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  152. begin
  153. core.dash;
  154. core.echo (core.comment, "Copyright (C) 2024 -- Ognjen 'xolatile' Milan Robovic");
  155. core.echo (core.comment, "Version -- 1.0.0");
  156. core.echo (core.comment, "License -- GNU/GPLv3+");
  157. core.echo (core.comment, "Xorana is free software, you can redistribute it and modify it under the terms of the GNU General Public License by Free Software Foundation.");
  158. core.dash;
  159. core.initialize;
  160. ui.active := ui.main;
  161. ui.configure;
  162. --~core.play (core.import_song (core.c_string (core.folder & "/song/main_menu.ogg")).index);
  163. core.echo (core.comment, "Configuring" & attribute.count'image & " attribute components...");
  164. --
  165. for index in attribute.enumeration loop
  166. attribute.icon (index) := core.import_sprite (core.folder & "/icon/attribute/" & core.lowercase (attribute.enumeration'image (index)) & ".png", 1, 1);
  167. end loop;
  168. core.echo (core.comment, "Configuring" & skill.count'image & " skill components...");
  169. --
  170. for index in skill.enumeration loop
  171. skill.icon (index) := core.import_sprite (core.folder & "/icon/skill/" & core.lowercase (skill.enumeration'image (index)) & ".png", 1, 1);
  172. end loop;
  173. core.echo (core.comment, "Configuring" & resource.count'image & " resource components...");
  174. --
  175. for index in resource.enumeration loop
  176. resource.icon (index) := core.import_sprite (core.folder & "/icon/resource/" & core.lowercase (resource.enumeration'image (index)) & ".png", 1, 1);
  177. end loop;
  178. core.echo (core.comment, "Configuring" & magic.count'image & " magic components...");
  179. --
  180. for index in magic.enumeration loop
  181. declare folder : constant string := core.lowercase (magic.school'image (magic.description (index).kind));
  182. file : constant string := core.lowercase (magic.enumeration'image (index));
  183. begin
  184. magic.view (index) := core.import_sprite (core.folder & "/view/magic/" & folder & "/" & file & ".png", 1, 1);
  185. end;
  186. end loop;
  187. core.echo (core.comment, "Configuring" & material.count'image & " material components...");
  188. --
  189. for index in material.enumeration loop
  190. material.icon (index) := core.import_sprite (core.folder & "/icon/material/" & core.lowercase (material.enumeration'image (index)) & ".png", 1, 1);
  191. end loop;
  192. core.echo (core.comment, "Configuring" & equipment.count'image & " equipment components...");
  193. --
  194. for index in equipment.enumeration loop
  195. declare folder : constant string := core.lowercase (equipment.slot'image (equipment.description (index).kind));
  196. file : constant string := core.lowercase (equipment.enumeration'image (index));
  197. begin
  198. equipment.sprite (index) := core.import_sprite (core.folder & "/game/equipment/" & folder & "/" & file & ".png", 4, 6);
  199. equipment.icon (index) := core.import_sprite (core.folder & "/icon/equipment/" & folder & "/" & file & ".png", 1, 1);
  200. end;
  201. end loop;
  202. unit.configure;
  203. core.echo (core.comment, "Configuring" & deity.count'image & " deity components...");
  204. --
  205. for index in deity.enumeration loop
  206. deity.sprite (index) := core.import_sprite (core.folder & "/game/deity/" & deity.enumeration'image (index) & ".png", 4, 1);
  207. end loop;
  208. core.echo (core.comment, "Configuring" & construction.count'image & "construction components...");
  209. --
  210. for index in construction.enumeration loop
  211. declare folder : constant string := core.lowercase (faction.enumeration'image (construction.description (index).kind));
  212. file : constant string := core.lowercase (construction.enumeration'image (index));
  213. frames : constant integer := construction.description (index).frames;
  214. begin
  215. construction.sprite (index) := core.import_sprite (core.folder & "/game/construction/" & folder & "/" & file & ".png", frames, 1);
  216. end;
  217. end loop;
  218. chad.configure;
  219. world.configure;
  220. --~ai.configure;
  221. world.make (world.grass, 640, 480, 8);
  222. world.add_chad (player);
  223. core.dash;
  224. core.echo (core.success, "Successfully initialized game data, entering main gameplay loop.");
  225. core.dash;
  226. for index in view loop
  227. view_icon (index) := core.import_sprite (core.folder & "/icon/engine/" & core.lowercase (view'image (index)) & ".png", 1, 2);
  228. end loop;
  229. game_title := core.import_sprite (core.folder & "/ui/game_title.png", 1, 1);
  230. for index in world.biome loop
  231. game_preview (index) := core.import_sprite (core.folder & "/ui/preview/" & core.lowercase (world.biome'image (index)) & "land.png", 1, 1);
  232. end loop;
  233. ------------------------------------------------------------------------------------------
  234. introduction_loop: loop
  235. core.synchronize;
  236. --
  237. exit when core.signal_mode = core.signal_space or core.cursor_mode = core.cursor_right;
  238. --
  239. case core.signal_mode is
  240. when core.signal_none => null;
  241. when core.signal_space => null;
  242. when others => switch := (switch + 1) mod world.biome_count;
  243. choose := world.biome'val (switch);
  244. end case;
  245. --
  246. core.draw (game_preview (choose), core.center_x (game_preview (choose).width * 2), core.center_y (game_preview (choose).height * 2), factor => 2);
  247. core.draw (game_title, core.center_x (game_title.width * 2), core.center_y (game_title.height * 2), factor => 2);
  248. --
  249. ui.write ("[-- Press Spacebar or RMB to continue]", 0, core.center_y (24), (102, 102, 102, 255));
  250. end loop introduction_loop;
  251. core.cursor_mode := core.cursor_none;
  252. main_menu_loop: loop
  253. core.synchronize;
  254. --
  255. exit when core.signal_mode = core.signal_space or core.cursor_mode = core.cursor_right;
  256. --
  257. declare source_code_data : core.string_box_data;
  258. begin
  259. core.import_text (source_code_data, to_string (source_code (view_source_code)));
  260. --
  261. if core.cursor_mode = core.cursor_left then
  262. view_source_code := (view_source_code + 1) mod 36;
  263. core.cursor_mode := core.cursor_none;
  264. core.wheel := 0.0;
  265. end if;
  266. --
  267. ui.write_ada_code (source_code_data, 0, integer (core.wheel) * 30);
  268. end;
  269. end loop main_menu_loop;
  270. core.cursor_mode := core.cursor_none;
  271. world.load ("heyo");
  272. ui.active := ui.style'val (faction.enumeration'pos (chad.description (player.index).kind) + 1);
  273. gameplay_loop: loop
  274. core.synchronize;
  275. --
  276. exit when core.engine_active = false;
  277. --
  278. if not view_list (status_preview_panel) then
  279. side_panel := 0;
  280. else
  281. side_panel := 376 + 2 * 32;
  282. end if;
  283. --
  284. preview_width := core.window_width - side_panel;
  285. preview_height := core.window_height - text_box_height;
  286. text_box_height := 32;
  287. --
  288. world.draw;
  289. --
  290. if view_list (map_preview_panel) then
  291. ui.draw_menu (0, 0, preview_width, preview_height);
  292. end if;
  293. --
  294. if view_list (status_preview_panel) then
  295. ui.draw_tiny_menu (preview_width, 0, side_panel, preview_height);
  296. chad.draw_data (world.map.chads (1), preview_width + 32, 32);
  297. --
  298. ui.draw_end_turn_button (x => core.window_width - side_panel + side_panel / 2 - core.icon,
  299. y => core.window_height - core.icon - text_box_height - 106 - 2 * core.icon);
  300. --
  301. ui.draw_console_box (x => core.window_width - core.icon - (side_panel - 2 * core.icon),
  302. y => core.window_height - core.icon - text_box_height - 106,
  303. width => side_panel - 2 * core.icon,
  304. height => 106);
  305. --~ui.draw_state_box (preview_width + 32, 32);
  306. end if;
  307. --
  308. if view_list (text_box_panel) then
  309. ui.draw_help_box (0, core.window_height - text_box_height, core.window_width - core.icon * (view'pos (view'last) + 1), text_box_height);
  310. --
  311. ui.write (core.framerate'image, core.window_width - core.icon * (view'pos (view'last) + 1) - 64, core.window_height - 27);
  312. end if;
  313. --
  314. for index in view loop
  315. ui.draw_frame (description => view_text (index),
  316. x => core.window_width - core.icon * (view'pos (view'last) + 1) + core.icon * view'pos (index),
  317. y => core.window_height - core.icon,
  318. width => core.icon,
  319. height => core.icon,
  320. action => view_show (index));
  321. --
  322. core.draw (data => view_icon (index),
  323. x => core.window_width - core.icon * (view'pos (view'last) + 1) + core.icon * view'pos (index),
  324. y => core.window_height - core.icon,
  325. ignore => true,
  326. u => 0,
  327. v => boolean'pos (view_list (index)) * core.icon,
  328. factor => 1);
  329. end loop;
  330. --
  331. for index in resource.enumeration loop
  332. ui.draw_icon (resource.icon (index), -(resource.description (index).text), (preview_width - 6 * core.icon * resource.count) / 2 + (6 * core.icon) * resource.enumeration'pos (index), core.base);
  333. ui.draw_frame (-(resource.description (index).text), (preview_width - 6 * core.icon * resource.count) / 2 + (6 * core.icon) * resource.enumeration'pos (index) + core.icon, core.base, 6 * core.icon, core.icon);
  334. --
  335. ui.write (text => world.map.chads (1).resources (index).value'image & " /" & world.map.chads (1).resources (index).limit'image,
  336. x => (preview_width - 6 * core.icon * resource.count) / 2 + (6 * core.icon) * resource.enumeration'pos (index) + core.icon - 1,
  337. y => core.base + 7,
  338. size => 18);
  339. end loop;
  340. --
  341. declare move_x : integer := (preview_width - core.icon * material.count) / 2;
  342. begin
  343. for index in material.enumeration loop
  344. if world.map.chads (1).materials (index).value > 0 then
  345. ui.draw_icon (material.icon (index), -(material.description (index).name), move_x, core.base + core.icon);
  346. ui.draw_text_box (move_x, core.base + core.icon + core.icon, core.icon, core.icon);
  347. ui.write (world.map.chads (1).materials (index).value'image, move_x, core.base + core.icon + core.icon + 8, (255, 255, 255, 255), 15, true);
  348. --
  349. move_x := move_x + core.icon;
  350. end if;
  351. end loop;
  352. end;
  353. --
  354. signal_list (core.signal_mode).all;
  355. --
  356. core.camera.x := world.map.chads (1).x;
  357. core.camera.y := world.map.chads (1).y;
  358. --
  359. --~world.draw_performance_box;
  360. --
  361. ui.synchronize;
  362. end loop gameplay_loop;
  363. world.save ("heyo");
  364. --~world.mapshot (folder & "/mapshot.png");
  365. ------------------------------------------------------------------------------------------
  366. core.deinitialize;
  367. core.dash;
  368. end main;