forked from SFTtech/openage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmom.cpp
253 lines (200 loc) · 6.33 KB
/
mom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2013-2019 the openage authors. See copying.md for legal info.
// [SFT]tech coding guidelines
// ---------------------------
//
// See `mom.h` for a header example.
//
// This is our attempt to creating a good-looking C++ style.
//
// For enhanced beauty: Use your brain!
// * Alphabetic sorting
// * Sensible documentation
// * Lovely indentation and alignment
// The associated header file comes first!
#include "mom.h"
// System includes follow, sorted alphabetically
#include <cmath>
#include <cstdio>
#include <iostream>
#include <SDL/SDL.h>
// Local includes next, sorted alphabetically
#include "../valve.h"
#include "half_life.h"
#include "log/log.h"
// Include errors are detected by having one associated cpp
// file for each header in the project.
// Indentation:
//
// Mix tabs and spaces in a sensible way:
// - Tabs for logical indentation
// - Spaces for alignments after that
//
// ==> see doc/code_style/tabs_n_spaces.md
//
// => this ensures custom tab-widths AND consistent lineup levels.
// struct:
// use it to pack variables
// class:
// use it for the object orientation stuff
//
// Name of struct: lower_case_with_underscores
// Name of class: CamelCaseAsYouKnowIt
// Member methods: lower_case_names
/**
* Example struct documentation.
* Followed by an incredibly good example for writing docs.
* Will be parsed by Doxygen.
*/
struct another_stupid_type {
rofl_t blergh_gnampf; //!< variables and functions in lowercase
// with _ as word separator.
epictype42_t løl; //!< no question, we use UTF-8 encoding.
ub0rtype røfëlkøptør; //!< proper documentation is Doxygen parsable.
};
/**
* Yet another Valve game. Go on, nothing to see here.
*/
class HalfLife3 : public ValveGame {
// vvv The access modifier is at the same indent level as the class definition:
public:
void release_game(const ValveTeam &team,
uint128_t passed_time,
const std::shared_ptr<Engine> &game_engine,
const std::shared_ptr<Internet> &community,
bool keep_secret = true) {
// ^ you can insert a empty line here to make it more readable.
// http://www.abstrusegoose.com/440
auto gaben = team.get_human("gaben");
auto min_quality = gaben.get_quality_requirement(Engine::type);
if (not keep_secret
and (this->story->awesomeness > 9000)
and (game_engine->quality_points > min_quality)) {
// ^ This empty line may help, too.
team->start_ARG(community);
// https://developer.valvesoftware.com/wiki/Valve_Time
this->estimated_release_time = passed_time + ValveTime("66 days").seconds();
team.get_product("steam")->release(this);
community->go_crazy();
World::infrastructure.collapse();
}
else {
gaben->motivate(team);
// When the argument list gets too long, align with spaces:
team.drop_hint(community,
"we think you're kind of wasting your time, "
"but it's your time to waste.");
// You may also start one line later
// and then indent with a tab:
team.relocate(
game_engine->get_team()->get_current_hq_location(),
gaben.get_table_location()
);
community->criticize(passed_time);
team.continue_selling_hats();
}
}
private:
/** Links to the story of the game. */
std::shared_ptr<Story> story;
/** Time in seconds estimated left until release. */
uint128_t estimated_release_time;
};
// ^ At the top level, use two newlines between statements.
// Note: Comments and preprocessor directives don't count as statements,
// In some cases variables and functions may be grouped, always judge
// yourself instead of blindly following the style guide, blah blah.
// Documentation for a namespace is in the header.
namespace parents::mom {
// <- A namespace does not increase the indentation level!
YourMom::YourMom(int her_mass) :
mass{her_mass},
number_of_moons{0},
stupidity{1} {
// ^ use {}-style initializers wherever possible
// < one tab indentation for the initializer list.
// use this-> for referencing member variables/functions!
this->number_of_moons = (this->mass - (this->mass % 10)) / 10;
// access static variables like this:
YourMom::sum_mom_masses += this->mass;
}
YourMom::~YourMom() {}
// <^ At the top level, use two newlines between statements.
void YourMom::print_mass() {
// write todos like this:
// TODO: mass could require uint128_t soon, adapt format string!
printf("current mass of your mom: %d\n", this->mass);
}
// Note the space: v
bool YourMom::tick() {
// Note the spaces!
//| | | |
//v v v v
if (this->stupidity > 5) {
// spaces here:
// v v
this->mass += 1;
}
// Spaces again:
// v v v v v v v v
for (int i = 0; i < 42; i++) {
// Always use braces for the conditional code block,
// even if it's one statement only:
if (this->mass > 20) {
// Black hole? I guess it depends on the radius.
break;
}
else if (this->stupidity > 100) {
this->die();
} else { // Both styles are allowed: '} else {' and '} \n else {'
// your mom eats a bit.
this->mass += 1;
// ^ We're not using var++, except in for (..)
if ((this->mass % 5) == 0) {
this->number_of_moons += 1;
}
}
}
switch (this->stupidity) {
// The case label is at the same indentation level
// as its switch statement:
// vvv
case 0:
throw ContentMafia::Error{"Your mom opposes DRM, investigation required."};
case 8: {
// ^ when defining new variables for a specific case,
// | braces are required by C++ because it is "special".
// | Use them like this.
Apple::iPhone shiny_thing;
this->buy(shiny_thing);
this->stupidity += 2;
return false;
}
case 42:
ContentMafia::try_recruit_lobbyist(this);
// fall through
default:
this->stupidity += 1;
break;
}
return true;
}
// Unused parameters are commented out:
void YourMom::buy(const Thing & /*item*/) {
// TODO: implement
}
} // namespace parents::mom
// ^ When closing namespaces,
// close all in the same line and write a comment like this.
int main(int argc, char **argv) {
// Use {}-style constructors!
parents::YourMom my_special_mom{1337};
// No space after function name calling!
// |
// v
my_special_mom.print_mass();
for (int i = 0; i < 235; i++) {
my_special_mom.tick();
}
my_special_mom.print_mass();
return 0;
}