]> jfr.im git - irc/gameservirc.git/blob - gameserv/script.cpp
updated dependencies
[irc/gameservirc.git] / gameserv / script.cpp
1
2 #include "script.h"
3 #include "extern.h"
4 #include <fstream>
5 #include <string>
6
7 using namespace std;
8
9 script::script()
10 {
11 scriptstr = "";
12 }
13
14 script::script(string &str)
15 {
16 scriptstr = str;
17 }
18
19 script::script(const char *str)
20 {
21 scriptstr = str;
22 }
23
24 script::~script()
25 {
26 scriptstr = "";
27 }
28
29 void script::setString(string &str)
30 {
31 scriptstr = str;
32 }
33
34 void script::setString(const char *str)
35 {
36 scriptstr = str;
37 }
38
39 bool script::executeScript(Player *p)
40 {
41 string line;
42 string::size_type begin = 0;
43 string::size_type end = scriptstr.find("\n", 0);
44 char *left, *op, *right, *buffer;
45 void *leftptr;
46
47 while (end != string::npos)
48 {
49 // Get the line based on what we just found
50 line = scriptstr.substr(begin, end - begin);
51
52 log("Line: %s", line.c_str());
53
54 if (line.c_str()[0] != '#')
55 {
56 // Not a comment
57 buffer = new char[line.length()];
58
59 left = strtok(buffer, " ");
60 op = strtok(NULL, " ");
61 right = strtok(NULL, " ");
62
63 if (stricmp(left, "GOLD") == 0)
64 {
65 (int*)leftptr = p->gold;
66 }
67 else if (stricmp(left, "STRENGTH") == 0)
68 {
69 (int*)leftptr = p->strength;
70 }
71 }
72
73 // Find the next line
74 begin = end + 1;
75 end = scriptstr.find("\n", begin);
76 }
77 return true;
78 }
79
80 bool script::loadScript(const char *filename)
81 {
82 char *buffer;
83 int length;
84 ifstream infile;
85 bool result;
86
87 infile.open(filename, ios::binary);
88
89 if (infile.fail())
90 {
91 log("Error opening script %s", filename);
92 return false;
93 }
94
95 // get length of file:
96 infile.seekg (0, ios::end);
97 length = infile.tellg();
98 infile.seekg (0, ios::beg);
99
100 if (length > 0)
101 {
102 buffer = new char[length];
103 infile.read(buffer, length);
104 scriptstr = buffer;
105 delete [] buffer;
106 result = true;
107 }
108 else
109 {
110 result = false;
111 }
112 infile.close();
113 return result;
114 }