home · contact · privacy
Moved game exiting into its own function.
[plomrogue] / src / main.c
index 53784b8a9e6edb545e4d7b10b2dd8546e25d5946..693f204d9f785d6034e765a33c1a5eb173bbee95 100644 (file)
@@ -8,6 +8,7 @@
 #include "keybindings.h"
 #include "readwrite.h"
 #include "map_objects.h"
+#include "map_object_actions.h"
 #include "map.h"
 #include "misc.h"
 
@@ -29,7 +30,7 @@ int main (int argc, char *argv[]) {
       default:
         exit(EXIT_FAILURE); } }
 
-  // Initialize log, player, monsters and items.
+  // Initialize log, player, monster/item definitions and monsters/items.
   world.log = calloc(1, sizeof(char));
   update_log (&world, " ");
   struct Player player;
@@ -37,34 +38,7 @@ int main (int argc, char *argv[]) {
   world.player = &player;
   world.monster = 0;
   world.item = 0;
-  struct MonsterDef monster_def_A;
-  monster_def_A.map_obj_def.id = 1;
-  monster_def_A.map_obj_def.mapchar = 'a';
-  monster_def_A.map_obj_def.desc = "ANT";
-  monster_def_A.hitpoints_start = 1;
-  struct MonsterDef monster_def_B;
-  monster_def_B.map_obj_def.id = 2;
-  monster_def_B.map_obj_def.mapchar = 'z';
-  monster_def_B.map_obj_def.desc = "ZOMBIE";
-  monster_def_B.hitpoints_start = 3;
-  struct MonsterDef monster_def_C;
-  monster_def_C.map_obj_def.id = 3;
-  monster_def_C.map_obj_def.mapchar = 'S';
-  monster_def_C.map_obj_def.desc = "SHOGGOTH";
-  monster_def_C.hitpoints_start = 9;
-  world.monster_def = &monster_def_A;
-  monster_def_A.map_obj_def.next = (struct MapObjDef *) &monster_def_B;
-  monster_def_B.map_obj_def.next = (struct MapObjDef *) &monster_def_C;
-  monster_def_C.map_obj_def.next = NULL;
-  struct ItemDef item_def_A;
-  item_def_A.map_obj_def.id = 4;
-  item_def_A.map_obj_def.mapchar = '#';
-  struct ItemDef item_def_B;
-  item_def_B.map_obj_def.id = 5;
-  item_def_B.map_obj_def.mapchar = '%';
-  world.item_def = &item_def_A;
-  item_def_A.map_obj_def.next = (struct MapObjDef *) &item_def_B;
-  item_def_B.map_obj_def.next = NULL;
+  init_map_object_defs(&world, "defs");
 
   // For interactive mode, try to load world state from savefile.
   FILE * file;
@@ -76,7 +50,7 @@ int main (int argc, char *argv[]) {
     player.pos.x = read_uint16_bigendian(file) - 1;
     player.hitpoints = fgetc(file);
     read_map_objects (&world.monster, file, sizeof(struct Monster), read_map_objects_monsterdata);
-    read_map_objects (&world.item,    file, sizeof(struct Item),    readwrite_map_objects_dummy);
+    read_map_objects (&world.item,    file, sizeof(struct Item),    NULL);
     fclose(file); }
 
   // For non-interactive mode, try to load world state from record file.
@@ -99,15 +73,15 @@ int main (int argc, char *argv[]) {
   world.map = ↦
   if (1 == world.turn) {
     player.pos = find_passable_pos(&map);
-    void * foo = build_map_objects (&world, &world.monster, 1, 1 + rrand(0,0) % 27, sizeof(struct Monster),
+    void * foo = build_map_objects (&world, &world.monster, 0, 1 + rrand(0,0) % 27, sizeof(struct Monster),
                                     build_map_objects_monsterdata);
-    foo = build_map_objects (&world, foo, 2, 1 + rrand(0,0) % 9, sizeof(struct Monster),
+    foo = build_map_objects (&world, foo, 1, 1 + rrand(0,0) % 9, sizeof(struct Monster),
                              build_map_objects_monsterdata);
-    build_map_objects (&world, foo, 3, 1 + rrand(0,0) % 3, sizeof(struct Monster),
+    build_map_objects (&world, foo, 2, 1 + rrand(0,0) % 3, sizeof(struct Monster),
                        build_map_objects_monsterdata);
-    foo = build_map_objects (&world, &world.item, 4, 1 + rrand(0,0) % 3, sizeof(struct Item),
+    foo = build_map_objects (&world, &world.item, 3, 1 + rrand(0,0) % 3, sizeof(struct Item),
                              build_map_objects_itemdata);
-    build_map_objects (&world, foo, 5, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); }
+    build_map_objects (&world, foo, 4, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); }
 
   // Initialize window system and windows.
   WINDOW * screen = initscr();
@@ -161,7 +135,7 @@ int main (int argc, char *argv[]) {
       else
         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
         if (1 == quit_called)
-          break; } }
+          exit_game(&world, &map); } }
 
   // Interactive mode.
   else {
@@ -187,14 +161,4 @@ int main (int argc, char *argv[]) {
       else
         quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
         if (1 == quit_called)
-          break; } }
-
-  // Clean up and exit.
-  free(map.cells);
-  for (key = 0; key <= world.keyswindata->max; key++)
-    free(world.keybindings[key].name);
-  free(world.keybindings);
-  free(world.keyswindata);
-  free(world.log);
-  endwin();
-  return 0; }
+          exit_game(&world, &map); } } }