Ejemplo n.º 1
0
int parseGame(const char *filename) {

   initParser();

   if (!parseGameFile(filename)) {
      g_outputError("failed to parse game file %s\n", filename);
      return 0;
   }

   /* make sure a room named "start" exists */
   if (NULL == g_hash_table_lookup(roomParsedTable, "start")) {
      g_outputError("room 'start' must be defined\n");
      return 0;
   }

   initGlobalEvents();
   initObjects();
   initCreatures();
   initRooms();

   /* make sure we didn't have any issues parsing Lua scripts */
   if (g_scriptError) {
      return 0;
   }

   destroyParser();
   return 1;
}
Ejemplo n.º 2
0
static int l_outputError(lua_State *L) {

   const char *string;
   int n = lua_gettop(L);

   /* not passing in any arguments results in outputting a new line */
   if (0 == n) {
      g_outputError("\n");
      return 0;
   }

   string = lua_tostring(L, 1);
   g_outputError(string);
   return 0;
}
Ejemplo n.º 3
0
void initTimer() {

   pthread_t timerThread;

   gameTime = 0;
   timerActive = 1;

   pthread_mutex_init(&timerMutex, NULL);

   if (pthread_create(&timerThread, NULL, timer, NULL)) {
      g_outputError("Failed to start timer!\n");
      exit(EXIT_FAILURE);
   }
}
Ejemplo n.º 4
0
static void connectRooms() {

   GList *roomNames = g_hash_table_get_keys(g_rooms);
   GList *currentRoomName = roomNames;

   while (currentRoomName != NULL) {

      Room *room = g_hash_table_lookup(g_rooms, currentRoomName->data);
      Room *connected = NULL;
      RoomParsed *roomDefinition = g_hash_table_lookup(roomParsedTable,
         currentRoomName->data);

      if (NULL != roomDefinition->north) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->north));

         /* game file specified a room that wasn't defined! */ 
         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->north), dstrview(roomDefinition->north));
            exit(EXIT_FAILURE);
         }

         room->north = connected;
      }

      if (NULL != roomDefinition->south) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->south));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->south), dstrview(roomDefinition->south));
            exit(EXIT_FAILURE);
         }

         room->south = connected;
      }

      if (NULL != roomDefinition->east) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->east));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->east), dstrview(roomDefinition->east));
            exit(EXIT_FAILURE);
         }

         room->east = connected;
      }

      if (NULL != roomDefinition->west) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->west));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->west), dstrview(roomDefinition->west));
            exit(EXIT_FAILURE);
         }

         room->west = connected;
      }

      if (NULL != roomDefinition->in) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->in));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->in), dstrview(roomDefinition->in));
            exit(EXIT_FAILURE);
         }

         room->in = connected;
      }

      if (NULL != roomDefinition->out) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->out));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->out), dstrview(roomDefinition->out));
            exit(EXIT_FAILURE);
         }

         room->out = connected;
      }

      if (NULL != roomDefinition->up) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->up));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->up), dstrview(roomDefinition->up));
            exit(EXIT_FAILURE);
         }

         room->up = connected;
      }

      if (NULL != roomDefinition->down) {

         connected = g_hash_table_lookup(g_rooms, dstrview(roomDefinition->down));

         if (NULL == connected) {
            g_outputError("error: room '%s' connects to room '%s', but "
               "room '%s' doesn't exist!\n", dstrview(room->name),
               dstrview(roomDefinition->down), dstrview(roomDefinition->down));
            exit(EXIT_FAILURE);
         }

         room->down = connected;
      }

      currentRoomName = currentRoomName->next;
   }

   g_list_free(roomNames);
   return;
}
Ejemplo n.º 5
0
static Creature *initCreature(CreatureParsed *creatureParsed) {

   int i;
   GList *nextEventHandler;
   Creature *creature = createCreature(FALSE);
   GArray *objectNames = creatureParsed->objects;

   creature->name = creatureParsed->name;
   creature->title = creatureParsed->title;
   creature->description = creatureParsed->description;
   creature->messages = creatureParsed->messages;
   creature->attackable = creatureParsed->attackable;
   creature->woundRate = creatureParsed->woundRate;
   creature->counterattack = creatureParsed->counterattack;

   /* creature's attributes */
   creature->attributes.strength = creatureParsed->attributes.strength;
   creature->attributes.dexterity = creatureParsed->attributes.dexterity;
   creature->attributes.intelligence = creatureParsed->attributes.intelligence;
   creature->attributes.initialTotal = creature->attributes.strength +
      creature->attributes.dexterity + creature->attributes.intelligence;

   /* health settings */
   creature->maxHealth = creatureParsed->maxHealth;
   creature->state.health = creatureParsed->health;
   creature->state.alive = creatureParsed->alive;

   if (NULL != creatureParsed->allegiance) {

      if (0 == strcmp(dstrview(creatureParsed->allegiance), "neutral")) {
         creature->allegiance = CREATURE_ALLEGIANCE_NEUTRAL;
      }
      
      else if (0 == strcmp(dstrview(creatureParsed->allegiance), "friend")) {
         creature->allegiance = CREATURE_ALLEGIANCE_FRIEND;
      }
      
      else if (0 == strcmp(dstrview(creatureParsed->allegiance), "enemy")) {
         creature->allegiance = CREATURE_ALLEGIANCE_ENEMY;
      }
      
      else {
         g_outputError("%s is not a valid allegiance for creature '%s'!\n",
            dstrview(creatureParsed->allegiance),
            dstrview(creatureParsed->name));
         exit(EXIT_FAILURE);
      }
   }

   /* initialize lua state for creature */
   creature->L = initLuaState(creatureParsed->scripts);

   /* initialize entity-specific event handlers */
   nextEventHandler = creatureParsed->eventHandlers;
   while (NULL != nextEventHandler) {
      EventHandlerParsed *handler = (EventHandlerParsed *)nextEventHandler->data;
      addLuaEventHandler(dstrview(handler->event), creature->events,
         dstrview(handler->function), creature->L);
      nextEventHandler = g_list_next(nextEventHandler);
   }

   /* add objects to the creature's inventory */
   for (i = 0; i < objectNames->len; i++) {

      dstring_t *objectName = g_array_index(objectNames, dstring_t, i);
      Object *object = g_hash_table_lookup(g_objects, (char *)dstrview(objectName));

      creature->inventory.list = addObject(creature->inventory.list,
         creature->inventory.byName, objectName);
      object->state.owner.entity = creature;
      object->state.owner.type = entity_creature;
   }

   return creature;
}