| 1 | /* |
| 2 | C-Dogs SDL |
| 3 | A port of the legendary (and fun) action/arcade cdogs. |
| 4 | Copyright (C) 1995 Ronny Wester |
| 5 | Copyright (C) 2003 Jeremy Chin |
| 6 | Copyright (C) 2003-2007 Lucas Martin-King |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | This file incorporates work covered by the following copyright and |
| 23 | permission notice: |
| 24 | |
| 25 | Copyright (c) 2013-2015, 2017, 2021 Cong Xu |
| 26 | All rights reserved. |
| 27 | |
| 28 | Redistribution and use in source and binary forms, with or without |
| 29 | modification, are permitted provided that the following conditions are met: |
| 30 | |
| 31 | Redistributions of source code must retain the above copyright notice, this |
| 32 | list of conditions and the following disclaimer. |
| 33 | Redistributions in binary form must reproduce the above copyright notice, |
| 34 | this list of conditions and the following disclaimer in the documentation |
| 35 | and/or other materials provided with the distribution. |
| 36 | |
| 37 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 38 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 39 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 40 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 41 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 42 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 43 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 44 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 45 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 47 | POSSIBILITY OF SUCH DAMAGE. |
| 48 | */ |
| 49 | #pragma once |
| 50 | |
| 51 | #ifdef __EMSCRIPTEN__ |
| 52 | #include <SDL.h> |
| 53 | #include <SDL2/SDL_mixer.h> |
| 54 | #else |
| 55 | #include <SDL_mixer.h> |
| 56 | #endif |
| 57 | |
| 58 | #include "c_array.h" |
| 59 | #include "game_events.h" |
| 60 | #include "pic.h" |
| 61 | #include "proto/msg.pb.h" |
| 62 | |
| 63 | typedef enum |
| 64 | { |
| 65 | ACTION_NULL, |
| 66 | ACTION_SETTRIGGER, |
| 67 | ACTION_CLEARTRIGGER, |
| 68 | ACTION_EVENT, |
| 69 | ACTION_ACTIVATEWATCH, |
| 70 | ACTION_DEACTIVATEWATCH |
| 71 | } ActionType; |
| 72 | |
| 73 | |
| 74 | // TODO: eliminate Action, replace with standard game events |
| 75 | typedef struct |
| 76 | { |
| 77 | ActionType Type; |
| 78 | union |
| 79 | { |
| 80 | struct vec2 pos; |
| 81 | int index; |
| 82 | GameEvent Event; |
| 83 | } u; |
| 84 | } Action; |
| 85 | |
| 86 | |
| 87 | typedef struct |
| 88 | { |
| 89 | int id; |
| 90 | int flags; |
| 91 | int isActive; |
| 92 | int cannotActivateLock; |
| 93 | CArray actions; // of Action |
| 94 | } Trigger; |
| 95 | |
| 96 | |
| 97 | typedef enum |
| 98 | { |
| 99 | CONDITION_TILECLEAR = 1 |
| 100 | } ConditionType; |
| 101 | typedef struct |
| 102 | { |
| 103 | ConditionType Type; |
| 104 | // How many ticks has this condition been fulfilled |
| 105 | // Reset to 0 when condition failed |
| 106 | int Counter; |
| 107 | int CounterMax; |
| 108 | struct vec2i Pos; |
| 109 | } Condition; |
| 110 | |
| 111 | |
| 112 | typedef struct |
| 113 | { |
| 114 | int index; |
| 115 | CArray conditions; // of Condition |
| 116 | CArray actions; // of Action |
| 117 | bool active; |
| 118 | } TWatch; |
| 119 | |
| 120 | |
| 121 | bool TriggerTryActivate(Trigger *t, const int flags, const struct vec2i tilePos); |
| 122 | bool TriggerCannotActivate(const Trigger *t); |
| 123 | void TriggerSetCannotActivate(Trigger *t); |
| 124 | void TriggerActivate(Trigger *t, CArray *mapTriggers); |
| 125 | void UpdateWatches(CArray *mapTriggers, const int ticks); |
| 126 | Trigger *TriggerNew(void); |
| 127 | void TriggerTerminate(Trigger *t); |
| 128 | Action *TriggerAddAction(Trigger *t); |
| 129 | |
| 130 | void WatchesInit(void); |
| 131 | void WatchesTerminate(void); |
| 132 | |
| 133 | TWatch *WatchNew(void); |
| 134 | Condition *WatchAddCondition( |
| 135 | TWatch *w, const ConditionType type, const int counterMax, |
| 136 | const struct vec2i pos); |
| 137 | Action *WatchAddAction(TWatch *w); |