Source: world_api/objects.js

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
require("./../rur.js");
require("./../utils/key_exist.js");
require("./../utils/validator.js");
require("./../recorder/record_frame.js");
require("./artefact.js");

/** @function add_object
 * @memberof RUR
 * @instance
 * @summary This function adds one or more of a given object at a location.
 *
 * @param {string} name Name of the object
 * @param {integer} x  Position: `1 <= x <= max_x`
 * @param {integer} y  Position: `1 <= y <= max_y`
 * @param {object} [options] A Javascript object (or Python dict) containing
 * additional arguments
 * @param {boolean} [options.goal] If `true`, this will represent a goal
 * i.e. the number of object that must be put at that location.
 * @param {integer} [options.number] The number of objects to **add** at that
 * location; it is 1 by default.
 * @param {boolean} [options.replace] If `true`, the specified number
 * (default=1) will **replace** the existing number of objects at that location.
 * During the Onload phase, this is automatically set to `true`.
 * @param {integer} [options.min] Specifies the minimum of objects to be
 * put at that location; together with `options.max`, it is used to choose
 * a random number of objects to be found at that location.
 * @param {integer} [options.max] Specifies the maximum number of objects to be
 * put at that location; together with `options.min`, it is used to choose
 * a random number of objects to be found at that location.
 *
 * @throws Will throw an error if `(x, y)` is not a valid location.
 * @throws Will throw an error if `name` is not a known thing.
 *
 */
RUR.add_object = function (name, x, y, options) {
    "use strict";
    var k, keys, args;

    args = {name: RUR.translate_to_english(name), x:x, y:y,
            type:"objects", valid_names: RUR.KNOWN_THINGS};
    if (options === undefined) {
        args.number = 1;
    } else {
        if (options.goal && options.goal == "all") {
            options.number = "all";
        } else if (options.min !== undefined) {
            if (options.max !== undefined && options.max > options.min) {
                options.number = options.min + "-" + options.max;
                args.replace = true;
            } else {
                options.number = options.min;
            }
        } else if (options.number === undefined) {
            options.number = 1
        }
        keys = Object.keys(options);
        for (k of keys) {
            args[k] = options[k];
        }
    }

    if (RUR.state.evaluating_onload) {
        args.replace = true;
    }
    if (args.replace) {
        RUR._set_nb_artefact(args);
    } else {
        RUR._add_artefact(args);
    }
    RUR.record_frame("RUR.add_object", args);
};


/** @function remove_object
 * @memberof RUR
 * @instance
 * @summary This function removes an object at a location.
 *
 * @param {string} name Name of the object
 * @param {integer} x  Position: `1 <= x <= max_x`
 * @param {integer} y  Position: `1 <= y <= max_y`
 * @param {object} [options] A Javascript object (or Python dict) containing
 * additional arguments
 *
 * @param {boolean} [options.goal] If `true`, this will represent a goal
 * i.e. the number of object that must be put at that location.
 * @param {integer} [options.number] The number of objects to **add** at that
 * location; it is 1 by default.
 * @param {boolean} [options.all] If `true`, all such objects will be removed.
 *
 * @throws Will throw an error if `(x, y)` is not a valid location.
 * @throws Will throw an error if `name` is not a known thing.
 * @throws Will throw an error if there is no object to remove
 *        at that location
 */
RUR.remove_object = function (name, x, y, options) {
    "use strict";
    var args, k, keys, world = RUR.get_current_world();

    args= {x:x, y:y, type:"objects", name:RUR.translate_to_english(name),
           valid_names: RUR.KNOWN_THINGS};
    if (options !== undefined) {
        keys = Object.keys(options);
        for (k of keys) {
            args[k] = options[k];
        }
    }
    try {
        RUR._remove_artefact(args);
    } catch (e) {
        if (e.message == "No artefact to remove") {
            throw new RUR.ReeborgError("No object to remove here.");
        } else {
            throw e;
        }
    }
    // For historical reason, worlds are always created with an "objects" attribute
    RUR.utils.ensure_key_for_obj_exists(world, "objects");
    RUR.record_frame("RUR.remove_object", args);
};


/** @function get_objects
 * @memberof RUR
 * @instance
 * @summary This function returns a Javascript Object containing
 * the names of the objects found at that location.
 * When using from Python, it should be explictly converted into a `dict`
 * using `dict(RUR.get_objects(x, y))`.
 *
 * If nothing is found at that location,
 * `null` is returned (which is converted to `None` in Python programs.)
 *
 * @param {integer} x  Position: `1 <= x <= max_x`
 * @param {integer} y  Position: `1 <= y <= max_y`
 *
 * @param {object} [options] A Javascript object (or Python dict) containing
 * additional arguments
 *
 * @param {boolean} [options.goal] If `true`, this will represent a goal
 * i.e. the number of object that must be put at that location.
 *
 * @throws Will throw an error if `(x, y)` is not a valid location.
 *
 */

RUR.get_objects = function (x, y, options) {
    "use strict";
    var args, obj, obj_en, k, keys;
    args = {x:x, y:y, type:"objects"}
    if (options!=undefined && options.goal != undefined) {
        args.goal = options.goal;
    }
    obj_en = RUR._get_artefacts(args);


    if (!obj_en) {
        return null;
    }

    obj = {};
    keys = Object.keys(obj_en);
    for (k of keys) {
        obj[RUR.translate(k)] = obj_en[k];
    }
    return obj;
};


/** @function is_object
 * @memberof RUR
 * @instance
 * @summary This function returns `true/True` if a named obstacle is present
 * at a given location, `false/False` otherwise
 *
 * @param {string} name The name of the obstacle
 * @param {integer} x  Position: `1 <= x <= max_x`
 * @param {integer} y  Position: `1 <= y <= max_y`
 *
 * @param {object} [options] A Javascript object (or Python dict) containing
 * additional arguments
 *
 * @param {boolean} [options.goal] If `true`, this will represent a goal
 * [i.e., the number of object that must be put at that location.]
 *
 * @throws Will throw an error if `(x, y)` is not a valid location.
 *
 */

RUR.is_object = function (name, x, y, options) {
    "use strict";
    var nb, args = {x:x, y:y, name:RUR.translate_to_english(name),
                    type:"objects", valid_names: RUR.KNOWN_THINGS};
    if (options !== undefined && options.goal !== undefined) {
        args.goal = options.goal;
    }
    nb = RUR._get_nb_artefact(args);
    if (nb === 0) {
        return false;
    } else {
        return true;
    }
};


/* The following is deprecated. Some worlds may have been created
  using it (e.g. in Vincent Maille's book) */
RUR.add_object_at_position = function(name, x, y, number) { // Vincent Maille's book
    RUR.add_object(name, x, y, {number:number});
}