Skip to content

Commit

Permalink
split events into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLubar committed Mar 28, 2020
1 parent b3f5a98 commit 5a3b535
Show file tree
Hide file tree
Showing 87 changed files with 4,593 additions and 5,252 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
weblegends-git-describe.h
weblegends-git-describe.tmp.h
events.h
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${git_describe_tmp_h} ${git_describe_h})

file(READ ../../library/xml/df.history.xml EVENT_TYPES)
string(REGEX MATCH "<enum-type type-name='history_event_type'(<[^/]|[^<])*</enum-type>" EVENT_TYPES "${EVENT_TYPES}")
string(REGEX MATCHALL "<enum-item[^>]* name='[^']*" EVENT_TYPES "${EVENT_TYPES}")
list(TRANSFORM EVENT_TYPES REPLACE ".*'" "")

set(EVENT_HEADER_1 "#pragma once\n\n")
set(EVENT_HEADER_2 "\n#define EVENT_TYPES")

foreach(EVENT_TYPE IN LISTS EVENT_TYPES)
string(TOLOWER "${EVENT_TYPE}" EVENT_TYPE_LOWER)
string(APPEND EVENT_HEADER_1 "#include \"df/history_event_${EVENT_TYPE_LOWER}st.h\"\n")
string(APPEND EVENT_HEADER_2 " \\\n EVENT_TYPE(${EVENT_TYPE}, ${EVENT_TYPE_LOWER})")

if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/events/${EVENT_TYPE_LOWER}.cpp")
file(WRITE "events/${EVENT_TYPE_LOWER}.cpp" "#include \"../helpers_event.h\"\n\n#include \"df/history_event_${EVENT_TYPE_LOWER}st.h\"\n\nvoid do_event(std::ostream & s, const event_context & context, df::history_event_${EVENT_TYPE_LOWER}st *event)\n{\n // TODO\n do_event_missing(s, context, event, __FILE__, __LINE__);\n}\n")
endif()
endforeach()

file(WRITE events.h "${EVENT_HEADER_1}${EVENT_HEADER_2}\n")

add_subdirectory(thirdparty/embed-resource EXCLUDE_FROM_ALL)

SET(PROJECT_SRCS
Expand Down Expand Up @@ -57,10 +77,14 @@ SET(PROJECT_HDRS
helpers.h
debug.h
resource.h
events.h
weblegends-plugin.h
weblegends-git-describe.h
)

FILE(GLOB EVENT_SRCS events/*.cpp)
LIST(APPEND PROJECT_SRCS ${EVENT_SRCS})

SET_SOURCE_FILES_PROPERTIES( ${PROJECT_HDRS} PROPERTIES HEADER_FILE_ONLY TRUE)

LIST(APPEND PROJECT_SRCS ${PROJECT_HDRS})
Expand Down
13 changes: 13 additions & 0 deletions events/_missing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "../weblegends.h"
#include "../helpers.h"

void do_event_missing(std::ostream & s, const event_context &, df::history_event *event, const char *file, int line)
{
std::string df_description;
df::history_event_context df_context;
event->getSentence(&df_description, &df_context);
s << "<abbr title=\"" << html_escape(df_description) << "\">" << enum_item_key_str(event->getType()) << ":" << event->id << "</abbr>";
#ifdef WEBLEGENDS_DEBUG
weblegends_debug_log() << "[weblegends] [" << weblegends_basename(file, file) << ":" << line << "] missing event type handler for " << enum_item_key_str(event->getType()) << ": event-" << event->id << ": " << df_description << std::endl;
#endif
}
152 changes: 152 additions & 0 deletions events/add_hf_entity_link.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include "../helpers_event.h"

#include "df/history_event_add_hf_entity_linkst.h"

void do_event(std::ostream & s, const event_context & context, df::history_event_add_hf_entity_linkst *event)
{
auto ent = df::historical_entity::find(event->civ);
auto hf = df::historical_figure::find(event->histfig);
df::entity_position *pos;
BEFORE_SWITCH(type, event->link_type);
switch (type)
{
case histfig_entity_link_type::MEMBER:
event_link(s, context, hf);
s << " became a member of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_MEMBER:
event_link(s, context, hf);
s << " became a former member of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::MERCENARY:
event_link(s, context, hf);
s << " became a mercenary of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_MERCENARY:
event_link(s, context, hf);
s << " became a former mercenary of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::SLAVE:
event_link(s, context, hf);
s << " became a slave of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_SLAVE:
event_link(s, context, hf);
s << " became a former slave of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::PRISONER:
event_link(s, context, hf);
s << " became a prisoner of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_PRISONER:
event_link(s, context, hf);
s << " became a former prisoner of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::ENEMY:
event_link(s, context, hf);
s << " became an enemy of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::CRIMINAL:
event_link(s, context, hf);
s << " became a criminal of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::POSITION:
pos = binsearch_in_vector(ent->positions.own, event->position_id);
event_link(s, context, hf);
s << " became ";
if (pos->number == 1)
{
s << "the ";
}
else
{
s << "a ";
}
if (hf->sex == 0 && !pos->name_female[0].empty())
{
s << pos->name_female[0];
}
else if (hf->sex == 1 && !pos->name_male[0].empty())
{
s << pos->name_male[0];
}
else
{
s << pos->name[0];
}
s << " of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_POSITION:
pos = binsearch_in_vector(ent->positions.own, event->position_id);
event_link(s, context, hf);
s << " became a former ";
if (hf->sex == 0)
{
s << pos->name_female[0];
}
else if (hf->sex == 1)
{
s << pos->name_male[0];
}
else
{
s << pos->name[0];
}
s << " of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::POSITION_CLAIM:
pos = binsearch_in_vector(ent->positions.own, event->position_id);
event_link(s, context, hf);
s << " claimed the position of ";
if (hf->sex == 0)
{
s << pos->name_female[0];
}
else if (hf->sex == 1)
{
s << pos->name_male[0];
}
else
{
s << pos->name[0];
}
s << " of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::SQUAD:
event_link(s, context, hf);
s << " became a squad member of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_SQUAD:
event_link(s, context, hf);
s << " became a former squad member of ";
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::OCCUPATION:
event_link(s, context, hf);
s << " became a worker of ";
// TODO: get occupation type
event_link(s, context, ent);
BREAK(type);
case histfig_entity_link_type::FORMER_OCCUPATION:
event_link(s, context, hf);
s << " became a former worker of ";
// TODO: get occupation type
event_link(s, context, ent);
BREAK(type);
}
AFTER_SWITCH(type, stl_sprintf("event-%d (ADD_HF_ENTITY_LINK)", event->id));
}
110 changes: 110 additions & 0 deletions events/add_hf_hf_link.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "../helpers_event.h"

#include "df/history_event_add_hf_hf_linkst.h"

void do_event(std::ostream & s, const event_context & context, df::history_event_add_hf_hf_linkst *event)
{
auto hf = df::historical_figure::find(event->hf);
auto hf_target = df::historical_figure::find(event->hf_target);
BEFORE_SWITCH(type, event->type);
switch (type)
{
case histfig_hf_link_type::MOTHER:
event_link(s, context, hf);
s << " was born to ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::FATHER:
event_link(s, context, hf);
s << " was fathered by ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::SPOUSE:
event_link(s, context, hf);
s << " married ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::CHILD:
event_link(s, context, hf);
if (hf->sex == 1)
{
s << " fathered ";
}
else
{
s << " gave birth to ";
}
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::DEITY:
event_link(s, context, hf);
s << " began worshipping ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::LOVER:
event_link(s, context, hf);
s << " fell in love with ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::PRISONER:
event_link(s, context, hf);
s << " imprisoned ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::IMPRISONER:
event_link(s, context, hf);
s << " was imprisoned by ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::MASTER:
event_link(s, context, hf);
s << " started an apprenticeship under ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::APPRENTICE:
event_link(s, context, hf);
s << " accepted ";
event_link(s, context, hf_target);
s << " as an apprentice";
BREAK(type);
case histfig_hf_link_type::COMPANION:
event_link(s, context, hf);
s << " became the companion of ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::FORMER_MASTER:
event_link(s, context, hf);
s << " was no longer the master of ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::FORMER_APPRENTICE:
event_link(s, context, hf);
s << " was no longer the apprentice of ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::PET_OWNER:
if (auto race = hf ? df::creature_raw::find(hf->race) : nullptr)
{
auto caste = race->caste.at(hf->caste);
if (caste->flags.is_set(caste_raw_flags::ADOPTS_OWNER))
{
std::swap(hf, hf_target);
}
}
event_link(s, context, hf);
s << " was adopted as the pet of ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::FORMER_SPOUSE:
event_link(s, context, hf);
s << " was no longer the spouse of ";
event_link(s, context, hf_target);
BREAK(type);
case histfig_hf_link_type::DECEASED_SPOUSE:
event_link(s, context, hf);
s << " became the deceased spouse of ";
event_link(s, context, hf_target);
BREAK(type);
}
AFTER_SWITCH(type, stl_sprintf("event-%d (ADD_HF_HF_LINK)", event->id));
}
59 changes: 59 additions & 0 deletions events/add_hf_site_link.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "../helpers_event.h"

#include "df/history_event_add_hf_site_linkst.h"

void do_event(std::ostream & s, const event_context & context, df::history_event_add_hf_site_linkst *event)
{
auto site = df::world_site::find(event->site);
auto structure = site ? binsearch_in_vector(site->buildings, event->structure) : nullptr;
auto histfig = df::historical_figure::find(event->histfig);
auto civ = df::historical_entity::find(event->civ);

event_link(s, context, histfig);
BEFORE_SWITCH(type, event->type);
switch (type)
{
case histfig_site_link_type::OCCUPATION:
s << " started working at ";
BREAK(type);
case histfig_site_link_type::SEAT_OF_POWER:
s << " ruled from ";
BREAK(type);
case histfig_site_link_type::HANGOUT:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
case histfig_site_link_type::HOME_SITE_ABSTRACT_BUILDING:
s << " took up residence in ";
BREAK(type);
case histfig_site_link_type::HOME_SITE_REALIZATION_BUILDING:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
case histfig_site_link_type::LAIR:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
case histfig_site_link_type::HOME_SITE_REALIZATION_SUL:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
case histfig_site_link_type::HOME_SITE_SAVED_CIVZONE:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
case histfig_site_link_type::PRISON_ABSTRACT_BUILDING:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
case histfig_site_link_type::PRISON_SITE_BUILDING_PROFILE:
do_event_missing(s, context, event, __FILE__, __LINE__);
BREAK(type);
}
AFTER_SWITCH(type, stl_sprintf("event-%d (ADD_HF_SITE_LINK)", event->id));
event_link(s, context, structure);
if (civ)
{
s << " of ";
event_link(s, context, civ);
}
if (civ)
{
s << " in ";
event_link(s, context, site);
}
}
Loading

0 comments on commit 5a3b535

Please sign in to comment.