Repository navigation
Expand file tree
/
Copy pathdistilleryscript.cpp
More file actions
executable file
·118 lines (91 loc) · 2.97 KB
/
Copy pathdistilleryscript.cpp
File metadata and controls
executable file
·118 lines (91 loc) · 2.97 KB
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
#include "distilleryscript.h"
#include "../xarchive/glstreamer.h"
#include "../xegame/spatialcomponent.h"
#include "../xegame/itemcomponent.h"
#include "../xegame/istringconst.h"
#include "../xegame/rendercomponent.h"
#include "../xegame/chitcontext.h"
#include "../game/lumoschitbag.h"
#include "../game/mapspatialcomponent.h"
#include "corescript.h"
#include "evalbuildingscript.h"
#include "../script/itemscript.h"
using namespace grinliz;
DistilleryScript::DistilleryScript() : progressTick( 2000 ), progress(0)
{
}
void DistilleryScript::Serialize( XStream* xs )
{
BeginSerialize(xs, Name());
XARC_SER( xs, progress );
progressTick.Serialize( xs, "progressTick" );
EndSerialize(xs);
}
int DistilleryScript::ElixirTime( int tech )
{
return ELIXIR_TIME * TECH_MAX / (tech+1);
}
void DistilleryScript::SetInfoText()
{
CStr<32> str;
ItemComponent* ic = parentChit->GetItemComponent();
RenderComponent* rc = parentChit->GetRenderComponent();
if ( ic && rc ) {
int nFruit = 0;
for( int i=1; i<ic->NumItems(); ++i ) {
if ( ic->GetItem( i )->IName() == ISC::fruit ) {
++nFruit;
}
}
double fraction = double(progress) / double(ELIXIR_TIME);
if (nFruit == 0)
fraction = 0;
str.Format( "Fruit: %d [%0d%%]", nFruit, int(fraction*100.0) );
rc->SetDecoText( str.c_str() );
}
}
int DistilleryScript::DoTick( U32 delta )
{
int n = progressTick.Delta( delta );
ItemComponent* ic = parentChit->GetItemComponent();
if ( n ) {
MapSpatialComponent* msc = GET_SUB_COMPONENT(parentChit, SpatialComponent, MapSpatialComponent);
GLASSERT( msc );
Vector2I sector = ToSector(parentChit->Position());
Rectangle2I porch = msc->PorchPos();
CoreScript* cs = CoreScript::GetCore( sector );
if (!cs) return VERY_LONG_TICK;
float tech = Max(cs->GetTech(), 0.8f);
double dProg = double(tech) * double(progressTick.Period()) / double(TECH_MAX);
double p = dProg*double(n);
EvalBuildingScript* evalScript = (EvalBuildingScript*) parentChit->GetComponent("EvalBuildingScript");
if (evalScript) {
p *= evalScript->Efficiency();
}
progress += int(p);
while (progress >= ELIXIR_TIME) {
progress -= ELIXIR_TIME;
if ( ic ) {
const GameItem* fruit = ic->FindItem( ISC::fruit );
if ( fruit ) {
//cs->nElixir += ELIXIR_PER_FRUIT;
GameItem* item = ic->RemoveFromInventory( fruit );
delete item;
const GameItem& def = ItemDefDB::Instance()->Get( "elixir" );
for (int k = 0; k < ELIXIR_PER_FRUIT; ++k) {
GameItem* gameItem = def.Clone();
Vector2F pos2 = RandomInRect(porch, &parentChit->random);
static const int ELIXIR_SELF_DESTRUCT = 60*1000;
Context()->chitBag->NewItemChit(ToWorld3F(pos2), gameItem, false, true, ELIXIR_SELF_DESTRUCT);
}
}
}
}
}
if (ic && ic->FindItem(ISC::fruit) < 0) {
// No fruit, no progress.
progress = 0;
}
SetInfoText();
return progressTick.Next();
}