| 1 |
/* |
| 2 |
* gameserv/happyfarm |
| 3 |
* |
| 4 |
* Copyright (c) 2011 William Pitcock <[email protected]> |
| 5 |
* Rights to this code are documented in doc/LICENSE. |
| 6 |
* |
| 7 |
* Grow crops, trade with and steal from your neighbors, and possibly sell your friends. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "atheme.h" |
| 11 |
#include "gameserv_common.h" |
| 12 |
|
| 13 |
DECLARE_MODULE_V1("gameserv/happyfarm", false, _modinit, _moddeinit, PACKAGE_STRING, VENDOR_STRING); |
| 14 |
|
| 15 |
/* Privatedata schema... */ |
| 16 |
#define SCHEMA_KEY_HAPPYFARMER "gameserv:happyfarm:farmer" |
| 17 |
|
| 18 |
/* |
| 19 |
* Types of plants you may grow. |
| 20 |
*/ |
| 21 |
typedef enum { |
| 22 |
PLANT_NOTHING = 0, |
| 23 |
PLANT_TURNIP, |
| 24 |
PLANT_TOMATO, |
| 25 |
PLANT_PARSLEY, |
| 26 |
PLANT_LETTUCE, |
| 27 |
PLANT_CORN, |
| 28 |
PLANT_POTATO, |
| 29 |
PLANT_BEANS, |
| 30 |
PLANT_PEACHES, |
| 31 |
PLANT_PEARS, |
| 32 |
PLANT_APPLES, |
| 33 |
PLANT_COUNT, |
| 34 |
} happy_planttype_t; |
| 35 |
|
| 36 |
typedef struct { |
| 37 |
unsigned int count; |
| 38 |
} happy_inventory_t; |
| 39 |
|
| 40 |
struct { |
| 41 |
const char *name; |
| 42 |
happy_planttype_t plant; |
| 43 |
} happy_planttype_mapping[] = { |
| 44 |
{N_("nothing"), PLANT_NOTHING}, |
| 45 |
{N_("turnip"), PLANT_TURNIP}, |
| 46 |
{N_("tomato"), PLANT_TOMATO}, |
| 47 |
{N_("parsley"), PLANT_PARSLEY}, |
| 48 |
{N_("lettuce"), PLANT_LETTUCE}, |
| 49 |
{N_("corn"), PLANT_CORN}, |
| 50 |
{N_("potato"), PLANT_POTATO}, |
| 51 |
{N_("beans"), PLANT_BEANS}, |
| 52 |
{N_("peaches"), PLANT_PEACHES}, |
| 53 |
{N_("pears"), PLANT_PEARS}, |
| 54 |
{N_("apples"), PLANT_APPLES}, |
| 55 |
{NULL, PLANT_NOTHING}, |
| 56 |
}; |
| 57 |
|
| 58 |
/* |
| 59 |
* Really, we're unsure of the emotional state of the farmer. However, |
| 60 |
* a sad farmer does not seem as amusing. Plus, since IRC does not |
| 61 |
* convey emotion, it is safe to assume that the farmer is always |
| 62 |
* happy. |
| 63 |
*/ |
| 64 |
typedef struct { |
| 65 |
/* |
| 66 |
* A farm may be either personal, or collectively owned. In Atheme terms, |
| 67 |
* this means it's owned by an entity instead of a user class. In terms of |
| 68 |
* the Chairman Mao communist revolution of 1969, this means that some farms |
| 69 |
* are the People's Happy Farms of Increased Profitability. Either way, |
| 70 |
* it means that we use a myentity_t as our derived base. |
| 71 |
*/ |
| 72 |
myentity_t *owner; |
| 73 |
|
| 74 |
/* |
| 75 |
* A farm, like any other kind of business entity has money. Money is used |
| 76 |
* to buy land and seeds. You can also hire gangsters to go rob other players |
| 77 |
* at knifepoint. But that might make some people Sad Farmers. And we wouldn't |
| 78 |
* want that, or would we? |
| 79 |
*/ |
| 80 |
int money; |
| 81 |
|
| 82 |
/* |
| 83 |
* A farm consists of plots of land joined together. Plots of land are planted |
| 84 |
* with seeds, which cause plants to grow. Then you sell your product for money. |
| 85 |
* Some product sells for more than other products. Anyway, this is a digital |
| 86 |
* representation of a farm, so we store the farm plots in a double-ended queue. |
| 87 |
*/ |
| 88 |
mowgli_list_t plots; |
| 89 |
|
| 90 |
/* |
| 91 |
* A farm's most critical thing is it's Inventory. According to the Ferengi |
| 92 |
* rules of acquisition, Profit is Life. Without an Inventory, there is |
| 93 |
* no Profit. |
| 94 |
*/ |
| 95 |
happy_inventory_t inventory[PLANT_COUNT]; |
| 96 |
|
| 97 |
/* |
| 98 |
* The second most important thing for a farm is it's seedbank. No seedbank, |
| 99 |
* means no Inventory, which means no Profit, which means Sad Starving Farmers. |
| 100 |
*/ |
| 101 |
happy_inventory_t seed_inventory[PLANT_COUNT]; |
| 102 |
} happy_farmer_t; |
| 103 |
|
| 104 |
/* |
| 105 |
* The game progresses at a rate of time that we will describe as The People's |
| 106 |
* Happy Time Unit For Gaming Satisfaction. All time in this game is described as |
| 107 |
* being in units of The People's Happy Time Unit For Gaming Satisfaction. |
| 108 |
* The below tunable defines, in seconds, how long a Happy Time Unit For Gaming |
| 109 |
* Satisfaction actually is. |
| 110 |
*/ |
| 111 |
#define PEOPLES_HAPPY_TIME_UNIT_FOR_GAMING_SATISFACTION (60) |
| 112 |
|
| 113 |
/* |
| 114 |
* And the below define is an alias so that I do not have to type out The People's |
| 115 |
* Happy Time Unit For Gaming Satisfaction a whole bunch. Because I really don't |
| 116 |
* want to do that. |
| 117 |
*/ |
| 118 |
#define TIME_UNIT_BASE (PEOPLES_HAPPY_TIME_UNIT_FOR_GAMING_SATISFACTION) |
| 119 |
|
| 120 |
/**********************************************************************************/ |
| 121 |
|
| 122 |
/* |
| 123 |
* Plots of land cost money. This is the cost of the plot. |
| 124 |
*/ |
| 125 |
#define PLOT_COST (50) |
| 126 |
|
| 127 |
/* |
| 128 |
* Plots of land grow over time. We need to determine how long it takes for |
| 129 |
* a crop to grow. I think that 7 Happy Time Units sounds good, so we'll make it 7. |
| 130 |
*/ |
| 131 |
#define GROWTH_RATE (7) |
| 132 |
|
| 133 |
/* |
| 134 |
* The People's Glorious Land Office of Arbitration Management needs to keep a record |
| 135 |
* of all Happy Plots in the world. We do so here. |
| 136 |
*/ |
| 137 |
mowgli_list_t happy_plot_list = { NULL, NULL, 0 }; |
| 138 |
|
| 139 |
/* |
| 140 |
* A plot of land consists of seeds that grow into plants. A plot is planted with X |
| 141 |
* number of seeds. There is an upper limit on the number of seeds. |
| 142 |
*/ |
| 143 |
#define MAX_SEEDS (30) |
| 144 |
|
| 145 |
/* |
| 146 |
* A plot of land, as previously mentioned, consists of seeds, which |
| 147 |
* grow into plants. Once GROWTH_RATE is reached, the plot may be harvested |
| 148 |
* and replotted. |
| 149 |
*/ |
| 150 |
typedef struct { |
| 151 |
/* |
| 152 |
* As previously mentioned, a plot of land contains seeds. All seeds on a |
| 153 |
* plot of land are the same type. So, plot::plant == PLANT_CORN means |
| 154 |
* you're growing corn. |
| 155 |
*/ |
| 156 |
happy_planttype_t plant; |
| 157 |
|
| 158 |
/* |
| 159 |
* Since plots of land contain seeds, we need to know how many seeds we have. |
| 160 |
*/ |
| 161 |
unsigned int count; |
| 162 |
|
| 163 |
/* |
| 164 |
* A node for storage in the farmer's plot list. |
| 165 |
*/ |
| 166 |
mowgli_node_t farmer_node; |
| 167 |
|
| 168 |
/* |
| 169 |
* A node for storage in the global plot list. |
| 170 |
*/ |
| 171 |
mowgli_node_t global_node; |
| 172 |
} happy_plot_t; |
| 173 |
|
| 174 |
/*******************************************************************************************/ |
| 175 |
|
| 176 |
/* |
| 177 |
* Our happy farmers are happily stored in a magazine allocator to be quickly allocated to |
| 178 |
* happy users. This allows for maximum efficiency in gameplay success. |
| 179 |
*/ |
| 180 |
mowgli_heap_t *farmer_heap = NULL; |
| 181 |
|
| 182 |
/* |
| 183 |
* We need to have a magazine allocator for plots too. |
| 184 |
*/ |
| 185 |
mowgli_heap_t *plot_heap = NULL; |
| 186 |
|
| 187 |
/* |
| 188 |
* When a happy farmer joins our happy little game, we have to create a happy_farmer_t |
| 189 |
* object for them. Which, you know, requires a constructor... |
| 190 |
*/ |
| 191 |
static happy_farmer_t *happy_farmer_create(myentity_t * mt) |
| 192 |
{ |
| 193 |
happy_farmer_t *farmer; |
| 194 |
|
| 195 |
return_val_if_fail(mt != NULL, NULL); |
| 196 |
|
| 197 |
farmer = mowgli_heap_alloc(farmer_heap); |
| 198 |
farmer->owner = mt; |
| 199 |
farmer->money = 100; |
| 200 |
|
| 201 |
privatedata_set(farmer->owner, SCHEMA_KEY_HAPPYFARMER, farmer); |
| 202 |
|
| 203 |
return farmer; |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* When a happy farmer decides to commit suicide, Government must intervene and start the |
| 208 |
* probate procedures (which in our case means we take all resources back). |
| 209 |
*/ |
| 210 |
static void happy_farmer_destroy(happy_farmer_t * farmer) |
| 211 |
{ |
| 212 |
mowgli_node_t *n, *tn; |
| 213 |
|
| 214 |
return_if_fail(farmer != NULL); |
| 215 |
|
| 216 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, farmer->plots.head) |
| 217 |
{ |
| 218 |
happy_plot_t *plot = n->data; |
| 219 |
|
| 220 |
mowgli_node_delete(&plot->farmer_node, &farmer->plots); |
| 221 |
mowgli_node_delete(&plot->global_node, &happy_plot_list); |
| 222 |
|
| 223 |
mowgli_heap_free(plot_heap, plot); |
| 224 |
} |
| 225 |
|
| 226 |
privatedata_set(farmer->owner, SCHEMA_KEY_HAPPYFARMER, NULL); |
| 227 |
|
| 228 |
mowgli_heap_free(farmer_heap, farmer); |
| 229 |
} |
| 230 |
|
| 231 |
/* |
| 232 |
* A farmer just bought a plot of land. |
| 233 |
*/ |
| 234 |
static happy_plot_t *happy_plot_create(happy_farmer_t * farmer) |
| 235 |
{ |
| 236 |
happy_plot_t *plot; |
| 237 |
|
| 238 |
return_val_if_fail(farmer != NULL, NULL); |
| 239 |
|
| 240 |
plot = mowgli_heap_alloc(plot_heap); |
| 241 |
mowgli_node_add(plot, &plot->farmer_node, &farmer->plots); |
| 242 |
mowgli_node_add(plot, &plot->global_node, &happy_plot_list); |
| 243 |
|
| 244 |
return plot; |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* The farmer has sold his plot of land back. |
| 249 |
*/ |
| 250 |
static void happy_plot_destroy(happy_farmer_t * farmer, happy_plot_t * plot) |
| 251 |
{ |
| 252 |
return_if_fail(farmer != NULL); |
| 253 |
return_if_fail(plot != NULL); |
| 254 |
|
| 255 |
mowgli_node_delete(&plot->farmer_node, &farmer->plots); |
| 256 |
mowgli_node_delete(&plot->global_node, &happy_plot_list); |
| 257 |
|
| 258 |
mowgli_heap_free(plot_heap, plot); |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
* Find the first plot that can actually be sold back. |
| 263 |
*/ |
| 264 |
static happy_plot_t *happy_farmer_find_vacant_plot(happy_farmer_t * farmer) |
| 265 |
{ |
| 266 |
mowgli_node_t *n; |
| 267 |
|
| 268 |
return_val_if_fail(farmer != NULL, NULL); |
| 269 |
|
| 270 |
MOWGLI_ITER_FOREACH(n, farmer->plots.head) |
| 271 |
{ |
| 272 |
happy_plot_t *plot = n->data; |
| 273 |
|
| 274 |
if (plot->plant == PLANT_NOTHING) |
| 275 |
return plot; |
| 276 |
} |
| 277 |
|
| 278 |
return NULL; |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* Determine a plant type, given user input. |
| 283 |
*/ |
| 284 |
static happy_planttype_t happy_plant_by_name(const char *name) |
| 285 |
{ |
| 286 |
unsigned int i; |
| 287 |
|
| 288 |
for (i = 0; i < ARRAY_SIZE(happy_planttype_mapping); i++) |
| 289 |
{ |
| 290 |
if (!strcasecmp(_(happy_planttype_mapping[i].name), name)) |
| 291 |
return happy_planttype_mapping[i].plant; |
| 292 |
} |
| 293 |
|
| 294 |
return PLANT_NOTHING; |
| 295 |
} |
| 296 |
|
| 297 |
/*******************************************************************************************/ |
| 298 |
|
| 299 |
/* |
| 300 |
* Syntax: JOIN |
| 301 |
* Result: Player joins the game. |
| 302 |
*/ |
| 303 |
static void __command_join(sourceinfo_t * si, int parc, char *parv[]) |
| 304 |
{ |
| 305 |
happy_farmer_t *farmer; |
| 306 |
|
| 307 |
farmer = happy_farmer_create(entity(si->smu)); |
| 308 |
|
| 309 |
command_success_nodata(si, _("Welcome to Happy Farm! May your farm be lucky.")); |
| 310 |
command_success_nodata(si, _("You have started with \2%d\2 money. For help, use \2/msg %s HELP HAPPYFARM\2."), farmer->money, si->service->nick); |
| 311 |
} |
| 312 |
|
| 313 |
command_t command_join = { "JOIN", N_("Join the Happy Farm game!"), AC_AUTHENTICATED, 0, __command_join, { .path = "gameserv/happyfarm_join" } }; |
| 314 |
|
| 315 |
/* |
| 316 |
* Syntax: BUYPLOT |
| 317 |
* Result: Player buys a plot if they have enough money. |
| 318 |
*/ |
| 319 |
static void __command_buyplot(sourceinfo_t * si, int parc, char *parv[]) |
| 320 |
{ |
| 321 |
myentity_t *mt; |
| 322 |
happy_farmer_t *farmer; |
| 323 |
|
| 324 |
mt = entity(si->smu); |
| 325 |
|
| 326 |
return_if_fail(mt != NULL); |
| 327 |
|
| 328 |
farmer = privatedata_get(mt, SCHEMA_KEY_HAPPYFARMER); |
| 329 |
if (farmer == NULL) |
| 330 |
{ |
| 331 |
command_fail(si, fault_noprivs, _("You do not appear to be playing Happy Farm. To join the game, use \2/msg %s HAPPYFARM JOIN\2."), si->service->nick); |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
/* check if the farmer has enough money. */ |
| 336 |
if (farmer->money < PLOT_COST) |
| 337 |
{ |
| 338 |
command_fail(si, fault_noprivs, _("You don't have enough money to buy a plot of land.")); |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
farmer->money -= PLOT_COST; |
| 343 |
happy_plot_create(farmer); |
| 344 |
|
| 345 |
command_success_nodata(si, _("You have bought a plot of land!")); |
| 346 |
command_success_nodata(si, _("You have \2%d\2 money available."), farmer->money); |
| 347 |
} |
| 348 |
|
| 349 |
command_t command_buyplot = { "BUYPLOT", N_("Buy a plot of land!"), AC_AUTHENTICATED, 0, __command_buyplot, { .path = "gameserv/happyfarm_buyplot" } }; |
| 350 |
|
| 351 |
/* |
| 352 |
* Syntax: SELLPLOT |
| 353 |
* Result: Player sells a plot back to the motherland |
| 354 |
*/ |
| 355 |
static void __command_sellplot(sourceinfo_t * si, int parc, char *parv[]) |
| 356 |
{ |
| 357 |
myentity_t *mt; |
| 358 |
happy_farmer_t *farmer; |
| 359 |
happy_plot_t *plot; |
| 360 |
|
| 361 |
mt = entity(si->smu); |
| 362 |
|
| 363 |
return_if_fail(mt != NULL); |
| 364 |
|
| 365 |
farmer = privatedata_get(mt, SCHEMA_KEY_HAPPYFARMER); |
| 366 |
if (farmer == NULL) |
| 367 |
{ |
| 368 |
command_fail(si, fault_noprivs, _("You do not appear to be playing Happy Farm. To join the game, use \2/msg %s HAPPYFARM JOIN\2."), si->service->nick); |
| 369 |
return; |
| 370 |
} |
| 371 |
|
| 372 |
if ((plot = happy_farmer_find_vacant_plot(farmer)) == NULL) |
| 373 |
{ |
| 374 |
command_fail(si, fault_noprivs, _("You do not have any vacant plots at this time.")); |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
farmer->money += PLOT_COST / 2; |
| 379 |
happy_plot_destroy(farmer, plot); |
| 380 |
|
| 381 |
command_success_nodata(si, _("You have sold a plot of land.")); |
| 382 |
command_success_nodata(si, _("You have \2%d\2 money available."), farmer->money); |
| 383 |
} |
| 384 |
|
| 385 |
command_t command_sellplot = { "SELLPLOT", N_("Sell a vacant plot of land."), AC_AUTHENTICATED, 0, __command_sellplot, { .path = "gameserv/happyfarm_sellplot" } }; |
| 386 |
|
| 387 |
/*******************************************************************************************/ |
| 388 |
|
| 389 |
mowgli_patricia_t *happyfarm_cmd_subtree = NULL; |
| 390 |
|
| 391 |
static void __command_trampoline(sourceinfo_t * si, int parc, char *parv[]) |
| 392 |
{ |
| 393 |
char *subcmd = parv[0]; |
| 394 |
command_t *c; |
| 395 |
|
| 396 |
if (subcmd == NULL) |
| 397 |
{ |
| 398 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "HAPPYFARM"); |
| 399 |
command_fail(si, fault_needmoreparams, _("Syntax: HAPPYFARM <command>")); |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
/* take the command through the hash table */ |
| 404 |
if ((c = command_find(happyfarm_cmd_subtree, subcmd))) |
| 405 |
{ |
| 406 |
command_exec(si->service, si, c, parc - 1, parv + 1); |
| 407 |
} |
| 408 |
else |
| 409 |
{ |
| 410 |
command_fail(si, fault_badparams, _("Invalid command. Use \2/%s%s HELP HAPPYFARM\2 for a command listing."), (ircd->uses_rcommand == false) ? "msg " : "", si->service->nick); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
command_t command_happyfarm = { "HAPPYFARM", N_("Happy Farm!"), AC_AUTHENTICATED, 2, __command_trampoline, { .path = "gameserv/happyfarm" } }; |
| 415 |
|
| 416 |
/*******************************************************************************************/ |
| 417 |
|
| 418 |
void _modinit(module_t * m) |
| 419 |
{ |
| 420 |
farmer_heap = mowgli_heap_create(sizeof(happy_farmer_t), 32, BH_LAZY); |
| 421 |
plot_heap = mowgli_heap_create(sizeof(happy_plot_t), 32, BH_LAZY); |
| 422 |
|
| 423 |
service_named_bind_command("gameserv", &command_happyfarm); |
| 424 |
|
| 425 |
happyfarm_cmd_subtree = mowgli_patricia_create(strcasecanon); |
| 426 |
|
| 427 |
command_add(&command_join, happyfarm_cmd_subtree); |
| 428 |
command_add(&command_buyplot, happyfarm_cmd_subtree); |
| 429 |
command_add(&command_sellplot, happyfarm_cmd_subtree); |
| 430 |
} |
| 431 |
|
| 432 |
void _moddeinit(module_unload_intent_t intent) |
| 433 |
{ |
| 434 |
command_delete(&command_join, happyfarm_cmd_subtree); |
| 435 |
command_delete(&command_buyplot, happyfarm_cmd_subtree); |
| 436 |
command_delete(&command_sellplot, happyfarm_cmd_subtree); |
| 437 |
|
| 438 |
mowgli_patricia_destroy(happyfarm_cmd_subtree, NULL, NULL); |
| 439 |
|
| 440 |
service_named_unbind_command("gameserv", &command_happyfarm); |
| 441 |
|
| 442 |
mowgli_heap_destroy(farmer_heap); |
| 443 |
mowgli_heap_destroy(plot_heap); |
| 444 |
} |