-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_old.cpp
More file actions
executable file
·306 lines (256 loc) · 6.97 KB
/
Copy pathmain_old.cpp
File metadata and controls
executable file
·306 lines (256 loc) · 6.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
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
#include "Aw.h"
#include "Reson.h"
#include "ConfigFile.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <fcntl.h>
//#include <errno.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#define true 1
#define false 0
using std::string;
using std::cout;
using std::endl;
//extern int h_errno;
static int entered=0,entercode=0;
static char* level_dir="levels";
static char* greed_message="";
void handle_avatar_add (void);
void handle_chat (void);
void handle_enter (int rc);
void save_world_settings(char* name);
void load_world_settings(char* name, int session);
char* stc(string value);
main (int argc, char *argv[])
{
char tmp[255];
int rc;
int i,n;
// Config file laden
sprintf(tmp, "%s.conf", argv[0]);
ConfigFile config(tmp);
// Defenitions foor config file
char* host = stc(config.read<string>("host","auth.activeworlds.com"));
int port = config.read("port",6670);
int cit = config.read("cit", 1);
char* priff = stc(config.read<string>("priff", "geheim"));
char* botname = stc(config.read<string>("name", "Level"));
char* world = stc(config.read<string>("world", "AW"));
int loc_x = config.read("loc_x", 0);
int loc_y = config.read("loc_y", 0);
int loc_z = config.read("loc_z", 0);
int loc_yaw = config.read("loc_yaw", 0);
int greed = config.read("greed", 1);
level_dir = stc(config.read<string>("level_dir", "levels"));
greed_message = stc(config.read<string>("greed_message", "Hallo %s"));
/* initialize Active Worlds API */
if(rc = aw_init(AW_BUILD)) {
printf ("Unable to initialize API (reason %d)\n", rc);
exit (1);
}
printf("AW Init called %d\n", AW_BUILD);
/* create bot instance */
if (rc = aw_create(host, port, 0 )) {
printf ("Unable to create bot instance (reason %d) %s@%d\n", rc, host, port);
exit (1);
}
printf("AW create called %s@%d\n", host, port);
/* install evend handlers */
aw_event_set(AW_EVENT_CHAT, handle_chat);
aw_callback_set(AW_CALLBACK_ENTER, handle_enter);
if(greed == 1) {
aw_event_set(AW_EVENT_AVATAR_ADD, handle_avatar_add);
printf("Greedings ar on with '%s'\n", greed_message);
}
else {
printf("Greedings ar off\n");
}
/* log bot into the universe */
aw_int_set(AW_LOGIN_OWNER, cit);
aw_string_set(AW_LOGIN_PRIVILEGE_PASSWORD, priff);
aw_string_set(AW_LOGIN_APPLICATION, "Hetwieg.NL Level Manager V0.0.0.1");
aw_string_set(AW_LOGIN_NAME, botname);
printf("aw_login call\n");
if (rc = aw_login ()) {
printf ("Unable to login (reason %d)\n", rc);
exit (1);
}
printf("aw_login called for %s @ %d\n", botname, cit);
/* log bot into the world */
printf ("Entering to world %s\n", world);
entered=false;
aw_bool_set(AW_ENTER_GLOBAL,true);
if (rc = aw_enter(world)) {
printf ("Unable to enter world (reason %d)\n", rc);
exit (1);
}
while (1) {
if (entered) break;
aw_wait(10);
}
if (entercode) {
printf ("Unable to enter world (reason %d)\n", rc);
aw_destroy();
aw_wait(0);
aw_term();
exit (1);
}
printf ("Entered to world %s \n",world);
/* announce our position in the world */
aw_int_set(AW_MY_X, loc_x);
aw_int_set(AW_MY_Y, loc_y);
aw_int_set(AW_MY_Z, loc_z);
aw_int_set(AW_MY_YAW, loc_yaw);
if(rc = aw_state_change()) {
printf ("Unable to change state (reason %d)\n", rc);
aw_destroy();
aw_wait(0);
aw_term();
exit(1);
}
printf ("State changed \n");
/* main event loop */
printf("type q <cr> for quit\n");
while (1) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to 100 mseconds. */
tv.tv_sec = 0;
tv.tv_usec = 100000;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval) {
if (getchar() == 'q') {
aw_destroy();
aw_wait(0);
aw_term();
return 0;
}
};
aw_wait(10);
}
/* close everything down */
aw_destroy ();
aw_wait(0);
aw_term ();
return 0;
}
void handle_avatar_add (void)
{
char message[AW_MAX_ATTRIBUTE_LENGTH+1];
// printf ("avatar_add by %s: %s\n",(char *)aw_user_data(), aw_string (AW_AVATAR_NAME));
if (aw_string(AW_AVATAR_NAME)[0]=='[') return; // don't greet bots
sprintf(message, greed_message, aw_string (AW_AVATAR_NAME));
aw_say(message);
printf("Bot %s\n", message);
/* log the event to the console */
}
void handle_enter(int rc)
{
entered=1;
printf("Enter code=%d\n",rc);
entercode=rc;
}
static char* chat_type[] = {"Chat ", "Broadcast ", "Whisper "};
void handle_chat(void)
{
char message[AW_MAX_ATTRIBUTE_LENGTH+1];
int session;
char tmp[AW_MAX_ATTRIBUTE_LENGTH+1];
sprintf(message, "%s", aw_string(AW_CHAT_MESSAGE));
session = aw_int(AW_AVATAR_SESSION);
printf("%s %s: %s\n",chat_type[aw_int (AW_CHAT_TYPE)], aw_string(AW_AVATAR_NAME),message);
sprintf(tmp, "%s version", aw_string(AW_LOGIN_NAME)); //TODO: CAPS CONTROLE
if(strcmp(message, "\\version") == 0 || strcmp(message, "/version") == 0 || strcmp(message, tmp) == 0) {
aw_whisper(session, "Ik ben level manager V0.0.0.1");
}
char* tmp2 = strtok(message, " ");
if(strcmp(tmp2, "\\load") == 0 || strcmp(tmp2, "/load") == 0) {
aw_whisper(session, "World config woord geladen");
load_world_settings(strtok (NULL, " ,.-"), session);
}
if(strcmp(tmp2, "\\save") == 0 || strcmp(tmp2, "/save") == 0) {
aw_whisper(session, "World config woord opgeslagen");
save_world_settings(strtok (NULL, " ,.-"));
}
}
void save_world_settings(char* name)
{
FILE* fp;
int rc;
int i;
int read_only;
char string[AW_MAX_ATTRIBUTE_LENGTH + 1];
sprintf(string, "%s/%s.txt",level_dir, name);
fp = fopen (string, "w");
for (i = 0; i < 200; i++) {
rc = aw_world_attribute_get(i, &read_only, string);
if (rc == RC_INVALID_ATTRIBUTE)
continue;
if (rc != RC_SUCCESS)
break;
if (read_only)
continue;
fprintf(fp, "%d %s\n", i, string);
}
fclose (fp);
}
void load_world_settings(char* name, int session)
{
FILE *fp;
int rc;
int id;
char string[4096];
sprintf(string, "%s/%s.txt",level_dir, name);
fp = fopen (string, "r");
if(fp == NULL) {
printf ("Unable to open %s/%s.txt\n", level_dir, name);
return;
}
for(;;) {
rc = fscanf(fp, "%d%[^\n]", &id, string);
if (rc == EOF)
break;
if(rc != 2) {
printf("Unable to load attributes: invalid format\n");
fclose(fp);
return;
}
/* string + 1 skips the space delimiter */
rc = aw_world_attribute_set(id, string + 1);
if (rc != RC_SUCCESS) {
printf("Unable to set attribute (reason %d)\n", rc);
fclose (fp);
return;
}
}
fclose (fp);
rc = aw_world_attributes_send(session);
if(rc != RC_SUCCESS) {
printf("Unable to load attributes (reason %d)\n", rc);
}
else {
printf("Attribute load complete for %d\n", session);
}
}
char* stc(string value)
{
//printf("%s\n",value);
char* cstr;
cstr = new char [value.size()+1];
strcpy(cstr, value.c_str());
return cstr;
}