root/deus/director_main.cpp

Revision 8, 7.7 kB (checked in by pantley2, 4 years ago)

added old DEUS project to SVN

Line 
1 /**
2  * director_main.cpp
3  */
4
5 // Director includes
6 #include "structures.h"
7 #include "AuthorInterface_impl.h"
8 #include "DirectorInterface_impl.h"
9 #include "forward_search.h"
10 #include "StoryTracer.h"
11
12 #include <time.h>
13
14 // Qt includes
15 #include <qapplication.h>
16 #include <qtimer.h>
17 #include <qlayout.h>
18
19
20 // Prototypes
21 void getAuthorInput(QApplication &app);
22 void direct(QApplication &app);
23 void MakeOps();
24
25 DirectorForm_impl *dfi = NULL;          // 6am day of EOH hack
26
27
28 /************************************************
29  * main()
30  ************************************************/
31 void main()
32 {
33         // Init Qt
34         int argc = 0;
35         QApplication app(argc, NULL);
36
37         // Open DB connection
38         conn = mysql_init(conn);
39         conn = mysql_real_connect(conn, "128.174.252.116", "deus",
40                                 "DeusExMachina", "deus", 0, NULL, 0);
41
42         // Populate the story graph beginning with story_root
43         getAuthorInput(app);
44
45         // Connect to Model
46         real_world = new World();
47         real_world->Locations = MapGenerator();
48         real_world->map_height = GetMapSize().first;
49         real_world->map_width = GetMapSize().second;
50         //recievedata();
51         MakeOps();
52
53         // Direct!
54         direct(app);
55
56         // Close Model/DB connections
57         mysql_close(conn);
58 }
59
60
61
62 /************************************************
63  * Functions called from main()
64  ************************************************/
65 void getAuthorInput(QApplication &app)
66 {
67         AuthorForm_impl f(NULL, "author_form");
68         QObject::connect(&app, SIGNAL(lastWindowClosed()), &f, SLOT(closing()));
69
70         f.show();
71         while (!f.is_closing) app.processEvents();
72 }
73
74 void direct(QApplication &app)
75 {
76         DirectorForm_impl f(NULL, "director_form");
77         dfi = &f;
78         QObject::connect(&app, SIGNAL(lastWindowClosed()), &f, SLOT(shuttingDown()));
79         QObject::connect(&app, SIGNAL(aboutToQuit()), &f, SLOT(shuttingDown()));
80
81        
82         //
83         //
84         // FOR SNAZZY SCREENSHOT, NOT REAL CODE
85
86         // Install stylish forward graph view pane
87         QBoxLayout *layout = new QVBoxLayout((QWidget *)f.ForwardFrame, 5);
88         PlanGraphDisplay *f_gd = new PlanGraphDisplay(forward_root, (QWidget*)f.ForwardFrame);
89         layout->addWidget(f_gd);
90
91         // Reverse panes
92         QBoxLayout *rlayout = new QVBoxLayout((QWidget *)f.RegressionFrame, 5);
93         PlanGraphDisplay *r_gd = new PlanGraphDisplay(regression_root, (QWidget*)f.RegressionFrame);
94         rlayout->addWidget(r_gd);
95
96         f.show();
97
98 /*
99         srand(783879292);
100
101 while (true)
102 {
103         delete forward_root; delete regression_root;
104         forward_root = NULL; regression_root = NULL;
105
106         bool done = false;
107         int i, j;
108         Operator none;
109         PlanNode *n1, *n = forward_root = new PlanNode(none, 0);
110         PlanNode *n2 = regression_root = new PlanNode(none, 0);
111         f_gd->root = n;
112         r_gd->root = n2;
113
114         list<PlanNode *> queue;
115         for (int depth = 0; !done && depth < 15; depth++) {
116                 //printf("depth: %d\n", depth);
117                 while (!done && n->depth == depth)
118                 {
119                         j = (int)(4*(rand()/(double)RAND_MAX));
120                         for (i = 0; i < j; i++)
121                         {
122                                 n1 = new PlanNode(none, depth+1);
123                                 //n1->icon->ul_x = n->icon->ul_x - ICON_W - 100 - (700 * (rand()/(double)RAND_MAX));
124                                 //n1->icon->ul_y = n->icon->ul_y + (20 * ICON_H * (rand()/(double)RAND_MAX) - (10*ICON_H));
125                                 n1->icon->ul_x = n->icon->ul_x + ICON_W + 200;
126                                 n1->icon->ul_y = n->icon->ul_y + (3*ICON_H*(j-i-(j/1.5)));
127
128                                 n->branches.push_back(n1);
129                                 queue.push_back(n1);
130                         }
131
132                         f_gd->fit();
133                         
134                         if (queue.size() > 0) {n = queue.front(); queue.pop_front();}
135                         else done = true;
136
137                         app.processEvents();
138
139
140 if (depth < 10) {
141         
142                 queue.clear();
143                         j = (int)(4*(rand()/(double)RAND_MAX));
144                         for (i = 0; i < j; i++)
145                         {
146                                 n1 = new PlanNode(none, depth+1);
147                                 n1->icon->ul_x = n2->icon->ul_x - ICON_W - 100 - (700 * (rand()/(double)RAND_MAX));
148                                 n1->icon->ul_y = n2->icon->ul_y + (20 * ICON_H * (rand()/(double)RAND_MAX) - (10*ICON_H));
149                                 //n1->icon->ul_x = n->icon->ul_x - ICON_W - 200;
150                                 //n1->icon->ul_y = n->icon->ul_y + (3*ICON_H*(j-i-(j/1.5)));
151
152                                 n2->branches.push_back(n1);
153                                 queue.push_back(n1);
154                         }
155
156                         r_gd->fit();
157                         
158                         if (queue.size() > 0) {n2 = queue.front(); queue.pop_front();}
159                         else done = true;
160 }
161
162                         app.processEvents();
163                 }
164         }
165 }
166         
167 */
168         // END SNAZZY SCREENSHOT CODE
169         //
170         //
171
172 /*
173         // Install stylish forward graph view pane
174         QBoxLayout *layout = new QVBoxLayout((QWidget *)f.ForwardFrame, 5);
175         PlanGraphDisplay *f_gd = new PlanGraphDisplay(forward_root, (QWidget*)f.ForwardFrame);
176         layout->addWidget(f_gd);
177
178
179         // Reverse panes
180         QBoxLayout *rlayout = new QVBoxLayout((QWidget *)f.RegressionFrame, 5);
181         PlanGraphDisplay *r_gd = new PlanGraphDisplay(regression_root, (QWidget*)f.RegressionFrame);
182         rlayout->addWidget(r_gd);
183 */
184
185 //      f.show();
186         //f_gd->fit();
187
188
189         // Generate NPCs
190         for (int i = 0; i < 50; i++)
191         {
192                 NPC n;
193
194                 n.name = "npc";
195
196                 n.xcoord = 300 * (rand()/(double)RAND_MAX);
197                 n.ycoord = 300 * (rand()/(double)RAND_MAX);
198
199                 n.attributes.insert(std::pair<string,int>("Hunger", 100*(rand()/(double)RAND_MAX)));
200                 n.attributes.insert(std::pair<string,int>("Occupation", 20*(rand()/(double)RAND_MAX)));
201                 n.attributes.insert(std::pair<string,int>("Drunk", 100*(rand()/(double)RAND_MAX)));
202                 n.attributes.insert(std::pair<string,int>("Thirst", 10*(rand()/(double)RAND_MAX)));
203                 n.attributes.insert(std::pair<string,int>("Money", 10000*(rand()/(double)RAND_MAX)));
204                 n.attributes.insert(std::pair<string,int>("Morality", 10*(rand()/(double)RAND_MAX)));
205                 n.attributes.insert(std::pair<string,int>("Fatigue", 10*(rand()/(double)RAND_MAX)));
206
207                 real_world->NPCs.push_back(n);
208         }
209
210
211         // Direct awayn.attributes.insert(std::pair<string,int>("Hunger", 100));
212         bool success = true, complete = false;
213         StoryTracer tracer;
214         while (story_root && !f.shutting_down && !complete)
215         {
216                 forward_search(0 /* unused */);
217                 f_gd->root = forward_root; f_gd->fit();
218                 app.processEvents();
219                 complete = tracer.Trace(success);
220                 r_gd->root = regression_root; r_gd->fit();
221                 app.processEvents();
222 //              if (matches) execute_
223 //              success = networking stuff
224                 success = false;
225                 app.processEvents();
226         }
227 }
228
229
230 void MakeOps()
231 {
232         Operator OP;
233        
234         // Change Attribute Operator
235         for(int i=0; i<NUM_COND; i++)
236         {
237                 for(int j=0; j<NUM_COND; j++)
238                 {
239                         ((OP.PreConds).conditions)[i][j]="";
240                         ((OP.PostConds).conditions)[i][j]="";
241                 }
242         }
243         ((OP.PreConds).conditions)[0][0]="npc";
244         ((OP.PostConds).conditions)[0][0]="npc";
245         ((OP.PostConds).conditions)[1][0]="attrib value";
246         OP.code=OP_CHANGEATTRIBUTE;
247         Ops.push_back(OP);
248
249         //MOVENPC Operator
250         for(i=0; i<NUM_COND; i++)
251         {
252                 for(int j=0; j<NUM_COND; j++)
253                 {
254                         ((OP.PreConds).conditions)[i][j]="";
255                         ((OP.PostConds).conditions)[i][j]="";
256                 }
257         }
258         ((OP.PreConds).conditions)[0][0]="npc";
259         ((OP.PostConds).conditions)[0][0]="npc";
260         ((OP.PostConds).conditions)[1][0]="xloc yloc";
261         OP.code=OP_MOVENPC;
262         Ops.push_back(OP);
263 }
264
265 void initRegressionPanes()
266 {
267 printf("Don't call initRegressionPanes()!\n");
268 /*
269         // Reverse panes
270         QGridLayout *r_layout = new QGridLayout((QWidget *)dfi->RegressionFrame, 1, 1, 5);
271         for (int i = 0; i < regression_roots.size(); i++)
272         {
273                 // Qt takes care of deallocation
274                 PlanGraphDisplay *r_gd1 = new PlanGraphDisplay(regression_roots[i]);
275                 QWidgetItem *wi = new QWidgetItem(r_gd1);
276                 r_layout->addItem(wi); 
277         }
278 */
279 }
280
281
282 /************************************************
283  * Global Data definition - declaration in structures.h
284  ************************************************/
285 StoryNode *story_root = NULL;           // root of story graph
286 World* real_world = NULL;                       // real world state
287 MYSQL* conn = NULL;                                     // database connection
288 list<Operator> Ops;                                     // operators
289
290 PlanNode* forward_root = NULL;          // forward search
291 PlanNode* regression_root;                      // regression plans
292 PlanNode* current_plan_root = NULL;     // complete plan being executed
293 list<vector<PlanNode*> > pending_execution;
294
Note: See TracBrowser for help on using the browser.