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