Skip to content

Commit af258a2

Browse files
committed
Transition to primarily C++-based implementation.
1 parent c8c1863 commit af258a2

File tree

15 files changed

+110
-101
lines changed

15 files changed

+110
-101
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
cmake_minimum_required(VERSION 3.1.0)
1+
cmake_minimum_required(VERSION 3.15.0)
22

33
project(
44
R136
5-
VERSION 3.2.0)
5+
VERSION 3.3.0)
66

77
set(CURSES_NEED_NCURSES 1)
88
set(CURSES_NEED_WIDE 1)

include/inlines/console.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,58 @@
44

55
#include "types/console.h"
66

7+
inline ColorSet::ColorSet(Color color, short foreground, short background) : ColorSet(color, foreground, background, 0) {}
8+
9+
inline ColorSet::ColorSet(Color color, short foreground, short background, chtype style) :
10+
color(color),
11+
foreground(foreground),
12+
background(background),
13+
style(style),
14+
value(),
15+
is_initialized(false)
16+
{}
17+
18+
inline Color ColorSet::get_color()
19+
{
20+
return color;
21+
}
22+
23+
inline chtype ColorSet::get_attrs()
24+
{
25+
if (!is_initialized)
26+
initialize();
27+
28+
return value;
29+
}
30+
31+
inline void ColorMap::add(Color color, short foreground, short background, chtype style)
32+
{
33+
add(new ColorSet(color, foreground, background, style));
34+
}
35+
36+
inline void ColorMap::add(Color color, short foreground, short background)
37+
{
38+
add(new ColorSet(color, foreground, background));
39+
}
40+
41+
inline void ColorMap::add(ColorSet* set)
42+
{
43+
auto element = color_sets.find(set->get_color());
44+
if (element == color_sets.end())
45+
color_sets.insert(std::make_pair(set->get_color(), set));
46+
else
47+
element->second = set;
48+
}
49+
50+
inline chtype ColorMap::get_attrs(Color color)
51+
{
52+
auto element = color_sets.find(color);
53+
if (element == color_sets.end())
54+
throw std::out_of_range("color");
55+
56+
return element->second->get_attrs();
57+
}
58+
759
inline Window::Window(WINDOW* wnd) : Window(wnd, false, Color::undefined) {}
860

961
inline Window::Window(WINDOW* wnd, bool enable_keypad) : Window(wnd, enable_keypad, Color::undefined) {}

include/inlines/r136.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
#include <thread>
66

77
inline CoreData::CoreData() :
8+
animates(EntityMap<AnimateID, Animate>(true)),
9+
items(EntityMap<ItemID, Item>(true)),
10+
rooms(EntityMap<RoomID, Room>(true)),
811
inventory(Inventory(max_owned_items))
12+
913
{}
1014

1115
inline size_t RoomConnections::count() const

include/templates/r136.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,27 @@ typename std::vector<TEntity>::iterator BoundedCollection<TEntity>::end()
8282
}
8383

8484
template<class TKey, class TValue>
85-
void EntityMap<TKey, TValue>::add_or_set(TKey key, TValue& value)
85+
inline EntityMap<TKey, TValue>::EntityMap(bool delete_values) :
86+
delete_values(delete_values)
87+
{}
88+
89+
template<class TKey, class TValue>
90+
inline EntityMap<TKey, TValue>::~EntityMap()
8691
{
92+
if (!delete_values)
93+
return;
94+
95+
for (auto element = map.begin(); element != map.end();)
96+
{
97+
delete element->second;
98+
element = map.erase(element);
99+
}
100+
}
101+
102+
template<class TKey, class TValue>
103+
void EntityMap<TKey, TValue>::add_or_set(TValue& value)
104+
{
105+
auto key = value.id;
87106
auto element = map.find(key);
88107
if (element == map.end())
89108
map.insert(std::make_pair(key, &value));
@@ -92,16 +111,16 @@ void EntityMap<TKey, TValue>::add_or_set(TKey key, TValue& value)
92111
}
93112

94113
template<class TKey, class TValue>
95-
void EntityMap<TKey, TValue>::add_or_set(TKey key, TValue* value)
114+
void EntityMap<TKey, TValue>::add_or_set(TValue* value)
96115
{
116+
auto key = value->id;
97117
auto element = map.find(key);
98118
if (element == map.end())
99119
map.insert(std::make_pair(key, value));
100120
else
101121
element->second = value;
102122
}
103123

104-
105124
template<class TKey, class TValue>
106125
bool EntityMap<TKey, TValue>::contains(TKey key)
107126
{

include/types/r136.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,17 @@ struct Status
238238
template<class TKey, class TValue>
239239
class EntityMap
240240
{
241+
static_assert(std::is_base_of<Entity<TKey>, TValue>::value, "TValue must inherit from Entity<TKey>");
242+
241243
std::map<TKey, TValue*> map;
244+
bool delete_values;
242245

243246
public:
244-
void add_or_set(TKey key, TValue& value);
245-
void add_or_set(TKey key, TValue* value);
247+
EntityMap(bool delete_values = false);
248+
~EntityMap();
249+
250+
void add_or_set(TValue& value);
251+
void add_or_set(TValue* value);
246252
bool contains(TKey key);
247253
void clear();
248254
TValue& operator[](TKey key);

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void show_splashscreen(void)
6767
console.main().print("\n\n");
6868

6969
console.main().set_attribute(A_BOLD);
70-
console.main().print_centered("Versie 3.2, (C) 1998, 2021 R.I.P.");
70+
console.main().print_centered("Versie 3.3, (C) 1998, 2021 R.I.P.");
7171
console.main().unset_attribute(A_BOLD);
7272
console.main().print("\n\n");
7373
}

src/act.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "r136.h"
22
#include "act.h"
3+
#include <cstring>
34

45
const char* dontOwnItemFormatString = "je hebt geen \"%s\"";
56

src/animates/barbecue.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "r136.h"
1+
#include "r136.h"
22
#include "animates.h"
33

44
Barbecue::Barbecue(RoomID room) : Animate(room) {}
@@ -34,9 +34,9 @@ bool Barbecue::progress_status(CoreData& core)
3434
break;
3535

3636
case AnimateStatus::cookie_is_baking:
37-
console.main().write(L"Een grote rookontwikkeling treedt op wanneer het tweede ingrediënt in de\n");
37+
console.main().write(L"Een grote rookontwikkeling treedt op wanneer het tweede ingrediënt in de\n");
3838
console.main().print("barbecue belandt.\n");
39-
console.main().write(L"Knetterend smelten de 2 ingrediënten om tot een koekje.\n\n");
39+
console.main().write(L"Knetterend smelten de 2 ingrediënten om tot een koekje.\n\n");
4040

4141
core.items[ItemID::cookie].room = core.status.current_room;
4242
core.animates[AnimateID::barbecue].status = AnimateStatus::initial_burn;

src/animates/door.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "r136.h"
1+
#include "r136.h"
22
#include "animates.h"
33

44
Door::Door(RoomID room) : Animate(room) {}
@@ -14,7 +14,7 @@ bool Door::progress_status(CoreData& core)
1414
break;
1515

1616
case AnimateStatus::door_open:
17-
console.main().write(L"Je zet één eind van het bot onder de deur, en op het andere begin je te duwen.\n");
17+
console.main().write(L"Je zet één eind van het bot onder de deur, en op het andere begin je te duwen.\n");
1818
console.main().print("Na lang wrikken begint de deur hevig te kraken en te piepen, en vormt zich een\n");
1919
console.main().print("kier. Je geeft nog een duw, en langzaam draait de deur open.\n\n");
2020

src/animates/gnu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "r136.h"
1+
#include "r136.h"
22
#include "animates.h"
33

44
Gnu::Gnu(RoomID room) : Animate(room) {}
@@ -27,7 +27,7 @@ bool Gnu::progress_status(CoreData& core)
2727
break;
2828

2929
case AnimateStatus::poisonous_meat_fed:
30-
console.main().write(L"De gnoe ziet het vlees, snuffelt er aan, en slokt het in één hap naar binnen.\n");
30+
console.main().write(L"De gnoe ziet het vlees, snuffelt er aan, en slokt het in één hap naar binnen.\n");
3131
console.main().print("Je ziet hem langzaam opzwellen en zijn hersens komen door zijn oogkassen naar\n");
3232
console.main().print("buiten. Hij zakt in elkaar en blijft roerloos liggen.\n\n");
3333

0 commit comments

Comments
 (0)