event.c 15.7 KB
Newer Older
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
/* 
 *
 *   Copyright (c) 1994, 2002, 2003 Johannes Prix
 *   Copyright (c) 1994, 2002 Reinhard Prix
 *   Copyright (c) 2004-2009 Arthur Huillet 
 *
 *
 *  This file is part of Freedroid
 *
 *  Freedroid is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Freedroid is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Freedroid; see the file COPYING. If not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *  MA  02111-1307  USA
 *
 */

/** 
28 29 30
 * This file contains functions related to events and event triggers. These functions 
 * check, whether the necessary conditions for an event are satisfied, and in case that
 * they are, they order the appropriate event to be executed.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 */

#define _event_c
#include "system.h"

#include "defs.h"
#include "struct.h"
#include "global.h"
#include "proto.h"

struct event_trigger {

	luacode lua_code;

	char *name;
	int enabled;		//is the trigger enabled?
	int silent;		//do we have to advertise this trigger to the user? (teleporters..)

	enum {
		POSITION,
51
		CHANGE_LEVEL,
52
		ENEMY_DEATH,
53 54
		ENEMY_HACK,
		OBSTACLE_ACTION,
55 56 57 58
	} trigger_type;

	union {
		struct {
59
			int lvl;
60 61
			int x;
			int y;
62
			int teleported;
63 64
		} position;
		struct {
65 66
			int exit_level;
			int enter_level;
67 68
		} change_level;
		struct {
69
			int lvl;
70 71 72
			int faction;
			char *dialog_name;
			int marker;
73 74 75 76 77 78
		} enemy_event;
		struct {
			int lvl;
			int type;
			char *label;
		} obstacle_action;
79 80 81 82 83 84 85 86 87
	} trigger;
};

static struct dynarray event_triggers;

/**
 * Delete all events and event triggers
 */
static void clear_out_events(void)
88 89 90 91 92 93 94 95 96 97 98
{
	// Remove existing triggers
	delete_events();

	dynarray_init(&event_triggers, 16, sizeof(struct event_trigger));
}

/**
 * Delete all events and event triggers
 */
void delete_events(void)
99 100 101 102 103 104 105 106 107 108
{
	// Remove existing triggers
	int i;
	struct event_trigger *arr = event_triggers.arr;
	for (i = 0; i < event_triggers.size; i++) {
		if (arr[i].name)
			free(arr[i].name);

		if (arr[i].lua_code)
			free(arr[i].lua_code);
109 110 111 112 113 114

		if (arr[i].trigger_type == OBSTACLE_ACTION) {
			free(arr[i].trigger.obstacle_action.label);
		} else if (arr[i].trigger_type == ENEMY_DEATH || arr[i].trigger_type == ENEMY_HACK) {
			free(arr[i].trigger.enemy_event.dialog_name);
		}
115 116 117 118 119 120 121
	}

	dynarray_free(&event_triggers);
}

#define EVENT_TRIGGER_BEGIN_STRING "* New event trigger *"
#define EVENT_TRIGGER_END_STRING "* End of trigger *"
122 123
#define EVENT_TRIGGER_NAME_MARKED_STRING "Name=_\""
#define EVENT_TRIGGER_NAME_STRING "Name=\""
124 125 126 127 128 129
#define EVENT_TRIGGER_LUACODE_STRING "<LuaCode>"
#define EVENT_TRIGGER_LUACODE_END_STRING "</LuaCode>"
#define EVENT_TRIGGER_ENABLED_STRING "Enable this trigger by default="

// For position-based events
#define EVENT_TRIGGER_LABEL_STRING "Trigger at label=\""
130
#define EVENT_TRIGGER_TELEPORTED "Teleported="
131 132
#define EVENT_TRIGGER_IS_SILENT_STRING "Silent="

133 134 135 136
// For level-change events
#define LEVEL_CHANGE_TRIGGER "Trigger changing level"
#define LEVEL_CHANGE_EXITING "Exiting level="
#define LEVEL_CHANGE_ENTERING "Entering level="
137 138

// For enemy-death events
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#define ENEMY_DEATH_TRIGGER "Trigger on enemy death"

// For enemy-hacked events
#define ENEMY_HACK_TRIGGER "Trigger on enemy hack"

// For all enemy events
#define ENEMY_TRIGGER "Trigger on enemy"
#define ENEMY_LVLNUM "Enemy level="
#define ENEMY_FACTION "Enemy faction=\""
#define ENEMY_DIALOG_NAME "Enemy dialog name=\""
#define ENEMY_MARKER "Enemy marker="

// For obstacle events
#define OBSTACLE_ACTION_TRIGGER "Trigger on obstacle"
#define OBSTACLE_ACTION_LVLNUM "Obstacle level number="
#define OBSTACLE_ACTION_TYPE "Obstacle type name=\""
#define OBSTACLE_ACTION_LABEL "Obstacle label=\""
156 157 158 159 160 161 162 163 164 165

/** 
 *
 *
 */
static void load_events(char *EventSectionPointer)
{
	char *EventPointer;
	char *EndOfEvent;
	char *TempMapLabelName;
166
	gps pos;
167 168 169
	char *TempEnemyFaction;
	char s;
	struct event_trigger temp;
170
	char *temp_str;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

	EventPointer = EventSectionPointer;
	while ((EventPointer = strstr(EventPointer, EVENT_TRIGGER_BEGIN_STRING)) != NULL) {
		memset(&temp, 0, sizeof(struct event_trigger));

		EventPointer += strlen(EVENT_TRIGGER_BEGIN_STRING) + 1;

		EndOfEvent = LocateStringInData(EventPointer, EVENT_TRIGGER_END_STRING);
		s = EndOfEvent[strlen(EVENT_TRIGGER_END_STRING) - 1];
		EndOfEvent[strlen(EVENT_TRIGGER_END_STRING) - 1] = 0;

		// Determine type of event condition
		if ((TempMapLabelName = strstr(EventPointer, EVENT_TRIGGER_LABEL_STRING))) {
			temp.trigger_type = POSITION;
			TempMapLabelName = ReadAndMallocStringFromData(EventPointer, EVENT_TRIGGER_LABEL_STRING, "\"");
186 187 188 189
			pos = get_map_label_center(TempMapLabelName);
			temp.trigger.position.x = (int)pos.x;
			temp.trigger.position.y = (int)pos.y;
			temp.trigger.position.lvl = pos.z;
190 191 192
			free(TempMapLabelName);
			ReadValueFromStringWithDefault(EventPointer, EVENT_TRIGGER_IS_SILENT_STRING, "%d", "1",
						&temp.silent, EndOfEvent);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
			ReadValueFromStringWithDefault(EventPointer, EVENT_TRIGGER_TELEPORTED, "%d", "-1",
						&temp.trigger.position.teleported, EndOfEvent);
		} else if (strstr(EventPointer, LEVEL_CHANGE_TRIGGER)) {
			temp.trigger_type = CHANGE_LEVEL;
			ReadValueFromStringWithDefault(EventPointer, LEVEL_CHANGE_ENTERING, "%d", "-1",
						&temp.trigger.change_level.enter_level, EndOfEvent);
			ReadValueFromStringWithDefault(EventPointer, LEVEL_CHANGE_EXITING, "%d", "-1",
						&temp.trigger.change_level.exit_level, EndOfEvent);
		} else if (strstr(EventPointer, ENEMY_TRIGGER)) {
			if (strstr(EventPointer, ENEMY_DEATH_TRIGGER)) {
				temp.trigger_type = ENEMY_DEATH;
			} else  {
				temp.trigger_type = ENEMY_HACK;
			}
			ReadValueFromStringWithDefault(EventPointer, ENEMY_LVLNUM, "%d", "-1",
						&temp.trigger.enemy_event.lvl, EndOfEvent);
			if (strstr(EventPointer, ENEMY_FACTION)) {
				TempEnemyFaction = ReadAndMallocStringFromData(EventPointer, ENEMY_FACTION, "\"");
				temp.trigger.enemy_event.faction = get_faction_id(TempEnemyFaction);
212 213
				free(TempEnemyFaction);
			} else {
214 215 216 217 218 219
				temp.trigger.enemy_event.faction = -1;
			}
			if (strstr(EventPointer, ENEMY_DIALOG_NAME)) {
				temp.trigger.enemy_event.dialog_name = ReadAndMallocStringFromData(EventPointer, ENEMY_DIALOG_NAME, "\"");
			} else {
				temp.trigger.enemy_event.dialog_name = NULL;
220
			}
221 222 223 224 225 226 227 228 229 230
			ReadValueFromStringWithDefault(EventPointer, ENEMY_MARKER, "%d", "-1",
						&temp.trigger.enemy_event.marker, EndOfEvent);
		} else if (strstr(EventPointer, OBSTACLE_ACTION_TRIGGER)) {
			temp.trigger_type = OBSTACLE_ACTION;
			ReadValueFromStringWithDefault(EventPointer, OBSTACLE_ACTION_LVLNUM, "%d", "-1",
						&temp.trigger.obstacle_action.lvl, EndOfEvent);
			temp_str = ReadAndMallocStringFromDataOptional(EventPointer, OBSTACLE_ACTION_TYPE, "\"");
			if (temp_str) {
				temp.trigger.obstacle_action.type = get_obstacle_type_by_name(temp_str);
				free(temp_str);
231
			} else {
232
				temp.trigger.obstacle_action.type = -1;
233
			}
234
			temp.trigger.obstacle_action.label = ReadAndMallocStringFromDataOptional(EventPointer, OBSTACLE_ACTION_LABEL, "\"");
235 236
		}

237 238 239 240
		temp.name = ReadAndMallocStringFromDataOptional(EventPointer, EVENT_TRIGGER_NAME_MARKED_STRING, "\"");
		if (!temp.name) {
			temp.name = ReadAndMallocStringFromData(EventPointer, EVENT_TRIGGER_NAME_STRING, "\"");
		}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

		temp.lua_code =
			ReadAndMallocStringFromData(EventPointer, EVENT_TRIGGER_LUACODE_STRING, EVENT_TRIGGER_LUACODE_END_STRING);

		ReadValueFromStringWithDefault(EventPointer, EVENT_TRIGGER_ENABLED_STRING, "%d", "1",
						&temp.enabled, EndOfEvent);

		EndOfEvent[strlen(EVENT_TRIGGER_END_STRING) - 1] = '\0';

		EndOfEvent[strlen(EVENT_TRIGGER_END_STRING) - 1] = s;

		dynarray_add(&event_triggers, &temp, sizeof(struct event_trigger));
	}			// While Event trigger begin string found...
}

/**
 * This function reads in the game events, i.e. the locations and conditions
 * under which some actions are triggered.
 */
void GetEventTriggers(const char *EventsAndEventTriggersFilename)
{
	char *EventSectionPointer;
263
	char fpath[PATH_MAX];
264 265 266

	clear_out_events();

267
	find_file(EventsAndEventTriggersFilename, MAP_DIR, fpath, PLEASE_INFORM | IS_FATAL);
268 269 270 271 272 273 274 275 276
	EventSectionPointer =
		ReadAndMallocAndTerminateFile(fpath, "*** END OF EVENT ACTION AND EVENT TRIGGER FILE *** LEAVE THIS TERMINATOR IN HERE ***");

	load_events(EventSectionPointer);

	free(EventSectionPointer);
};

/**
277 278 279
 * \brief Trigger a position-based events for the given positions. 
 * \param cur_pos The current position.
 * \param teleported TRUE if the event come from a teleportation, FALSE otherwise.
280
 */
281
void event_position_changed(gps pos, int teleported)
282 283 284 285 286 287 288 289 290 291 292 293
{
	int i;
	struct event_trigger *arr = event_triggers.arr;

	for (i = 0; i < event_triggers.size; i++) {

		if (arr[i].trigger_type != POSITION)
			continue;

		if (!arr[i].enabled)
			continue;

294 295
		if (arr[i].trigger.position.lvl != pos.z)
 			continue;
296

297 298 299 300 301 302
		if (arr[i].trigger.position.teleported != -1)
			if (arr[i].trigger.position.teleported != teleported)
				continue;

		if (((int)pos.x == arr[i].trigger.position.x) 
				&& ((int)pos.y == arr[i].trigger.position.y)) {
303

304
			run_lua(LUA_DIALOG, arr[i].lua_code);
305 306 307 308 309 310 311 312 313 314 315 316 317
		}
	}
}

/**
 * Trigger level-enter and level-exit events for the given level
 */
void event_level_changed(int past_lvl, int cur_lvl)
{
	int i;
	struct event_trigger *arr = event_triggers.arr;

	for (i = 0; i < event_triggers.size; i++) {
318 319 320 321 322 323 324 325
		if (arr[i].trigger_type != CHANGE_LEVEL)
			continue;

		if (!arr[i].enabled)
			continue;

		if (arr[i].trigger.change_level.exit_level != -1)
			if (arr[i].trigger.change_level.exit_level != past_lvl)
326 327
				continue;

328 329
		if (arr[i].trigger.change_level.enter_level != -1)
			if (arr[i].trigger.change_level.enter_level != cur_lvl)
330 331
				continue;

332 333 334 335 336 337 338 339 340 341 342 343 344 345
		run_lua(LUA_DIALOG, arr[i].lua_code);
	}
}

/**
 * Trigger on enemy events (ENEMY_DEATH or ENEMY_HACK).
 */
static void event_enemy(enemy *target, int event)
{
	int i;
	struct event_trigger *arr = event_triggers.arr;

	for (i = 0; i < event_triggers.size; i++) {
		if (arr[i].trigger_type != event)
346 347 348 349 350
			continue;

		if (!arr[i].enabled)
			continue;

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		if (arr[i].trigger.enemy_event.lvl != -1)
			if (arr[i].trigger.enemy_event.lvl != target->pos.z)
				continue;

		if (arr[i].trigger.enemy_event.faction != -1)
			if (arr[i].trigger.enemy_event.faction != target->faction)
				continue;

		if (arr[i].trigger.enemy_event.dialog_name != NULL)
			if (strcmp(arr[i].trigger.enemy_event.dialog_name, target->dialog_section_name))
				continue;

		if (arr[i].trigger.enemy_event.marker != -1)
			if (arr[i].trigger.enemy_event.marker != target->marker)
				continue;

367 368 369 370
		run_lua(LUA_DIALOG, arr[i].lua_code);
	}
}

371 372 373 374 375 376 377 378 379 380
void event_enemy_died(enemy *dead)
{
	event_enemy(dead, ENEMY_DEATH);
}

void event_enemy_hacked(enemy *hacked)
{
	event_enemy(hacked, ENEMY_HACK);
}

381
/**
382
 * Trigger action event on obstacle.
383
 */
384
void event_obstacle_action(obstacle *o)
385 386 387 388 389
{
	int i;
	struct event_trigger *arr = event_triggers.arr;

	for (i = 0; i < event_triggers.size; i++) {
390
		if (arr[i].trigger_type != OBSTACLE_ACTION)
391
			continue;
392
			
393 394 395
		if (!arr[i].enabled)
			continue;

396 397
		if (arr[i].trigger.obstacle_action.lvl != -1)
			if (arr[i].trigger.obstacle_action.lvl != o->pos.z)
398 399
				continue;

400 401
		if (arr[i].trigger.obstacle_action.type != -1)
			if (arr[i].trigger.obstacle_action.type != o->type)
402 403
				continue;

404 405 406 407 408 409
		if (arr[i].trigger.obstacle_action.label) {
			
			level *lvl = curShip.AllLevels[o->pos.z];
			char *label = (char *) get_obstacle_extension(lvl, o, OBSTACLE_EXTENSION_LABEL);
			
			if (!label || strcmp(arr[i].trigger.obstacle_action.label, label))
410
				continue;
411
		}
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

		run_lua(LUA_DIALOG, arr[i].lua_code);
	}
}

/**
 *
 *
 */
const char *teleporter_square_below_mouse_cursor(void)
{
	finepoint MapPositionOfMouse;
	struct event_trigger *arr;

	if (MouseCursorIsInUserRect(GetMousePos_x(), GetMousePos_y())) {
		MapPositionOfMouse.x = translate_pixel_to_map_location((float)input_axis.x, (float)input_axis.y, TRUE);
		MapPositionOfMouse.y = translate_pixel_to_map_location((float)input_axis.x, (float)input_axis.y, FALSE);
 		arr = visible_event_at_location((int)MapPositionOfMouse.x, (int)MapPositionOfMouse.y, Me.pos.z);
		if (arr)
			return D_(arr->name);
	}
	return NULL;
}


/**
 * Visible event Trigger at a location
 */
struct event_trigger * visible_event_at_location(int x, int y, int z)
{
	int i;
	struct event_trigger *arr = event_triggers.arr;
	for (i = 0; i < event_triggers.size; i++) {
		if (arr[i].trigger_type != POSITION)
			continue;

448
		if (z != arr[i].trigger.position.lvl)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
 			continue;

		if (x != arr[i].trigger.position.x)
			continue;

		if (y != arr[i].trigger.position.y)
			continue;

		if (arr[i].silent)
			continue;

		if (!arr[i].enabled)
			continue;

		return &arr[i];
	}
	return NULL;
}
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

/**
 * Validate Lua code of events
 * This validator loads the events, and checks for the syntax of their lua code.
 * It tries to compile each Lua code snippet, checking them for syntax as well.
 * It then proceeds to executing each Lua code snippet, which makes it possible to check for some errors
 * such as typos in function calls (calls to non existing functions). However, this runtime check does not cover 100% of
 * the Lua code because of branches (when it encounters a if, it will not test both the "then" and "else" branches).
 *
 * As a result, the fact that the validator finds no error does not imply there are no errors in dialogs.
 * Syntax is checked fully, but runtime validation cannot check all of the code.
 */
int validate_events()
{
	int error_caught = FALSE;

	skip_initial_menus = 1;

	/* Disable sound to speed up validation. */
	int old_sound_on = sound_on;
	sound_on = FALSE;

	char fpath[PATH_MAX];
	find_file("levels.dat", MAP_DIR, fpath, PLEASE_INFORM | IS_FATAL);
	LoadShip(fpath, 0);
	PrepareStartOfNewCharacter("NewTuxStartGameSquare");

	/* Temporarily disable screen fadings to speed up validation. */
	GameConfig.do_fadings = FALSE;

	/* _says functions are not run by the validator, as they display
	   text on screen and wait for clicks */
	run_lua(LUA_DIALOG, "function chat_says(a)\nend\n");
	run_lua(LUA_DIALOG, "function cli_says(a)\nend\n");

	/* Subdialogs currently call run_chat and we cannot do that when validating dialogs */
	run_lua(LUA_DIALOG, "function start_chat(a)\nend\n");

	/* Shops must not be run (display + wait for clicks) */
	run_lua(LUA_DIALOG, "function trade_with(a)\nend\n");

	run_lua(LUA_DIALOG, "function user_input_string(a)\nreturn \"dummy\";\nend\n");

	run_lua(LUA_DIALOG, "function upgrade_items(a)\nend\n");
	run_lua(LUA_DIALOG, "function craft_addons(a)\nend\n");

	/* takeover requires user input - hardcode it to win */
	run_lua(LUA_DIALOG, "function takeover(a)\nreturn true\nend\n");

	/* set_mouse_move_target() breaks validator */
	run_lua(LUA_DIALOG, "function set_mouse_move_target(a)\nend\n");

	/* win_game() causes silly animations and delays the process. */
	run_lua(LUA_DIALOG, "function win_game(a)\nend\n");

	/* We do not want to actually exit the game. */
	run_lua(LUA_DIALOG, "function exit_game(a)\nend\n");

	// Loop on all events
	int i;

	GetEventTriggers("events.dat");
	struct event_trigger *arr = event_triggers.arr;

	for (i = 0; i < event_triggers.size; i++) {
		printf("Testing event \"%s\"...\n", arr[i].name);
		int rtn = run_lua(LUA_DIALOG, arr[i].lua_code);
		if (rtn)
			error_caught = TRUE;
		if (term_has_color_cap)
			printf("Result: %s\n", !rtn ? "\033[32msuccess\033[0m" : "\033[31mfailed\033[0m");
		else
			printf("Result: %s\n", !rtn ? "success" : "failed");
	}

	/* Re-enable sound as needed. */
	sound_on = old_sound_on;

	/* Re-enable screen fadings. */
	GameConfig.do_fadings = TRUE;

	return error_caught;
}