| 1 | # bMotion - interbot stuff |
|---|
| 2 | # |
|---|
| 3 | # $Id$# |
|---|
| 4 | # |
|---|
| 5 | |
|---|
| 6 | ############################################################################### |
|---|
| 7 | # bMotion - an 'AI' TCL script for eggdrops |
|---|
| 8 | # Copyright (C) James Michael Seward 2000-2002 |
|---|
| 9 | # |
|---|
| 10 | # This program is free software; you can redistribute it and/or modify |
|---|
| 11 | # it under the terms of the GNU General Public License as published by |
|---|
| 12 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | # (at your option) any later version. |
|---|
| 14 | # |
|---|
| 15 | # This program is distributed in the hope that it will be useful, but |
|---|
| 16 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 18 | # General Public License for more details. |
|---|
| 19 | # |
|---|
| 20 | # You should have received a copy of the GNU General Public License |
|---|
| 21 | # along with this program; if not, write to the Free Software |
|---|
| 22 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | ############################################################################### |
|---|
| 24 | |
|---|
| 25 | # Elect a new bot to speak for each channel |
|---|
| 26 | proc bMotion_interbot_next_elect { } { |
|---|
| 27 | #send a message to all the bots on each of my channels |
|---|
| 28 | # I pick a number and send it |
|---|
| 29 | # This makes them all pick a number too and send that as a reply to all the other bots too |
|---|
| 30 | # Each bot tracks the numbers, highest bot wins and speaks next |
|---|
| 31 | |
|---|
| 32 | global bMotionInfo bMotion_interbot_timer |
|---|
| 33 | catch { |
|---|
| 34 | foreach chan $bMotionInfo(randomChannels) { |
|---|
| 35 | bMotion_interbot_next_elect_do $chan |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | set bMotion_interbot_timer 1 |
|---|
| 39 | set delay [expr [rand 200] + 1700] |
|---|
| 40 | bMotion_putloglev 2 * "bMotion: starting election timer" |
|---|
| 41 | utimer $delay bMotion_interbot_next_elect |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | proc bMotion_interbot_next_elect_do { channel } { |
|---|
| 45 | global bMotion_interbot_nextbot_score bMotion_interbot_nextbot_nick botnick bMotionInfo |
|---|
| 46 | |
|---|
| 47 | set myScore [rand 100] |
|---|
| 48 | if {$bMotionInfo(away) == 1} { |
|---|
| 49 | set myScore -2 |
|---|
| 50 | } |
|---|
| 51 | set bMotion_interbot_nextbot_score($channel) $myScore |
|---|
| 52 | set bMotion_interbot_nextbot_nick($channel) $botnick |
|---|
| 53 | |
|---|
| 54 | catch { |
|---|
| 55 | set bots [chanlist $channel] |
|---|
| 56 | foreach bot $bots { |
|---|
| 57 | #not me you idiot |
|---|
| 58 | if [isbotnick $bot] { continue } |
|---|
| 59 | set handle [nick2hand $bot $channel] |
|---|
| 60 | if [matchattr $handle b&K $channel] { |
|---|
| 61 | bMotion_putloglev 2 * "bMotion: sending elect_initial to $bot for $channel" |
|---|
| 62 | putbot $handle "bmotion elect_initial $channel $myScore" |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | proc bMotion_interbot_catch { bot cmd args } { |
|---|
| 69 | global bMotionInfo |
|---|
| 70 | bMotion_putloglev 3 * "bMotion: incoming !$args!" |
|---|
| 71 | set args [lindex $args 0] |
|---|
| 72 | regexp {([^ ]+) (.+)} $args matches function params |
|---|
| 73 | |
|---|
| 74 | bMotion_putloglev 2 * "bMotion: got command $function ($params) from $bot" |
|---|
| 75 | |
|---|
| 76 | switch -exact $function { |
|---|
| 77 | "say" { |
|---|
| 78 | # bmotion say <channel> <text> |
|---|
| 79 | bMotionCatchSayChan $bot $params |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | "elect_initial" { |
|---|
| 83 | bMotion_interbot_next_incoming $bot $params |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | "elect_reply" { |
|---|
| 87 | bMotion_interbot_next_incoming_reply $bot $params |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | return 0 |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | proc bMotion_interbot_next_incoming { bot params } { |
|---|
| 96 | #another bot is forcing an election |
|---|
| 97 | global bMotion_interbot_nextbot_score bMotion_interbot_nextbot_nick botnick bMotionInfo |
|---|
| 98 | |
|---|
| 99 | bMotion_putloglev 1 * "bMotion: Incoming election from $bot" |
|---|
| 100 | |
|---|
| 101 | regexp "(#.+) (.+)" $params matches channel score |
|---|
| 102 | if {$score > $bMotion_interbot_nextbot_score($channel)} { |
|---|
| 103 | bMotion_putloglev 2 * "bMotion: $bot now has highest score on $channel" |
|---|
| 104 | set bMotion_interbot_nextbot_score($channel) $score |
|---|
| 105 | set bMotion_interbot_nextbot_nick($channel) $bot |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | set myScore [rand 100] |
|---|
| 109 | if {$bMotionInfo(away) == 1} { |
|---|
| 110 | set myScore -2 |
|---|
| 111 | } |
|---|
| 112 | bMotion_putloglev 2 * "bMotion: My score is $myScore" |
|---|
| 113 | |
|---|
| 114 | if {$myScore > $bMotion_interbot_nextbot_score($channel)} { |
|---|
| 115 | bMotion_putloglev 2 * "bMotion: Actually, I have highest score on $channel, sending out reply" |
|---|
| 116 | set bMotion_interbot_nextbot_score($channel) $myScore |
|---|
| 117 | set bMotion_interbot_nextbot_nick($channel) $botnick |
|---|
| 118 | |
|---|
| 119 | set bots [chanlist $channel] |
|---|
| 120 | foreach bot $bots { |
|---|
| 121 | #not me you idiot |
|---|
| 122 | if [isbotnick $bot] { continue } |
|---|
| 123 | set handle [nick2hand $bot $channel] |
|---|
| 124 | if [matchattr $handle b&K $channel] { |
|---|
| 125 | putbot $handle "bmotion elect_reply $channel $myScore" |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | proc bMotion_interbot_next_incoming_reply { bot params } { |
|---|
| 132 | #another bot is forcing an election |
|---|
| 133 | global bMotion_interbot_nextbot_score bMotion_interbot_nextbot_nick |
|---|
| 134 | |
|---|
| 135 | bMotion_putloglev 1 * "bMotion: Incoming election reply from $bot" |
|---|
| 136 | |
|---|
| 137 | regexp "(#.+) (.+)" $params matches channel score |
|---|
| 138 | if {$score > $bMotion_interbot_nextbot_score($channel)} { |
|---|
| 139 | bMotion_putloglev 2 * "bMotion: $bot now has highest score on $channel" |
|---|
| 140 | set bMotion_interbot_nextbot_score($channel) $score |
|---|
| 141 | set bMotion_interbot_nextbot_nick($channel) $bot |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | proc bMotionSendSayChan { channel text thisBot} { |
|---|
| 146 | #global bMotionAvailableBots |
|---|
| 147 | #global bMotionCache |
|---|
| 148 | |
|---|
| 149 | #set thisBot $bMotionCache(remoteBot) |
|---|
| 150 | |
|---|
| 151 | #replace all ¬ with % |
|---|
| 152 | set text [bMotionInsertString $text "¬" "%"] |
|---|
| 153 | bMotion_putloglev 1 * "bMotion: pushing command say ($channel $text) to $thisBot" |
|---|
| 154 | putbot $thisBot "bmotion say $channel $text" |
|---|
| 155 | return $thisBot |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | proc bMotionCatchSayChan { bot params } { |
|---|
| 159 | global bMotionInfo |
|---|
| 160 | |
|---|
| 161 | #bMotion_putloglev d * "bMotion: got command $function ($params) from $bot" |
|---|
| 162 | |
|---|
| 163 | regexp {(#[^ ]+) (.+)} $params matches channel txt |
|---|
| 164 | global bMotionQueueTimer |
|---|
| 165 | if {$bMotionQueueTimer == 0} { |
|---|
| 166 | set bMotionQueueTimer 1 |
|---|
| 167 | utimer 4 bMotionProcessQueue |
|---|
| 168 | } |
|---|
| 169 | if {$bMotionInfo(silence) == 1} { |
|---|
| 170 | set bMotionInfo(silence) 2 |
|---|
| 171 | } |
|---|
| 172 | bMotionDoAction $channel $bot $txt |
|---|
| 173 | bMotion_putloglev 1 * "bMotion: done say command from $bot" |
|---|
| 174 | if {$bMotionInfo(silence) == 2} { |
|---|
| 175 | set bMotionInfo(silence) 1 |
|---|
| 176 | } |
|---|
| 177 | return 0 |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | if {![info exists bMotion_interbot_nextbot_score]} { |
|---|
| 182 | # init the nextbot array for each channel |
|---|
| 183 | |
|---|
| 184 | foreach chan $bMotionInfo(randomChannels) { |
|---|
| 185 | set bMotion_interbot_nextbot_score($chan) "-1" |
|---|
| 186 | set bMotion_interbot_nextbot_nick($chan) "" |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | # Check if we're due to talk next on the channel |
|---|
| 191 | # if yes, then force an election for that channel immediately afterwards |
|---|
| 192 | proc bMotion_interbot_me_next { channel } { |
|---|
| 193 | global bMotion_interbot_nextbot_nick bMotion_interbot_nextbot_score botnick |
|---|
| 194 | |
|---|
| 195 | set channel [string tolower $channel] |
|---|
| 196 | |
|---|
| 197 | if {$bMotion_interbot_nextbot_score($channel) < 0} { |
|---|
| 198 | return 0 |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | if {$bMotion_interbot_nextbot_nick($channel) == $botnick} { |
|---|
| 202 | bMotion_interbot_next_elect_do $channel |
|---|
| 203 | return 1 |
|---|
| 204 | } |
|---|
| 205 | #if it's noone, the winning bot will force an election anyway |
|---|
| 206 | return 0 |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | #interbot stuff |
|---|
| 210 | bind bot I "bmotion" bMotion_interbot_catch |
|---|
| 211 | |
|---|
| 212 | bMotion_putloglev d * "bMotion: interbot module loaded" |
|---|