Decoded: Rogue (1980) by Toy, Arnold, Wichman DOS version (1983) by Mel Sibony and Jon Lane Source file: DAEMON.C Beginner friendly, line-by-line code walkthrough by MaiZure DAEMON.C handles background daemon systems. Creation, tracking, and execution. Original code: https://britzl.github.io/roguearchive/ Original code with line numbers http://www.maizure.org/projects/decoded-rogue/DAEMON_linenum.txt 1 COMMENT 2 COMMENT 3 COMMENT 4 COMMENT 5 COMMENT 6 COMMENT 7 BLANK 8 Include the game header 9 Include the console management header 10 BLANK 11 Declare marco for EMPTY as 0 (null) 12 Declare macro for FULL as i 13 Declare macro for DAEMON as -1 (used as an init value) 14 Declares a macro for the maximum number of daemons in operation (20) 15 BLANK DEFINE A DAEMON STRUCTURE 16 Declares a struct to hold a daemon 17 Declares an event function to execute when the daemon triggers 18 Declares an argument for the daemon to execute 19 Declares a timer used to trigger daemon events (next time to execute) 20 Ends declaration of daemon structure and allocates 20 in an array 21 BLANK 22 COMMENT 23 COMMENT 24 COMMENT 25 COMMENT FIND DAEMON SLOT 26 d_slot returns a daemon structure 27 Define d_slot with no arguments 28 BLOCK START - d_slot returns an unused daemon slot 29 Declares a pointer to a daemon 30 BLANK 31 Loop through the daemon array 32 If an empty slot is found (empty means a slot with no function) 33 Return that slot 34 Check for debug mode (not enabled) 35 If we made it this far, we failed to find an empty. Print message 36 End check for debug mode 37 Return failure 38 BLOCK END - d_slot 39 BLANK 40 COMMENT 41 COMMENT 42 COMMENT 43 COMMENT FIND DAEMON BY FUNCTION 44 find_slot returns a pointer to a daemon 45 Define find_slot with one argument 46 Argument 1 is a function to execute 47 BLOCK START - find_slot, returns a daemon by its function 48 Declare a pointer to a daemon slot 49 BLANK 50 Loop through the daemon list 51 If the current daemon's function matches the input.. 52 Return that daemon 53 Return NULL if no match is found 54 BLOCK END - find_slot 55 BLANK 56 COMMENT 57 COMMENT 58 COMMENT 59 COMMENT INITAILIZE A DAEMON 60 Defines daemon with two arguments 61 Argument 1 is a function to execute, Argument 2 is the function argument 62 BLOCK START - daemon, creates a daemon 63 Declares a pointer to a daemon 64 BLANK 65 Get an empty slot 66 Set the slot's function 67 Set the slot's argument 68 Set the slot's timer to an initial value of -1 (execute immediately) 69 BLOCK END - daemon 70 BLANK 71 COMMENT 72 COMMENT 73 COMMENT 74 COMMENT PROCESS ALL DAEMONS 75 Declare do_daemons with no arguments 76 BLOCK START - do_daemons, run daemons that are ready 77 Declare a pointer to a daemon 78 BLANK 79 COMMENT 80 COMMENT 81 COMMENT 82 Loop through the daemon list 83 COMMENT 84 COMMENT 85 COMMENT 86 If the daemon's time is expired and it has a valid function... 87 Execute that function with the arguments 88 BLOCK END - do_daemons 89 BLANK 90 COMMENT 91 COMMENT 92 COMMENT 93 COMMENT START A DAEMON 94 Declare fuse with three arguments 95 Arg 1 is the function, argument 2 is the input arg, and 3 is the delay 96 BLOCK START - fuse, sets a daemon to go off after a delay 97 Declare a pointer to the daemon 98 BLANK 99 Find an empty daemon slot 100 Set the function 101 Set the argument 102 Set the time delay 103 BLOCK END - fuse 104 BLANK 105 COMMENT 106 COMMENT 107 COMMENT 108 COMMENT EXTEND A DAEMON 109 Declare lengthen with two arguments 110 Argument 1 is the function to delay 111 Argument 2 is the time to delay 112 BLOCK START - lengthen, increases the time delay of a daemon 113 Declare a pointer to a daemon 114 BLANK 115 If we cannot find a daemon with the desired function... 116 Nothing to lengthen, return failure 117 Otherwise, add the delay time to the daemon we found 118 BLOCK END - lengthen 119 BLANK 120 COMMENT 121 COMMENT 122 COMMENT 123 COMMENT DESTROY A DAEMON 124 Declares extinguish with one argument 125 Argument 1 is a daemon function 126 BLOCK START - extinguish, destroys a daemon 127 Declare a pointer to a daemon 128 BLANK 129 If there is no daemon with matching function... 130 Nothing to destroy, return 131 Remove the daemon's function, effectively ending its processing 132 BLOCK END - extinguish 133 BLANK 134 COMMENT 135 COMMENT 136 COMMENT 137 COMMENT UPDATE AND TRIGGER DAEMONS 138 Declare do_fuses with no arguments 139 BLOCK START - do_fuses, goes through timed daemons and decrements timers 140 Declare a pointer to a daemon 141 BLANK 142 COMMENT 143 COMMENT 144 COMMENT 145 Loop through all the daemons 146 COMMENT 147 COMMENT 148 COMMENT 149 COMMENT 150 If the daemon is active, has a valid timer, but gets decremented to zero 151 BLOCK START - trigger daemon 152 Execeute the daemon function 153 Remove that daemon (only one-shot) 154 BLOCK END - trigger daemon 155 Repeat loop through all 20 daemon slots 156 BLOCK END - do_fuses 157 EOF˙