Source code for bruhanimate.bruheffect.automaton_effect
"""Copyright 2023 Ethan ChristensenLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License."""importnumpyasnpfrombruhcolorimportbruhcoloredasbcfrom..bruhutilimportBufferfrom.base_effectimportBaseEffectfrom.settingsimportAutomatonSettingsdef_heat(r:float)->int:r=max(0.0,min(1.0,r))ifr<0.25:return17+int(r*4*18)elifr<0.5:return51+int((r-0.25)*4*12)elifr<0.75:return82+int((r-0.5)*4*12)else:return220+int((r-0.75)*4*3)
[docs]classAutomatonEffect(BaseEffect):""" Wolfram 1-D elementary cellular automaton. Each generation is computed from the previous row using a 3-cell neighbourhood lookup table determined by the rule number (0–255). New generations scroll downward from the top, producing a 2-D space-time diagram. Rule 30 (default) is chaotic; Rule 90 produces Sierpinski triangles; Rule 110 is Turing-complete. Call :meth:`set_rule` to switch rules and reset the seed mid-run. """
[docs]defset_rule(self,rule_num:int):"""Switch to a new rule number (0–255) and reset the initial seed."""self._rule_table=self._make_table(rule_num)self._row[:]=0self._row[self._w//2]=1self.buffer.clear_buffer(val=self.background)
[docs]defrender_frame(self,frame_number:int):# Compute next generationleft=np.roll(self._row,1)right=np.roll(self._row,-1)idx=(left*4+self._row*2+right).astype(int)next_row=self._rule_table[idx]# Scroll buffer down and insert new row at topself.buffer.buffer.insert(0,[self.background]*self._w)self.buffer.buffer.pop()# Color each live cell by its horizontal positionforcol,aliveinenumerate(next_row):ifalive:ratio=col/max(1,self._w-1)color=_heat(ratio)ifself.colorelseNonech=bc(self.char,color=color)ifcolorisnotNoneelseself.charself.buffer.buffer[0][col]=chself._row=next_row