-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.cpp
708 lines (620 loc) · 22.1 KB
/
object.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
#include "object.h"
#include "translator.h"
#include <iostream>
#include <algorithm>
#include "sdl_camera.h"
#include "craft_mgr.h"
/********************************************************************/
Object::Object(
const std::string& icon_name,
const std::string& user_name,
const std::string& name
) : icon_name_(icon_name), user_name_(user_name), name_(name) {
}
SDL_Texture* Object::getTexture(SDLCamera* camera, int /*index*/) {
if( images_.size() == 0 ) {
SDL_Surface* surface = Utility::IMGLoad(icon_name_);
// Transparency with green color
Uint32 key = SDL_MapRGB(surface->format, 0, 255, 0);
SDL_SetColorKey(surface , SDL_TRUE, key);
width_ = surface->w;
height_ = surface->h;
SDL_Renderer* main_renderer = camera->main_renderer();
images_.push_back( SDL_CreateTextureFromSurface(main_renderer, surface) );
SDL_FreeSurface(surface);
}
return images_[0]; // TODO : take into account the orientation of the object
}
void Object::render(SDLCamera* camera, const SDL_Rect& original_rect) {
SDL_Rect rect(original_rect);
SDL_Texture* texture = getTexture(camera);
rect.w = pixel_width()*camera->scale();
rect.h = pixel_height()*camera->scale();
rect.y = rect.y + Utility::tileSize*camera->scale() - rect.h;
camera->displayTexture( texture, &rect);
// object toolbar when crafting
if( percentageAccomplished() != -1 ) {
int activity_percent = percentageAccomplished();
static int offset = 3;
static int toolbar_height = 9;
SDL_Rect activity_rect = {rect.x+offset,rect.y+rect.h-offset-toolbar_height,0,0};
SDL_SetRenderDrawColor( camera->main_renderer(), 0, 0, 250, 255 );
int width = activity_percent / 100.0 * (Utility::tileSize*camera->scale()-2*offset);
activity_rect.w = width;
activity_rect.h = toolbar_height;
SDL_RenderFillRect( camera->main_renderer(), &activity_rect );
SDL_SetRenderDrawColor( camera->main_renderer(), 0, 128, 255, 255 );
activity_rect.w = Utility::tileSize*camera->scale()-2*offset;
activity_rect.h = toolbar_height;
SDL_RenderDrawRect( camera->main_renderer(), &activity_rect );
}
}
int Object::getNodeCount() const {
return crafts_.size();
}
// <craft nb="10">glass</craft>
const std::string Object::getNodeName(int node_index) const {
std::ignore = node_index;
return "craft";
}
int Object::getAttributeCount(int node_index) const {
std::ignore = node_index;
return 1; // nb
}
const std::string Object::getAttributeName(int node_index,int attr_index) const {
std::ignore = node_index;
std::ignore = attr_index;
return "nb";
}
const std::string Object::getAttributeValue(int node_index,int attr_index) const {
std::ignore = attr_index;
int occ = crafts_[node_index].second;
return Utility::itos(occ);
}
const std::string Object::getNodeValue(int node_index) const {
Craft* craft = crafts_[node_index].first;
return craft->name();
}
const std::string Object::getNodeString(int node_index) const {
std::ignore = node_index;
return "";
}
void Object::setNode(const std::string& node_name, std::vector<std::pair<std::string, std::string>> attributes, const std::string& value) {
if( node_name == "craft" ) {
if( attributes.size() != 1 ) return;
if( attributes[0].first != "nb" ) return;
std::string nb_str = attributes[0].second;
Craft* craft = CraftMgr::instance()->findCraft(value, this->name());
addCraft(craft,atoi( nb_str.c_str() ));
}
}
Object* Object::getObjectFromName(const std::string& name) {
Object* object = nullptr;
if( name == "chest" ) {
object = new Chest(2);
} else if( name == "iron_chest" ) {
object = new IronChest(8);
} else if( name == "workbench" ) {
object = new WorkBench();
} else if( name == "breaker" ) {
object = new Breaker();
} else if( name == "engine" ) {
object = new Engine();
} else if( name == "motor" ) {
object = new Motor();
} else if( name == "stone_furnace" ) {
object = new StoneFurnace();
} else if( name == "assembler" ) {
object = new Assembler();
} else if( name == "field" ) {
object = new Field();
} else if( name == "command_center" ) {
object = new CommandCenter();
} else if( name == "electric_furnace" ) {
object = new ElectricFurnace();
}
return object;
}
void Object::addCraft(Craft* craft, int occ) {
crafts_.push_back(std::pair<Craft*,int>(craft, occ));
}
void Object::removeCraft(int index) {
crafts_.erase( crafts_.begin() + index );
if( index == 0 ) {
// remove the cur_craft and reset the time if needed
if( has_ingredients_ ) {
// drop all items
MapView* map_view = MapView::cur_map;
MapData* data = map_view->data();
Position tile_position = this->tilePosition();
Tile& tile = data->tile(tile_position.x, tile_position.y);
const std::vector<CountedItem>& items = cur_craft_->getItems();
for( auto counted_item : items ) {
tile.addItem(counted_item.item(), counted_item.count());
}
has_ingredients_ = false;
}
cur_craft_ = nullptr;
cur_craft_time_ms_ = 0;
max_craft_time_ms_ = 0;
}
}
void Object::animate(double delta_ms) { // TODO
if( inPause() ) return;
if( crafts_.size() == 0 ) return;
if( has_ingredients_ && cur_craft_ != nullptr && cur_craft_time_ms_ > delta_ms ) {
cur_craft_time_ms_ -= delta_ms;
}
if( cur_craft_ == nullptr ) {
has_ingredients_ = true;
cur_craft_ = crafts_.at(0).first;
checkIngredients();
}
if( !hasIngredients() ) {
checkIngredients();
}
if( hasIngredients() ) {
if( cur_craft_time_ms_ <= delta_ms ) {
cur_craft_time_ms_ = 0;
int occ = crafts_.at(0).second;
MapView* map_view = MapView::cur_map;
map_view->data()->store(BasicItem(cur_craft_->name()), tilePosition(), cur_craft_->occurrence());
if( occ > 1) {
crafts_.at(0).second = occ - 1;
} else {
crafts_.erase(crafts_.begin());
}
cur_craft_ = nullptr;
}
}
}
bool Object::hasIngredients() const {
return has_ingredients_;
}
void Object::checkIngredients() { // TODO
if( cur_craft_ == nullptr ) return;
// get list of ingredients and check that ingredients are available in a chest
const std::vector<CountedItem>& items = cur_craft_->getItems();
CommandCenter* cc = CommandCenter::cur_command_center;
// if ingredients are all present, start craft
for( auto counted_item : items ) {
int stored_nb = cc->countedItems(counted_item.item());
int nb = counted_item.count();
if( stored_nb >= nb ) {
continue;
} else {
// otherwise: do not start the craft !!
if( has_ingredients_ ) {
std::string message = tr("Missing $1 element(s) of $2 to craft $3");
Utility::replace(message, "$1", Utility::itos(nb-stored_nb));
Utility::replace(message, "$2", tr(counted_item.item().name()));
Utility::replace(message, "$3", tr(cur_craft_->name()));
Logger::warning() << message << Logger::endl;
}
has_ingredients_ = false;
return;
}
}
// get list of chests
std::vector<Chest*> chests;
MapView* mv = MapView::cur_map;
for( auto object : mv->data()->objects() ) {
Chest* chest = dynamic_cast<Chest*>(object);
if( chest != nullptr ) {
chests.push_back(chest);
}
}
// remove all ingredients from various chests
for( auto counted_item : items ) {
int nb = counted_item.count();
for( auto chest : chests ) {
nb = chest->removeItem(counted_item.item(), nb);
if( nb == 0 ) {
break;
}
}
}
has_ingredients_ = true;
cur_craft_time_ms_ = cur_craft_->time()*1000;
max_craft_time_ms_ = cur_craft_time_ms_;
}
/*!
* if an item is currently crafted, returns the percentage of accomplishment between 0 and 100
* if no craft, returns -1
*/
int Object::percentageAccomplished() const {
if( cur_craft_ == nullptr ) {
return -1;
}
if( max_craft_time_ms_ == 0 ) {
return -1;
}
return (cur_craft_time_ms_*100/max_craft_time_ms_);
}
void Object::releaseItems() {
// does nothing
}
/********************************************************************/
void ChestFilter::addFilter(const std::string& basic_item) {
if( std::find(filters_.begin(), filters_.end(), basic_item) != filters_.end() ) {
return; // already in filter
}
filters_.push_back(basic_item);
}
void ChestFilter::removeFilter(const std::string& basic_item) {
std::vector<std::string>::iterator it = std::find(filters_.begin(), filters_.end(), basic_item);
if( it == filters_.end() ) {
return; // already not in filter
}
int idx = std::distance(filters_.begin(), it);
filters_.erase(filters_.begin()+idx);
}
bool ChestFilter::isFiltered(const std::string& basic_item) const {
return std::find(filters_.begin(), filters_.end(), basic_item) != filters_.end();
}
/********************************************************************/
Chest::Chest(
const std::string& icon_name,
const std::string& user_name,
const std::string& name,
int size
) : Object(icon_name, user_name, name), max_size_(size) {
is_crafter_ = false;
}
Chest::Chest(int size) : Object("objects/chest.png", tr("Chest"), "chest"), max_size_(size) {
is_crafter_ = false;
}
void Chest::setNode(const std::string& node_name, std::vector<Attr> attributes, const std::string& value) {
if( node_name == "counted_item" ) {
if( attributes.size() != 1 ) return;
if( attributes[0].first != "nb" ) return;
std::string nb_str = attributes[0].second;
addItem(BasicItem(value), atoi( nb_str.c_str() ));
}
}
int Chest::getNodeCount() const {
return items_.size();
}
const std::string Chest::getNodeName(int node_index) const {
std::ignore = node_index;
return "counted_item";
}
int Chest::getAttributeCount(int node_index) const {
std::ignore = node_index;
return 1;
}
const std::string Chest::getAttributeName(int /*node_index*/,int /*attr_index*/) const {
return "nb";
}
const std::string Chest::getAttributeValue(int node_index,int /*attr_index*/) const {
return Utility::itos(items_[node_index].count());
}
const std::string Chest::getNodeValue(int node_index) const {
return items_[node_index].item().name();
}
const std::string Chest::getNodeString(int node_index) const {
std::string result = items_[node_index].item().name();
result = tr(result) + " x " + getAttributeValue(node_index,0);
return result;
}
void Chest::render(SDLCamera* sdl_camera, const SDL_Rect& rect) {
// first: display texture of the chest
Object::render(sdl_camera, rect);
// second: display items stored in the chest
const std::vector<CountedItem>& items = this->items();
int scaled_tile_size = Utility::tileSize * sdl_camera->scale();
for( int i = 0; i < std::min(4, int(items.size())); i++ ) {
const CountedItem& cur = items[i];
SDL_Texture* item_texture = cur.texture();
SDL_Rect dest;
int half_scaled_tile_size = scaled_tile_size/2;
dest.x = rect.x; // + half_scaled_tile_size/2;
if( i == 1 ) dest.x += half_scaled_tile_size;
if( i == 3 ) dest.x += half_scaled_tile_size;
dest.y = rect.y; // + half_scaled_tile_size/2;
if( i == 2 ) dest.y += half_scaled_tile_size;
if( i == 3 ) dest.y += half_scaled_tile_size;
dest.w = half_scaled_tile_size;
dest.h = half_scaled_tile_size;
sdl_camera->displayTexture(item_texture, &dest);
}
}
// return the number of items not added in this Chest
int Chest::addItem(const BasicItem& item, int count) {
int total_count = count;
// find item if already in the chest
for( auto& counted_item : items_ ) {
if( counted_item.item() == item ) {
total_count = counted_item.addItem(total_count);
CommandCenter::addItems(item, count-total_count);
if( total_count == 0 ) {
return 0;
}
}
}
// there are still some items to add...
while( max_size_ > int(items_.size()) ) {
int c = total_count;
if( total_count > CountedItem::maxCount() ) {
c = CountedItem::maxCount();
}
items_.push_back(CountedItem(item, c));
CommandCenter::addItems(item, c);
total_count -= c;
if( total_count == 0 ) {
return 0;
}
}
return total_count;
}
// return the number of items that cannot be removed from the Chest
int Chest::removeItem(const BasicItem& item, int item_count) {
// find item in the chest if any
int total_count = item_count;
// find item if already in the chest
for( auto& counted_item : items_ ) {
if( counted_item.item() == item ) {
total_count = counted_item.removeItem(total_count);
CommandCenter::removeItems(item, item_count-total_count);
if( total_count == 0 ) {
break;
}
}
}
// remove all items that are empty
bool to_remove = true;
while( to_remove ) {
int index = 0;
for( auto counted_item : items_ ) {
if( counted_item.count() == 0 ) {
break;
}
index++;
}
if( index < int(items_.size()) && items_[index].count() == 0 ) {
items_.erase(items_.begin()+index);
} else {
to_remove = false;
}
}
return total_count;
}
const std::string Chest::tooltip() const {
std::string text = Object::tooltip();
if( items_.size() == 0 ) {
return text;
}
text.append(":");
for(auto item : items_) {
text.append(" ");
std::string s = tr(item.item().name());
std::replace(s.begin(), s.end(), '_', ' ');
text.append(s);
text.append("(");
text.append( Utility::itos(item.count()) );
text.append(")");
}
return text;
}
void Chest::releaseItems() {
MapView* map_view = MapView::cur_map;
MapData* data = map_view->data();
Position tile_position = this->tilePosition();
Tile& tile = data->tile(tile_position.x, tile_position.y);
for( auto counted_item : items_ ) {
tile.addItem(counted_item.item(), counted_item.count());
}
items_.clear();
}
/********************************************************************/
IronChest::IronChest(int size) : Chest("objects/iron_chest.png", tr("IronChest"), "iron_chest", size) {
}
/********************************************************************/
Furnace::Furnace(
const std::string& icon_name,
const std::string& user_name,
const std::string& name
) : Object(icon_name, user_name, name) {
}
void Furnace::animate(double delta_ms) {
if( inPause() ) return;
if( crafts_.size() == 0 ) return;
// no fuel in furnace or no storage : get fuel
if( fuel_time_ms_ == 0 || fuel_.count() == 0 ) {
getFuel();
}
if( fuel_time_ms_ > 0 ) {
Object::animate(delta_ms);
}
// do not use coal if craft is not ready
if( !has_ingredients_ ) return;
if( fuel_time_ms_ > delta_ms ) {
fuel_time_ms_ -= delta_ms;
} else {
fuel_time_ms_ = 0;
}
}
const std::string Furnace::tooltip() const {
std::string text = Object::tooltip();
if( fuel_time_ms_ > 0 ) {
text.append("\n");
text.append( tr("fuel time (in sec): ") ) ;
text.append( Utility::itos(int(fuel_time_ms_/1000)) );
}
return text;
}
void Furnace::getFuel() {
if( fuel_time_ms_ == 0 && !fuel_.isNull() ) {
fuel_time_ms_ = 80000;
fuel_.removeItem(1);
}
// find fuel in the nearest chest
// or ask a robot to get fuel : TODO
MapView* map_view = MapView::cur_map;
// find coal in chest
if( map_view->data()->removeItemFromChest(tilePosition(), BasicItem("coal")) ) {
// coal has been found and removed from the chest,
// furnace can use it
if( fuel_time_ms_ == 0 ) {
fuel_time_ms_ = 80000;
} else {
if( fuel_.isNull() ) {
fuel_ = CountedItem(BasicItem("coal"));
} else {
fuel_.addItem(1);
}
}
}
}
// save also fuel and fuel_time_ms
int Furnace::getNodeCount() const {
return Object::getNodeCount() + 1;
}
// <craft nb="10">glass</craft>
const std::string Furnace::getNodeName(int node_index) const {
if( node_index == Object::getNodeCount() ) {
return "fuel";
}
return "craft";
}
int Furnace::getAttributeCount(int node_index) const {
if( node_index == Object::getNodeCount() ) {
return 1; // time in ms
}
return 1; // nb
}
const std::string Furnace::getAttributeName(int node_index,int attr_index) const {
if( node_index == Object::getNodeCount() ) {
return "fuel_time_ms";
}
std::ignore = attr_index;
return "nb";
}
const std::string Furnace::getAttributeValue(int node_index,int attr_index) const {
std::ignore = attr_index;
if( node_index == Object::getNodeCount() ) {
return Utility::itos(fuel_time_ms_);
}
int occ = crafts_[node_index].second;
return Utility::itos(occ);
}
const std::string Furnace::getNodeValue(int node_index) const {
if( node_index == Object::getNodeCount() ) {
return Utility::itos( fuel_.count() ); // nb fuel item
}
Craft* craft = crafts_[node_index].first;
return craft->name();
}
void Furnace::setNode(const std::string& node_name, std::vector<std::pair<std::string, std::string>> attributes, const std::string& value) {
if( node_name == "craft" ) {
if( attributes.size() != 1 ) return;
if( attributes[0].first != "nb" ) return;
std::string nb_str = attributes[0].second;
Craft* craft = CraftMgr::instance()->findCraft(value, this->name());
addCraft(craft,atoi( nb_str.c_str() ));
} else if( node_name == "fuel" ) {
if( attributes.size() != 1 ) return;
if( attributes[0].first != "fuel_time_ms" ) return;
std::string nb_str = attributes[0].second;
// value is the number of fuel
// attr is the fuel time in ms
int nb_fuel = atoi(value.c_str());
if( nb_fuel == 0 ) {
fuel_ = CountedItem();
} else {
fuel_ = CountedItem(BasicItem("coal"), nb_fuel);
}
fuel_time_ms_ = atoi(nb_str.c_str());
}
}
StoneFurnace::StoneFurnace() : Furnace("objects/stone_furnace.png", tr("StoneFurnace"), "stone_furnace") {
}
Breaker::Breaker() : Object("objects/breaker.png", tr("Breaker"), "breaker") {
}
Engine::Engine() : Object("objects/engine.png", tr("Engine"), "engine") {
}
Motor::Motor(int power) : Object("objects/motor.png", tr("Motor"), "motor"), power_(power) {
}
WorkBench::WorkBench() : Object("objects/workbench.png", tr("WorkBench"), "workbench") {
}
Assembler::Assembler() : Object("objects/assembler.png", tr("Assembler"), "assembler") {
}
ElectricFurnace::ElectricFurnace() : Furnace("objects/electric_furnace.png", tr("ElectricFurnace"), "electric_furnace") {
}
Field::Field() : Object("objects/field.png", tr("Field"), "field") {
}
/********************************************************************/
CommandCenter* CommandCenter::cur_command_center = nullptr;
CommandCenter::CommandCenter() : Object("objects/command_center.png", tr("CommandCenter"), "command_center") {
if( cur_command_center == nullptr ) {
cur_command_center = this;
}
}
const std::vector<CountedItem>& CommandCenter::storedItems() const {
return stored_items_;
}
std::vector<CountedItem>& CommandCenter::storedItems() {
return stored_items_;
}
void CommandCenter::addItems(const BasicItem& item, int nb) {
CommandCenter* cc = CommandCenter::cur_command_center;
if( cc == nullptr ) return;
std::vector<CountedItem>& stored_items = cc->storedItems();
for( auto& counted_item : stored_items ) {
if( counted_item.item().name() == item.name() ) {
counted_item.addItem(nb);
return;
}
}
stored_items.push_back( CountedItem(item, nb) );
}
int CommandCenter::countedItems(const BasicItem& item) const {
for( auto& counted_item : storedItems() ) {
if( counted_item.item().name() == item.name() ) {
return counted_item.count();
}
}
return 0;
}
/*!
* \return the number of items not removed. if 0, all items have been removed
*/
int CommandCenter::removeItems(const BasicItem& item, int nb) {
int total = nb;
CommandCenter* cc = CommandCenter::cur_command_center;
if( cc == nullptr ) return total;
std::vector<CountedItem>& stored_items = cc->storedItems();
int index = 0;
for( auto& counted_item : stored_items ) {
if( counted_item.item().name() == item.name() ) {
// if cc has more items, just remove the wanted number
if( nb < counted_item.count() ) {
counted_item.removeItem(nb);
total = 0;
} else { // otherwise, remove everything and remove the counted item too
total = total - counted_item.count();
counted_item.removeItem(counted_item.count());
stored_items.erase(stored_items.begin()+index);
}
return total;
}
index++;
}
return total;
}
void CommandCenter::init(CommandCenter* cc, std::vector<Chest*> chests) {
if( cc == nullptr ) { return; }
for( auto chest : chests ) {
const std::vector<CountedItem>& counted_items = chest->items();
for( auto counted_item : counted_items ) {
cc->addItems(counted_item.item(), counted_item.count());
}
}
}
void CommandCenter::reset() {
stored_items_.clear();
}
void CommandCenter::destroy() {
CommandCenter::cur_command_center = nullptr;
}
/********************************************************************/